1
0
Fork 0
forked from lthn/blockchain

LMDB: implemented experimental conversion from 4k pages to 16k pages (untested)

This commit is contained in:
sowle 2024-08-13 21:04:51 +02:00
parent 9f0fa8a390
commit f0c7d57c41
No known key found for this signature in database
GPG key ID: C07A24B2D89D49FC
3 changed files with 97 additions and 3 deletions

View file

@ -1,4 +1,4 @@
// Copyright (c) 2014-2019 Zano Project
// Copyright (c) 2014-2024 Zano Project
// Copyright (c) 2014-2018 The Louisdor Project
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
@ -394,10 +394,67 @@ namespace tools
}
return true;
}
const char* lmdb_db_backend::name()
{
return "lmdb";
}
bool lmdb_db_backend::convert_db_4kb_page_to_16kb_page(const std::string& source_path, const std::string& destination_path)
{
#define MDB_CHECK(x, msg) {int rc = x; CHECK_AND_ASSERT_MES(rc == MDB_SUCCESS, false, "LMDB 4k->16k error: " << msg << ": " << mdb_strerror(rc));}
MDB_env *env_src = nullptr, *env_dst = nullptr;
// source
MDB_CHECK(mdb_env_create(&env_src), "failed to create LMDB environment");
MDB_CHECK(mdb_env_set_mapsize(env_src, 4 * 1024 * 1024), "failed to set mapsize"); // mapsize ?
MDB_CHECK(mdb_env_open(env_src, source_path.c_str(), 0, 0664), "failed to open source LMDB");
// destination (16k page size)
MDB_CHECK(mdb_env_create(&env_dst), "failed to create LMDB environment");
MDB_CHECK(mdb_env_set_mapsize(env_dst, 16 * 1024 * 1024), "failed to set mapsize"); // mapsize ?
// TODO uncomment after mdb_env_set_pagesize is supported
// MDB_CHECK(mdb_env_set_pagesize(env_dst, 16 * 1024), "failed to set page size to 16K");
MDB_CHECK(mdb_env_open(env_dst, destination_path.c_str(), 0, 0664), "failed to open destination LMDB");
// begin transactions
MDB_txn *txn_src = nullptr, *txn_dst = nullptr;
MDB_dbi dbi_src, dbi_dst;
MDB_CHECK(mdb_txn_begin(env_src, nullptr, MDB_RDONLY, &txn_src), "failed to begin source transaction");
MDB_CHECK(mdb_dbi_open(txn_src, nullptr, 0, &dbi_src), "failed to open source database");
MDB_CHECK(mdb_txn_begin(env_dst, nullptr, 0, &txn_dst), "failed to begin destination transaction");
MDB_CHECK(mdb_dbi_open(txn_dst, nullptr, MDB_CREATE, &dbi_dst), "failed to open destination database");
MDB_cursor *cursor;
MDB_val key, data;
// Iterate over the source database and copy all key-value pairs to the destination database
MDB_CHECK(mdb_cursor_open(txn_src, dbi_src, &cursor), "failed to open cursor");
while (mdb_cursor_get(cursor, &key, &data, MDB_NEXT) == MDB_SUCCESS)
{
MDB_CHECK(mdb_put(txn_dst, dbi_dst, &key, &data, 0), "failed to put data in destination database");
}
mdb_cursor_close(cursor);
// commit transactions
MDB_CHECK(mdb_txn_commit(txn_src), "failed to commit source transaction");
MDB_CHECK(mdb_txn_commit(txn_dst), "failed to commit destination transaction");
mdb_dbi_close(env_src, dbi_src);
mdb_dbi_close(env_dst, dbi_dst);
mdb_env_close(env_src);
mdb_env_close(env_dst);
return true;
#undef MDB_CHECK
}
}
}

View file

@ -1,4 +1,4 @@
// Copyright (c) 2014-2019 Zano Project
// Copyright (c) 2014-2024 Zano Project
// Copyright (c) 2014-2018 The Louisdor Project
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
@ -36,6 +36,7 @@ namespace tools
boost::recursive_mutex m_write_exclusive_lock;
std::map<std::thread::id, transactions_list> m_txs; // size_t -> count of nested read_only transactions
bool pop_tx_entry(tx_entry& txe);
public:
lmdb_db_backend();
~lmdb_db_backend();
@ -60,6 +61,8 @@ namespace tools
bool have_tx();
MDB_txn* get_current_tx();
static bool convert_db_4kb_page_to_16kb_page(const std::string& source_path, const std::string& destination_path);
};
}
}

View file

@ -1,4 +1,4 @@
// Copyright (c) 2014-2019 Zano Project
// Copyright (c) 2014-2024 Zano Project
// Copyright (c) 2014-2018 The Louisdor Project
// Copyright (c) 2012-2013 The Cryptonote developers
// Distributed under the MIT/X11 software license, see the accompanying
@ -25,6 +25,7 @@ using namespace epee;
#include "storages/http_abstract_invoke.h"
#include "net/http_client.h"
#include "currency_core/genesis_acc.h"
#include "common/db_backend_lmdb.h"
#include <cstdlib>
namespace po = boost::program_options;
@ -63,6 +64,7 @@ namespace
const command_line::arg_descriptor<std::string> arg_pack_file ("pack-file", "perform gzip-packing and calculate hash for a given file");
const command_line::arg_descriptor<std::string> arg_unpack_file ("unpack-file", "Perform gzip-unpacking and calculate hash for a given file");
const command_line::arg_descriptor<std::string> arg_target_file ("target-file", "Specify target file for pack-file and unpack-file commands");
const command_line::arg_descriptor<std::string> arg_lmdb_page_4to16 ("convert-lmdb-4to16", "Perform LMDB conversion from 4k page size to 16k page size");
//const command_line::arg_descriptor<std::string> arg_send_ipc ("send-ipc", "Send IPC request to UI");
}
@ -1251,6 +1253,34 @@ bool handle_pack_file(po::variables_map& vm)
}
}
bool handle_lmdb_page_4to16(po::variables_map& vm)
{
std::string path_source;
std::string path_target;
if (!command_line::has_arg(vm, arg_lmdb_page_4to16))
return false;
path_source = command_line::get_arg(vm, arg_lmdb_page_4to16);
if (!command_line::has_arg(vm, arg_target_file))
{
std::cout << "Error: Parameter target_file is not set." << ENDL;
return false;
}
path_target = command_line::get_arg(vm, arg_target_file);
if (tools::db::lmdb_db_backend::convert_db_4kb_page_to_16kb_page(path_source, path_target))
{
std::cout << "Conversion failed" << ENDL;
return false;
}
std::cout << "Converted successfully" << ENDL;
return true;
}
//---------------------------------------------------------------------------------------------------------------
int main(int argc, char* argv[])
@ -1375,6 +1405,10 @@ int main(int argc, char* argv[])
{
return handle_pack_file(vm) ? EXIT_SUCCESS : EXIT_FAILURE;
}
else if (command_line::has_arg(vm, arg_lmdb_page_4to16))
{
return handle_lmdb_page_4to16(vm) ? EXIT_SUCCESS : EXIT_FAILURE;
}
/*else if (command_line::has_arg(vm, arg_send_ipc))
{
handle_send_ipc(command_line::get_arg(vm, arg_send_ipc)) ? EXIT_SUCCESS : EXIT_FAILURE;