예제 #1
0
        /// <summary>
        /// 根据键值获取所在的数据库
        /// </summary>
        /// <param name="keyvalue"></param>
        /// <returns></returns>
        public DbContext GetDbContextByKey(long keyvalue)
        {
            DbShardingItem dbShardingItem = null;

            foreach (var item in ShardingInfos)
            {
                if (keyvalue >= item.IdMin && keyvalue <= item.IdMax)
                {
                    dbShardingItem = item;
                    break;
                }
            }

            if (dbShardingItem == null)
            {
                throw new Exception($" keyvalue '{keyvalue}' out of sharding range");
            }

            //根据id求模,选出数据库
            var index = (int)(keyvalue % (dbShardingItem.Servers.Count()));

            var dbserver = dbShardingItem.Servers[index];

            DbContext dbContext = GetDbContext(dbserver);

            return(dbContext);
        }
예제 #2
0
        public async Task <T> FindAsync <T> (long id)
            where T : class
        {
            DbShardingItem dbShardingItem = null;

            foreach (var item in ShardingInfos)
            {
                if (id >= item.IdMin && id <= item.IdMax)
                {
                    dbShardingItem = item;
                    break;
                }
            }

            if (dbShardingItem == null)
            {
                throw new Exception($" idvalue '{id}' out of sharding range");
            }

            //hash模式
            if (dbShardingItem.ReadMode == ShardingReadMode.Hash)
            {
                //根据id求模,选出数据库
                var index = (int)(id % (dbShardingItem.Servers.Count()));

                var dbserver = dbShardingItem.Servers[index];

                DbContext dbContext = GetDbContext(dbserver);

                return(await dbContext.FindAsync <T>(id));
            }
            //完全模式
            else
            {
                List <Task <T> > tasks = new List <Task <T> >();
                foreach (var dbserver in dbShardingItem.Servers)
                {
                    DbContext dbContext = GetDbContext(dbserver);

                    tasks.Add(dbContext.FindAsync <T>(id));
                }

                var result = await Task.WhenAll(tasks);

                return(result.FirstOrDefault());
            }
        }
        public List <DbShardingItem> GetDbShardingItems( )
        {
            List <DbShardingItem> dbShardingItems = new List <DbShardingItem>();

            foreach (var item in this)
            {
                DbShardingItem dbShardingItem = new DbShardingItem(item.Servers.Select(t => new DbInfo
                {
                    Name             = t.Key,
                    ConnectionString = t.Value
                }).ToList());
                dbShardingItem.IdMax    = item.IdMax;
                dbShardingItem.IdMin    = item.IdMin;
                dbShardingItem.ReadMode = item.ReadMode;

                dbShardingItems.Add(dbShardingItem);
            }

            return(dbShardingItems);
        }