-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(tiering): Serialized map #5896
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
Merged
+185
−2
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d92eabc
feat(tiering): Serialized map
dranikpg 74bff56
std::find is picky with iterator types
dranikpg fabf8b7
small comments
dranikpg aa638f7
Merge branch 'main' into tiering-serialized-map
dranikpg 4c46fd1
replace magic numbers with constexpr
dranikpg 410fe1b
remove space
dranikpg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#include "server/tiering/serialized_map.h" | ||
|
||
#include <absl/base/internal/endian.h> | ||
|
||
#include "base/logging.h" | ||
|
||
namespace dfly::tiering { | ||
|
||
SerializedMap::Iterator& SerializedMap::Iterator::operator++() { | ||
slice_.remove_prefix(8 + key_.size() + value_.size()); | ||
Read(); | ||
return *this; | ||
} | ||
|
||
SerializedMap::Iterator::Iterator(std::string_view buffer) : slice_{buffer} { | ||
Read(); | ||
} | ||
|
||
void SerializedMap::Iterator::Read() { | ||
if (slice_.empty()) | ||
return; | ||
|
||
uint32_t key_len = absl::little_endian::Load32(slice_.data()); | ||
uint32_t value_len = absl::little_endian::Load32(slice_.data() + 4); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please give name to 4 |
||
key_ = {slice_.data() + 8, key_len}; | ||
value_ = {slice_.data() + 8 + key_len, value_len}; | ||
} | ||
|
||
SerializedMap::SerializedMap(std::string_view slice) { | ||
size_ = absl::little_endian::Load32(slice.data()); | ||
DCHECK_GT(size_, 0u); | ||
slice_ = slice; | ||
} | ||
|
||
SerializedMap::Iterator SerializedMap::Find(std::string_view key) const { | ||
return std::find_if(begin(), end(), [key](auto p) { return p.first == key; }); | ||
} | ||
|
||
SerializedMap::Iterator SerializedMap::begin() const { | ||
return Iterator{slice_.substr(4)}; | ||
} | ||
|
||
SerializedMap::Iterator SerializedMap::end() const { | ||
return Iterator{slice_.substr(slice_.size(), 0)}; | ||
} | ||
|
||
size_t SerializedMap::size() const { | ||
return size_; | ||
} | ||
|
||
constexpr size_t kLenBytes = 4; | ||
|
||
size_t SerializedMap::SerializeSize(Input input) { | ||
size_t out = kLenBytes; // number of entries | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. four * two or even better four times two sounds like math poetry 🤣 |
||
for (const auto& [key, value] : input) | ||
out += kLenBytes * 2 /* string lengts */ + key.size() + value.size(); | ||
return out; | ||
} | ||
|
||
size_t SerializedMap::Serialize(Input input, absl::Span<char> buffer) { | ||
DCHECK_GE(buffer.size(), SerializeSize(input)); | ||
char* ptr = buffer.data(); | ||
absl::little_endian::Store32(ptr, input.size()); | ||
ptr += kLenBytes; | ||
|
||
for (const auto& [key, value] : input) { | ||
absl::little_endian::Store32(ptr, key.length()); | ||
ptr += kLenBytes; | ||
absl::little_endian::Store32(ptr, value.length()); | ||
ptr += kLenBytes; | ||
memcpy(ptr, key.data(), key.length()); | ||
ptr += key.length(); | ||
memcpy(ptr, value.data(), value.length()); | ||
ptr += value.length(); | ||
} | ||
|
||
return ptr - buffer.data(); | ||
} | ||
|
||
} // namespace dfly::tiering |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#pragma once | ||
|
||
#include <absl/types/span.h> | ||
|
||
#include <string_view> | ||
|
||
namespace dfly::tiering { | ||
|
||
// Map built over single continuous byte slice to allow easy read operations. | ||
struct SerializedMap { | ||
struct Iterator { | ||
using iterator_category = std::forward_iterator_tag; | ||
using difference_type = std::ptrdiff_t; | ||
using value_type = std::pair<std::string_view, std::string_view>; | ||
using reference = value_type; | ||
using pointer = value_type*; | ||
|
||
Iterator& operator++(); | ||
|
||
bool operator==(const Iterator& other) const { | ||
return slice_.data() == other.slice_.data() && slice_.size() == other.slice_.size(); | ||
} | ||
|
||
bool operator!=(const Iterator& other) const { | ||
return !operator==(other); | ||
} | ||
|
||
std::pair<std::string_view, std::string_view> operator*() const { | ||
return {key_, value_}; | ||
} | ||
|
||
private: | ||
friend struct SerializedMap; | ||
|
||
explicit Iterator(std::string_view buffer); | ||
void Read(); | ||
|
||
std::string_view slice_; // the part left | ||
std::string_view key_, value_; | ||
}; | ||
|
||
explicit SerializedMap(std::string_view slice); | ||
|
||
Iterator Find(std::string_view key) const; // Linear search | ||
Iterator begin() const; | ||
Iterator end() const; | ||
size_t size() const; | ||
|
||
// Input for serialization | ||
using Input = const absl::Span<const std::pair<std::string_view, std::string_view>>; | ||
|
||
// Buffer size required for serialization | ||
static size_t SerializeSize(Input); | ||
|
||
// Write a slice that can be used to a SerializedMap on top of it. | ||
// Returns number of bytes written | ||
static size_t Serialize(Input, absl::Span<char> buffer); | ||
|
||
private: | ||
size_t size_; | ||
std::string_view slice_; | ||
}; | ||
|
||
} // namespace dfly::tiering |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include "server/tiering/serialized_map.h" | ||
|
||
#include <map> | ||
|
||
#include "base/logging.h" | ||
#include "gmock/gmock.h" | ||
|
||
namespace dfly::tiering { | ||
|
||
using namespace std; | ||
|
||
class SerializedMapTest : public ::testing::Test {}; | ||
|
||
TEST_F(SerializedMapTest, TestBasic) { | ||
const vector<std::pair<string_view, string_view>> kBase = {{"first key", "first value"}, | ||
{"second key", "second value"}, | ||
{"third key", "third value"}, | ||
{"fourth key", "fourth value"}, | ||
{"fifth key", "fifth value"}}; | ||
|
||
// Serialize kBase to buffer | ||
std::string buffer; | ||
buffer.resize(SerializedMap::SerializeSize(kBase)); | ||
size_t written = SerializedMap::Serialize(kBase, absl::MakeSpan(buffer)); | ||
EXPECT_GT(written, 0u); | ||
|
||
// Build map over buffer and check size | ||
SerializedMap map{buffer}; | ||
EXPECT_EQ(map.size(), kBase.size()); | ||
|
||
// Check entries | ||
size_t idx = 0; | ||
for (auto it = map.begin(); it != map.end(); ++it, ++idx) { | ||
EXPECT_EQ(*it, kBase[idx]); | ||
} | ||
} | ||
|
||
} // namespace dfly::tiering |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add constexpr for 8