예제 #1
0
        public override async ETTask Run()
        {
            DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>();

            try
            {
                int      s  = 0;
                string[] st = LSS.Split('|');
                Json = st[0];
                int.TryParse(st[1], out s);
                skip = s;
                int.TryParse(st[2], out s);
                limit = s;
                sort  = st[3];
                //Log.Debug("DBQueryLSSJsonTask Json: " + Json);
                //Log.Debug("DBQueryLSSJsonTask 从第几个拿skip: " + skip);
                //Log.Debug("DBQueryLSSJsonTask 获取多少个limit: "+limit);
                //Log.Debug("DBQueryLSSJsonTask Sort: "+ sort);
                // 执行查询数据库任务
                FilterDefinition <ComponentWithId> filterDefinition = new JsonFilterDefinition <ComponentWithId>(this.Json);
                IAsyncCursor <ComponentWithId>     cursor           = await dbComponent.GetCollection(this.CollectionName).Find(filterDefinition).Skip(skip).Limit(limit).Sort(sort).ToCursorAsync();

                List <ComponentWithId> components = await cursor.ToListAsync();

                this.Tcs.SetResult(components);
            }
            catch (Exception e)
            {
                this.Tcs.SetException(new Exception($"查询数据库异常! {CollectionName} {this.Json}", e));
            }
        }
예제 #2
0
        public override async ETTask Run()
        {
            DBComponent            dbComponent = Game.Scene.GetComponent <DBComponent>();
            List <ComponentWithId> result      = new List <ComponentWithId>();

            try
            {
                // 执行查询数据库任务
                foreach (long id in IdList)
                {
                    IAsyncCursor <ComponentWithId> cursor = await dbComponent.GetCollection(this.CollectionName).FindAsync((s) => s.Id == id);

                    ComponentWithId component = await cursor.FirstOrDefaultAsync();

                    if (component == null)
                    {
                        continue;
                    }
                    result.Add(component);
                }

                this.Tcs.SetResult(result);
            }
            catch (Exception e)
            {
                this.Tcs.SetException(new Exception($"查询数据库异常! {this.CollectionName} {IdList.ListToString()}", e));
            }
        }
예제 #3
0
        public override async Task Run()
        {
            DBCacheComponent dbCacheComponent = Game.Scene.GetComponent <DBCacheComponent>();
            DBComponent      dbComponent      = Game.Scene.GetComponent <DBComponent>();
            List <Component> result           = new List <Component>();

            try
            {
                // 执行查询数据库任务
                foreach (long id in IdList)
                {
                    Component disposer = dbCacheComponent.GetFromCache(this.CollectionName, id);
                    if (disposer == null)
                    {
                        disposer = await dbComponent.GetCollection(this.CollectionName).FindAsync((s) => s.Id == id).Result.FirstOrDefaultAsync();

                        dbCacheComponent.AddToCache(disposer);
                    }

                    if (disposer == null)
                    {
                        continue;
                    }
                    result.Add(disposer);
                }

                this.Tcs.SetResult(result);
            }
            catch (Exception e)
            {
                this.Tcs.SetException(new Exception($"查询数据库异常! {this.CollectionName} {IdList.ListToString()}", e));
            }
        }
예제 #4
0
        public override async Task Run()
        {
            DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>();

            foreach (Component disposer in this.Disposers)
            {
                if (disposer == null)
                {
                    continue;
                }

                try
                {
                    // 执行保存数据库任务
                    await dbComponent.GetCollection(this.CollectionName).ReplaceOneAsync(s => s.Id == disposer.Id, disposer, new UpdateOptions {
                        IsUpsert = true
                    });
                }
                catch (Exception e)
                {
                    Log.Debug($"{disposer.GetType().Name} {disposer.ToJson()} {e}");
                    this.Tcs.SetException(new Exception($"保存数据失败! {CollectionName} {this.Disposers.ListToString()}", e));
                }
            }
            this.Tcs.SetResult(true);
        }
예제 #5
0
        public override async Task Run()
        {
            DBCacheComponent dbCacheComponent = Game.Scene.GetComponent <DBCacheComponent>();
            DBComponent      dbComponent      = Game.Scene.GetComponent <DBComponent>();
            // 执行查询前先看看cache中是否已经存在
            ComponentWithId component = dbCacheComponent.GetFromCache(this.CollectionName, this.Id);

            if (component != null)
            {
                this.Tcs.SetResult(component);
                return;
            }
            try
            {
                // 执行查询数据库任务
                component = await dbComponent.GetCollection(this.CollectionName).FindAsync((s) => s.Id == this.Id).Result.FirstOrDefaultAsync();

                if (component != null)
                {
                    dbCacheComponent.AddToCache(component);
                }
                this.Tcs.SetResult(component);
            }
            catch (Exception e)
            {
                this.Tcs.SetException(new Exception($"查询数据库异常! {CollectionName} {Id}", e));
            }
        }
예제 #6
0
        public static async ETTask <T> Query <T>(this DBComponent self, long id, string collection = null) where T : Entity
        {
            using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, id % DBComponent.TaskCount))
            {
                IAsyncCursor <T> cursor = await self.GetCollection <T>(collection).FindAsync(d => d.Id == id);

                return(await cursor.FirstOrDefaultAsync());
            }
        }
예제 #7
0
        public static async ETTask <long> Remove <T>(this DBComponent self, Expression <Func <T, bool> > filter, string collection = null) where T : Entity
        {
            using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, RandomHelper.RandInt64() % DBComponent.TaskCount))
            {
                DeleteResult result = await self.GetCollection <T>(collection).DeleteManyAsync(filter);

                return(result.DeletedCount);
            }
        }
예제 #8
0
 /// <summary>
 /// 根据表达式删除
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="self"></param>
 /// <param name="exp"></param>
 /// <returns></returns>
 public static async ETTask DeleteAll <T>(this DBProxyComponent self, Expression <Func <T, bool> > exp)
 {
     DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>();
     ExpressionFilterDefinition <T> filter             = new ExpressionFilterDefinition <T>(exp);
     IBsonSerializerRegistry        serializerRegistry = BsonSerializer.SerializerRegistry;
     IBsonSerializer <T>            documentSerializer = serializerRegistry.GetSerializer <T>();
     string json = filter.Render(documentSerializer, serializerRegistry).ToJson();
     await dbComponent.GetCollection(typeof(T).Name).FindOneAndDeleteAsync(json);
 }
예제 #9
0
        public static async ETTask <long> Remove <T>(this DBComponent self, long taskId, long id, string collection = null) where T : Entity
        {
            using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, taskId % DBComponent.TaskCount))
            {
                DeleteResult result = await self.GetCollection <T>(collection).DeleteOneAsync(d => d.Id == id);

                return(result.DeletedCount);
            }
        }
예제 #10
0
        public static async ETTask <List <T> > Query <T>(this DBComponent self, long taskId, Expression <Func <T, bool> > filter, string collection = null)
            where T : Entity
        {
            using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, taskId % DBComponent.TaskCount))
            {
                IAsyncCursor <T> cursor = await self.GetCollection <T>(collection).FindAsync(filter);

                return(await cursor.ToListAsync());
            }
        }
예제 #11
0
        public static async ETTask <List <T> > QueryJson <T>(this DBComponent self, long taskId, string json, string collection = null) where T : Entity
        {
            using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, RandomHelper.RandInt64() % DBComponent.TaskCount))
            {
                FilterDefinition <T> filterDefinition = new JsonFilterDefinition <T>(json);
                IAsyncCursor <T>     cursor           = await self.GetCollection <T>(collection).FindAsync(filterDefinition);

                return(await cursor.ToListAsync());
            }
        }
예제 #12
0
        public static async ETTask InsertBatch <T>(this DBComponent self, IEnumerable <T> list, string collection = null) where T : Entity
        {
            if (collection == null)
            {
                collection = typeof(T).Name;
            }

            using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, RandomHelper.RandInt64() % DBComponent.TaskCount))
            {
                await self.GetCollection(collection).InsertManyAsync(list);
            }
        }
        public override async ETTask Run()
        {
            DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>();

            try
            {
                var result = await dbComponent.GetCollection(this.CollectionName).DeleteOneAsync(s => s.Id == this.Id);

                //enforces to throw a exception when 'result.IsAcknowledged' is equal to false
                this.Tcs.SetResult(result.DeletedCount > 0);
            }
            catch (Exception e)
            {
                this.Tcs.SetException(new Exception($"deletes document failed on collection = {CollectionName} with id = {this.Id}", e));
            }
        }
예제 #14
0
        public override async ETTask Run()
        {
            DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>();

            try
            {
                // 执行保存数据库任务
                DeleteResult deleteResult = await dbComponent.GetCollection(this.CollectionName).DeleteManyAsync(s => s.Id == this.Id);

                this.Tcs.SetResult(deleteResult.DeletedCount);
            }
            catch (Exception e)
            {
                this.Tcs.SetException(new Exception($"删除数据失败!  {CollectionName} {Id}", e));
            }
        }
        public override async ETTask Run()
        {
            DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>();

            try
            {
                FilterDefinition <ComponentWithId> filterDefinition = new JsonFilterDefinition <ComponentWithId>(this.Json);
                long count = await dbComponent.GetCollection(this.CollectionName).CountDocumentsAsync(filterDefinition);

                this.Tcs.SetResult(count);
            }
            catch (Exception e)
            {
                this.Tcs.SetException(new Exception($"查询数据库异常! {CollectionName} {this.Json}", e));
            }
        }
예제 #16
0
        public override async ETTask Run()
        {
            DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>();

            try
            {
                FilterDefinition <ComponentWithId> filter = new JsonFilterDefinition <ComponentWithId>(this.Json);
                await dbComponent.GetCollection(this.CollectionName).DeleteManyAsync(filter);

                this.Tcs.SetResult();
            }
            catch (Exception e)
            {
                this.Tcs.SetException(new Exception($"删除数据库异常! {CollectionName} {this.Json}", e));
            }
        }
        public override async ETTask Run()
        {
            DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>();

            try
            {
                IAsyncCursor <ComponentWithId> cursor = await dbComponent.GetCollection(this.CollectionName).AggregateAsync(pipeline);

                List <ComponentWithId> components = await cursor.ToListAsync();

                this.Tcs.SetResult(components);
            }
            catch (Exception e)
            {
                this.Tcs.SetException(new Exception($"查询数据库异常! {CollectionName} {this.pipeline.ToString()}", e));
            }
        }
예제 #18
0
        public override async Task Run()
        {
            DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>();

            try
            {
                // 执行查询数据库任务
                FilterDefinition <Component> filterDefinition = new JsonFilterDefinition <Component>(this.Json);
                List <Component>             disposers        = await dbComponent.GetCollection(this.CollectionName).FindAsync(filterDefinition).Result.ToListAsync();

                this.Tcs.SetResult(disposers);
            }
            catch (Exception e)
            {
                this.Tcs.SetException(new Exception($"查询数据库异常! {CollectionName} {this.Json}", e));
            }
        }
예제 #19
0
        public override async ETTask Run()
        {
            DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>();

            try
            {
                var result = await dbComponent.GetCollection(this.CollectionName).ReplaceOneAsync(s => s.Id == entity.Id, entity, new UpdateOptions {
                    IsUpsert = true
                });

                //enforces to throw a exception when 'result.IsAcknowledged' is equal to false
                this.Tcs.SetResult(result.ModifiedCount > 0 ? entity : null);
            }
            catch (Exception e)
            {
                this.Tcs.SetException(new Exception($"upserts document failed on collection = {CollectionName} with id = {entity.Id}", e));
            }
        }
예제 #20
0
        public override async ETTask Run()
        {
            DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>();

            try
            {
                // 执行查询数据库任务
                IAsyncCursor <ComponentWithId> cursor = await dbComponent.GetCollection(this.CollectionName).FindAsync((s) => s.Id == this.Id);

                ComponentWithId component = await cursor.FirstOrDefaultAsync();

                this.Tcs.SetResult(component);
            }
            catch (Exception e)
            {
                this.Tcs.SetException(new Exception($"查询数据库异常! {CollectionName} {Id}", e));
            }
        }
예제 #21
0
        public override async Task Run()
        {
            DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>();

            try
            {
                // 执行保存数据库任务
                await dbComponent.GetCollection(this.CollectionName).ReplaceOneAsync(s => s.Id == this.Disposer.Id, this.Disposer, new UpdateOptions {
                    IsUpsert = true
                });

                this.Tcs.SetResult(true);
            }
            catch (Exception e)
            {
                this.Tcs.SetException(new Exception($"保存数据失败!  {CollectionName} {Id}", e));
            }
        }
예제 #22
0
        public override async ETTask Run()
        {
            DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>();

            try
            {
                // 执行查询数据库任务
                FilterDefinition <ComponentWithId> filterDefinition = new JsonFilterDefinition <ComponentWithId>(this.Json);
                IAsyncCursor <ComponentWithId>     cursor           = await dbComponent.GetCollection(this.CollectionName).FindAsync(filterDefinition);

                List <ComponentWithId> components = await cursor.ToListAsync();

                this.Tcs.SetResult(components);
            }
            catch (Exception e)
            {
                this.Tcs.SetException(new Exception($"查询数据库异常! {CollectionName} {this.Json}", e));
            }
        }
예제 #23
0
        public override async ETTask Run()
        {
            DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>();

            List <ComponentWithId> components = new List <ComponentWithId>();

            try
            {
                FilterDefinitionBuilder <ComponentWithId> builderFilter = Builders <ComponentWithId> .Filter;
                components = dbComponent.GetCollection(this.CollectionName).Find(builderFilter.Empty).Skip(page).Limit(dbsize).ToList();
                await Task.Delay(0);

                this.Tcs.SetResult(components);
            }
            catch (Exception e)
            {
                this.Tcs.SetException(new Exception($"查询数据库异常! {CollectionName} {Id}", e));
            }
        }
예제 #24
0
        public static async ETTask Save <T>(this DBComponent self, long taskId, T entity, string collection = null) where T : Entity
        {
            if (entity == null)
            {
                Log.Error($"save entity is null: {typeof (T).Name}");

                return;
            }

            if (collection == null)
            {
                collection = entity.GetType().Name;
            }

            using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, taskId % DBComponent.TaskCount))
            {
                await self.GetCollection(collection).ReplaceOneAsync(d => d.Id == entity.Id, entity, new UpdateOptions {
                    IsUpsert = true
                });
            }
        }
예제 #25
0
        public override async ETTask Run()
        {
            DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>();
            long        deleteCount = 0;

            try
            {
                // 执行删除数据库任务
                foreach (long id in IdList)
                {
                    DeleteResult deleteResult = await dbComponent.GetCollection(this.CollectionName).DeleteManyAsync((s) => s.Id == id);

                    deleteCount += deleteResult.DeletedCount;
                }

                this.Tcs.SetResult(deleteCount);
            }
            catch (Exception e)
            {
                this.Tcs.SetException(new Exception($"删除数据库异常! {this.CollectionName} {IdList.ListToString()}", e));
            }
        }
예제 #26
0
        public override async ETTask Run()
        {
            DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>();

            try
            {
                BsonDocument bsons     = BsonSerializer.Deserialize <BsonDocument>(this.Json);
                string       QueryJson = bsons["QueryJson"].ToJson();
                var          set       = bsons["UpdateJson"];
                bsons.Clear();
                bsons["$set"] = set;
                string UpdateJson = bsons.ToJson();
                FilterDefinition <ComponentWithId> queryFilter  = new JsonFilterDefinition <ComponentWithId>(QueryJson);
                UpdateDefinition <ComponentWithId> updateFilter = new JsonUpdateDefinition <ComponentWithId>(UpdateJson);
                await dbComponent.GetCollection(this.CollectionName).UpdateOneAsync(queryFilter, updateFilter);

                this.Tcs.SetResult();
            }
            catch (Exception e)
            {
                this.Tcs.SetException(new Exception($"更改数据库异常! {CollectionName} {this.Json}", e));
            }
        }
예제 #27
0
        public static async ETTask Save(this DBComponent self, long id, List <Entity> entities)
        {
            if (entities == null)
            {
                Log.Error($"save entity is null");
                return;
            }

            using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, id % DBComponent.TaskCount))
            {
                foreach (Entity entity in entities)
                {
                    if (entity == null)
                    {
                        continue;
                    }

                    await self.GetCollection(entity.GetType().Name)
                    .ReplaceOneAsync(d => d.Id == entity.Id, entity, new UpdateOptions {
                        IsUpsert = true
                    });
                }
            }
        }
예제 #28
0
        public static async ETTask Query(this DBComponent self, long id, List <string> collectionNames, List <Entity> result)
        {
            if (collectionNames == null || collectionNames.Count == 0)
            {
                return;
            }

            using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, id % DBComponent.TaskCount))
            {
                foreach (string collectionName in collectionNames)
                {
                    IAsyncCursor <Entity> cursor = await self.GetCollection(collectionName).FindAsync(d => d.Id == id);

                    Entity e = await cursor.FirstOrDefaultAsync();

                    if (e == null)
                    {
                        continue;
                    }

                    result.Add(e);
                }
            }
        }
예제 #29
0
        public override async Task Run()
        {
            DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>();

            try
            {
                // 执行查询数据库任务
                //  FilterDefinition<ComponentWithId> filterDefinition = new JsonFilterDefinition<ComponentWithId>(this.QueryJson);
                //  IAsyncCursor<ComponentWithId> cursor = await dbComponent.GetCollection(this.CollectionName).FindAsync(filterDefinition);
                // List<ComponentWithId> components = await cursor.ToListAsync();
                //  this.Tcs.SetResult(components);

                FilterDefinition <ComponentWithId>             filterDefinition = new JsonFilterDefinition <ComponentWithId>(this.QueryJson);
                SortDefinition <ComponentWithId>               sortDefinition   = new JsonSortDefinition <ComponentWithId>(this.SortJson);
                IFindFluent <ComponentWithId, ComponentWithId> ifindiluent      = dbComponent.GetCollection(this.CollectionName).Find(filterDefinition).Sort(sortDefinition).Limit(this.Count);
                List <ComponentWithId> components = await ifindiluent.ToCursor().ToListAsync();

                this.Tcs.SetResult(components);
            }
            catch (Exception e)
            {
                this.Tcs.SetException(new Exception($"查询数据库异常! {CollectionName} {this.QueryJson}", e));
            }
        }
예제 #30
0
 /// <summary>
 /// 清空表
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="self"></param>
 /// <returns></returns>
 public static async ETTask DeleteAll <T>(this DBProxyComponent self)
 {
     DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>();
     var         filter      = Builders <ComponentWithId> .Filter.Empty;
     await dbComponent.GetCollection(typeof(T).Name).DeleteManyAsync(filter);
 }