DBComponentSystem.cs 8.5 KB

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