// Copyright (c) 2019 Zano Project // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #include "crypto/crypto.h" #include "gtest/gtest.h" #include "common/db_backend_lmdb.h" #include "common/db_abstract_accessor.h" using namespace tools; namespace lmdb_test { TEST(lmdb, 2gb_test) { bool r = false; epee::shared_recursive_mutex rw_lock; epee::log_space::log_singletone::enable_channels("lmdb"); static const uint64_t buffer_size = 64 * 1024; // 64 KB static const uint64_t db_total_size = static_cast(2.1 * 1024 * 1024 * 1024); // 2.1 GB -- a bit more than 2GB to test 2GB boundary static const std::string db_file_path = "2gb_lmdb_test"; std::shared_ptr lmdb_ptr = std::make_shared(); db::basic_db_accessor bdba(lmdb_ptr, rw_lock); // // write data // r = bdba.open(db_file_path, CACHE_SIZE); ASSERT_TRUE(r); db::container_handle h; r = lmdb_ptr->open_container("c1", h); ASSERT_TRUE(r); ASSERT_TRUE(lmdb_ptr->begin_transaction()); ASSERT_TRUE(lmdb_ptr->clear(h)); std::vector buffer; buffer.resize(buffer_size); crypto::generate_random_bytes(buffer_size, buffer.data()); uint64_t total_data = 0; for (uint64_t key = 0; key < db_total_size / buffer_size; ++key) { r = lmdb_ptr->set(h, (char*)&key, sizeof key, reinterpret_cast(buffer.data()), buffer_size); ASSERT_TRUE(r); total_data += buffer_size; if (key % 1024 == 0) { ASSERT_TRUE(lmdb_ptr->commit_transaction()); ASSERT_TRUE(lmdb_ptr->resize_if_needed()); ASSERT_TRUE(lmdb_ptr->begin_transaction()); std::cout << total_data / 1024 / 1024 << " MB written to DB" << ENDL; } } ASSERT_TRUE(lmdb_ptr->commit_transaction()); r = bdba.close(); ASSERT_TRUE(r); // // read data and check // r = bdba.open(db_file_path); ASSERT_TRUE(r); r = lmdb_ptr->open_container("c1", h); ASSERT_TRUE(r); ASSERT_TRUE(lmdb_ptr->begin_transaction()); std::string out_buffer; total_data = 0; for (uint64_t key = 0; key < db_total_size / buffer_size; ++key) { r = lmdb_ptr->get(h, (char*)&key, sizeof key, out_buffer); ASSERT_TRUE(r); ASSERT_EQ(buffer_size, out_buffer.size()); ASSERT_TRUE(0 == memcmp(buffer.data(), out_buffer.c_str(), buffer_size)); total_data += buffer_size; if (key % 1024 == 0) std::cout << total_data / 1024 / 1024 << " MB read from DB" << ENDL; } ASSERT_TRUE(lmdb_ptr->commit_transaction()); r = bdba.close(); ASSERT_TRUE(r); boost::filesystem::remove_all(db_file_path); } }