Skip to content

Commit

Permalink
adding leveldb
Browse files Browse the repository at this point in the history
  • Loading branch information
JslYoon committed Feb 6, 2025
1 parent 9261534 commit 38f2373
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
path = external/rocksdb
url = [email protected]:BU-DiSC/rocksdb.git
branch = sortedness
[submodule "external/leveldb_bliss"]
path = external/leveldb_bliss
url = [email protected]:JslYoon/leveldb_bliss.git
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ project(bliss_benchmark VERSION 1.0
LANGUAGES CXX
)

set(BUILD_TESTING OFF CACHE BOOL "Disable testing" FORCE)
set(LEVELDB_BUILD_TESTS OFF CACHE BOOL "Disable LevelDB tests" FORCE)


include(ExternalProject)
message(STATUS "CXX : ${CMAKE_CXX_COMPILER}")
set(CMAKE_CXX_STANDARD 17)
Expand Down Expand Up @@ -54,6 +58,7 @@ add_library(bliss OBJECT
${CMAKE_SOURCE_DIR}/src/bliss/bench_skiplist.h
${CMAKE_SOURCE_DIR}/src/bliss/bench_pgm.h
${CMAKE_SOURCE_DIR}/src/bliss/bench_art.h
${CMAKE_SOURCE_DIR}/src/bliss/bench_leveldb.h
)

target_compile_features(bliss PUBLIC
Expand Down Expand Up @@ -97,3 +102,9 @@ add_executable(bliss_bench src/bliss_bench.cpp)
target_link_libraries(bliss_bench PUBLIC
bliss
)

add_subdirectory(${CMAKE_SOURCE_DIR}/external/leveldb_bliss)
target_include_directories(bliss PUBLIC ${CMAKE_SOURCE_DIR}/external/leveldb_bliss/include)
target_link_libraries(bliss PRIVATE leveldb)

include_directories(${CMAKE_SOURCE_DIR}/external/leveldb_bliss/include)
1 change: 1 addition & 0 deletions external/leveldb_bliss
Submodule leveldb_bliss added at 434e20
80 changes: 80 additions & 0 deletions src/bliss/bench_leveldb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#ifndef BLISS_BENCH_LEVELDB
#define BLISS_BENCH_LEVELDB

#include <vector>
#include <string>
#include <sstream>
#include <cstdlib>
#include <iostream>

#include "bliss/bliss_index.h"
#include "leveldb/db.h"

namespace bliss {

template <typename KEY_TYPE, typename VALUE_TYPE>
class BlissLevelDBIndex : public BlissIndex<KEY_TYPE, VALUE_TYPE> {
public:
leveldb::DB* db_;


BlissLevelDBIndex() {
leveldb::Options options;
options.create_if_missing = true;

std::string db_path = "/tmp/bliss_leveldb_index";
leveldb::Status status = leveldb::DB::Open(options, db_path, &db_);
if (!status.ok()) {
std::cerr << "Failed to open LevelDB at " << db_path << ": "
<< status.ToString() << std::endl;
std::exit(1);
}
}


void bulkload(std::vector<std::pair<KEY_TYPE, VALUE_TYPE>> values) override {
for (const auto& kv : values) {
put(kv.first, kv.second);
}
}


bool get(KEY_TYPE key) override {
std::string key_str = to_string(key);
std::string value;
leveldb::Status status = db_->Get(leveldb::ReadOptions(), key_str, &value);
return status.ok();
}


void put(KEY_TYPE key, VALUE_TYPE value) override {
std::string key_str = to_string(key);
std::string value_str = to_string(value);
leveldb::Status status = db_->Put(leveldb::WriteOptions(), key_str, value_str);
if (!status.ok()) {
std::cerr << "LevelDB put error: " << status.ToString() << std::endl;
}
}


void end_routine() override {
delete db_;
}

private:
template <typename T>
std::string to_string(const T& t) {
std::ostringstream oss;
oss << t;
return oss.str();
}


std::string to_string(const std::string& s) {
return s;
}
};

} // namespace bliss

#endif // BLISS_BENCH_LEVELDB
3 changes: 3 additions & 0 deletions src/bliss_bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "bliss/bench_art.h"
#include "bliss/bench_btree.h"
#include "bliss/bench_lipp.h"
#include "bliss/bench_leveldb.h"
#include "bliss/bench_skiplist.h"
#include "bliss/bliss_index.h"
#include "bliss/util/args.h"
Expand Down Expand Up @@ -179,6 +180,8 @@ int main(int argc, char *argv[]) {
index.reset(new bliss::BlissARTIndex<key_type, value_type>());
} else if (config.index == "pgm") {
index.reset(new bliss::BlissPGMIndex<key_type, value_type>());
} else if (config.index == "leveldb") {
index.reset(new bliss::BlissLevelDBIndex<key_type, value_type>());
} else {
spdlog::error(config.index + " not implemented yet", 1);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ add_subdirectory(test_lipp)
add_subdirectory(test_btree)
add_subdirectory(test_skiplist)
add_subdirectory(test_art)
add_subdirectory(test_leveldb)

3 changes: 2 additions & 1 deletion tests/bliss_index_tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "bliss/bench_lipp.h"
#include "bliss/bench_skiplist.h"
#include "bliss/bench_pgm.h"
#include "bliss/bench_leveldb.h"
#include "bliss/bliss_index.h"
#include "bliss/util/args.h"
#include "bliss/util/config.h"
Expand All @@ -36,7 +37,7 @@ class BlissIndexTest : public testing::Test
{
protected:
std::unique_ptr<bliss::BlissIndex<key_type, value_type>> index;
std::string indexes[6] = {"alex", "lipp", "btree", "skiplist", "pgm", "art"};
std::string indexes[7] = {"alex", "lipp", "btree", "skiplist", "pgm", "art", "leveldb"};

int num_keys = 100000;

Expand Down
9 changes: 9 additions & 0 deletions tests/test_leveldb/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
get_filename_component(EXEC ${CMAKE_CURRENT_SOURCE_DIR} NAME)
file(GLOB_RECURSE CPP_TESTS "*_tests.cpp")
add_executable(${EXEC} ${CPP_TESTS})
target_link_libraries(${EXEC} PRIVATE
bliss
bliss_test_infra
GTest::gtest_main)
include(GoogleTest)
gtest_discover_tests(${EXEC})
40 changes: 40 additions & 0 deletions tests/test_leveldb/leveldb_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "bliss_index_tests.h"

class LevelDBTest : public BlissIndexTest {};

TEST_F(LevelDBTest, TestLevelDB_Sanity) {
index.reset(new bliss::BlissLevelDBIndex<key_type, key_type>());
std::vector<key_type> data;
int key = 100;
int value = 123;
index->put(key, value);
EXPECT_TRUE(index->get(key));
}

TEST_F(LevelDBTest, TestLevelDB_Sorted) {
index.reset(new bliss::BlissLevelDBIndex<key_type, key_type>());
std::vector<key_type> data;
GenerateData(data, num_keys);

auto insert_start = data.begin();
auto insert_end = data.end();
executor::execute_inserts(*index, insert_start, insert_end);

for (auto key : data) {
EXPECT_TRUE(index->get(key));
}
}

TEST_F(LevelDBTest, TestLevelDB_Random) {
index.reset(new bliss::BlissLevelDBIndex<key_type, key_type>());
std::vector<key_type> data;
GenerateData(data, num_keys, false);

auto insert_start = data.begin();
auto insert_end = data.end();
executor::execute_inserts(*index, insert_start, insert_end);

for (auto key : data) {
EXPECT_TRUE(index->get(key));
}
}

0 comments on commit 38f2373

Please sign in to comment.