DBComponentSystem.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq.Expressions;
  4. using MongoDB.Driver;
  5. namespace ET
  6. {
  7. public class DBComponentAwakeSystem : AwakeSystem<DBComponent, string, string>
  8. {
  9. public override void Awake(DBComponent self, string dbConnection, string dbName)
  10. {
  11. self.mongoClient = new MongoClient(dbConnection);
  12. self.database = self.mongoClient.GetDatabase(dbName);
  13. self.Transfers.Clear();
  14. foreach (Type type in Game.EventSystem.GetTypes())
  15. {
  16. if (type == typeof (IDBCollection))
  17. {
  18. continue;
  19. }
  20. if (!typeof(IDBCollection).IsAssignableFrom(type))
  21. {
  22. continue;
  23. }
  24. self.Transfers.Add(type.Name);
  25. }
  26. DBComponent.Instance = self;
  27. }
  28. }
  29. public class DBComponentDestroySystem: DestroySystem<DBComponent>
  30. {
  31. public override void Destroy(DBComponent self)
  32. {
  33. DBComponent.Instance = null;
  34. self.Transfers.Clear();
  35. }
  36. }
  37. public static class DBComponentSystem
  38. {
  39. #region Query
  40. public static async ETTask<T> Query<T>(this DBComponent self, long id, string collection = null) where T : Entity
  41. {
  42. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, id % DBComponent.TaskCount))
  43. {
  44. IAsyncCursor<T> cursor = await self.GetCollection<T>(collection).FindAsync(d => d.Id == id);
  45. return await cursor.FirstOrDefaultAsync();
  46. }
  47. }
  48. public static async ETTask<List<T>> Query<T>(this DBComponent self, Expression<Func<T, bool>> filter, string collection = null)
  49. where T : Entity
  50. {
  51. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, RandomHelper.RandInt64() % DBComponent.TaskCount))
  52. {
  53. IAsyncCursor<T> cursor = await self.GetCollection<T>(collection).FindAsync(filter);
  54. return await cursor.ToListAsync();
  55. }
  56. }
  57. public static async ETTask<List<T>> Query<T>(this DBComponent self, long taskId, Expression<Func<T, bool>> filter, string collection = null)
  58. where T : Entity
  59. {
  60. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, taskId % DBComponent.TaskCount))
  61. {
  62. IAsyncCursor<T> cursor = await self.GetCollection<T>(collection).FindAsync(filter);
  63. return await cursor.ToListAsync();
  64. }
  65. }
  66. public static async ETTask Query(this DBComponent self, long id, List<string> collectionNames, List<Entity> result)
  67. {
  68. if (collectionNames == null || collectionNames.Count == 0)
  69. {
  70. return;
  71. }
  72. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, id % DBComponent.TaskCount))
  73. {
  74. foreach (string collectionName in collectionNames)
  75. {
  76. IAsyncCursor<Entity> cursor = await self.GetCollection(collectionName).FindAsync(d => d.Id == id);
  77. Entity e = await cursor.FirstOrDefaultAsync();
  78. if (e == null)
  79. {
  80. continue;
  81. }
  82. result.Add(e);
  83. }
  84. }
  85. }
  86. public static async ETTask<List<T>> QueryJson<T>(this DBComponent self, string json, string collection = null) where T : Entity
  87. {
  88. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, RandomHelper.RandInt64() % DBComponent.TaskCount))
  89. {
  90. FilterDefinition<T> filterDefinition = new JsonFilterDefinition<T>(json);
  91. IAsyncCursor<T> cursor = await self.GetCollection<T>(collection).FindAsync(filterDefinition);
  92. return await cursor.ToListAsync();
  93. }
  94. }
  95. public static async ETTask<List<T>> QueryJson<T>(this DBComponent self, long taskId, string json, string collection = null) where T : Entity
  96. {
  97. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, RandomHelper.RandInt64() % DBComponent.TaskCount))
  98. {
  99. FilterDefinition<T> filterDefinition = new JsonFilterDefinition<T>(json);
  100. IAsyncCursor<T> cursor = await self.GetCollection<T>(collection).FindAsync(filterDefinition);
  101. return await cursor.ToListAsync();
  102. }
  103. }
  104. #endregion
  105. #region Insert
  106. public static async ETTask InsertBatch<T>(this DBComponent self, IEnumerable<T> list, string collection = null) where T: Entity
  107. {
  108. if (collection == null)
  109. {
  110. collection = typeof (T).Name;
  111. }
  112. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, RandomHelper.RandInt64() % DBComponent.TaskCount))
  113. {
  114. await self.GetCollection(collection).InsertManyAsync(list);
  115. }
  116. }
  117. #endregion
  118. #region Save
  119. public static async ETTask Save<T>(this DBComponent self, T entity, string collection = null) where T : Entity
  120. {
  121. if (entity == null)
  122. {
  123. Log.Error($"save entity is null: {typeof (T).Name}");
  124. return;
  125. }
  126. if (collection == null)
  127. {
  128. collection = entity.GetType().Name;
  129. }
  130. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, entity.Id % DBComponent.TaskCount))
  131. {
  132. await self.GetCollection(collection).ReplaceOneAsync(d => d.Id == entity.Id, entity, new ReplaceOptions { IsUpsert = true });
  133. }
  134. }
  135. public static async ETTask Save<T>(this DBComponent self, long taskId, T entity, string collection = null) where T : Entity
  136. {
  137. if (entity == null)
  138. {
  139. Log.Error($"save entity is null: {typeof (T).Name}");
  140. return;
  141. }
  142. if (collection == null)
  143. {
  144. collection = entity.GetType().Name;
  145. }
  146. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, taskId % DBComponent.TaskCount))
  147. {
  148. await self.GetCollection(collection).ReplaceOneAsync(d => d.Id == entity.Id, entity, new ReplaceOptions { IsUpsert = true });
  149. }
  150. }
  151. public static async ETTask Save(this DBComponent self, long id, List<Entity> entities)
  152. {
  153. if (entities == null)
  154. {
  155. Log.Error($"save entity is null");
  156. return;
  157. }
  158. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, id % DBComponent.TaskCount))
  159. {
  160. foreach (Entity entity in entities)
  161. {
  162. if (entity == null)
  163. {
  164. continue;
  165. }
  166. await self.GetCollection(entity.GetType().Name)
  167. .ReplaceOneAsync(d => d.Id == entity.Id, entity, new ReplaceOptions { IsUpsert = true });
  168. }
  169. }
  170. }
  171. public static async ETVoid SaveNotWait<T>(this DBComponent self, T entity, long taskId = 0, string collection = null) where T : Entity
  172. {
  173. if (taskId == 0)
  174. {
  175. await self.Save(entity, collection);
  176. return;
  177. }
  178. await self.Save(taskId, entity, collection);
  179. }
  180. #endregion
  181. #region Remove
  182. public static async ETTask<long> Remove<T>(this DBComponent self, long id, string collection = null) where T : Entity
  183. {
  184. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, id % DBComponent.TaskCount))
  185. {
  186. DeleteResult result = await self.GetCollection<T>(collection).DeleteOneAsync(d => d.Id == id);
  187. return result.DeletedCount;
  188. }
  189. }
  190. public static async ETTask<long> Remove<T>(this DBComponent self, long taskId, long id, string collection = null) where T : Entity
  191. {
  192. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, taskId % DBComponent.TaskCount))
  193. {
  194. DeleteResult result = await self.GetCollection<T>(collection).DeleteOneAsync(d => d.Id == id);
  195. return result.DeletedCount;
  196. }
  197. }
  198. public static async ETTask<long> Remove<T>(this DBComponent self, Expression<Func<T, bool>> filter, string collection = null) where T : Entity
  199. {
  200. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, RandomHelper.RandInt64() % DBComponent.TaskCount))
  201. {
  202. DeleteResult result = await self.GetCollection<T>(collection).DeleteManyAsync(filter);
  203. return result.DeletedCount;
  204. }
  205. }
  206. public static async ETTask<long> Remove<T>(this DBComponent self, long taskId, Expression<Func<T, bool>> filter, string collection = null)
  207. where T : Entity
  208. {
  209. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, taskId % DBComponent.TaskCount))
  210. {
  211. DeleteResult result = await self.GetCollection<T>(collection).DeleteManyAsync(filter);
  212. return result.DeletedCount;
  213. }
  214. }
  215. #endregion
  216. }
  217. }