Skip to content

Commit

Permalink
Set default backend
Browse files Browse the repository at this point in the history
  • Loading branch information
RickiNano committed Jan 21, 2025
1 parent f1dd4f7 commit d019754
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
49 changes: 38 additions & 11 deletions nano/node/nodeconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ nano::error nano::node_config::deserialize_toml (nano::tomlconfig & toml)
toml.get<bool> ("allow_local_peers", allow_local_peers);
toml.get<unsigned> (signature_checker_threads_key, signature_checker_threads);

database_backend = get_database_backend (toml);
database_backend = get_config_backend (toml);

if (toml.has_key ("lmdb"))
{
Expand Down Expand Up @@ -661,9 +661,9 @@ void nano::node_config::deserialize_address (std::string const & entry_a, std::v
}
}

std::string nano::node_config::serialize_database_backend (nano::database_backend mode_a) const
std::string nano::node_config::serialize_database_backend (nano::database_backend backend) const
{
switch (mode_a)
switch (backend)
{
case nano::database_backend::rocksdb:
return "rocksdb";
Expand All @@ -673,23 +673,50 @@ std::string nano::node_config::serialize_database_backend (nano::database_backen
debug_assert (false);
}

nano::database_backend nano::node_config::get_database_backend (nano::tomlconfig & toml)
std::optional<nano::database_backend> nano::node_config::deserialize_database_backend (std::string backend_str) const
{
if (backend_str == "rocksdb")
return database_backend::rocksdb;
if (backend_str == "lmdb")
return database_backend::lmdb;
return std::nullopt;
}

nano::database_backend nano::node_config::get_config_backend (nano::tomlconfig & toml)
{
if (toml.has_key ("database_backend"))
{
auto backend_string (toml.get<std::string> ("database_backend"));
if (backend_string == "rocksdb")
return database_backend::rocksdb;
if (backend_string == "lmdb")
return database_backend::lmdb;

toml.get_error ().set ("Unknown database_backend type: " + backend_string);
auto backend_str = (toml.get<std::string> ("database_backend"));
auto backend = deserialize_database_backend (backend_str);
if (backend.has_value())
{
return backend.value ();
}
toml.get_error ().set ("Unknown database_backend type: " + backend_str);
return nano::database_backend::lmdb;
}
// Default to LMDB
return nano::database_backend::lmdb;
}

nano::database_backend nano::node_config::get_default_backend ()
{
auto backend_str = nano::env::get<std::string> ("BACKEND");

if (backend_str.has_value ())
{
auto backend = deserialize_database_backend (backend_str.value ());
if (backend.has_value ())
{
std::cerr << "Database backend overridden by BACKEND environment variable: " << *backend_str << std::endl;
return backend.value ();
}

std::cerr << "Unknown database backend in BACKEND environment variable: " << *backend_str << std::endl;
}
return database_backend::lmdb;
}

nano::account nano::node_config::random_representative () const
{
debug_assert (!preconfigured_representatives.empty ());
Expand Down
6 changes: 4 additions & 2 deletions nano/node/nodeconfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class node_config
uint64_t max_pruning_depth{ 0 };
nano::rocksdb_config rocksdb_config;
nano::lmdb_config lmdb_config;
nano::database_backend database_backend{ std::string (std::getenv ("BACKEND") ? std::getenv ("BACKEND") : "") == "rocksdb" ? nano::database_backend::rocksdb : nano::database_backend::lmdb };
nano::database_backend database_backend{ get_default_backend () };
bool enable_upnp{ true };
std::size_t max_ledger_notifications{ 8 };

Expand All @@ -164,7 +164,9 @@ class node_config
/** Entry is ignored if it cannot be parsed as a valid address:port */
void deserialize_address (std::string const &, std::vector<std::pair<std::string, uint16_t>> &) const;
std::string serialize_database_backend (nano::database_backend) const;
nano::database_backend get_database_backend (nano::tomlconfig & toml);
std::optional<nano::database_backend> deserialize_database_backend (std::string backend_str) const;
nano::database_backend get_config_backend (nano::tomlconfig & toml);
nano::database_backend get_default_backend ();

private:
static std::optional<unsigned> env_io_threads ();
Expand Down

0 comments on commit d019754

Please sign in to comment.