Пример #1
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));
            }
        }
Пример #2
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));
            }
        }
Пример #3
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);
        }
Пример #4
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));
            }
        }
Пример #5
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));
            }
        }
Пример #6
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);
            }
        }
Пример #7
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());
            }
        }
Пример #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);
 }
        public static ETTask <bool> Delete <T>(this DBComponent db, string collectionName, long id) where T : ComponentWithId
        {
            ETTaskCompletionSource <bool> tcs = new ETTaskCompletionSource <bool>();
            DBDeleteTask <T> task             = ComponentFactory.CreateWithId <DBDeleteTask <T>, string, ETTaskCompletionSource <bool> >
                                                    (id, collectionName, tcs);

            db.tasks[(int)((ulong)task.Id % DBComponent.taskCount)].Add(task);
            return(tcs.Task);
        }
        public static ETTask <T> Upsert <T>(this DBComponent db, string collectionName, T entity) where T : ComponentWithId
        {
            ETTaskCompletionSource <T> tcs  = new ETTaskCompletionSource <T>();
            DBUpsertTask <T>           task = ComponentFactory.Create <DBUpsertTask <T>, string, T, ETTaskCompletionSource <T> >
                                                  (collectionName, entity, tcs);

            db.tasks[(int)((ulong)task.Id % DBComponent.taskCount)].Add(task);
            return(tcs.Task);
        }
Пример #11
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);
            }
        }
Пример #12
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());
            }
        }
Пример #13
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());
            }
        }
        public static ETTask <List <T> > FindAllByIndex <T>(this DBComponent db, string collectionName, string indexName, T entity) where T : ComponentWithId
        {
            ETTaskCompletionSource <List <T> > tcs = new ETTaskCompletionSource <List <T> >();
            var parameters = DBHelper.GetParameterList(typeof(T), indexName, entity);
            DBQueryAllWithIndexTask task = ComponentFactory.Create <DBQueryAllWithIndexTask, string, List <Tuple <PropertyInfo, object> >, ETTaskCompletionSource <List <T> > >
                                               (collectionName, parameters, tcs);

            db.tasks[(int)((ulong)task.Id % DBComponent.taskCount)].Add(task);
            return(tcs.Task);
        }
Пример #15
0
        public static async ETVoid SaveNotWait <T>(this DBComponent self, T entity, long taskId = 0, string collection = null) where T : Entity
        {
            if (taskId == 0)
            {
                await self.Save(entity, collection);

                return;
            }

            await self.Save(taskId, entity, collection);
        }
Пример #16
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));
            }
        }
Пример #18
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));
            }
        }
Пример #19
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
            {
                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));
            }
        }
Пример #21
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));
            }
        }
        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));
            }
        }
Пример #23
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));
            }
        }
Пример #24
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));
            }
        }
Пример #25
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));
            }
        }
Пример #26
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));
            }
        }
Пример #27
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));
            }
        }
Пример #28
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
                });
            }
        }
Пример #29
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));
            }
        }
Пример #30
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));
            }
        }