DBComponentSystem.cs 8.3 KB

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