#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 #include #include #include #include #include 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 allowedFileTypes; uint32_t fileCleanupIntervalSec = 3600; // Encryption settings bool encryptionEnabled = true; bool autoGenerateKey = true; // Replication settings bool replicationEnabled = true; std::vector 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 running_{false}; std::atomic stopRequested_{false}; // Components std::unique_ptr store_; std::unique_ptr persistence_; std::unique_ptr encryption_; std::unique_ptr events_; std::unique_ptr files_; std::unique_ptr replication_; // gRPC std::unique_ptr storageImpl_; std::unique_ptr replicationImpl_; std::unique_ptr grpcServer_; std::thread serverThread_; // Migration runner std::unique_ptr migrationRunner_; }; } // namespace smartbotic::database