DBComponentSystem.cs 8.4 KB

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