Skip to content

Commit

Permalink
Optimize snapshot creation by excluding pre-sorted files
Browse files Browse the repository at this point in the history
  • Loading branch information
umegane committed Oct 2, 2024
1 parent aed365d commit 8209c50
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion include/limestone/api/cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class cursor {
* @brief returns the storage ID of the entry at the current cursor position
* @return the storage ID of the current entry
*/
storage_id_type storage() const noexcept;
[[nodiscard]] storage_id_type storage() const noexcept;

/**
* @brief returns the key byte string of the entry at the current cursor position
Expand Down
6 changes: 2 additions & 4 deletions include/limestone/api/snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,9 @@ class snapshot {
[[nodiscard]] std::unique_ptr<cursor> scan(storage_id_type storage_id, std::string_view entry_key, bool inclusive) const noexcept;

private:
boost::filesystem::path dir_{};
boost::filesystem::path location_{};

[[nodiscard]] boost::filesystem::path file_path() const noexcept;

explicit snapshot(const boost::filesystem::path& location) noexcept;
explicit snapshot(const boost::filesystem::path location) noexcept;

Check warning on line 78 in include/limestone/api/snapshot.h

View workflow job for this annotation

GitHub Actions / Clang-Tidy

readability-avoid-const-params-in-decls

parameter 'location' is const-qualified in the function declaration; const-qualification of parameters only has an effect in function definitions

friend class datastore;
};
Expand Down
4 changes: 2 additions & 2 deletions src/limestone/cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ cursor::~cursor() noexcept {
}

bool cursor::next() {
// Keep calling next() until we find a non-remove_entry, or no more entries
// Keep calling next() until we find a noraml_entry, or no more entries
while (log_entry_tracker_->next()) {
if (log_entry_tracker_->type() != log_entry::entry_type::remove_entry) {
if (log_entry_tracker_->type() == limestone::api::log_entry::entry_type::normal_entry) {
return true;
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/limestone/datastore_snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,7 @@ std::set<std::string> assemble_snapshot_input_filenames(
}
if (boost::filesystem::is_regular_file(it->path())) {
std::string filename = it->path().filename().string();
// if (detached_pwals.find(filename) == detached_pwals.end() && filename != compaction_catalog::get_compacted_filename()) {
if (detached_pwals.find(filename) == detached_pwals.end()) {
if (detached_pwals.find(filename) == detached_pwals.end() && filename != compaction_catalog::get_compacted_filename()) {
filename_set.insert(filename);
}
}
Expand Down Expand Up @@ -365,7 +364,9 @@ void datastore::create_snapshot() {
}
setvbuf(ostrm, nullptr, _IOFBF, 128L * 1024L); // NOLINT, NB. glibc may ignore size when _IOFBF and buffer=NULL
auto write_snapshot_entry = [&ostrm](std::string_view key, std::string_view value){log_entry::write(ostrm, key, value);};
sortdb_foreach(sctx, write_snapshot_entry, true);

bool skip_remove_entry = compaction_catalog_->get_compacted_files().empty();
sortdb_foreach(sctx, write_snapshot_entry, skip_remove_entry);
if (fclose(ostrm) != 0) { // NOLINT(*-owning-memory)
LOG_AND_THROW_IO_EXCEPTION("cannot close snapshot file (" + snapshot_file.string() + ")", errno);
}
Expand Down
16 changes: 9 additions & 7 deletions src/limestone/snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@

#include <glog/logging.h>
#include <limestone/logging.h>
#include "compaction_catalog.h"
#include "logging_helper.h"

namespace limestone::api { // FIXME fill implementation

snapshot::snapshot(const boost::filesystem::path& location) noexcept : dir_(location / boost::filesystem::path(std::string(subdirectory_name_))) {
}
snapshot::snapshot(boost::filesystem::path location) noexcept : location_(std::move(location)) {}

std::unique_ptr<cursor> snapshot::get_cursor() const {
return std::unique_ptr<cursor>(new cursor(file_path()));
boost::filesystem::path compacted_file = location_ / limestone::internal::compaction_catalog::get_compacted_filename();
boost::filesystem::path snapshot_file = location_ / std::string(subdirectory_name_) / std::string(file_name_);

if (boost::filesystem::exists(compacted_file)) {
return std::unique_ptr<cursor>(new cursor(snapshot_file, compacted_file));
}
return std::unique_ptr<cursor>(new cursor(snapshot_file));
}

std::unique_ptr<cursor> snapshot::find([[maybe_unused]] storage_id_type storage_id, [[maybe_unused]] std::string_view entry_key) const noexcept {
Expand All @@ -38,8 +44,4 @@ std::unique_ptr<cursor> snapshot::scan([[maybe_unused]] storage_id_type storage_
std::abort(); // FIXME should implement
}

boost::filesystem::path snapshot::file_path() const noexcept {
return dir_ / boost::filesystem::path(std::string(file_name_));
}

} // namespace limestone::api

0 comments on commit 8209c50

Please sign in to comment.