DBTask.cs 5.4 KB

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