database_service.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #pragma once
  2. #include "document.hpp"
  3. #include "memory_store.hpp"
  4. #include "database_grpc_impl.hpp"
  5. #include "persistence/persistence_manager.hpp"
  6. #include "encryption/encryption_manager.hpp"
  7. #include "events/event_manager.hpp"
  8. #include "files/file_manager.hpp"
  9. #include "replication/replication_manager.hpp"
  10. #include "migrations/migration_runner.hpp"
  11. #include <nlohmann/json.hpp>
  12. #include <atomic>
  13. #include <filesystem>
  14. #include <memory>
  15. #include <string>
  16. #include <thread>
  17. namespace grpc {
  18. class Server;
  19. }
  20. namespace smartbotic::database {
  21. /**
  22. * Main storage service application.
  23. * Coordinates all components and manages the gRPC server.
  24. */
  25. class DatabaseService {
  26. public:
  27. struct Config {
  28. std::string nodeId = "storage-1";
  29. std::string bindAddress = "0.0.0.0";
  30. uint16_t rpcPort = 9004;
  31. // Bootstrap/service registration
  32. std::string webapiUrl;
  33. std::string serviceKey;
  34. uint32_t heartbeatIntervalSec = 30;
  35. std::filesystem::path dataDirectory;
  36. std::filesystem::path keyFilePath;
  37. // Persistence settings
  38. uint32_t walSyncIntervalMs = 100;
  39. uint32_t snapshotIntervalSec = 3600;
  40. bool compressionEnabled = true;
  41. // File storage settings
  42. uint64_t maxFileSizeMb = 500;
  43. std::vector<std::string> allowedFileTypes;
  44. uint32_t fileCleanupIntervalSec = 3600;
  45. // Encryption settings
  46. bool encryptionEnabled = true;
  47. bool autoGenerateKey = true;
  48. // Replication settings
  49. bool replicationEnabled = true;
  50. std::vector<std::string> peerAddresses;
  51. std::string conflictResolution = "last_writer_wins";
  52. // Migration settings
  53. struct MigrationsConfig {
  54. bool enabled = true;
  55. std::filesystem::path directory;
  56. bool autoApply = true;
  57. bool failOnError = true;
  58. } migrations;
  59. };
  60. explicit DatabaseService(Config config);
  61. ~DatabaseService();
  62. /**
  63. * Initialize the service.
  64. * Loads configuration, initializes components, and prepares for startup.
  65. */
  66. bool initialize();
  67. /**
  68. * Start the service.
  69. * Starts the gRPC server and all background threads.
  70. */
  71. void start();
  72. /**
  73. * Stop the service.
  74. * Gracefully shuts down all components.
  75. */
  76. void stop();
  77. /**
  78. * Wait for the service to stop.
  79. */
  80. void wait();
  81. /**
  82. * Check if the service is running.
  83. */
  84. [[nodiscard]] bool isRunning() const { return running_; }
  85. /**
  86. * Signal the service to stop (for signal handlers).
  87. */
  88. void signalStop();
  89. /**
  90. * Get service statistics.
  91. */
  92. [[nodiscard]] nlohmann::json getStats() const;
  93. /**
  94. * Load configuration from a JSON file.
  95. */
  96. static Config loadConfig(const std::filesystem::path& configPath);
  97. private:
  98. void setupComponents();
  99. void startGrpcServer();
  100. void stopGrpcServer();
  101. void applyReplicatedEntry(const databasepb::ReplicationEntry& entry);
  102. bool runMigrations();
  103. Config config_;
  104. std::atomic<bool> running_{false};
  105. std::atomic<bool> stopRequested_{false};
  106. // Components
  107. std::unique_ptr<MemoryStore> store_;
  108. std::unique_ptr<PersistenceManager> persistence_;
  109. std::unique_ptr<EncryptionManager> encryption_;
  110. std::unique_ptr<EventManager> events_;
  111. std::unique_ptr<FileManager> files_;
  112. std::unique_ptr<ReplicationManager> replication_;
  113. // gRPC
  114. std::unique_ptr<DatabaseGrpcImpl> storageImpl_;
  115. std::unique_ptr<DatabaseReplicationGrpcImpl> replicationImpl_;
  116. std::unique_ptr<grpc::Server> grpcServer_;
  117. std::thread serverThread_;
  118. // Migration runner
  119. std::unique_ptr<MigrationRunner> migrationRunner_;
  120. };
  121. } // namespace smartbotic::database