DBTask.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Base;
  5. using Model;
  6. using MongoDB.Bson;
  7. using MongoDB.Driver;
  8. namespace Model
  9. {
  10. public abstract class DBTask : Entity
  11. {
  12. protected DBTask()
  13. {
  14. }
  15. protected DBTask(long id): base(id)
  16. {
  17. }
  18. public abstract Task Run();
  19. }
  20. public sealed class DBSaveTask : DBTask
  21. {
  22. public Entity Entity;
  23. public string CollectionName { get; }
  24. public TaskCompletionSource<bool> Tcs;
  25. public DBSaveTask(Entity entity, string collectionName, TaskCompletionSource<bool> tcs) : base(entity.Id)
  26. {
  27. this.Entity = entity;
  28. this.CollectionName = collectionName;
  29. this.Tcs = tcs;
  30. }
  31. public override async Task Run()
  32. {
  33. DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
  34. try
  35. {
  36. // 执行保存数据库任务
  37. await dbComponent.GetCollection(this.CollectionName).ReplaceOneAsync(s => s.Id == this.Entity.Id, this.Entity, new UpdateOptions {IsUpsert = true});
  38. this.Tcs.SetResult(true);
  39. }
  40. catch (Exception e)
  41. {
  42. this.Tcs.SetException(new Exception($"保存数据失败! {CollectionName} {Id}", e));
  43. }
  44. }
  45. }
  46. public sealed class DBSaveBatchTask : DBTask
  47. {
  48. public string CollectionName { get; }
  49. public List<Entity> Entitys;
  50. public TaskCompletionSource<bool> Tcs;
  51. public DBSaveBatchTask(List<Entity> entitys, string collectionName, TaskCompletionSource<bool> tcs)
  52. {
  53. this.Entitys = entitys;
  54. this.CollectionName = collectionName;
  55. this.Tcs = tcs;
  56. }
  57. public override async Task Run()
  58. {
  59. DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
  60. foreach (Entity entity in this.Entitys)
  61. {
  62. if (entity == null)
  63. {
  64. continue;
  65. }
  66. try
  67. {
  68. // 执行保存数据库任务
  69. await dbComponent.GetCollection(this.CollectionName).ReplaceOneAsync(s => s.Id == entity.Id, entity, new UpdateOptions { IsUpsert = true });
  70. }
  71. catch (Exception e)
  72. {
  73. Log.Debug($"{entity.GetType().Name} {entity.ToJson()}" + e.ToString());
  74. this.Tcs.SetException(new Exception($"保存数据失败! {CollectionName} {this.Entitys.ListToString()}", e));
  75. }
  76. }
  77. this.Tcs.SetResult(true);
  78. }
  79. }
  80. public sealed class DBQueryTask : DBTask
  81. {
  82. public string CollectionName { get; }
  83. public TaskCompletionSource<Entity> Tcs { get; }
  84. public DBQueryTask(long id, string collectionName, TaskCompletionSource<Entity> tcs) : base(id)
  85. {
  86. this.CollectionName = collectionName;
  87. this.Tcs = tcs;
  88. }
  89. public override async Task Run()
  90. {
  91. DBCacheComponent dbCacheComponent = Game.Scene.GetComponent<DBCacheComponent>();
  92. DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
  93. // 执行查询前先看看cache中是否已经存在
  94. Entity entity = dbCacheComponent.GetFromCache(this.CollectionName, this.Id);
  95. if (entity != null)
  96. {
  97. this.Tcs.SetResult(entity);
  98. return;
  99. }
  100. try
  101. {
  102. // 执行查询数据库任务
  103. entity = await dbComponent.GetCollection(this.CollectionName).FindAsync((s) => s.Id == this.Id).Result.FirstOrDefaultAsync();
  104. if (entity != null)
  105. {
  106. dbCacheComponent.AddToCache(entity);
  107. }
  108. this.Tcs.SetResult(entity);
  109. }
  110. catch (Exception e)
  111. {
  112. this.Tcs.SetException(new Exception($"查询数据库异常! {CollectionName} {Id}", e));
  113. }
  114. }
  115. }
  116. public sealed class DBQueryBatchTask : DBTask
  117. {
  118. public string CollectionName { get; }
  119. public List<long> IdList { get; }
  120. public TaskCompletionSource<List<Entity>> Tcs { get; }
  121. public DBQueryBatchTask(List<long> list, string collectionName, TaskCompletionSource<List<Entity>> tcs)
  122. {
  123. this.IdList = list;
  124. this.CollectionName = collectionName;
  125. this.Tcs = tcs;
  126. }
  127. public override async Task Run()
  128. {
  129. DBCacheComponent dbCacheComponent = Game.Scene.GetComponent<DBCacheComponent>();
  130. DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
  131. List<Entity> result = new List<Entity>();
  132. try
  133. {
  134. // 执行查询数据库任务
  135. foreach (long id in IdList)
  136. {
  137. Entity entity = dbCacheComponent.GetFromCache(this.CollectionName, id);
  138. if (entity == null)
  139. {
  140. entity = await dbComponent.GetCollection(this.CollectionName).FindAsync((s) => s.Id == id).Result.FirstOrDefaultAsync();
  141. dbCacheComponent.AddToCache(entity);
  142. }
  143. if (entity == null)
  144. {
  145. continue;
  146. }
  147. result.Add(entity);
  148. }
  149. this.Tcs.SetResult(result);
  150. }
  151. catch (Exception e)
  152. {
  153. this.Tcs.SetException(new Exception($"查询数据库异常! {this.CollectionName} {IdList.ListToString()}", e));
  154. }
  155. }
  156. }
  157. public sealed class DBQueryJsonTask : DBTask
  158. {
  159. public string CollectionName { get; }
  160. public string Json { get; }
  161. public TaskCompletionSource<List<Entity>> Tcs { get; }
  162. public DBQueryJsonTask(string collectionName, string json, TaskCompletionSource<List<Entity>> tcs)
  163. {
  164. this.CollectionName = collectionName;
  165. this.Json = json;
  166. this.Tcs = tcs;
  167. }
  168. public override async Task Run()
  169. {
  170. DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
  171. try
  172. {
  173. // 执行查询数据库任务
  174. FilterDefinition<Entity> filterDefinition = new JsonFilterDefinition<Entity>(this.Json);
  175. List<Entity> entitys = await dbComponent.GetCollection(this.CollectionName).FindAsync(filterDefinition).Result.ToListAsync();
  176. this.Tcs.SetResult(entitys);
  177. }
  178. catch (Exception e)
  179. {
  180. this.Tcs.SetException(new Exception($"查询数据库异常! {CollectionName} {this.Json}", e));
  181. }
  182. }
  183. }
  184. }