DBProxyComponentSystem.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq.Expressions;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using ETModel;
  7. using MongoDB.Bson;
  8. using MongoDB.Bson.Serialization;
  9. using MongoDB.Bson.Serialization.Serializers;
  10. using MongoDB.Driver;
  11. namespace ETHotfix
  12. {
  13. [ObjectSystem]
  14. public class DbProxyComponentSystem : AwakeSystem<DBProxyComponent>
  15. {
  16. public override void Awake(DBProxyComponent self)
  17. {
  18. self.Awake();
  19. }
  20. }
  21. /// <summary>
  22. /// 用来与数据库操作代理
  23. /// </summary>
  24. public static class DBProxyComponentEx
  25. {
  26. public static void Awake(this DBProxyComponent self)
  27. {
  28. StartConfig dbStartConfig = Game.Scene.GetComponent<StartConfigComponent>().DBConfig;
  29. self.dbAddress = dbStartConfig.GetComponent<InnerConfig>().IPEndPoint;
  30. }
  31. public static async Task Save(this DBProxyComponent self, ComponentWithId component, bool needCache = true)
  32. {
  33. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  34. await session.Call(new DBSaveRequest { Component = component, NeedCache = needCache});
  35. }
  36. public static async Task SaveBatch(this DBProxyComponent self, List<ComponentWithId> components, bool needCache = true)
  37. {
  38. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  39. await session.Call(new DBSaveBatchRequest { Components = components, NeedCache = needCache});
  40. }
  41. public static async Task Save(this DBProxyComponent self, ComponentWithId component, bool needCache, CancellationToken cancellationToken)
  42. {
  43. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  44. await session.Call(new DBSaveRequest { Component = component, NeedCache = needCache}, cancellationToken);
  45. }
  46. public static async void SaveLog(this DBProxyComponent self, ComponentWithId component)
  47. {
  48. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  49. await session.Call(new DBSaveRequest { Component = component, NeedCache = false, CollectionName = "Log" });
  50. }
  51. public static async Task<T> Query<T>(this DBProxyComponent self, long id, bool needCache = true) where T: ComponentWithId
  52. {
  53. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  54. DBQueryResponse dbQueryResponse = (DBQueryResponse)await session.Call(new DBQueryRequest { CollectionName = typeof(T).Name, Id = id, NeedCache = needCache });
  55. return (T)dbQueryResponse.Component;
  56. }
  57. public static async Task<List<T>> Query<T>(this DBProxyComponent self, Expression<Func<T ,bool>> exp, bool needCache = true) where T: ComponentWithId
  58. {
  59. List<T> list = new List<T>();
  60. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  61. ExpressionFilterDefinition<T> filter = new ExpressionFilterDefinition<T>(exp);
  62. IBsonSerializerRegistry serializerRegistry = BsonSerializer.SerializerRegistry;
  63. IBsonSerializer<T> documentSerializer = serializerRegistry.GetSerializer<T>();
  64. string json = filter.Render(documentSerializer, serializerRegistry).ToJson();
  65. DBQueryJsonResponse dbQueryResponse = (DBQueryJsonResponse)await session.Call(new DBQueryJsonRequest { CollectionName = typeof(T).Name, Json = json });
  66. foreach (ComponentWithId component in dbQueryResponse.Components)
  67. {
  68. list.Add((T)component);
  69. }
  70. return list;
  71. }
  72. public static async Task<List<T>> QueryBatch<T>(this DBProxyComponent self, List<long> ids, bool needCache = true) where T : ComponentWithId
  73. {
  74. List<T> list = new List<T>();
  75. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  76. DBQueryBatchResponse dbQueryBatchResponse = (DBQueryBatchResponse)await session.Call(new DBQueryBatchRequest { CollectionName = typeof(T).Name, IdList = ids, NeedCache = needCache});
  77. foreach (ComponentWithId component in dbQueryBatchResponse.Components)
  78. {
  79. list.Add((T)component);
  80. }
  81. return list;
  82. }
  83. public static async Task<List<T>> QueryJson<T>(this DBProxyComponent self, string json) where T : ComponentWithId
  84. {
  85. List<T> list = new List<T>();
  86. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  87. DBQueryJsonResponse dbQueryJsonResponse = (DBQueryJsonResponse)await session.Call(new DBQueryJsonRequest { CollectionName = typeof(T).Name, Json = json });
  88. foreach (ComponentWithId component in dbQueryJsonResponse.Components)
  89. {
  90. list.Add((T)component);
  91. }
  92. return list;
  93. }
  94. }
  95. }