From 0f13296d5d92dfb59576daa0480988dfa7f29b29 Mon Sep 17 00:00:00 2001 From: Fred Morcos Date: Thu, 17 Oct 2024 10:27:35 +0200 Subject: [PATCH] Refactor shards cmdline handling --- modules/lmdbbackend/lmdbbackend.cc | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/modules/lmdbbackend/lmdbbackend.cc b/modules/lmdbbackend/lmdbbackend.cc index d3a2be6ab3..f708d84fd1 100644 --- a/modules/lmdbbackend/lmdbbackend.cc +++ b/modules/lmdbbackend/lmdbbackend.cc @@ -756,20 +756,35 @@ LMDBBackend::LMDBBackend(const std::string& suffix) auto txn = d_tdomains->getEnv()->getRWTransaction(); + const auto configShardsTemp = atoi(getArg("shards").c_str()); + if (configShardsTemp < 0) { + throw std::runtime_error("a negative shards value is not supported"); + } + if (configShardsTemp == 0) { + throw std::runtime_error("a shards value of 0 is not supported"); + } + const auto configShards = static_cast(configShardsTemp); + MDBOutVal shards{}; if (txn->get(pdnsdbi, "shards", shards) == 0) { s_shards = shards.get(); if (mustDo("lightning-stream") && s_shards != 1) { - throw std::runtime_error(std::string("running with Lightning Stream support enabled requires a database with exactly 1 shard")); + throw std::runtime_error("running with Lightning Stream support enabled requires a database with exactly 1 shard"); } - if (s_shards != atoi(getArg("shards").c_str())) { - g_log << Logger::Warning << "Note: configured number of lmdb shards (" << atoi(getArg("shards").c_str()) << ") is different from on-disk (" << s_shards << "). Using on-disk shard number" << endl; + if (s_shards != configShards) { + g_log << Logger::Warning + << "Note: configured number of lmdb shards (" + << atoi(getArg("shards").c_str()) + << ") is different from on-disk (" + << s_shards + << "). Using on-disk shard number" + << endl; } } else { - s_shards = atoi(getArg("shards").c_str()); + s_shards = configShards; txn->put(pdnsdbi, "shards", s_shards); } -- 2.47.2