public Subset(
     ShardedSessionFactoryImpl parent,
     IEnumerable <ShardId> shardIds,
     IShardStrategyFactory shardStrategyFactory)
     : base(parent, shardIds, shardStrategyFactory)
 {
 }
Example #2
0
 public ShardedConfiguration(Configuration prototypeConfiguration,
                             IList <IShardConfiguration> shardConfigs,
                             IShardStrategyFactory shardStrategyFactory)
     :
     this(prototypeConfiguration, shardConfigs, shardStrategyFactory, new Dictionary <int, int>())
 {
 }
        protected ShardedSessionFactoryImpl(
            ShardedSessionFactoryImpl parent,
            IEnumerable <ShardId> shardIds,
            IShardStrategyFactory shardStrategyFactory)
        {
            this.shardIdsBySessionFactory                    = new Dictionary <ISessionFactoryImplementor, ICollection <ShardId> >();
            this.classesWithoutTopLevelSaveSupport           = parent.classesWithoutTopLevelSaveSupport;
            this.checkAllAssociatedObjectsForDifferentShards = parent.checkAllAssociatedObjectsForDifferentShards;
            this.controlSessionFactory = parent.controlSessionFactory;

            var uniqueShardIds = new HashSet <ShardId>(shardIds);

            foreach (var pair in parent.shardIdsBySessionFactory)
            {
                var shardIdsSubset = new HashSet <ShardId>(pair.Value);
                shardIdsSubset.IntersectWith(uniqueShardIds);
                if (shardIdsSubset.Count > 0)
                {
                    this.shardIdsBySessionFactory.Add(pair.Key, shardIdsSubset);
                }
            }

            shardStrategy = shardStrategyFactory.NewShardStrategy(
                shardIdsBySessionFactory.SelectMany(c => c.Value));
        }
 public ShardedConfiguration(
     Configuration prototypeConfiguration,
     IEnumerable <IShardConfiguration> shardConfigs,
     IShardStrategyFactory shardStrategyFactory)
     : this(prototypeConfiguration, shardConfigs, shardStrategyFactory, new Dictionary <short, short>())
 {
 }
 public SubsetShardedSessionFactoryImpl(ICollection <ShardId> shardIds,
                                        IDictionary <ISessionFactoryImplementor, Set <ShardId> > sessionFactoryShardIdMap,
                                        IShardStrategyFactory shardStrategyFactory,
                                        Set <System.Type> classesWithoutTopLevelSaveSupport,
                                        bool checkAllAssociatedObjectsForDifferentShards) : base(shardIds, sessionFactoryShardIdMap, shardStrategyFactory, classesWithoutTopLevelSaveSupport, checkAllAssociatedObjectsForDifferentShards)
 {
 }
Example #6
0
        public ISessionFactory CreateSessionFactory(IList <IShardConfiguration> shardConfigs)
        {
            Configuration         prototypeConfig      = GetConfigurationTemplate("Shard1", 1);
            IShardStrategyFactory shardStrategyFactory = BuildShardStrategyFactory();
            var shardedConfig = new ShardedConfiguration(prototypeConfig, shardConfigs, shardStrategyFactory);

            return(shardedConfig.BuildShardedSessionFactory());
        }
 public SubsetShardedSessionFactoryImpl(ICollection<ShardId> shardIds,
                                        IDictionary<ISessionFactoryImplementor,Set<ShardId>> sessionFactoryShardIdMap,
                                        IShardStrategyFactory shardStrategyFactory,
                                        Set<System.Type> classesWithoutTopLevelSaveSupport,
                                        bool checkAllAssociatedObjectsForDifferentShards)
     : base(shardIds,sessionFactoryShardIdMap,shardStrategyFactory,classesWithoutTopLevelSaveSupport,checkAllAssociatedObjectsForDifferentShards)
 {
 }
 /// <summary>
 /// This method is provided to allow a client to work on a subset of
 /// shards or a specialized <see cref="IShardStrategyFactory"/>.  By providing
 /// the desired shardIds, the client can limit operations to these shards.
 /// Alternatively, this method can be used to create a ShardedSessionFactory
 /// with different strategies that might be appropriate for a specific operation.
 ///
 /// The factory returned will not be stored as one of the factories that would
 /// be returned by a call to getSessionFactories.
 /// </summary>
 /// <param name="shardIds"></param>
 /// <param name="shardStrategyFactory"></param>
 /// <returns>specially configured ShardedSessionFactory</returns>
 public IShardedSessionFactory GetSessionFactory(IList <ShardId> shardIds, IShardStrategyFactory shardStrategyFactory)
 {
     return(new SubsetShardedSessionFactoryImpl(
                shardIds,
                fullSessionFactoryShardIdMap,
                shardStrategyFactory,
                classesWithoutTopLevelSaveSupport,
                checkAllAssociatedObjectsForDifferentShards));
 }
 /// <summary>
 /// Constructs a ShardedSessionFactoryImpl
 /// </summary>
 /// <param name="sessionFactoryShardIdMap">Mapping of SessionFactories to shard ids.
 /// When using virtual shards, this map associates SessionFactories (physical
 /// shards) with virtual shards (shard ids).  Map cannot be empty.
 /// Map keys cannot be null.  Map values cannot be null or empty.</param>
 /// <param name="shardStrategyFactory">factory that knows how to create the <see cref="IShardStrategy"/>
 /// that will be used for all shard-related operations</param>
 /// <param name="classesWithoutTopLevelSaveSupport">All classes that cannot be saved
 /// as top-level objects</param>
 /// <param name="checkAllAssociatedObjectsForDifferentShards">Flag that controls
 ///whether or not we do full cross-shard relationshp checking (very slow)</param>
 public ShardedSessionFactoryImpl(
     IDictionary <ISessionFactoryImplementor, Set <ShardId> > sessionFactoryShardIdMap,
     IShardStrategyFactory shardStrategyFactory,
     ISet <System.Type> classesWithoutTopLevelSaveSupport,
     bool checkAllAssociatedObjectsForDifferentShards)
     : this(new List <ShardId>(sessionFactoryShardIdMap.Values.Concatenation().Cast <ShardId>()),
            sessionFactoryShardIdMap,
            shardStrategyFactory,
            classesWithoutTopLevelSaveSupport,
            checkAllAssociatedObjectsForDifferentShards)
 {
 }
Example #10
0
        public IShardedSessionFactory GetSessionFactory(
      IEnumerable<string> connStrNames,
      IShardStrategyFactory shardStrategyFactory)
        {
            var prototypeCfg = GetConfiguration(
            connStrNames.First(), 1);

              var cfg = new ShardedConfiguration(
            prototypeCfg,
            GetShardCfg(connStrNames),
            shardStrategyFactory);

              return cfg.BuildShardedSessionFactory();
        }
        public ShardedConfiguration(
            Configuration prototypeConfiguration,
            IEnumerable <IShardConfiguration> shardConfigs,
            IShardStrategyFactory shardStrategyFactory,
            Dictionary <short, short> virtualShardToShardMap)
        {
            Preconditions.CheckNotNull(prototypeConfiguration);
            Preconditions.CheckNotNull(shardConfigs);
            Preconditions.CheckNotNull(shardStrategyFactory);
            Preconditions.CheckNotNull(virtualShardToShardMap);

            this.prototypeConfiguration = prototypeConfiguration;
            this.prototypeConfiguration.SetProperty(NHibernate.Cfg.Environment.QueryLinqProvider, typeof(ShardedQueryProvider).AssemblyQualifiedName);

            this.shardConfigs = shardConfigs.ToList();
            Preconditions.CheckArgument(this.shardConfigs.Count > 0);

            this.shardStrategyFactory   = shardStrategyFactory;
            this.virtualShardToShardMap = virtualShardToShardMap;

            if (virtualShardToShardMap.Count != 0)
            {
                // build the map from shard to set of virtual shards
                shardToVirtualShardIdMap = new Dictionary <short, ICollection <ShardId> >();

                foreach (var pair in virtualShardToShardMap)
                {
                    var set = shardToVirtualShardIdMap[(pair.Value)];
                    // see if we already have a set of virtual shards
                    if (set == null)
                    {
                        // we don't, so create it and add it to the map
                        set = new HashSet <ShardId>();
                        shardToVirtualShardIdMap.Add(pair.Value, set);
                    }
                    set.Add(new ShardId(pair.Key));
                }
            }
            else
            {
                shardToVirtualShardIdMap = new Dictionary <short, ICollection <ShardId> >();
            }
        }
        public ShardedConfiguration(
            Configuration prototypeConfiguration,
            IEnumerable<IShardConfiguration> shardConfigs,
            IShardStrategyFactory shardStrategyFactory,
            Dictionary<short, short> virtualShardToShardMap)
        {
            Preconditions.CheckNotNull(prototypeConfiguration);
            Preconditions.CheckNotNull(shardConfigs);
            Preconditions.CheckNotNull(shardStrategyFactory);
            Preconditions.CheckNotNull(virtualShardToShardMap);

            this.prototypeConfiguration = prototypeConfiguration;
            this.shardConfigs = shardConfigs.ToList();
            Preconditions.CheckArgument(this.shardConfigs.Count > 0);

            this.shardStrategyFactory = shardStrategyFactory;
            this.virtualShardToShardMap = virtualShardToShardMap;

            if (!(virtualShardToShardMap.Count == 0))
            {
                // build the map from shard to set of virtual shards
                shardToVirtualShardIdMap = new Dictionary<short, ICollection<ShardId>>();

                foreach (var pair in virtualShardToShardMap)
                {
                    var set = shardToVirtualShardIdMap[(pair.Value)];
                    // see if we already have a set of virtual shards
                    if (set == null)
                    {
                        // we don't, so create it and add it to the map
                        set = new HashSet<ShardId>();
                        shardToVirtualShardIdMap.Add(pair.Value, set);
                    }
                    set.Add(new ShardId(pair.Key));
                }
            }
            else
            {
                shardToVirtualShardIdMap = new Dictionary<short, ICollection<ShardId>>();
            }
        }
Example #13
0
        public ShardedConfiguration(Configuration prototypeConfiguration, IList <IShardConfiguration> shardConfigs,
                                    IShardStrategyFactory shardStrategyFactory, Dictionary <int, int> virtualShardToShardMap)
        {
            Preconditions.CheckNotNull(prototypeConfiguration);
            Preconditions.CheckNotNull(shardConfigs);
            Preconditions.CheckArgument(!(shardConfigs.Count == 0));
            Preconditions.CheckNotNull(shardStrategyFactory);
            Preconditions.CheckNotNull(virtualShardToShardMap);

            this.prototypeConfiguration = prototypeConfiguration;
            this.shardConfigs           = shardConfigs;
            this.shardStrategyFactory   = shardStrategyFactory;
            this.virtualShardToShardMap = virtualShardToShardMap;

            if (!(virtualShardToShardMap.Count == 0))
            {
                // build the map from shard to set of virtual shards
                shardToVirtualShardIdMap = new Dictionary <int, Set <ShardId> >();

                foreach (var pair in virtualShardToShardMap)
                {
                    Set <ShardId> set = shardToVirtualShardIdMap[(pair.Value)];
                    // see if we already have a set of virtual shards
                    if (set == null)
                    {
                        // we don't, so create it and add it to the map
                        set = new HashedSet <ShardId>();
                        shardToVirtualShardIdMap.Add(pair.Value, set);
                    }
                    set.Add(new ShardId(pair.Key));
                }
            }
            else
            {
                shardToVirtualShardIdMap = new Dictionary <int, Set <ShardId> >();
            }
        }
 /// <summary>
 /// This method is provided to allow a client to work on a subset of
 /// shards or a specialized <see cref="IShardStrategyFactory"/>.  By providing
 /// the desired shardIds, the client can limit operations to these shards.
 /// Alternatively, this method can be used to create a ShardedSessionFactory
 /// with different strategies that might be appropriate for a specific operation.
 ///
 /// The factory returned will not be stored as one of the factories that would
 /// be returned by a call to getSessionFactories.
 /// </summary>
 /// <param name="shardIds"></param>
 /// <param name="shardStrategyFactory"></param>
 /// <returns>specially configured ShardedSessionFactory</returns>
 public IShardedSessionFactory GetSessionFactory(IEnumerable <ShardId> shardIds, IShardStrategyFactory shardStrategyFactory)
 {
     return(new Subset(this, shardIds, shardStrategyFactory));
 }
Example #15
0
 public IStore SetShardingStrategy(IShardStrategyFactory shardStrategyFactory)
 {
     _shardStrategyFactory = shardStrategyFactory;
     return(this);
 }
 /// <summary>
 /// This method is provided to allow a client to work on a subset of
 /// shards or a specialized <see cref="IShardStrategyFactory"/>.  By providing
 /// the desired shardIds, the client can limit operations to these shards.
 /// Alternatively, this method can be used to create a ShardedSessionFactory
 /// with different strategies that might be appropriate for a specific operation.
 ///
 /// The factory returned will not be stored as one of the factories that would
 /// be returned by a call to getSessionFactories.
 /// </summary>
 /// <param name="shardIds"></param>
 /// <param name="shardStrategyFactory"></param>
 /// <returns>specially configured ShardedSessionFactory</returns>
 public IShardedSessionFactory GetSessionFactory(IList<ShardId> shardIds, IShardStrategyFactory shardStrategyFactory)
 {
     throw new NotImplementedException();
 }
        /// <summary>
        /// Constructs a ShardedSessionFactoryImpl
        /// </summary>
        /// <param name="sessionFactoryShardIdMap">Mapping of SessionFactories to shard ids.
        /// When using virtual shards, this map associates SessionFactories (physical
        /// shards) with virtual shards (shard ids).  Map cannot be empty.
        /// Map keys cannot be null.  Map values cannot be null or empty.</param>
        /// <param name="shardStrategyFactory">factory that knows how to create the <see cref="IShardStrategy"/> 
        /// that will be used for all shard-related operations</param>
        /// <param name="classesWithoutTopLevelSaveSupport">All classes that cannot be saved
        /// as top-level objects</param>
        /// <param name="checkAllAssociatedObjectsForDifferentShards">Flag that controls
        ///whether or not we do full cross-shard relationshp checking (very slow)</param>
        public ShardedSessionFactoryImpl(
			IDictionary<ISessionFactoryImplementor, Set<ShardId>> sessionFactoryShardIdMap,
			IShardStrategyFactory shardStrategyFactory,
			ISet<System.Type> classesWithoutTopLevelSaveSupport,
			bool checkAllAssociatedObjectsForDifferentShards)
            : this(new List<ShardId>(sessionFactoryShardIdMap.Values.Concatenation().Cast<ShardId>()),
				   sessionFactoryShardIdMap,
				   shardStrategyFactory,
				   classesWithoutTopLevelSaveSupport,
				   checkAllAssociatedObjectsForDifferentShards)
        {
        }
 public ShardedConfiguration(Configuration prototypeConfiguration,
     IList<IShardConfiguration> shardConfigs,
     IShardStrategyFactory shardStrategyFactory)
     : this(prototypeConfiguration, shardConfigs, shardStrategyFactory, new Dictionary<int, int>())
 {
 }
 /// <summary>
 /// This method is provided to allow a client to work on a subset of
 /// shards or a specialized <see cref="IShardStrategyFactory"/>.  By providing
 /// the desired shardIds, the client can limit operations to these shards.
 /// Alternatively, this method can be used to create a ShardedSessionFactory
 /// with different strategies that might be appropriate for a specific operation.
 ///
 /// The factory returned will not be stored as one of the factories that would
 /// be returned by a call to getSessionFactories.
 /// </summary>
 /// <param name="shardIds"></param>
 /// <param name="shardStrategyFactory"></param>
 /// <returns>specially configured ShardedSessionFactory</returns>
 public IShardedSessionFactory GetSessionFactory(IList<ShardId> shardIds, IShardStrategyFactory shardStrategyFactory)
 {
     return new SubsetShardedSessionFactoryImpl(
         shardIds,
         fullSessionFactoryShardIdMap,
         shardStrategyFactory,
         classesWithoutTopLevelSaveSupport,
         checkAllAssociatedObjectsForDifferentShards);
 }
        /// <summary>
        /// Constructs a ShardedSessionFactoryImpl
        /// </summary>
        /// <param name="shardIds"> The ids of the shards with which this SessionFactory should be associated.</param>
        /// <param name="sessionFactoryShardIdMap">Mapping of SessionFactories to shard ids.
        ///  When using virtual shards, this map associates SessionFactories (physical
        ///  shards) with virtual shards (shard ids).  Map cannot be empty.
        ///  Map keys cannot be null.  Map values cannot be null or empty.</param>
        /// <param name="shardStrategyFactory">factory that knows how to create the <see cref="IShardStrategy"/>
        ///  that will be used for all shard-related operations</param>
        /// <param name="classesWithoutTopLevelSaveSupport"> All classes that cannot be saved
        ///  as top-level objects</param>
        /// <param name="checkAllAssociatedObjectsForDifferentShards">Flag that controls
        ///  whether or not we do full cross-shard relationshp checking (very slow)</param>
        public ShardedSessionFactoryImpl(
            ICollection <ShardId> shardIds,
            IDictionary <ISessionFactoryImplementor, Set <ShardId> > sessionFactoryShardIdMap,
            IShardStrategyFactory shardStrategyFactory,
            ISet <System.Type> classesWithoutTopLevelSaveSupport,
            bool checkAllAssociatedObjectsForDifferentShards)
        {
            Preconditions.CheckNotNull(sessionFactoryShardIdMap);
            Preconditions.CheckArgument(!(sessionFactoryShardIdMap.Count == 0));
            Preconditions.CheckNotNull(shardStrategyFactory);
            Preconditions.CheckNotNull(classesWithoutTopLevelSaveSupport);

            sessionFactories = new List <ISessionFactoryImplementor>(sessionFactoryShardIdMap.Keys);
            this.sessionFactoryShardIdMap                    = new Dictionary <ISessionFactoryImplementor, Set <ShardId> >();
            fullSessionFactoryShardIdMap                     = sessionFactoryShardIdMap;
            this.classesWithoutTopLevelSaveSupport           = new HashedSet <System.Type>(classesWithoutTopLevelSaveSupport);
            this.checkAllAssociatedObjectsForDifferentShards = checkAllAssociatedObjectsForDifferentShards;
            Set <ShardId> uniqueShardIds = new HashedSet <ShardId>();
            ISessionFactoryImplementor controlSessionFactoryToSet = null;

            foreach (var entry in sessionFactoryShardIdMap)
            {
                ISessionFactoryImplementor implementor = entry.Key;
                Preconditions.CheckNotNull(implementor);
                Set <ShardId> shardIdSet = entry.Value;
                Preconditions.CheckNotNull(shardIdSet);
                Preconditions.CheckState(!(shardIdSet.Count == 0));

                foreach (ShardId shardId in shardIdSet)
                {
                    //TODO: we should change it so we specify control shard in configuration
                    if (shardId.Id == CONTROL_SHARD_ID)
                    {
                        controlSessionFactoryToSet = implementor;
                    }
                    if (!uniqueShardIds.Add(shardId))
                    {
                        string msg = string.Format("Cannot have more than one shard with shard id {0}.", shardId.Id);
                        log.Error(msg);
                        throw new HibernateException(msg);
                    }
                    if (shardIds.Contains(shardId))
                    {
                        if (!this.sessionFactoryShardIdMap.ContainsKey(implementor))
                        {
                            this.sessionFactoryShardIdMap.Add(implementor, new HashedSet <ShardId>());
                        }

                        this.sessionFactoryShardIdMap[implementor].Add(shardId);
                    }
                }
            }
            // make sure someone didn't associate a session factory with a shard id
            // that isn't in the full list of shards
            foreach (ShardId shardId in shardIds)
            {
                Preconditions.CheckState(uniqueShardIds.Contains(shardId));
            }
            controlSessionFactory = controlSessionFactoryToSet;
            // now that we have all our shard ids, construct our shard strategy
            shardStrategy = shardStrategyFactory.NewShardStrategy(shardIds);
            SetupIdGenerators();
        }
        /// <summary>
        /// Constructs a ShardedSessionFactoryImpl
        /// </summary>
        /// <param name="shardIds"> The ids of the shards with which this SessionFactory should be associated.</param>
        /// <param name="sessionFactoryShardIdMap">Mapping of SessionFactories to shard ids. 
        ///  When using virtual shards, this map associates SessionFactories (physical
        ///  shards) with virtual shards (shard ids).  Map cannot be empty.
        ///  Map keys cannot be null.  Map values cannot be null or empty.</param>
        /// <param name="shardStrategyFactory">factory that knows how to create the <see cref="IShardStrategy"/> 
        ///  that will be used for all shard-related operations</param>
        /// <param name="classesWithoutTopLevelSaveSupport"> All classes that cannot be saved
        ///  as top-level objects</param>
        /// <param name="checkAllAssociatedObjectsForDifferentShards">Flag that controls
        ///  whether or not we do full cross-shard relationshp checking (very slow)</param>
        public ShardedSessionFactoryImpl(
			ICollection<ShardId> shardIds,
			IDictionary<ISessionFactoryImplementor, Set<ShardId>> sessionFactoryShardIdMap,
			IShardStrategyFactory shardStrategyFactory,
			ISet<System.Type> classesWithoutTopLevelSaveSupport,
			bool checkAllAssociatedObjectsForDifferentShards)
        {
            Preconditions.CheckNotNull(sessionFactoryShardIdMap);
            Preconditions.CheckArgument(!(sessionFactoryShardIdMap.Count == 0));
            Preconditions.CheckNotNull(shardStrategyFactory);
            Preconditions.CheckNotNull(classesWithoutTopLevelSaveSupport);

            sessionFactories = new List<ISessionFactoryImplementor>(sessionFactoryShardIdMap.Keys);
            this.sessionFactoryShardIdMap = new Dictionary<ISessionFactoryImplementor, Set<ShardId>>();
            fullSessionFactoryShardIdMap = sessionFactoryShardIdMap;
            this.classesWithoutTopLevelSaveSupport = new HashedSet<System.Type>(classesWithoutTopLevelSaveSupport);
            this.checkAllAssociatedObjectsForDifferentShards = checkAllAssociatedObjectsForDifferentShards;
            Set<ShardId> uniqueShardIds = new HashedSet<ShardId>();
            ISessionFactoryImplementor controlSessionFactoryToSet = null;

            foreach (var entry in sessionFactoryShardIdMap)
            {
                ISessionFactoryImplementor implementor = entry.Key;
                Preconditions.CheckNotNull(implementor);
                Set<ShardId> shardIdSet = entry.Value;
                Preconditions.CheckNotNull(shardIdSet);
                Preconditions.CheckState(!(shardIdSet.Count == 0));

                foreach (ShardId shardId in shardIdSet)
                {
                    //TODO: we should change it so we specify control shard in configuration
                    if (shardId.Id == CONTROL_SHARD_ID)
                    {
                        controlSessionFactoryToSet = implementor;
                    }
                    if (!uniqueShardIds.Add(shardId))
                    {
                        string msg = string.Format("Cannot have more than one shard with shard id {0}.", shardId.Id);
                        log.Error(msg);
                        throw new HibernateException(msg);
                    }
                    if (shardIds.Contains(shardId))
                    {
                        if (!this.sessionFactoryShardIdMap.ContainsKey(implementor))
                            this.sessionFactoryShardIdMap.Add(implementor, new HashedSet<ShardId>());

                        this.sessionFactoryShardIdMap[implementor].Add(shardId);
                    }
                }
            }
            // make sure someone didn't associate a session factory with a shard id
            // that isn't in the full list of shards
            foreach (ShardId shardId in shardIds)
            {
                Preconditions.CheckState(uniqueShardIds.Contains(shardId));
            }
            controlSessionFactory = controlSessionFactoryToSet;
            // now that we have all our shard ids, construct our shard strategy
            shardStrategy = shardStrategyFactory.NewShardStrategy(shardIds);
            SetupIdGenerators();
        }
        protected ShardedSessionFactoryImpl(
            ShardedSessionFactoryImpl parent,
            IEnumerable<ShardId> shardIds,
            IShardStrategyFactory shardStrategyFactory)
        {
            this.shardIdsBySessionFactory = new Dictionary<ISessionFactoryImplementor, ICollection<ShardId>>();
            this.classesWithoutTopLevelSaveSupport = parent.classesWithoutTopLevelSaveSupport;
            this.checkAllAssociatedObjectsForDifferentShards = parent.checkAllAssociatedObjectsForDifferentShards;
            this.controlSessionFactory = parent.controlSessionFactory;

            var uniqueShardIds = new HashSet<ShardId>(shardIds);
            foreach (var pair in parent.shardIdsBySessionFactory)
            {
                var shardIdsSubset = new HashSet<ShardId>(pair.Value);
                shardIdsSubset.IntersectWith(uniqueShardIds);
                if (shardIdsSubset.Count > 0)
                {
                    this.shardIdsBySessionFactory.Add(pair.Key, shardIdsSubset);
                }
            }

            shardStrategy = shardStrategyFactory.NewShardStrategy(
                shardIdsBySessionFactory.SelectMany(c => c.Value));
        }
 public ShardedConfiguration(Configuration prototypeConfiguration, IEnumerable<IShardConfiguration> shardConfigs, IShardStrategyFactory shardStrategyFactory)
     : this(prototypeConfiguration, shardConfigs, shardStrategyFactory, new Dictionary<short, short>())
 {
 }
 /// <summary>
 /// This method is provided to allow a client to work on a subset of
 /// shards or a specialized <see cref="IShardStrategyFactory"/>.  By providing
 /// the desired shardIds, the client can limit operations to these shards.
 /// Alternatively, this method can be used to create a ShardedSessionFactory
 /// with different strategies that might be appropriate for a specific operation.
 ///
 /// The factory returned will not be stored as one of the factories that would
 /// be returned by a call to getSessionFactories.
 /// </summary>
 /// <param name="shardIds"></param>
 /// <param name="shardStrategyFactory"></param>
 /// <returns>specially configured ShardedSessionFactory</returns>
 public IShardedSessionFactory GetSessionFactory(IEnumerable<ShardId> shardIds, IShardStrategyFactory shardStrategyFactory)
 {
     return new Subset(this, shardIds, shardStrategyFactory);
 }
        /// <summary>
        /// Constructs a ShardedSessionFactoryImpl
        /// </summary>
        /// <param name="shardIdsBySessionFactory">Mapping of SessionFactories to shard ids.
        ///  When using virtual shards, this map associates SessionFactories (physical
        ///  shards) with virtual shards (shard ids).  Map cannot be empty.
        ///  Map keys cannot be null.  Map values cannot be null or empty.</param>
        /// <param name="shardStrategyFactory">factory that knows how to create the <see cref="IShardStrategy"/>
        ///  that will be used for all shard-related operations</param>
        /// <param name="classesWithoutTopLevelSaveSupport"> All classes that cannot be saved
        ///  as top-level objects</param>
        /// <param name="checkAllAssociatedObjectsForDifferentShards">Flag that controls
        ///  whether or not we do full cross-shard relationshp checking (very slow)</param>
        public ShardedSessionFactoryImpl(
            IDictionary <ISessionFactoryImplementor, ICollection <ShardId> > shardIdsBySessionFactory,
            IShardStrategyFactory shardStrategyFactory,
            IEnumerable <System.Type> classesWithoutTopLevelSaveSupport,
            bool checkAllAssociatedObjectsForDifferentShards)
        {
            Preconditions.CheckNotNull(shardIdsBySessionFactory);
            Preconditions.CheckNotNull(shardStrategyFactory);
            Preconditions.CheckNotNull(classesWithoutTopLevelSaveSupport);

            this.shardIdsBySessionFactory                    = new Dictionary <ISessionFactoryImplementor, ICollection <ShardId> >(shardIdsBySessionFactory);
            this.classesWithoutTopLevelSaveSupport           = new HashSet <System.Type>(classesWithoutTopLevelSaveSupport);
            this.checkAllAssociatedObjectsForDifferentShards = checkAllAssociatedObjectsForDifferentShards;

            var uniqueShardIds = new HashSet <ShardId>();

            foreach (var entry in shardIdsBySessionFactory)
            {
                ISessionFactoryImplementor implementor = entry.Key;
                Preconditions.CheckNotNull(implementor);

                var shardIdSet = entry.Value;
                Preconditions.CheckNotNull(shardIdSet);
                Preconditions.CheckState(!(shardIdSet.Count == 0));

                foreach (ShardId shardId in shardIdSet)
                {
                    //TODO: we should change it so we specify control shard in configuration
                    if (shardId.Id == CONTROL_SHARD_ID)
                    {
                        this.controlSessionFactory = implementor;
                    }
                    if (!uniqueShardIds.Add(shardId))
                    {
                        string msg = string.Format("Cannot have more than one shard with shard id {0}.", shardId.Id);
                        Log.Error(msg);
                        throw new HibernateException(msg);
                    }

                    if (!this.shardIdsBySessionFactory.ContainsKey(implementor))
                    {
                        this.shardIdsBySessionFactory.Add(implementor, new HashSet <ShardId>());
                    }
                    this.shardIdsBySessionFactory[implementor].Add(shardId);
                }
            }

            if (this.controlSessionFactory == null)
            {
                string message = string.Format(
                    CultureInfo.InvariantCulture,
                    "Cannot find control shard. Please ensure that one control shard exists with shard id '{0}'. " +
                    "A control shard is required for operations that cannot be distributed across shards, such as the " +
                    "generation of unique sequence numbers within the shard.",
                    CONTROL_SHARD_ID);
                Log.Error(message);
                throw new ArgumentException(message, "shardIdsBySessionFactory");
            }

            // now that we have all our shard ids, construct our shard strategy
            shardStrategy = shardStrategyFactory.NewShardStrategy(
                shardIdsBySessionFactory.SelectMany(c => c.Value));
            SetupIdGenerators();
        }
        /// <summary>
        /// Constructs a ShardedSessionFactoryImpl
        /// </summary>
        /// <param name="shardIdsBySessionFactory">Mapping of SessionFactories to shard ids. 
        ///  When using virtual shards, this map associates SessionFactories (physical
        ///  shards) with virtual shards (shard ids).  Map cannot be empty.
        ///  Map keys cannot be null.  Map values cannot be null or empty.</param>
        /// <param name="shardStrategyFactory">factory that knows how to create the <see cref="IShardStrategy"/> 
        ///  that will be used for all shard-related operations</param>
        /// <param name="classesWithoutTopLevelSaveSupport"> All classes that cannot be saved
        ///  as top-level objects</param>
        /// <param name="checkAllAssociatedObjectsForDifferentShards">Flag that controls
        ///  whether or not we do full cross-shard relationshp checking (very slow)</param>
        public ShardedSessionFactoryImpl(
            IDictionary<ISessionFactoryImplementor, ICollection<ShardId>> shardIdsBySessionFactory,
            IShardStrategyFactory shardStrategyFactory,
            IEnumerable<System.Type> classesWithoutTopLevelSaveSupport,
            bool checkAllAssociatedObjectsForDifferentShards)
        {
            Preconditions.CheckNotNull(shardIdsBySessionFactory);
            Preconditions.CheckNotNull(shardStrategyFactory);
            Preconditions.CheckNotNull(classesWithoutTopLevelSaveSupport);

            this.shardIdsBySessionFactory = new Dictionary<ISessionFactoryImplementor, ICollection<ShardId>>(shardIdsBySessionFactory);
            this.classesWithoutTopLevelSaveSupport = new HashSet<System.Type>(classesWithoutTopLevelSaveSupport);
            this.checkAllAssociatedObjectsForDifferentShards = checkAllAssociatedObjectsForDifferentShards;

            var uniqueShardIds = new HashSet<ShardId>();
            foreach (var entry in shardIdsBySessionFactory)
            {
                ISessionFactoryImplementor implementor = entry.Key;
                Preconditions.CheckNotNull(implementor);

                var shardIdSet = entry.Value;
                Preconditions.CheckNotNull(shardIdSet);
                Preconditions.CheckState(!(shardIdSet.Count == 0));

                foreach (ShardId shardId in shardIdSet)
                {
                    //TODO: we should change it so we specify control shard in configuration
                    if (shardId.Id == CONTROL_SHARD_ID)
                    {
                        this.controlSessionFactory = implementor;
                    }
                    if (!uniqueShardIds.Add(shardId))
                    {
                        string msg = string.Format("Cannot have more than one shard with shard id {0}.", shardId.Id);
                        Log.Error(msg);
                        throw new HibernateException(msg);
                    }

                    if (!this.shardIdsBySessionFactory.ContainsKey(implementor))
                    {
                        this.shardIdsBySessionFactory.Add(implementor, new HashSet<ShardId>());
                    }
                    this.shardIdsBySessionFactory[implementor].Add(shardId);
                }
            }

            if (this.controlSessionFactory == null)
            {
                string message = string.Format(
                    CultureInfo.InvariantCulture,
                    "Cannot find control shard. Please ensure that one control shard exists with shard id '{0}'. " +
                    "A control shard is required for operations that cannot be distributed across shards, such as the " +
                    "generation of unique sequence numbers within the shard.",
                    CONTROL_SHARD_ID);
                Log.Error(message);
                throw new ArgumentException(message, "shardIdsBySessionFactory");
            }

            // now that we have all our shard ids, construct our shard strategy
            shardStrategy = shardStrategyFactory.NewShardStrategy(
                shardIdsBySessionFactory.SelectMany(c => c.Value));
            SetupIdGenerators();
        }
 public Subset(
     ShardedSessionFactoryImpl parent,
     IEnumerable<ShardId> shardIds,
     IShardStrategyFactory shardStrategyFactory)
     : base(parent, shardIds, shardStrategyFactory)
 {
 }