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)); } }
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)); } }
public Task <List <ComponentWithId> > GetBatch(string collectionName, List <long> idList) { List <ComponentWithId> components = new List <ComponentWithId>(); bool isAllInCache = true; foreach (long id in idList) { ComponentWithId component = this.GetFromCache(collectionName, id); if (component == null) { isAllInCache = false; break; } components.Add(component); } if (isAllInCache) { return(Task.FromResult(components)); } TaskCompletionSource <List <ComponentWithId> > tcs = new TaskCompletionSource <List <ComponentWithId> >(); DBQueryBatchTask dbQueryBatchTask = ComponentFactory.Create <DBQueryBatchTask, List <long>, string, TaskCompletionSource <List <ComponentWithId> > >(idList, collectionName, tcs); this.tasks[(int)((ulong)dbQueryBatchTask.Id % taskCount)].Add(dbQueryBatchTask); return(tcs.Task); }
/// <summary> /// 創造 /// </summary> /// <param name="collectionName"></param> /// <param name="entity"></param> /// <returns></returns> public ETTask <ComponentWithId> Create(string collectionName, ComponentWithId entity) { ETTaskCompletionSource <ComponentWithId> tcs = new ETTaskCompletionSource <ComponentWithId>(); CacheCreateTask cacheCreateTask = ComponentFactory.Create <CacheCreateTask, string, ComponentWithId, ETTaskCompletionSource <ComponentWithId> >(collectionName, entity, tcs); this.tasks[(int)((ulong)cacheCreateTask.Id % taskCount)].Add(cacheCreateTask); return(tcs.Task); }
/// <summary> /// 泛型 创建组件 带三个个参数用于 Awake初始化 /// </summary> /// <typeparam name="T">组件类型</typeparam> /// <typeparam name="A">参数类型a</typeparam> /// <typeparam name="B">参数类型b</typeparam> /// <typeparam name="C">参数类型c</typeparam> /// <param name="a"></param> /// <param name="b"></param> /// <param name="c"></param> /// <returns></returns> public static T Create <T, A, B, C>(A a, B b, C c) where T : Component { T component = Game.ObjectPool.Fetch <T>(); ComponentWithId componentWithId = component as ComponentWithId; if (componentWithId != null) { componentWithId.Id = component.InstanceId; } Game.EventSystem.Awake(component, a, b, c); return(component); }
/// <summary> /// 泛型 创建组件 /// </summary> /// <param name="type">需创建的组件类型</param> /// <returns>返回的是指定的T类型</returns> public static T Create <T>() where T : Component { T component = Game.ObjectPool.Fetch <T>(); ComponentWithId componentWithId = component as ComponentWithId; //如果他是一个带ID的组件 if (componentWithId != null) { componentWithId.Id = component.InstanceId; //将他的ID = 唯一的InstanceId } Game.EventSystem.Awake(component); return(component); }
/// <summary> /// 创建组件并指定其父组件(实体) /// </summary> /// <param name="type">需创建的组件类型</param> /// <param name="parent">父组件(实体)</param> /// <returns>返回的是Component类型</returns> public static Component CreateWithParent(Type type, Component parent) { Component component = Game.ObjectPool.Fetch(type); //从池中生成 component.Parent = parent; //制定父组件(实体) ComponentWithId componentWithId = component as ComponentWithId; //如果他是一个带ID的组件 if (componentWithId != null) { componentWithId.Id = component.InstanceId; //将他的ID = 唯一的InstanceId } Game.EventSystem.Awake(component); //调用初始化Awake方法 return(component); }
/// <summary> /// 泛型 创建组件并指定其父组件(实体) 可带两个参数 用于Awake初始化 /// </summary> /// <typeparam name="T">类型</typeparam> /// <typeparam name="A">参数类型1</typeparam> /// <typeparam name="B">参数类型2</typeparam> /// <param name="parent">父组件(实体)</param> /// <param name="a">传递参数a</param> /// <param name="b">传递参数b</param> /// <returns>返回的是指定的T类型</returns> public static T CreateWithParent <T, A, B>(Component parent, A a, B b) where T : Component { T component = Game.ObjectPool.Fetch <T>(); component.Parent = parent; ComponentWithId componentWithId = component as ComponentWithId; //如果他是一个带ID的组件 if (componentWithId != null) { componentWithId.Id = component.InstanceId; //将他的ID = 唯一的InstanceId } Game.EventSystem.Awake(component, a, b); return(component); }
public static Component CreateWithParent(Type type, Component parent) { Component component = Game.ObjectPool.Fetch(type); component.Parent = parent; ComponentWithId componentWithId = component as ComponentWithId; if (componentWithId != null) { componentWithId.Id = component.InstanceId; } Game.EventSystem.Awake(component); return(component); }
public static T CreateWithParent <T, A>(Component parent, A a) where T : Component { T component = Game.ObjectPool.Fetch <T>(); component.Parent = parent; ComponentWithId componentWithId = component as ComponentWithId; if (componentWithId != null) { componentWithId.Id = component.InstanceId; } Game.EventSystem.Awake(component, a); return(component); }
public Task <bool> Add(ComponentWithId component, string collectionName = "") { TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>(); if (string.IsNullOrEmpty(collectionName)) { collectionName = component.GetType().Name; } DBSaveTask task = ComponentFactory.CreateWithId <DBSaveTask, ComponentWithId, string, TaskCompletionSource <bool> >(component.Id, component, collectionName, tcs); this.tasks[(int)((ulong)task.Id % taskCount)].Add(task); return(tcs.Task); }
/// <summary> /// 添加组件到缓存中 /// </summary> /// <param name="component"></param> /// <param name="collectionName"></param> public void AddToCache(ComponentWithId component, string collectionName = "") { if (string.IsNullOrEmpty(collectionName)) { collectionName = component.GetType().Name; } Dictionary <long, ComponentWithId> collection; // 保存组件ID 和组件 if (!this.cache.TryGetValue(collectionName, out collection)) //如果字典中没有这个类型 { collection = new Dictionary <long, ComponentWithId>(); this.cache.Add(collectionName, collection); //组件类型 和 嵌套的字典( 保存组件ID 和组件) } collection[component.Id] = component; //写入字典 }
public void AddToCache(ComponentWithId component, string collectionName = "") { if (string.IsNullOrEmpty(collectionName)) { collectionName = component.GetType().Name; } Dictionary <long, ComponentWithId> collection; if (!this.cache.TryGetValue(collectionName, out collection)) { collection = new Dictionary <long, ComponentWithId>(); this.cache.Add(collectionName, collection); } collection[component.Id] = component; }
public Task <ComponentWithId> Get(string collectionName, long id) { ComponentWithId component = GetFromCache(collectionName, id); if (component != null) { return(Task.FromResult(component)); } TaskCompletionSource <ComponentWithId> tcs = new TaskCompletionSource <ComponentWithId>(); DBQueryTask dbQueryTask = ComponentFactory.CreateWithId <DBQueryTask, string, TaskCompletionSource <ComponentWithId> >(id, collectionName, tcs); this.tasks[(int)((ulong)id % taskCount)].Add(dbQueryTask); return(tcs.Task); }
public override async ETTask Run() { var cacheComponent = Game.Scene.GetComponent <CacheComponent>(); var cache = cacheComponent.GetCache(CollectionName); try { ComponentWithId res = await cache.FindOneByUnique(cache.type, UniqueName, BsonSerializer.Deserialize <BsonDocument>(Json)); this.Tcs.SetResult(res); } catch (Exception e) { this.Tcs.SetException(new Exception($"deletes cache failed on class = {CollectionName} with id = {this.Id}", e)); } }
public override async ETTask Run() { var cacheComponent = Game.Scene.GetComponent <CacheComponent>(); var cache = cacheComponent.GetCache(CollectionName); try { ComponentWithId res = await cache.FindOne(cache.type, Id, Fields); this.Tcs.SetResult(res); } catch (Exception e) { this.Tcs.SetException(new Exception($"deletes cache failed on class = {CollectionName} with id = {this.Id}", e)); } }
public override async ETTask Run() { var cacheComponent = Game.Scene.GetComponent <CacheComponent>(); var cache = cacheComponent.GetCache(CollectionName); try { ComponentWithId res = await cache.Update(cache.type, Id, BsonSerializer.Deserialize <BsonDocument>(DataJson)); this.Tcs.SetResult(res); } catch (Exception e) { this.Tcs.SetException(new Exception($"update cache by id failed on class = {CollectionName} with id = {this.Id}", e)); } }
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)); } }
public override async ETTask Run() { DBEXComponent dbComponent = Game.Scene.GetComponent <DBEXComponent>(); try { var filter = CreateFilter(); IAsyncCursor <ComponentWithId> cursor = await dbComponent.GetCollection <ComponentWithId>(this.CollectionName).FindAsync(filter); ComponentWithId component = await cursor.FirstOrDefaultAsync(); this.Tcs.SetResult(component); } catch (Exception e) { this.Tcs.SetException(new Exception($"find one failed on collection = {CollectionName} with" + $" {GetCondition()}", e)); } }
public async Task SaveVideo(ComponentWithId component) { await dbProxyComponent.Save(component); }
public static async Task Save(this DBProxyComponent self, ComponentWithId component, bool needCache = true) { Session session = Game.Scene.GetComponent <NetInnerComponent>().Get(self.dbAddress); await session.Call(new DBSaveRequest { Component = component, NeedCache = needCache }); }
public async void SaveLog(ComponentWithId component) { Session session = Game.Scene.GetComponent <NetInnerComponent>().Get(dbAddress); await session.Call(new DBSaveRequest { Component = component, NeedCache = false, CollectionName = "Log" }); }
public async Task Save(ComponentWithId component, bool needCache, CancellationToken cancellationToken) { Session session = Game.Scene.GetComponent <NetInnerComponent>().Get(dbAddress); await session.Call(new DBSaveRequest { Component = component, NeedCache = needCache }, cancellationToken); }
public static async ETVoid SaveLog(this DBProxyComponent self, ComponentWithId component) { Session session = Game.Scene.GetComponent <NetInnerComponent>().Get(self.dbAddress); await session.Call(new DBSaveRequest { Component = component, CollectionName = "Log" }); }
public static async ETTask Save(this DBProxyComponent self, ComponentWithId component) { Session session = Game.Scene.GetComponent <NetInnerComponent>().Get(self.dbAddress); await session.Call(new DBSaveRequest { Component = component }); }
public override void Awake(DBSaveTask self, ComponentWithId component, string collectionName, ETTaskCompletionSource <bool> tcs) { self.Component = component; self.CollectionName = collectionName; self.Tcs = tcs; }