|
|
@@ -1,5 +1,8 @@
|
|
|
#include "database_service.hpp"
|
|
|
#include "json_parse.hpp"
|
|
|
+#include "storage/document_store.hpp"
|
|
|
+#include "storage/document_store_lmdb.hpp"
|
|
|
+#include "storage/lmdb_env.hpp"
|
|
|
|
|
|
#include <grpcpp/grpcpp.h>
|
|
|
#include <grpcpp/resource_quota.h>
|
|
|
@@ -505,6 +508,52 @@ DatabaseService::Config DatabaseService::parseConfig(const nlohmann::json& json)
|
|
|
}
|
|
|
|
|
|
void DatabaseService::setupComponents() {
|
|
|
+ // v2.0 storage substrate (Stage 4 scaffold).
|
|
|
+ //
|
|
|
+ // Open the LMDB environment at <dataDir>/env/ — same path the v1.x
|
|
|
+ // auto-migration tool in main.cpp writes to. By the time setupComponents
|
|
|
+ // runs, either:
|
|
|
+ // (a) the env already exists and contains migrated v2.0 data, or
|
|
|
+ // (b) this is a fresh deployment and the env is empty.
|
|
|
+ // Both cases are fine: LmdbDocumentStore opens sub-dbs lazily on first
|
|
|
+ // put/read, so an empty env is a valid steady state.
|
|
|
+ //
|
|
|
+ // map_size_bytes / max_dbs match the values main.cpp uses for migration
|
|
|
+ // so that opening the same env from both call sites is consistent.
|
|
|
+ //
|
|
|
+ // Failure to open the env is currently logged-and-tolerated: Stage 4
|
|
|
+ // does not yet route any handlers through doc_store_, so a missing or
|
|
|
+ // unopenable env should not kill the service. Stage 5+ may upgrade this
|
|
|
+ // to a hard failure once handlers depend on the store.
|
|
|
+ {
|
|
|
+ smartbotic::db::storage::LmdbEnvOpts envOpts;
|
|
|
+ envOpts.path = (config_.dataDirectory / "env").string();
|
|
|
+ envOpts.map_size_bytes = 4ULL << 30; // 4 GiB initial; LMDB grows lazily.
|
|
|
+ envOpts.max_dbs = 1024;
|
|
|
+
|
|
|
+ std::error_code ec;
|
|
|
+ std::filesystem::create_directories(envOpts.path, ec);
|
|
|
+ if (ec) {
|
|
|
+ spdlog::warn("v2.0 storage: failed to create env directory '{}': {} -- "
|
|
|
+ "doc_store_ will be left null; Stage 4 still routes through "
|
|
|
+ "MemoryStore so the service continues to boot",
|
|
|
+ envOpts.path, ec.message());
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ env_ = std::make_unique<smartbotic::db::storage::LmdbEnv>(envOpts);
|
|
|
+ doc_store_ = std::make_unique<smartbotic::db::storage::LmdbDocumentStore>(*env_);
|
|
|
+ spdlog::info("v2.0 storage: LMDB env opened at {} (mapsize={}MB, max_dbs={})",
|
|
|
+ envOpts.path, envOpts.map_size_bytes >> 20, envOpts.max_dbs);
|
|
|
+ } catch (const std::exception& e) {
|
|
|
+ spdlog::warn("v2.0 storage: failed to open LMDB env at '{}': {} -- "
|
|
|
+ "doc_store_ will be left null; service continues with v1.x",
|
|
|
+ envOpts.path, e.what());
|
|
|
+ env_.reset();
|
|
|
+ doc_store_.reset();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// Create memory store with eviction config
|
|
|
MemoryStore::Config storeConfig;
|
|
|
storeConfig.nodeId = config_.nodeId;
|