public static async ETTask <long> GetIncrementId <T>(this DBComponent self) { var collection = self.GetCollection <CollectionIdentity>(); CollectionIdentity id = await collection.FindOneAndUpdateAsync(Builders <CollectionIdentity> .Filter.Eq(f => f.Id, typeof(T).Name), Builders <CollectionIdentity> .Update.Inc(f => f.Value, 1), options); return(id.Value); }
public static async Task <long> QueryJsonCount <T>(this DBProxyComponent self, string json) { DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>(); FilterDefinition <ComponentWithId> filterDefinition = new JsonFilterDefinition <ComponentWithId>(json); long count = await dbComponent.GetCollection(typeof(T).Name).CountAsync(filterDefinition); return(count); }
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()); } }
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); } }
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); } }
/// <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 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 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()); } }
public static void Update <T>(this DBProxyComponent self, string fieldname, string fieldvalue, string aaa) { //第一个参数是需要更改的在数据库中的对象 第二个是要更改的对象的值 第三个是更改后的对象的值 对象也可以改 多传点参数就行 DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>(); var filter1 = Builders <ComponentWithId> .Filter.Eq(fieldname, fieldvalue); var updata1 = Builders <ComponentWithId> .Update.Set(fieldname, aaa); dbComponent.GetCollection(typeof(T).Name).UpdateOne(filter1, updata1); }
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 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 }); } }
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); } } }
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 }); } } }
/// <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); }
public static async ETTask Delete <T>(this DBProxyComponent self, long id) { DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>(); await dbComponent.GetCollection(typeof(T).Name).DeleteOneAsync(i => i.Id == id); }
protected override async void Run(Session session, C2G_Email message, Action <G2C_Email> reply) { G2C_Email response = new G2C_Email(); try { DBProxyComponent proxyComponent = Game.Scene.GetComponent <DBProxyComponent>(); DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>(); FilterDefinition <ComponentWithId> filterDefinition = new JsonFilterDefinition <ComponentWithId>($"{{UId:{message.Uid}}}"); List <ComponentWithId> components = await dbComponent.GetCollection(typeof(EmailInfo).Name).FindAsync(filterDefinition).Result.ToListAsync(); List <EmailInfo> listEmail = new List <EmailInfo>(); // 未读 for (int i = components.Count - 1; i >= 0; i--) { if (((EmailInfo)components[i]).State == 0) { listEmail.Add((EmailInfo)components[i]); if (listEmail.Count >= 50) { break; } } } // 已读 if (listEmail.Count < 50) { for (int i = components.Count - 1; i >= 0; i--) { if (((EmailInfo)components[i]).State == 1) { listEmail.Add((EmailInfo)components[i]); if (listEmail.Count >= 50) { break; } } } } List <Email> emailList = new List <Email>(); for (int i = 0; i < listEmail.Count; ++i) { EmailInfo info = listEmail[i]; Email email = new Email(); email.EmailTitle = info.EmailTitle; email.Content = info.Content; email.State = info.State; email.RewardItem = info.RewardItem; email.Date = info.CreateTime; email.EId = info.EmailId; emailList.Add(email); } response.EmailInfoList = emailList; reply(response); } catch (Exception e) { ReplyError(response, e, reply); } }