DBComponent.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System.Collections.Generic;
  2. using MongoDB.Driver;
  3. namespace ETModel
  4. {
  5. [ObjectSystem]
  6. public class DbComponentSystem : AwakeSystem<DBComponent>
  7. {
  8. public override void Awake(DBComponent self)
  9. {
  10. self.Awake();
  11. }
  12. }
  13. /// <summary>
  14. /// 用来缓存数据
  15. /// </summary>
  16. public class DBComponent : Component
  17. {
  18. public MongoClient mongoClient;
  19. public IMongoDatabase database;
  20. public const int taskCount = 32;
  21. public List<DBTaskQueue> tasks = new List<DBTaskQueue>(taskCount);
  22. public void Awake()
  23. {
  24. DBConfig config = StartConfigComponent.Instance.StartConfig.GetComponent<DBConfig>();
  25. string connectionString = config.ConnectionString;
  26. mongoClient = new MongoClient(connectionString);
  27. this.database = this.mongoClient.GetDatabase(config.DBName);
  28. for (int i = 0; i < taskCount; ++i)
  29. {
  30. DBTaskQueue taskQueue = ComponentFactory.Create<DBTaskQueue>();
  31. this.tasks.Add(taskQueue);
  32. }
  33. }
  34. public IMongoCollection<ComponentWithId> GetCollection(string name)
  35. {
  36. return this.database.GetCollection<ComponentWithId>(name);
  37. }
  38. public ETTask Add(ComponentWithId component, string collectionName = "")
  39. {
  40. ETTaskCompletionSource tcs = new ETTaskCompletionSource();
  41. if (string.IsNullOrEmpty(collectionName))
  42. {
  43. collectionName = component.GetType().Name;
  44. }
  45. DBSaveTask task = ComponentFactory.CreateWithId<DBSaveTask, ComponentWithId, string, ETTaskCompletionSource>(component.Id, component, collectionName, tcs);
  46. this.tasks[(int)((ulong)task.Id % taskCount)].Add(task);
  47. return tcs.Task;
  48. }
  49. public ETTask AddBatch(List<ComponentWithId> components, string collectionName)
  50. {
  51. ETTaskCompletionSource tcs = new ETTaskCompletionSource();
  52. DBSaveBatchTask task = ComponentFactory.Create<DBSaveBatchTask, List<ComponentWithId>, string, ETTaskCompletionSource>(components, collectionName, tcs);
  53. this.tasks[(int)((ulong)task.Id % taskCount)].Add(task);
  54. return tcs.Task;
  55. }
  56. public ETTask<ComponentWithId> Get(string collectionName, long id)
  57. {
  58. ETTaskCompletionSource<ComponentWithId> tcs = new ETTaskCompletionSource<ComponentWithId>();
  59. DBQueryTask dbQueryTask = ComponentFactory.CreateWithId<DBQueryTask, string, ETTaskCompletionSource<ComponentWithId>>(id, collectionName, tcs);
  60. this.tasks[(int)((ulong)id % taskCount)].Add(dbQueryTask);
  61. return tcs.Task;
  62. }
  63. public ETTask<List<ComponentWithId>> GetBatch(string collectionName, List<long> idList)
  64. {
  65. ETTaskCompletionSource<List<ComponentWithId>> tcs = new ETTaskCompletionSource<List<ComponentWithId>>();
  66. DBQueryBatchTask dbQueryBatchTask = ComponentFactory.Create<DBQueryBatchTask, List<long>, string, ETTaskCompletionSource<List<ComponentWithId>>>(idList, collectionName, tcs);
  67. this.tasks[(int)((ulong)dbQueryBatchTask.Id % taskCount)].Add(dbQueryBatchTask);
  68. return tcs.Task;
  69. }
  70. public ETTask<List<ComponentWithId>> GetJson(string collectionName, string json)
  71. {
  72. ETTaskCompletionSource<List<ComponentWithId>> tcs = new ETTaskCompletionSource<List<ComponentWithId>>();
  73. DBQueryJsonTask dbQueryJsonTask = ComponentFactory.Create<DBQueryJsonTask, string, string, ETTaskCompletionSource<List<ComponentWithId>>>(collectionName, json, tcs);
  74. this.tasks[(int)((ulong)dbQueryJsonTask.Id % taskCount)].Add(dbQueryJsonTask);
  75. return tcs.Task;
  76. }
  77. }
  78. }