From 9b4cd6ffb2535ac3245e17e7c76a4d9ccf25642f Mon Sep 17 00:00:00 2001 From: Douglas Rumbaugh Date: Tue, 9 May 2023 10:26:46 -0400 Subject: Templatized Record with key, value, and optional weight --- include/ds/BloomFilter.h | 4 ++-- include/ds/PriorityQueue.h | 28 ++++++++++++++-------------- include/util/Record.h | 22 +++++++++------------- 3 files changed, 25 insertions(+), 29 deletions(-) (limited to 'include') diff --git a/include/ds/BloomFilter.h b/include/ds/BloomFilter.h index dbaab2f..fddd1fb 100644 --- a/include/ds/BloomFilter.h +++ b/include/ds/BloomFilter.h @@ -14,9 +14,9 @@ #include "ds/BitArray.h" #include "util/hash.h" #include "util/base.h" -#include "util/record.h" +#include "util/Record.h" -namespace lsm { +namespace de { class BloomFilter { public: diff --git a/include/ds/PriorityQueue.h b/include/ds/PriorityQueue.h index 49f3e41..290d5c8 100644 --- a/include/ds/PriorityQueue.h +++ b/include/ds/PriorityQueue.h @@ -1,5 +1,5 @@ /* - * include/ds/PriorityQueue.h + * include/ds/BloomFilter.h * * Copyright (C) 2023 Douglas Rumbaugh * Dong Xie @@ -12,16 +12,18 @@ #include #include -#include "util/record.h" +#include "util/Record.h" -namespace lsm { +namespace de { +template struct queue_record { - const char *data; + const Record *data; size_t version; }; +template class PriorityQueue { public: PriorityQueue(size_t size) : data(size), tail(0) {} @@ -52,7 +54,7 @@ public: } } - void push(const char* record, size_t version=0) { + void push(const Record* record, size_t version=0) { assert(tail != this->data.size()); size_t new_idx = this->tail++; @@ -65,7 +67,7 @@ public: } - queue_record peek(size_t depth=0) { + queue_record peek(size_t depth=0) { ssize_t idx = 0; size_t cur_depth = 0; @@ -79,7 +81,7 @@ public: } private: - std::vector data; + std::vector> data; size_t tail; /* @@ -122,13 +124,11 @@ private: } inline bool heap_cmp(size_t a, size_t b) { - auto cmp = record_cmp(this->data[a].data, this->data[b].data); - if (cmp == 0) { - if (this->data[a].version != this->data[b].version) - return this->data[a].version < this->data[b].version; - else return is_tombstone(this->data[a].data) && is_tombstone(this->data[b].data); - } - return cmp == -1; + if (!data[a].data->match(data[b].data)) { + return *(data[a].data) < *(data[b].data); + } else if (data[a].version != data[b].version) + return data[a].version < data[b].version; + else return data[a].data->is_tombstone() && data[b].data->is_tombstone(); } }; diff --git a/include/util/Record.h b/include/util/Record.h index 7e64959..687e745 100644 --- a/include/util/Record.h +++ b/include/util/Record.h @@ -15,18 +15,14 @@ namespace de { -typedef uint32_t hdr_t; -typedef uint64_t key_t; -typedef uint32_t value_t; -typedef uint64_t weight_t; - +template struct Record { - key_t key; - value_t value; - hdr_t header; - weight_t weight; + K key; + V value; + typename std::conditional::value, W, std::false_type>::type weight; + uint32_t header; - inline bool match(key_t k, value_t v, bool is_tombstone) const { + inline bool match(K k, V v, bool is_tombstone) const { return (key == k) && (value == v) && ((header & 1) == is_tombstone); } @@ -50,14 +46,14 @@ struct Record { return key < other.key || (key == other.key && value < other.value); } - inline bool lt(const key_t& k, const value_t& v) const { + inline bool lt(const K& k, const V& v) const { return key < k || (key == k && value < v); } }; -static_assert(sizeof(Record) == 24, "Record is not 24 bytes long."); -static bool memtable_record_cmp(const Record& a, const Record& b) { +template +static bool memtable_record_cmp(const Record& a, const Record& 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