Skip to content

Commit

Permalink
refactor: hide leveldb interface into leveldb_wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
ban-nobuhiro committed Jun 6, 2023
1 parent 464246a commit 3f307c7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
23 changes: 8 additions & 15 deletions src/limestone/datastore_snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ epoch_id_type datastore::last_durable_epoch_in_dir() noexcept {

void datastore::create_snapshot() noexcept {
auto& from_dir = location_;
auto lvldb = std::make_unique<leveldb_wrapper>(from_dir);
auto db = std::make_unique<leveldb_wrapper>(from_dir);

epoch_id_type ld_epoch = last_durable_epoch_in_dir();
epoch_id_switched_.store(ld_epoch + 1);
Expand Down Expand Up @@ -100,8 +100,7 @@ void datastore::create_snapshot() noexcept {
e.write_version(write_version);

// skip older entry than already inserted
leveldb::ReadOptions read_options;
if (auto status = lvldb->db()->Get(read_options, e.key_sid(), &value); status.ok()) {
if (db->get(e.key_sid(), &value)) {
if (write_version < write_version_type(value.substr(1))) {
need_write = false;
}
Expand All @@ -110,8 +109,7 @@ void datastore::create_snapshot() noexcept {
std::string db_value;
db_value.append(1, static_cast<char>(e.type()));
db_value.append(e.value_etc());
leveldb::WriteOptions write_options;
lvldb->db()->Put(write_options, e.key_sid(), db_value);
db->put(e.key_sid(), db_value);
}
}
break;
Expand All @@ -125,8 +123,7 @@ void datastore::create_snapshot() noexcept {
e.write_version(write_version);

// skip older entry than already inserted
leveldb::ReadOptions read_options;
if (auto status = lvldb->db()->Get(read_options, e.key_sid(), &value); status.ok()) {
if (db->get(e.key_sid(), &value)) {
if (write_version < write_version_type(value.substr(1))) {
need_write = false;
}
Expand All @@ -135,8 +132,7 @@ void datastore::create_snapshot() noexcept {
std::string db_value;
db_value.append(1, static_cast<char>(e.type()));
db_value.append(e.value_etc());
leveldb::WriteOptions write_options;
lvldb->db()->Put(write_options, e.key_sid(), db_value);
db->put(e.key_sid(), db_value);
}
}
break;
Expand Down Expand Up @@ -167,25 +163,22 @@ void datastore::create_snapshot() noexcept {
LOG_LP(ERROR) << "cannot create snapshot file (" << snapshot_file << ")";
std::abort();
}
leveldb::Iterator* it = lvldb->db()->NewIterator(leveldb::ReadOptions()); // NOLINT (typical usage of leveldb)
for (it->SeekToFirst(); it->Valid(); it->Next()) {
leveldb::Slice db_value = it->value();
db->each([&ostrm](std::string_view db_key, std::string_view db_value) {
static_assert(sizeof(log_entry::entry_type) == 1);
auto entry_type = static_cast<log_entry::entry_type>(db_value[0]);
db_value.remove_prefix(1);
switch (entry_type) {
case log_entry::entry_type::normal_entry:
log_entry::write(ostrm, it->key().ToString(), db_value.ToString());
log_entry::write(ostrm, db_key, db_value);
break;
case log_entry::entry_type::remove_entry:
break; // skip
default:
LOG_LP(ERROR) << "never reach " << static_cast<int>(entry_type);

Check warning on line 177 in src/limestone/datastore_snapshot.cpp

View workflow job for this annotation

GitHub Actions / Clang-Tidy (ubuntu-20.04)

bugprone-lambda-function-name

inside a lambda, '__FUNCTION__' expands to the name of the function call operator; consider capturing the name of the enclosing function explicitly
std::abort();
}
}
});
ostrm.close();
delete it; // NOLINT (typical usage of leveldb)
}

} // namespace limestone::api
28 changes: 23 additions & 5 deletions src/limestone/leveldb_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
// #include <boost/foreach.hpp>
#include <leveldb/db.h>

// #include <glog/logging.h>
// #include <limestone/logging.h>
#include <glog/logging.h>
#include <limestone/logging.h>

// #include <limestone/api/datastore.h>
// #include "log_entry.h"
Expand Down Expand Up @@ -58,10 +58,28 @@ class leveldb_wrapper {
leveldb_wrapper(leveldb_wrapper&& other) noexcept = delete;
leveldb_wrapper& operator=(leveldb_wrapper&& other) noexcept = delete;

[[nodiscard]] leveldb::DB* db() const noexcept {
return lvldb_;
bool put(const std::string& key, const std::string& value) {
leveldb::WriteOptions write_options{};
auto status = lvldb_->Put(write_options, key, value);
return status.ok();
}


bool get(const std::string& key, std::string* value) {
leveldb::ReadOptions read_options{};
auto status = lvldb_->Get(read_options, key, value);
return status.ok();
}

void each(std::function<void(std::string_view, std::string_view)> fun) {

Check warning on line 73 in src/limestone/leveldb_wrapper.h

View workflow job for this annotation

GitHub Actions / Clang-Tidy (ubuntu-20.04)

performance-unnecessary-value-param

the parameter 'fun' is copied for each invocation but only used as a const reference; consider making it a const reference
leveldb::Iterator* it = lvldb_->NewIterator(leveldb::ReadOptions()); // NOLINT (typical usage of leveldb)
for (it->SeekToFirst(); it->Valid(); it->Next()) {
leveldb::Slice key = it->key();
leveldb::Slice value = it->value();
fun(std::string_view(key.data(), key.size()), std::string_view(value.data(), value.size()));
}
delete it; // NOLINT (typical usage of leveldb)
}

private:
leveldb::DB* lvldb_{};
boost::filesystem::path lvldb_path_;
Expand Down

0 comments on commit 3f307c7

Please sign in to comment.