1#ifndef STORAGE_MEM_TABLE_H
2#define STORAGE_MEM_TABLE_H
4#include <vkdb/concepts.h>
5#include <vkdb/string.h>
6#include <vkdb/time_series_key.h>
7#include <vkdb/data_range.h>
16template <ArithmeticNoCVRefQuals TValue>
20 using mapped_type = std::optional<TValue>;
21 using value_type = std::pair<const key_type, mapped_type>;
22 using size_type = uint64_t;
23 using table_type = std::map<const key_type, mapped_type>;
25 static constexpr size_type C0_LAYER_SSTABLE_MAX_ENTRIES{100'000};
38 : table_{std::move(entries)} {}
76 table_.insert_or_assign(key, value);
88 return in_range(key) && table_.contains(key);
100 if (!in_range(key)) {
101 throw std::invalid_argument{
102 "MemTable::get(): Key '" + key.
str() +
"' not in the memtable."
105 auto it{table_.find(key)};
106 if (it != table_.end()) {
109 throw std::invalid_argument{
110 "MemTable::get(): Key '" + key.
str() +
"' not in the memtable."
127 if (!overlaps_with(start, end)) {
130 std::vector<value_type> entries;
131 for (
auto it{table_.lower_bound(start)};
132 it != table_.end() && it->first < end; ++it) {
133 entries.push_back(*it);
153 [[nodiscard]] table_type
table() const noexcept {
162 [[nodiscard]] size_type
size() const noexcept {
163 return table_.size();
172 [[nodiscard]]
bool empty() const noexcept {
173 return table_.empty();
181 [[nodiscard]] std::string
str() const noexcept {
182 std::stringstream ss;
184 for (
const auto& entry :
table()) {
185 ss << entryToString<TValue>(entry);
199 std::stringstream ss(
str);
203 std::string entry_str;
204 std::getline(ss, entry_str,
'[');
206 while (std::getline(ss, entry_str,
'[')) {
207 auto [entry_key, entry_value]
208 = entryFromString<TValue>(std::move(entry_str));
209 table.put(entry_key, entry_value);
221 [[nodiscard]]
bool in_range(
const key_type& key)
const noexcept {
222 return time_range_.
inRange(key.timestamp()) && key_range_.
inRange(key);
230 void update_ranges(
const key_type& key)
noexcept {
243 [[nodiscard]]
bool overlaps_with(
const key_type& start,
244 const key_type& end)
const noexcept {
245 return time_range_.
overlapsWith(start.timestamp(), end.timestamp())
253 TimeRange time_range_;
278template <ArithmeticNoCVRefQuals TValue>
279std::ostream& operator<<(std::ostream& os,
const MemTable<TValue>& table) {
294template <ArithmeticNoCVRefQuals TValue>
295std::istream& operator>>(std::istream& is, MemTable<TValue>& table) {
bool overlapsWith(const data_type &start, const data_type &end) const noexcept
Check if the range overlaps with the given range.
void clear() noexcept
Clear the range.
bool inRange(const data_type &data) const noexcept
Check if the data is in the range.
void updateRange(const data_type &data) noexcept
Update the range with the given data.
In-memory table for storing key-value pairs.
static void fromString(const std::string &str, MemTable &table)
Converts a string representation to a table.
bool empty() const noexcept
Checks if the table is empty.
MemTable() noexcept=default
Construct a new MemTable object.
void put(const key_type &key, const mapped_type &value)
Inserts or updates a key-value pair in the table.
void clear() noexcept
Clears the table.
size_type size() const noexcept
Returns the size of the table.
mapped_type get(const key_type &key) const
Retrieves the value associated with a key.
bool contains(const key_type &key) const noexcept
Checks if the table contains a key.
MemTable(MemTable &&) noexcept=default
Move-construct a MemTable object.
table_type table() const noexcept
Returns the table.
std::string str() const noexcept
Converts the table to a string representation.
std::vector< value_type > getRange(const key_type &start, const key_type &end) const
Retrieves a range of key-value pairs.
Represents a key in vkdb.
std::string str() const noexcept
Get the string representation of the key.