DBComponentSystem.cs 8.4 KB

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