| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- #pragma once
- #include "document.hpp"
- #include "memory_store.hpp"
- #include "database_grpc_impl.hpp"
- #include "persistence/persistence_manager.hpp"
- #include "encryption/encryption_manager.hpp"
- #include "events/event_manager.hpp"
- #include "files/file_manager.hpp"
- #include "replication/replication_manager.hpp"
- #include "migrations/migration_runner.hpp"
- #include <nlohmann/json.hpp>
- #include <atomic>
- #include <filesystem>
- #include <memory>
- #include <string>
- #include <thread>
- namespace grpc {
- class Server;
- }
- namespace smartbotic::database {
- /**
- * Main storage service application.
- * Coordinates all components and manages the gRPC server.
- */
- class DatabaseService {
- public:
- struct Config {
- std::string nodeId = "storage-1";
- std::string bindAddress = "0.0.0.0";
- uint16_t rpcPort = 9004;
- // Bootstrap/service registration
- std::string webapiUrl;
- std::string serviceKey;
- uint32_t heartbeatIntervalSec = 30;
- std::filesystem::path dataDirectory;
- std::filesystem::path keyFilePath;
- // Persistence settings
- uint32_t walSyncIntervalMs = 100;
- uint32_t snapshotIntervalSec = 3600;
- bool compressionEnabled = true;
- // File storage settings
- uint64_t maxFileSizeMb = 500;
- std::vector<std::string> allowedFileTypes;
- uint32_t fileCleanupIntervalSec = 3600;
- // Encryption settings
- bool encryptionEnabled = true;
- bool autoGenerateKey = true;
- // Replication settings
- bool replicationEnabled = true;
- std::vector<std::string> peerAddresses;
- std::string conflictResolution = "last_writer_wins";
- // Migration settings
- struct MigrationsConfig {
- bool enabled = true;
- std::filesystem::path directory;
- bool autoApply = true;
- bool failOnError = true;
- } migrations;
- };
- explicit DatabaseService(Config config);
- ~DatabaseService();
- /**
- * Initialize the service.
- * Loads configuration, initializes components, and prepares for startup.
- */
- bool initialize();
- /**
- * Start the service.
- * Starts the gRPC server and all background threads.
- */
- void start();
- /**
- * Stop the service.
- * Gracefully shuts down all components.
- */
- void stop();
- /**
- * Wait for the service to stop.
- */
- void wait();
- /**
- * Check if the service is running.
- */
- [[nodiscard]] bool isRunning() const { return running_; }
- /**
- * Signal the service to stop (for signal handlers).
- */
- void signalStop();
- /**
- * Get service statistics.
- */
- [[nodiscard]] nlohmann::json getStats() const;
- /**
- * Load configuration from a JSON file.
- */
- static Config loadConfig(const std::filesystem::path& configPath);
- private:
- void setupComponents();
- void startGrpcServer();
- void stopGrpcServer();
- void applyReplicatedEntry(const databasepb::ReplicationEntry& entry);
- bool runMigrations();
- Config config_;
- std::atomic<bool> running_{false};
- std::atomic<bool> stopRequested_{false};
- // Components
- std::unique_ptr<MemoryStore> store_;
- std::unique_ptr<PersistenceManager> persistence_;
- std::unique_ptr<EncryptionManager> encryption_;
- std::unique_ptr<EventManager> events_;
- std::unique_ptr<FileManager> files_;
- std::unique_ptr<ReplicationManager> replication_;
- // gRPC
- std::unique_ptr<DatabaseGrpcImpl> storageImpl_;
- std::unique_ptr<DatabaseReplicationGrpcImpl> replicationImpl_;
- std::unique_ptr<grpc::Server> grpcServer_;
- std::thread serverThread_;
- // Migration runner
- std::unique_ptr<MigrationRunner> migrationRunner_;
- };
- } // namespace smartbotic::database
|