Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wip/log 0.7 #57

Merged
merged 22 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
accc0c6
Add an internal document
umegane Sep 30, 2024
1a48442
Refactor to isolate testable units for easier testing
umegane Sep 30, 2024
68480e0
Add flag to snapshot creation to skip entry removal
umegane Sep 30, 2024
aed365d
Make cursor class handle two streams
umegane Sep 30, 2024
b01068c
cleanup: clang-tidy
umegane Oct 2, 2024
8b8b8d6
Update an internal document
umegane Oct 3, 2024
0146118
Add test cases
umegane Oct 3, 2024
c5b067f
Refactor: Rename test file and class for compaction functionality
umegane Oct 4, 2024
605844e
Add test cases
umegane Oct 4, 2024
2f313b7
Merge branch 'master' into wip/log-0.7
umegane Oct 7, 2024
d514252
Refactor: Separate snapshot_tracker class into standalone files
umegane Oct 10, 2024
b44d6c5
Add test cases
umegane Oct 10, 2024
7654a92
Refactor: Apply Pimpl Idiom to cursor class and rename snapshot_track…
umegane Oct 17, 2024
f85c130
Refactor: Apply Pimpl Idiom to snapshot class to hide implementation …
umegane Oct 17, 2024
1bf2a15
Refactor: Move sorting_context class to separate files
umegane Oct 17, 2024
aa64958
feat: Enable cursor to access clear_storage from sorting_context
umegane Oct 17, 2024
434d44c
cleanup: clang-tidy
umegane Oct 17, 2024
f8799b5
Fix: Ensure proper deletion of entries in snapshot when drop or trunc…
umegane Oct 18, 2024
b1be94a
Add test cases
umegane Oct 18, 2024
544b6ca
cleanup: clang-tidy
umegane Oct 18, 2024
acc1f2e
Update an internal document
umegane Oct 21, 2024
061fdb4
Remove db_startup_sort_improvement.md before merge to master
umegane Oct 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions include/limestone/api/cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,25 @@

#include <memory>
#include <vector>
#include <optional>

#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>

#include <limestone/api/storage_id_type.h>
#include <limestone/api/large_object_view.h>


namespace limestone::internal {
class cursor_impl;
}

namespace limestone::api {

class log_entry;
class snapshot;


/**
* @brief a cursor to scan entries on the snapshot
*/
Expand Down Expand Up @@ -56,7 +63,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 All @@ -77,13 +84,14 @@ class cursor {
std::vector<large_object_view>& large_objects() noexcept;

private:
boost::filesystem::ifstream istrm_{};
std::unique_ptr<log_entry> log_entry_;
std::unique_ptr<internal::cursor_impl> pimpl;

std::vector<large_object_view> large_objects_{};

explicit cursor(const boost::filesystem::path& file);

friend class snapshot;
explicit cursor(const boost::filesystem::path& snapshot_file);
explicit cursor(const boost::filesystem::path& snapshot_file, const boost::filesystem::path& compacted_file);

friend class internal::cursor_impl;
};

} // namespace limestone::api
6 changes: 4 additions & 2 deletions include/limestone/api/datastore.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <set>
#include <mutex>
#include <queue>
#include <map>

#include <boost/filesystem.hpp>

Expand Down Expand Up @@ -309,7 +310,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 All @@ -324,7 +325,8 @@ class datastore {
void rotate_epoch_file();

int64_t current_unix_epoch_in_millis();


std::map<storage_id_type, write_version_type> clear_storage;
};

} // namespace limestone::api
2 changes: 1 addition & 1 deletion include/limestone/api/log_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class rotation_result;
* @details this object is not thread-safe, assuming each thread uses its own log_channel
*/
class log_channel {
friend class rotation_task;

public:
/**
Expand Down Expand Up @@ -180,6 +179,7 @@ class log_channel {
rotation_result do_rotate_file(epoch_id_type epoch = 0);

friend class datastore;
friend class rotation_task;
};

} // namespace limestone::api
19 changes: 14 additions & 5 deletions include/limestone/api/snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,30 @@

#include <memory>
#include <string_view>

#include <map>
#include <boost/filesystem.hpp>

#include <limestone/api/cursor.h>
#include <limestone/api/write_version_type.h>

namespace limestone::internal {
class snapshot_impl;
}

namespace limestone::api {

/**
* @brief a snapshot of the data at a point in time on the data store
*/
class snapshot {

public:
snapshot() noexcept = delete;
snapshot(const snapshot&) = delete;
snapshot& operator=(const snapshot&) = delete;
snapshot(snapshot&&) noexcept = delete;
snapshot& operator=(snapshot&&) noexcept = delete;
~snapshot();

/**
* @brief directory name of a snapshot
Expand Down Expand Up @@ -73,11 +84,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_{};

[[nodiscard]] boost::filesystem::path file_path() const noexcept;
std::unique_ptr<internal::snapshot_impl> pimpl;

explicit snapshot(const boost::filesystem::path& location) noexcept;
explicit snapshot(boost::filesystem::path location, std::map<storage_id_type, write_version_type> clear_storage) noexcept;

friend class datastore;
};
Expand Down
36 changes: 15 additions & 21 deletions src/limestone/cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,37 @@
#include "logging_helper.h"
#include "limestone_exception_helper.h"
#include "log_entry.h"
#include "cursor_impl.h"


namespace limestone::api {

cursor::cursor(const boost::filesystem::path& file) : log_entry_(std::make_unique<log_entry>()) {
istrm_.open(file, std::ios_base::in | std::ios_base::binary );
if (!istrm_.good()) {
LOG_AND_THROW_EXCEPTION("file stream of the cursor is not good (" + file.string() + ")");
}
}

cursor::cursor(const boost::filesystem::path& snapshot_file)
: pimpl(std::make_unique<limestone::internal::cursor_impl>(snapshot_file)) {}

cursor::cursor(const boost::filesystem::path& snapshot_file, const boost::filesystem::path& compacted_file)
: pimpl(std::make_unique<limestone::internal::cursor_impl>(snapshot_file, compacted_file)) {}

cursor::~cursor() noexcept {
istrm_.close();
// TODO: handle close failure
pimpl->close();
}

bool cursor::next() {
if (!istrm_.good()) {
DVLOG_LP(log_trace) << "file stream of the cursor is not good";
return false;
}
if (istrm_.eof()) {
DVLOG_LP(log_trace) << "already detected eof of the cursor";
return false;
}
auto rv = log_entry_->read(istrm_);
DVLOG_LP(log_trace) << (rv ? "read an entry from the cursor" : "detect eof of the cursor");
return rv;
return pimpl->next();
}

storage_id_type cursor::storage() const noexcept {
return log_entry_->storage();
return pimpl->storage();
}

void cursor::key(std::string& buf) const noexcept {
log_entry_->key(buf);
pimpl->key(buf);
}

void cursor::value(std::string& buf) const noexcept {
log_entry_->value(buf);
pimpl->value(buf);
}

std::vector<large_object_view>& cursor::large_objects() noexcept {
Expand Down
Loading