DBComponentSystem.cs 8.2 KB

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