1
0
mirror of https://github.com/mariadb-corporation/mariadb-columnstore-engine.git synced 2025-04-18 21:44:02 +03:00
This patch moves FDB to utils dir and adds test on `remove keys range`.
This commit is contained in:
Denis Khalikov 2024-08-28 05:11:09 +00:00 committed by Leonid Fedorov
parent a02d15ad11
commit bb861f8fab
7 changed files with 151 additions and 101 deletions

View File

@ -77,22 +77,6 @@ set_property(TARGET StorageManager PROPERTY CXX_STANDARD 20)
set(TMPDIR ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
find_library(FDB_LIB fdb_c)
if (NOT FDB_LIB)
MESSAGE_ONCE(FATAL_ERROR "FoundationDB not installed properly, please install packages foundationdb-cliends and foundationdb-server")
return()
endif()
add_library(fdbcs SHARED fdb_wrapper_cpp/src/fdbcs.cpp)
target_link_libraries(fdbcs fdb_c)
add_executable(test_fdb_api fdb_wrapper_cpp/test/test_fdb_api.cpp)
add_dependencies(test_fdb_api fdbcs)
target_link_libraries(test_fdb_api fdbcs fdb_c)
# tests hangs on Rocky8 and Rocky9 started without systemd
#add_test(NAME columnstore:test_fdb_api COMMAND test_fdb_api)
add_executable(unit_tests src/unit_tests.cpp)
target_compile_definitions(unit_tests PUBLIC BOOST_NO_CXX11_SCOPED_ENUMS)
target_link_libraries(unit_tests storagemanager)
@ -175,13 +159,3 @@ install(FILES storagemanager.cnf
DESTINATION ${ENGINE_SYSCONFDIR}/columnstore
COMPONENT columnstore-engine
)
install(TARGETS fdbcs
LIBRARY DESTINATION ${ENGINE_LIBDIR}
COMPONENT columnstore-engine
)
install(TARGETS test_fdb_api
RUNTIME DESTINATION ${ENGINE_BINDIR}
COMPONENT columnstore-engine
)

View File

@ -1,72 +0,0 @@
/* Copyright (C) 2024 MariaDB Corporation
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2 of
the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA. */
#include "../fdb_wrapper_cpp/include/fdbcs.hpp"
using namespace std;
using namespace FDBCS;
template <typename T>
void assert_internal(const T& value)
{
if (!value)
abort();
}
int main()
{
std::string path = "/etc/foundationdb/fdb.cluster";
assert_internal(setAPIVersion());
FDBNetwork netWork;
// Create and run network.
assert_internal(netWork.setUpAndRunNetwork());
// Create database.
std::unique_ptr<FDBDataBase> db = DataBaseCreator::createDataBase(path);
assert_internal(db);
assert_internal(db->isDataBaseReady());
std::string key1 = "fajsdlkfjaskljfewiower39423fds";
std::string value1 = "gfdgjksdflfdsjkslkdrewuior39243";
// Set a key/value.
{
auto tnx = db->createTransaction();
tnx->set(key1, value1);
assert_internal(tnx->commit());
}
// Get a value by a key.
{
auto tnx = db->createTransaction();
auto p = tnx->get(key1);
assert_internal(p.first);
assert_internal(p.second == value1);
}
// Remove a key.
{
auto tnx = db->createTransaction();
tnx->remove(key1);
assert_internal(tnx->commit());
}
// Check that key is not presetnt anymore.
{
auto tnx = db->createTransaction();
auto p = tnx->get(key1);
assert_internal(!p.first);
}
return 0;
}

View File

@ -25,3 +25,4 @@ add_subdirectory(regr)
add_subdirectory(cloudio)
add_subdirectory(libmarias3)
add_subdirectory(pron)
add_subdirectory(fdb_wrapper_cpp)

View File

@ -0,0 +1,24 @@
include_directories(${ENGINE_COMMON_INCLUDES})
find_library(FDB_LIB fdb_c)
if (NOT FDB_LIB)
MESSAGE_ONCE(FATAL_ERROR "FoundationDB not installed properly, please install packages foundationdb-cliends and foundationdb-server")
return()
endif()
add_library(fdbcs SHARED src/fdbcs.cpp)
target_link_libraries(fdbcs fdb_c)
add_executable(test_fdb_api test/test_fdb_api.cpp)
add_dependencies(test_fdb_api fdbcs)
target_link_libraries(test_fdb_api fdbcs fdb_c)
install(TARGETS fdbcs
LIBRARY DESTINATION ${ENGINE_LIBDIR}
COMPONENT columnstore-engine
)
install(TARGETS test_fdb_api
RUNTIME DESTINATION ${ENGINE_BINDIR}
COMPONENT columnstore-engine
)

View File

@ -20,6 +20,7 @@
#include <string>
#include <iostream>
#include <thread>
#include <memory>
// https://apple.github.io/foundationdb/api-c.html
// We have to define `FDB_API_VERSION` before include `fdb_c.h` header.
@ -31,6 +32,7 @@ namespace FDBCS
// TODO: How about uint8_t.
using ByteArray = std::string;
// Represensts a `transaction`.
class Transaction
{
public:
@ -42,15 +44,22 @@ class Transaction
explicit Transaction(FDBTransaction* tnx);
~Transaction();
// Sets a given `key` and given `value`.
void set(const ByteArray& key, const ByteArray& value) const;
// Gets a `value` by the given `key`.
std::pair<bool, ByteArray> get(const ByteArray& key) const;
// Removes a given `key` from database.
void remove(const ByteArray& key) const;
// Removes all keys in the given range, starting from `beginKey` until `endKey`, but not including `endKey`.
void removeRange(const ByteArray& beginKey, const ByteArray& endKey) const;
// Commits transaction.
bool commit() const;
private:
FDBTransaction* tnx_{nullptr};
};
// Represents network class.
class FDBNetwork
{
public:
@ -67,6 +76,7 @@ class FDBNetwork
std::thread netThread;
};
// Represents database class.
class FDBDataBase
{
public:
@ -86,10 +96,12 @@ class FDBDataBase
const uint32_t secondsToWait_ = 3;
};
// Represents a creator class for the `FDBDatabase`.
class DataBaseCreator
{
public:
static std::unique_ptr<FDBDataBase> createDataBase(const std::string clusterFilePath);
// Creates a `FDBDataBase` from the given `clusterFilePath` (path to the cluster file).
static std::shared_ptr<FDBDataBase> createDataBase(const std::string clusterFilePath);
};
bool setAPIVersion();

View File

@ -90,6 +90,15 @@ void Transaction::remove(const ByteArray& key) const
}
}
void Transaction::removeRange(const ByteArray& beginKey, const ByteArray& endKey) const
{
if (tnx_)
{
fdb_transaction_clear_range(tnx_, (uint8_t*)beginKey.c_str(), beginKey.length(), (uint8_t*)endKey.c_str(),
endKey.length());
}
}
bool Transaction::commit() const
{
if (tnx_)
@ -185,7 +194,7 @@ bool FDBDataBase::isDataBaseReady() const
return ready;
}
std::unique_ptr<FDBDataBase> DataBaseCreator::createDataBase(const std::string clusterFilePath)
std::shared_ptr<FDBDataBase> DataBaseCreator::createDataBase(const std::string clusterFilePath)
{
FDBDatabase* database;
auto err = fdb_create_database(clusterFilePath.c_str(), &database);
@ -194,7 +203,7 @@ std::unique_ptr<FDBDataBase> DataBaseCreator::createDataBase(const std::string c
std::cerr << "fdb_create_database error, code: " << (int)err << std::endl;
return nullptr;
}
return std::make_unique<FDBDataBase>(database);
return std::make_shared<FDBDataBase>(database);
}
bool setAPIVersion()

View File

@ -0,0 +1,102 @@
/* Copyright (C) 2024 MariaDB Corporation
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2 of
the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA. */
#include <vector>
#include "../include/fdbcs.hpp"
using namespace std;
using namespace FDBCS;
template <typename T>
static void assert_internal(const T& value, const std::string& errMessage)
{
if (!value)
{
std::cerr << errMessage << std::endl;
abort();
}
}
int main()
{
std::string path = "/etc/foundationdb/fdb.cluster";
assert_internal(setAPIVersion(), "Set api version failed.");
FDBNetwork netWork;
// Create and run network.
assert_internal(netWork.setUpAndRunNetwork(), "Set up network failed.");
// Create database.
auto db = DataBaseCreator::createDataBase(path);
assert_internal(db, "Cannot create FDB.");
assert_internal(db->isDataBaseReady(), "FDB is down.");
const std::vector<std::pair<std::string, std::string>> kvArray{
{"key1", "value1"}, {"key2", "value2"}, {"key3", "value3"}};
const std::string endKey = "key4";
// Set a key/value.
{
auto tnx = db->createTransaction();
tnx->set(kvArray[0].first, kvArray[0].second);
assert_internal(tnx->commit(), "Cannot commit set().");
}
// Get a value by a key.
{
auto tnx = db->createTransaction();
auto p = tnx->get(kvArray[0].first);
assert_internal(p.first, "get() failed.");
assert_internal(p.second == kvArray[0].second, "get(): keys are not matched.");
}
// Remove a key.
{
auto tnx = db->createTransaction();
tnx->remove(kvArray[0].first);
assert_internal(tnx->commit(), "Cannot commit remove().");
}
// Check that key is not presetnt anymore.
{
auto tnx = db->createTransaction();
auto p = tnx->get(kvArray[0].first);
assert_internal(!p.first, "Key exists after remove().");
}
// Remove range.
for (const auto& [key, value] : kvArray)
{
{
auto tnx = db->createTransaction();
tnx->set(key, value);
assert_internal(tnx->commit(), "Cannot commit set() for range.");
}
}
{
auto tnx = db->createTransaction();
tnx->removeRange(kvArray.front().first, endKey);
assert_internal(tnx->commit(), "Cannot commit removeRange().");
}
for (const auto& [key, value] : kvArray)
{
{
auto tnx = db->createTransaction();
auto rPair = tnx->get(key);
assert_internal(!rPair.first, "Key exists after remove range.");
}
}
return 0;
}