Skip to content

Commit

Permalink
Refactor to isolate testable units for easier testing
Browse files Browse the repository at this point in the history
  • Loading branch information
umegane committed Sep 30, 2024
1 parent accc0c6 commit 1a48442
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
2 changes: 1 addition & 1 deletion include/limestone/api/datastore.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ class datastore {
* @param from the location of log files
* @attention this function is not thread-safe.
*/
void create_snapshot(const std::set<std::string>& file_names);
void create_snapshot();

epoch_id_type last_durable_epoch_in_dir();

Expand Down
27 changes: 4 additions & 23 deletions src/limestone/datastore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,33 +104,14 @@ void datastore::recover() const noexcept {
}

void datastore::ready() {
// create filename set for snapshot
std::set<std::string> detached_pwals = compaction_catalog_->get_detached_pwals();

std::set<std::string> filename_set;
boost::system::error_code error;
boost::filesystem::directory_iterator it(location_, error);
boost::filesystem::directory_iterator end;
if (error) {
LOG_AND_THROW_IO_EXCEPTION("Failed to initialize directory iterator, path: " + location_.string(), error);
}
for (; it != end; it.increment(error)) {
if (error) {
LOG_AND_THROW_IO_EXCEPTION("Failed to access directory entry: , path: " + location_.string(), error);
}
if (boost::filesystem::is_regular_file(it->path())) {
std::string filename = it->path().filename().string();
if (detached_pwals.find(filename) == detached_pwals.end()) {
filename_set.insert(filename);
}
}
}

create_snapshot(filename_set);
create_snapshot();
online_compaction_worker_future_ = std::async(std::launch::async, &datastore::online_compaction_worker, this);
state_ = state::ready;
}




std::unique_ptr<snapshot> datastore::get_snapshot() const {
check_after_ready(static_cast<const char*>(__func__));
return std::unique_ptr<snapshot>(new snapshot(location_));
Expand Down
32 changes: 31 additions & 1 deletion src/limestone/datastore_snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <limestone/api/datastore.h>
#include "limestone_exception_helper.h"
#include "compaction_catalog.h"
#include "dblog_scan.h"
#include "internal.h"
#include "log_entry.h"
Expand Down Expand Up @@ -299,13 +300,42 @@ void create_compact_pwal(
}
}

std::set<std::string> assemble_snapshot_input_filenames(
const std::unique_ptr<compaction_catalog>& compaction_catalog,
const boost::filesystem::path& location) {
std::set<std::string> detached_pwals = compaction_catalog->get_detached_pwals();

std::set<std::string> filename_set;
boost::system::error_code error;
boost::filesystem::directory_iterator it(location, error);
boost::filesystem::directory_iterator end;
if (error) {
LOG_AND_THROW_IO_EXCEPTION("Failed to initialize directory iterator, path: " + location.string(), error);
}
for (; it != end; it.increment(error)) {
if (error) {
LOG_AND_THROW_IO_EXCEPTION("Failed to access directory entry, path: " + location.string(), error);
}
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()) {
filename_set.insert(filename);
}
}
}

return filename_set;
}

}

namespace limestone::api {
using namespace limestone::internal;

void datastore::create_snapshot(const std::set<std::string>& file_names) {
void datastore::create_snapshot() {
const auto& from_dir = location_;
std::set<std::string> file_names = assemble_snapshot_input_filenames(compaction_catalog_, from_dir);
auto [max_appeared_epoch, sctx] = create_sorted_from_wals(from_dir, recover_max_parallelism_, file_names);
epoch_id_switched_.store(max_appeared_epoch);
epoch_id_informed_.store(max_appeared_epoch);
Expand Down

0 comments on commit 1a48442

Please sign in to comment.