diff --git a/src/limestone/datastore_snapshot.cpp b/src/limestone/datastore_snapshot.cpp index 8329e6f..5b7c5b8 100644 --- a/src/limestone/datastore_snapshot.cpp +++ b/src/limestone/datastore_snapshot.cpp @@ -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(from_dir); + auto db = std::make_unique(from_dir); epoch_id_type ld_epoch = last_durable_epoch_in_dir(); epoch_id_switched_.store(ld_epoch + 1); @@ -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; } @@ -110,8 +109,7 @@ void datastore::create_snapshot() noexcept { std::string db_value; db_value.append(1, static_cast(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; @@ -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; } @@ -135,8 +132,7 @@ void datastore::create_snapshot() noexcept { std::string db_value; db_value.append(1, static_cast(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; @@ -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(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(entry_type); + LOG(ERROR) << "never reach " << static_cast(entry_type); std::abort(); } - } + }); ostrm.close(); - delete it; // NOLINT (typical usage of leveldb) } } // namespace limestone::api diff --git a/src/limestone/leveldb_wrapper.h b/src/limestone/leveldb_wrapper.h index 496013e..bccff48 100644 --- a/src/limestone/leveldb_wrapper.h +++ b/src/limestone/leveldb_wrapper.h @@ -17,8 +17,8 @@ // #include #include -// #include -// #include +#include +#include // #include // #include "log_entry.h" @@ -58,8 +58,26 @@ 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(const std::function& fun) { + 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: