From ff000799c3254f52e0beabbe9c62d10c3fc4178e Mon Sep 17 00:00:00 2001 From: Douglas Rumbaugh Date: Mon, 15 May 2023 16:48:56 -0400 Subject: Record format generalization Currently, tombstone counting is bugged. But the rest of it appears to be working. --- include/util/Cursor.h | 26 ++++++++-------- include/util/Record.h | 86 +++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 86 insertions(+), 26 deletions(-) (limited to 'include/util') diff --git a/include/util/Cursor.h b/include/util/Cursor.h index 2339800..2609ae5 100644 --- a/include/util/Cursor.h +++ b/include/util/Cursor.h @@ -14,10 +14,10 @@ #include "io/PagedFile.h" namespace de { -template +template struct Cursor { - Record *ptr; - const Record *end; + R *ptr; + R *end; size_t cur_rec_idx; size_t rec_cnt; @@ -36,8 +36,8 @@ struct Cursor { * be updated to be equal to end, and false will be returned. Iterators will * not be closed. */ -template -inline static bool advance_cursor(Cursor &cur, PagedFileIterator *iter = nullptr) { +template +inline static bool advance_cursor(Cursor &cur, PagedFileIterator *iter = nullptr) { cur.ptr++; cur.cur_rec_idx++; @@ -45,8 +45,8 @@ inline static bool advance_cursor(Cursor &cur, PagedFileIterator *iter = if (cur.ptr >= cur.end) { if (iter && iter->next()) { - cur.ptr = (Record*)iter->get_item(); - cur.end = cur.ptr + (PAGE_SIZE / sizeof(Record)); + cur.ptr = (R*)iter->get_item(); + cur.end = cur.ptr + (PAGE_SIZE / sizeof(R)); return true; } @@ -62,14 +62,14 @@ inline static bool advance_cursor(Cursor &cur, PagedFileIterator *iter = * This allows for "peaking" at the next largest element after the current * largest is processed. */ -template -inline static Cursor *get_next(std::vector> &cursors, Cursor *current=nullptr) { - const Record *min_rec = nullptr; - Cursor *result = nullptr; +template +inline static Cursor *get_next(std::vector> &cursors, Cursor *current=nullptr) { + const R *min_rec = nullptr; + Cursor *result = nullptr; for (size_t i=0; i< cursors.size(); i++) { - if (cursors[i] == (Cursor) {0} ) continue; + if (cursors[i] == (Cursor) {0} ) continue; - const Record *rec = (&cursors[i] == current) ? cursors[i].ptr + 1 : cursors[i].ptr; + const R *rec = (&cursors[i] == current) ? cursors[i].ptr + 1 : cursors[i].ptr; if (rec >= cursors[i].end) continue; if (min_rec == nullptr) { diff --git a/include/util/Record.h b/include/util/Record.h index 687e745..ce101f4 100644 --- a/include/util/Record.h +++ b/include/util/Record.h @@ -10,50 +10,110 @@ #pragma once #include +#include #include "util/base.h" namespace de { -template +template +concept RecordInterface = requires(R r, R s) { + r.key; + r.value; + r.header; + + {r.is_tombstone()} -> std::convertible_to; + {r.is_deleted()} -> std::convertible_to; + r.set_delete(); + r.set_tombstone(std::declval); + { r < s } ->std::convertible_to; + { r == s } ->std::convertible_to; + { r.header < s.header } -> std::convertible_to; +}; + +template +concept WeightedRecordInterface = RecordInterface && requires(R r) { + {r.weight} -> std::convertible_to; +}; + +template struct Record { K key; V value; - typename std::conditional::value, W, std::false_type>::type weight; - uint32_t header; + uint32_t header = 0; + + inline void set_delete() { + header |= 2; + } + + inline bool is_deleted() const { + return header & 2; + } + + inline void set_tombstone(bool val=true) { + if (val) { + header |= val; + } else { + header &= 0; + } + } + + inline bool is_tombstone() const { + return header & 1; + } + + inline bool operator<(const Record& other) const { + return key < other.key || (key == other.key && value < other.value); + } - inline bool match(K k, V v, bool is_tombstone) const { - return (key == k) && (value == v) && ((header & 1) == is_tombstone); + inline bool operator==(const Record& other) const { + return key == other.key && value == other.value; } +}; + +template +struct WeightedRecord { + K key; + V value; + W weight = 1; + uint32_t header = 0; - inline void set_delete_status() { + inline void set_delete() { header |= 2; } - inline bool get_delete_status() const { + inline bool is_deleted() const { return header & 2; } + inline void set_tombstone(bool val=true) { + if (val) { + header |= val; + } else { + header &= 0; + } + } + inline bool is_tombstone() const { return header & 1; } - inline int match(const Record* other) const { + inline int match(const WeightedRecord* other) const { return key == other->key && value == other->value; } - inline bool operator<(const Record& other) const { + inline bool operator<(const WeightedRecord& other) const { return key < other.key || (key == other.key && value < other.value); } - inline bool lt(const K& k, const V& v) const { - return key < k || (key == k && value < v); + inline bool operator==(const WeightedRecord& other) const { + return key == other.key && value == other.value; } }; -template -static bool memtable_record_cmp(const Record& a, const Record& b) { +template +static bool memtable_record_cmp(const R& a, const R& b) { return (a.key < b.key) || (a.key == b.key && a.value < b.value) || (a.key == b.key && a.value == b.value && a.header < b.header); } -- cgit v1.2.3