Skip to content

Commit

Permalink
cleanup: make some functions const and emphasize endianness in log_entry
Browse files Browse the repository at this point in the history
  • Loading branch information
ban-nobuhiro committed Aug 27, 2024
1 parent 3531182 commit a0eb980
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/limestone/datastore_snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static int comp_twisted_key(const std::string_view& a, const std::string_view& b
}

[[maybe_unused]]
static void insert_entry_or_update_to_max(sortdb_wrapper* sortdb, log_entry& e) {
static void insert_entry_or_update_to_max(sortdb_wrapper* sortdb, const log_entry& e) {
bool need_write = true;
// skip older entry than already inserted
std::string value;
Expand All @@ -73,7 +73,7 @@ static void insert_entry_or_update_to_max(sortdb_wrapper* sortdb, log_entry& e)
}

[[maybe_unused]]
static void insert_twisted_entry(sortdb_wrapper* sortdb, log_entry& e) {
static void insert_twisted_entry(sortdb_wrapper* sortdb, const log_entry& e) {
// key_sid: storage_id[8] key[*], value_etc: epoch[8]LE minor_version[8]LE value[*], type: type[1]
// db_key: epoch[8]BE minor_version[8]BE storage_id[8] key[*], db_value: type[1] value[*]
std::string db_key(write_version_size + e.key_sid().size(), '\0');
Expand Down Expand Up @@ -105,7 +105,7 @@ static std::pair<epoch_id_type, std::unique_ptr<sortdb_wrapper>> create_sortdb_f
const auto add_entry_to_point = insert_entry_or_update_to_max;
bool works_with_multi_thread = false;
#endif
auto add_entry = [&sortdb, &add_entry_to_point](log_entry& e){
auto add_entry = [&sortdb, &add_entry_to_point](const log_entry& e){
switch (e.type()) {
case log_entry::entry_type::normal_entry:
case log_entry::entry_type::remove_entry:
Expand Down
21 changes: 14 additions & 7 deletions src/limestone/log_entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,18 +325,19 @@ class log_entry {
return true;
}

void write_version(write_version_type& buf) {
memcpy(static_cast<void*>(&buf), value_etc_.data(), sizeof(epoch_id_type) + sizeof(std::uint64_t));
void write_version(write_version_type& buf) const {
buf.epoch_number_ = write_version_epoch_number(value_etc_);
buf.minor_write_version_ = write_version_minor_write_version(value_etc_);
}
[[nodiscard]] storage_id_type storage() const {
storage_id_type storage_id{};
memcpy(static_cast<void*>(&storage_id), key_sid_.data(), sizeof(storage_id_type));
return storage_id;
return le64toh(storage_id);
}
void value(std::string& buf) {
void value(std::string& buf) const {
buf = value_etc_.substr(sizeof(epoch_id_type) + sizeof(std::uint64_t));
}
void key(std::string& buf) {
void key(std::string& buf) const {
buf = key_sid_.substr(sizeof(storage_id_type));
}
[[nodiscard]] entry_type type() const {
Expand All @@ -350,18 +351,24 @@ class log_entry {
std::string& value_etc() {
return value_etc_;
}
[[nodiscard]] const std::string& value_etc() const {
return value_etc_;
}
std::string& key_sid() {
return key_sid_;
}
[[nodiscard]] const std::string& key_sid() const {
return key_sid_;
}
static epoch_id_type write_version_epoch_number(std::string_view value_etc) {
epoch_id_type epoch_id{};
memcpy(static_cast<void*>(&epoch_id), value_etc.data(), sizeof(epoch_id_type));
return epoch_id;
return le64toh(epoch_id);
}
static std::uint64_t write_version_minor_write_version(std::string_view value_etc) {
std::uint64_t minor_write_version{};
memcpy(static_cast<void*>(&minor_write_version), value_etc.data() + sizeof(epoch_id_type), sizeof(std::uint64_t));
return minor_write_version;
return le64toh(minor_write_version);
}

private:
Expand Down

0 comments on commit a0eb980

Please sign in to comment.