Skip to content

Commit

Permalink
Add a test for nearest search on deleted keys (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
greensky00 committed Feb 16, 2024
1 parent 31c8daa commit eca5ca0
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ We welcome contributions. If you find any bugs, potential flaws and edge cases,

Contact
-------
* Jung-Sang Ahn <junahn@ebay.com>
* Jung-Sang Ahn <jungsang.ahn@gmail.com>


Coding Convention
Expand Down Expand Up @@ -162,7 +162,7 @@ int helper_function() {

License Information
--------------------
Copyright 2017-2019 eBay Inc.
Copyright 2017-present eBay Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Expand Down
2 changes: 1 addition & 1 deletion manifest.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
FORESTDB_COMMIT="c2a0cf7e708e2a1fb883bca1f2f3d6c3c648812e"
FORESTDB_COMMIT="3b330182b792fe8665ecce0f3847212cd6612de8"
111 changes: 106 additions & 5 deletions tests/jungle/nearest_search_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ int get_nearest_test(int flush_options) {
// Shuffle (0 -- 99).
std::vector<size_t> idx_arr(NUM);
std::iota(idx_arr.begin(), idx_arr.end(), 0);
/*
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(idx_arr.begin(), idx_arr.end(), g);
*/
for (size_t ii = 0; ii < NUM; ++ii) {
size_t jj = std::rand() % NUM;
std::swap(idx_arr[ii], idx_arr[jj]);
Expand Down Expand Up @@ -160,6 +155,109 @@ int get_nearest_test(int flush_options) {
return 0;
}

int get_nearest_on_deleted_keys_test(int flush_options) {
// flush_options:
// 0: all in the log.
// 1: 3/4 in the log and 1/4 in the table.
// 2: 1/2 in the log and 1/2 in the table.
// 3: 1/4 in the log and 3/4 in the table.
// 4: all in the table.

std::string filename;
TEST_SUITE_PREPARE_PATH(filename);

jungle::Status s;
jungle::DBConfig config;
TEST_CUSTOM_DB_CONFIG(config);
jungle::DB* db;

config.maxEntriesInLogFile = 20;
CHK_Z(jungle::DB::open(&db, filename, config));

const size_t NUM = 100;

// Shuffle (0 -- 99).
std::vector<size_t> idx_arr(NUM);
std::iota(idx_arr.begin(), idx_arr.end(), 0);
for (size_t ii = 0; ii < NUM; ++ii) {
size_t jj = std::rand() % NUM;
std::swap(idx_arr[ii], idx_arr[jj]);
}

for (size_t kk = 0; kk < 2; ++kk) {
for (size_t ii = 0; ii < NUM; ++ii) {
size_t idx = idx_arr[ii] * 10;
std::string key_str = "key" + TestSuite::lzStr(5, idx);
std::string val_str = "val" + TestSuite::lzStr(2, kk) +
"_" + TestSuite::lzStr(5, idx);

if (kk == 0) {
CHK_Z( db->set( jungle::KV(key_str, val_str) ) );
} else if (kk == 1) {
CHK_Z( db->del( key_str ) );
}

if ( (kk == 0 && ii == NUM/2 - 1 && flush_options == 1) ||
(kk == 1 && ii == NUM/2 - 1 && flush_options == 3) ) {
CHK_Z( db->sync(false) );
CHK_Z( db->flushLogs() );
}
}
CHK_Z( db->sync(false) );
if (kk == 0 && flush_options == 2) {
CHK_Z( db->flushLogs() );
}
}
CHK_Z( db->sync(false) );
if (flush_options == 4) {
CHK_Z( db->flushLogs() );
}

auto verify_func = [&]( size_t ii,
jungle::SearchOptions s_opt,
bool exact_query ) -> int {
TestSuite::setInfo("ii %zu, s_opt %d, exact_query %d",
ii, s_opt, exact_query);

int64_t idx = exact_query ? ii * 10 : ii * 10 + (s_opt.isGreater() ? 1 : -1);

std::string key_str;
if (idx >= 0) {
key_str = "key" + TestSuite::lzStr(5, idx);
} else {
key_str = "000";
}

jungle::Record rec_out;
jungle::Record::Holder h_rec_out(rec_out);
s = db->getNearestRecordByKey(jungle::SizedBuf(key_str), rec_out, s_opt);
if (s.ok()) {
CHK_TRUE(rec_out.isDel());
}

return 0;
};

using jungle::SearchOptions;
for (size_t ii = 0; ii < NUM; ++ii) {
for (bool exact_query: {true, false}) {
for (SearchOptions s_opt: { SearchOptions::GREATER_OR_EQUAL,
SearchOptions::GREATER,
SearchOptions::SMALLER_OR_EQUAL,
SearchOptions::SMALLER,
SearchOptions::EQUAL }) {
CHK_Z( verify_func(ii, s_opt, exact_query) );
}
}
}

CHK_Z(jungle::DB::close(db));
CHK_Z(jungle::shutdown());

TEST_SUITE_CLEANUP_PATH();
return 0;
}

int get_by_prefix_test(size_t hash_len) {
std::string filename;
TEST_SUITE_PREPARE_PATH(filename);
Expand Down Expand Up @@ -357,6 +455,9 @@ int main(int argc, char** argv) {
ts.doTest("get nearest test",
get_nearest_test,
TestRange<int>(0, 4, 1, StepType::LINEAR));
ts.doTest("get nearest on deleted keys test",
get_nearest_on_deleted_keys_test,
TestRange<int>(0, 4, 1, StepType::LINEAR));
ts.doTest("get by prefix test",
get_by_prefix_test, TestRange<size_t>( {0, 8, 16} ));
ts.doTest("get by prefix frequent update test",
Expand Down
2 changes: 1 addition & 1 deletion third_party/forestdb

0 comments on commit eca5ca0

Please sign in to comment.