diff options
| author | Douglas Rumbaugh <dbr4@psu.edu> | 2023-11-06 15:18:53 -0500 |
|---|---|---|
| committer | Douglas Rumbaugh <dbr4@psu.edu> | 2023-11-06 15:18:53 -0500 |
| commit | e02742b07540dd5a9bcbb44dae14856bf10955ed (patch) | |
| tree | 839533401683d6ae875adbf7af2ee4c0d0f5e483 /include/shard | |
| parent | 0b723322a611de83872dd83b55d2e10e8886a283 (diff) | |
| download | dynamic-extension-e02742b07540dd5a9bcbb44dae14856bf10955ed.tar.gz | |
Refactoring progress
Diffstat (limited to 'include/shard')
| -rw-r--r-- | include/shard/Alias.h (renamed from include/shard/WSS.h) | 218 | ||||
| -rw-r--r-- | include/shard/ISAMTree.h (renamed from include/shard/MemISAM.h) | 44 | ||||
| -rw-r--r-- | include/shard/PGM.h | 267 | ||||
| -rw-r--r-- | include/shard/TrieSpline.h | 184 |
4 files changed, 31 insertions, 682 deletions
diff --git a/include/shard/WSS.h b/include/shard/Alias.h index 4e3a326..b6b16c5 100644 --- a/include/shard/WSS.h +++ b/include/shard/Alias.h @@ -1,5 +1,5 @@ /* - * include/shard/WSS.h + * include/shard/Alias.h * * Copyright (C) 2023 Douglas B. Rumbaugh <drumbaugh@psu.edu> * Dong Xie <dongx@psu.edu> @@ -9,7 +9,6 @@ */ #pragma once - #include <vector> #include <cassert> #include <queue> @@ -20,7 +19,7 @@ #include "psu-ds/PriorityQueue.h" #include "util/Cursor.h" -#include "psu-ds/Alias.h" +#include "psu-ds/psudb::Alias.h" #include "psu-ds/BloomFilter.h" #include "util/bf_config.h" @@ -28,59 +27,20 @@ using psudb::CACHELINE_SIZE; using psudb::BloomFilter; using psudb::PriorityQueue; using psudb::queue_record; -using psudb::Alias; namespace de { thread_local size_t wss_cancelations = 0; template <WeightedRecordInterface R> -struct wss_query_parms { - size_t sample_size; - gsl_rng *rng; -}; - -template <WeightedRecordInterface R, bool Rejection> -class WSSQuery; - -template <WeightedRecordInterface R> -struct WSSState { - decltype(R::weight) total_weight; - size_t sample_size; - - WSSState() { - total_weight = 0; - } -}; - -template <WeightedRecordInterface R> -struct WSSBufferState { - size_t cutoff; - size_t sample_size; - Alias* alias; - decltype(R::weight) max_weight; - decltype(R::weight) total_weight; - - ~WSSBufferState() { - delete alias; - } - -}; - -template <WeightedRecordInterface R> -class WSS { +class Alias { private: typedef decltype(R::key) K; typedef decltype(R::value) V; typedef decltype(R::weight) W; public: - - // FIXME: there has to be a better way to do this - friend class WSSQuery<R, true>; - friend class WSSQuery<R, false>; - - WSS(MutableBuffer<R>* buffer) + Alias(MutableBuffer<R>* buffer) : m_reccnt(0), m_tombstone_cnt(0), m_total_weight(0), m_alias(nullptr), m_bf(nullptr) { m_alloc_size = (buffer->get_record_count() * sizeof(Wrapped<R>)) + (CACHELINE_SIZE - (buffer->get_record_count() * sizeof(Wrapped<R>)) % CACHELINE_SIZE); @@ -132,7 +92,7 @@ public: } } - WSS(WSS** shards, size_t len) + Alias(Alias** shards, size_t len) : m_reccnt(0), m_tombstone_cnt(0), m_total_weight(0), m_alias(nullptr), m_bf(nullptr) { std::vector<Cursor<Wrapped<R>>> cursors; cursors.reserve(len); @@ -195,7 +155,7 @@ public: } } - ~WSS() { + ~Alias() { if (m_data) free(m_data); if (m_alias) delete m_alias; if (m_bf) delete m_bf; @@ -277,11 +237,11 @@ private: } // build the alias structure - m_alias = new Alias(norm_weights); + m_alias = new psudb::Alias(norm_weights); } Wrapped<R>* m_data; - Alias *m_alias; + psudb::Alias *m_alias; W m_total_weight; size_t m_reccnt; size_t m_tombstone_cnt; @@ -289,165 +249,3 @@ private: size_t m_alloc_size; BloomFilter<R> *m_bf; }; - - -template <WeightedRecordInterface R, bool Rejection=true> -class WSSQuery { -public: - - constexpr static bool EARLY_ABORT=false; - constexpr static bool SKIP_DELETE_FILTER=false; - - static void *get_query_state(WSS<R> *wss, void *parms) { - auto res = new WSSState<R>(); - res->total_weight = wss->m_total_weight; - res->sample_size = 0; - - return res; - } - - static void* get_buffer_query_state(MutableBuffer<R> *buffer, void *parms) { - WSSBufferState<R> *state = new WSSBufferState<R>(); - auto parameters = (wss_query_parms<R>*) parms; - if constexpr (Rejection) { - state->cutoff = buffer->get_record_count() - 1; - state->max_weight = buffer->get_max_weight(); - state->total_weight = buffer->get_total_weight(); - return state; - } - - std::vector<double> weights; - - state->cutoff = buffer->get_record_count() - 1; - double total_weight = 0.0; - - for (size_t i = 0; i <= state->cutoff; i++) { - auto rec = buffer->get_data() + i; - weights.push_back(rec->rec.weight); - total_weight += rec->rec.weight; - } - - for (size_t i = 0; i < weights.size(); i++) { - weights[i] = weights[i] / total_weight; - } - - state->alias = new Alias(weights); - state->total_weight = total_weight; - - return state; - } - - static void process_query_states(void *query_parms, std::vector<void*> &shard_states, void *buff_state) { - auto p = (wss_query_parms<R> *) query_parms; - auto bs = (WSSBufferState<R> *) buff_state; - - std::vector<size_t> shard_sample_sizes(shard_states.size()+1, 0); - size_t buffer_sz = 0; - - std::vector<decltype(R::weight)> weights; - weights.push_back(bs->total_weight); - - decltype(R::weight) total_weight = 0; - for (auto &s : shard_states) { - auto state = (WSSState<R> *) s; - total_weight += state->total_weight; - weights.push_back(state->total_weight); - } - - std::vector<double> normalized_weights; - for (auto w : weights) { - normalized_weights.push_back((double) w / (double) total_weight); - } - - auto shard_alias = Alias(normalized_weights); - for (size_t i=0; i<p->sample_size; i++) { - auto idx = shard_alias.get(p->rng); - if (idx == 0) { - buffer_sz++; - } else { - shard_sample_sizes[idx - 1]++; - } - } - - - bs->sample_size = buffer_sz; - for (size_t i=0; i<shard_states.size(); i++) { - auto state = (WSSState<R> *) shard_states[i]; - state->sample_size = shard_sample_sizes[i+1]; - } - } - - static std::vector<Wrapped<R>> query(WSS<R> *wss, void *q_state, void *parms) { - auto rng = ((wss_query_parms<R> *) parms)->rng; - - auto state = (WSSState<R> *) q_state; - auto sample_size = state->sample_size; - - std::vector<Wrapped<R>> result_set; - - if (sample_size == 0) { - return result_set; - } - size_t attempts = 0; - do { - attempts++; - size_t idx = wss->m_alias->get(rng); - result_set.emplace_back(*wss->get_record_at(idx)); - } while (attempts < sample_size); - - return result_set; - } - - static std::vector<Wrapped<R>> buffer_query(MutableBuffer<R> *buffer, void *state, void *parms) { - auto st = (WSSBufferState<R> *) state; - auto p = (wss_query_parms<R> *) parms; - - std::vector<Wrapped<R>> result; - result.reserve(st->sample_size); - - if constexpr (Rejection) { - for (size_t i=0; i<st->sample_size; i++) { - auto idx = gsl_rng_uniform_int(p->rng, st->cutoff); - auto rec = buffer->get_data() + idx; - - auto test = gsl_rng_uniform(p->rng) * st->max_weight; - - if (test <= rec->rec.weight) { - result.emplace_back(*rec); - } - } - return result; - } - - for (size_t i=0; i<st->sample_size; i++) { - auto idx = st->alias->get(p->rng); - result.emplace_back(*(buffer->get_data() + idx)); - } - - return result; - } - - static std::vector<R> merge(std::vector<std::vector<Wrapped<R>>> &results, void *parms) { - std::vector<R> output; - - for (size_t i=0; i<results.size(); i++) { - for (size_t j=0; j<results[i].size(); j++) { - output.emplace_back(results[i][j].rec); - } - } - - return output; - } - - static void delete_query_state(void *state) { - auto s = (WSSState<R> *) state; - delete s; - } - - static void delete_buffer_query_state(void *state) { - auto s = (WSSBufferState<R> *) state; - delete s; - } -}; - -} diff --git a/include/shard/MemISAM.h b/include/shard/ISAMTree.h index 6962c19..a610c09 100644 --- a/include/shard/MemISAM.h +++ b/include/shard/ISAMTree.h @@ -1,5 +1,5 @@ /* - * include/shard/MemISAM.h + * include/shard/ISAMTree.h * * Copyright (C) 2023 Douglas B. Rumbaugh <drumbaugh@psu.edu> * Dong Xie <dongx@psu.edu> @@ -32,11 +32,8 @@ namespace de { thread_local size_t mrun_cancelations = 0; template <RecordInterface R> -class MemISAM { +class ISAMTree { private: - friend class IRSQuery<R, true>; - friend class IRSQuery<R, false>; - friend class ISAMRangeQuery<R>; typedef decltype(R::key) K; typedef decltype(R::value) V; @@ -44,7 +41,7 @@ typedef decltype(R::value) V; constexpr static size_t inmem_isam_node_size = 256; constexpr static size_t inmem_isam_fanout = inmem_isam_node_size / (sizeof(K) + sizeof(char*)); -struct InMemISAMNode { +struct InternalNode { K keys[inmem_isam_fanout]; char* child[inmem_isam_fanout]; }; @@ -52,10 +49,10 @@ struct InMemISAMNode { constexpr static size_t inmem_isam_leaf_fanout = inmem_isam_node_size / sizeof(R); constexpr static size_t inmem_isam_node_keyskip = sizeof(K) * inmem_isam_fanout; -static_assert(sizeof(InMemISAMNode) == inmem_isam_node_size, "node size does not match"); +static_assert(sizeof(InternalNode) == inmem_isam_node_size, "node size does not match"); public: - MemISAM(MutableBuffer<R>* buffer) + ISAMTree(MutableBuffer<R>* buffer) :m_reccnt(0), m_tombstone_cnt(0), m_isam_nodes(nullptr), m_deleted_cnt(0) { m_bf = new BloomFilter<R>(BF_FPR, buffer->get_tombstone_count(), BF_HASH_FUNCS); @@ -112,7 +109,7 @@ public: auto level_time = TIMER_RESULT(); } - MemISAM(MemISAM** runs, size_t len) + ISAMTree(ISAMTree** runs, size_t len) : m_reccnt(0), m_tombstone_cnt(0), m_deleted_cnt(0), m_isam_nodes(nullptr) { std::vector<Cursor<Wrapped<R>>> cursors; cursors.reserve(len); @@ -173,7 +170,7 @@ public: } } - ~MemISAM() { + ~ISAMTree() { if (m_data) free(m_data); if (m_isam_nodes) free(m_isam_nodes); if (m_bf) delete m_bf; @@ -222,19 +219,18 @@ public: return 0; } -private: size_t get_lower_bound(const K& key) const { - const InMemISAMNode* now = m_root; + const InternalNode* now = m_root; while (!is_leaf(reinterpret_cast<const char*>(now))) { - const InMemISAMNode* next = nullptr; + const InternalNode* next = nullptr; for (size_t i = 0; i < inmem_isam_fanout - 1; ++i) { if (now->child[i + 1] == nullptr || key <= now->keys[i]) { - next = reinterpret_cast<InMemISAMNode*>(now->child[i]); + next = reinterpret_cast<InternalNode*>(now->child[i]); break; } } - now = next ? next : reinterpret_cast<const InMemISAMNode*>(now->child[inmem_isam_fanout - 1]); + now = next ? next : reinterpret_cast<const InternalNode*>(now->child[inmem_isam_fanout - 1]); } const Wrapped<R>* pos = reinterpret_cast<const Wrapped<R>*>(now); @@ -244,17 +240,17 @@ private: } size_t get_upper_bound(const K& key) const { - const InMemISAMNode* now = m_root; + const InternalNode* now = m_root; while (!is_leaf(reinterpret_cast<const char*>(now))) { - const InMemISAMNode* next = nullptr; + const InternalNode* next = nullptr; for (size_t i = 0; i < inmem_isam_fanout - 1; ++i) { if (now->child[i + 1] == nullptr || key < now->keys[i]) { - next = reinterpret_cast<InMemISAMNode*>(now->child[i]); + next = reinterpret_cast<InternalNode*>(now->child[i]); break; } } - now = next ? next : reinterpret_cast<const InMemISAMNode*>(now->child[inmem_isam_fanout - 1]); + now = next ? next : reinterpret_cast<const InternalNode*>(now->child[inmem_isam_fanout - 1]); } const Wrapped<R>* pos = reinterpret_cast<const Wrapped<R>*>(now); @@ -263,6 +259,8 @@ private: return pos - m_data; } + +private: void build_internal_levels() { size_t n_leaf_nodes = m_reccnt / inmem_isam_leaf_fanout + (m_reccnt % inmem_isam_leaf_fanout != 0); size_t level_node_cnt = n_leaf_nodes; @@ -275,11 +273,11 @@ private: m_alloc_size = (node_cnt * inmem_isam_node_size) + (CACHELINE_SIZE - (node_cnt * inmem_isam_node_size) % CACHELINE_SIZE); assert(m_alloc_size % CACHELINE_SIZE == 0); - m_isam_nodes = (InMemISAMNode*)std::aligned_alloc(CACHELINE_SIZE, m_alloc_size); + m_isam_nodes = (InternalNode*)std::aligned_alloc(CACHELINE_SIZE, m_alloc_size); m_internal_node_cnt = node_cnt; memset(m_isam_nodes, 0, node_cnt * inmem_isam_node_size); - InMemISAMNode* current_node = m_isam_nodes; + InternalNode* current_node = m_isam_nodes; const Wrapped<R>* leaf_base = m_data; const Wrapped<R>* leaf_stop = m_data + m_reccnt; @@ -330,8 +328,8 @@ private: // Members: sorted data, internal ISAM levels, reccnt; Wrapped<R>* m_data; psudb::BloomFilter<R> *m_bf; - InMemISAMNode* m_isam_nodes; - InMemISAMNode* m_root; + InternalNode* m_isam_nodes; + InternalNode* m_root; size_t m_reccnt; size_t m_tombstone_cnt; size_t m_internal_node_cnt; diff --git a/include/shard/PGM.h b/include/shard/PGM.h index 6d76376..6b66b7d 100644 --- a/include/shard/PGM.h +++ b/include/shard/PGM.h @@ -31,34 +31,6 @@ using psudb::Alias; namespace de { -template <RecordInterface R> -struct pgm_range_query_parms { - decltype(R::key) lower_bound; - decltype(R::key) upper_bound; -}; - -template <RecordInterface R> -struct PGMPointLookupParms { - decltype(R::key) target_key; -}; - -template <RecordInterface R> -class PGMRangeQuery; - -template <RecordInterface R> -class PGMPointLookup; - -template <RecordInterface R> -struct PGMState { - size_t start_idx; - size_t stop_idx; -}; - -template <RecordInterface R> -struct PGMBufferState { - size_t cutoff; -}; - template <RecordInterface R, size_t epsilon=128> class PGM { private: @@ -67,11 +39,6 @@ private: public: - - // FIXME: there has to be a better way to do this - friend class PGMRangeQuery<R>; - friend class PGMPointLookup<R>; - PGM(MutableBuffer<R>* buffer) : m_reccnt(0), m_tombstone_cnt(0) { @@ -80,8 +47,6 @@ public: m_data = (Wrapped<R>*)std::aligned_alloc(CACHELINE_SIZE, m_alloc_size); std::vector<K> keys; - //m_bf = new BloomFilter<R>(BF_FPR, buffer->get_tombstone_count(), BF_HASH_FUNCS); - size_t offset = 0; m_reccnt = 0; auto base = buffer->get_data(); @@ -110,13 +75,6 @@ public: base->header &= 3; m_data[m_reccnt++] = *base; keys.emplace_back(base->rec.key); - - /* - if (m_bf && base->is_tombstone()) { - m_tombstone_cnt++; - m_bf->insert(base->rec); - }*/ - base++; } @@ -148,8 +106,6 @@ public: } } - //m_bf = new BloomFilter<R>(BF_FPR, tombstone_count, BF_HASH_FUNCS); - m_alloc_size = (attemp_reccnt * sizeof(Wrapped<R>)) + (CACHELINE_SIZE - (attemp_reccnt * sizeof(Wrapped<R>)) % CACHELINE_SIZE); assert(m_alloc_size % CACHELINE_SIZE == 0); m_data = (Wrapped<R>*)std::aligned_alloc(CACHELINE_SIZE, m_alloc_size); @@ -172,10 +128,6 @@ public: if (!cursor.ptr->is_deleted()) { m_data[m_reccnt++] = *cursor.ptr; keys.emplace_back(cursor.ptr->rec.key); - /*if (m_bf && cursor.ptr->is_tombstone()) { - ++m_tombstone_cnt; - if (m_bf) m_bf->insert(cursor.ptr->rec); - }*/ } pq.pop(); @@ -190,15 +142,9 @@ public: ~PGM() { if (m_data) free(m_data); - //if (m_bf) delete m_bf; - } Wrapped<R> *point_lookup(const R &rec, bool filter=false) { - //if (filter && !m_bf->lookup(rec)) { - // return nullptr; - //} - size_t idx = get_lower_bound(rec.key); if (idx >= m_reccnt) { return nullptr; @@ -284,219 +230,6 @@ private: K m_max_key; K m_min_key; pgm::PGMIndex<K, epsilon> m_pgm; - //BloomFilter<R> *m_bf; -}; -template <RecordInterface R> -class PGMPointLookup { -public: - constexpr static bool EARLY_ABORT=false; - constexpr static bool SKIP_DELETE_FILTER=false; - - static void *get_query_state(PGM<R> *ts, void *parms) { - return nullptr; - } - - static void* get_buffer_query_state(MutableBuffer<R> *buffer, void *parms) { - return nullptr; - } - - static void process_query_states(void *query_parms, std::vector<void*> &shard_states, void *buff_state) { - return; - } - - static std::vector<Wrapped<R>> query(PGM<R> *ts, void *q_state, void *parms) { - std::vector<Wrapped<R>> records; - auto p = (PGMPointLookupParms<R> *) parms; - auto s = (PGMState<R> *) q_state; - - size_t idx = ts->get_lower_bound(p->target_key); - if (ts->get_record_at(idx)->rec.key == p->target_key) { - records.emplace_back(*ts->get_record_at(idx)); - } - - return records; - } - - static std::vector<Wrapped<R>> buffer_query(MutableBuffer<R> *buffer, void *state, void *parms) { - auto p = (PGMPointLookupParms<R> *) parms; - auto s = (PGMBufferState<R> *) state; - - std::vector<Wrapped<R>> records; - for (size_t i=0; i<buffer->get_record_count(); i++) { - auto rec = buffer->get_data() + i; - if (rec->rec.key == p->target_key) { - records.emplace_back(*rec); - return records; - } - } - - return records; - } - - static std::vector<R> merge(std::vector<std::vector<Wrapped<R>>> &results, void *parms) { - std::vector<R> output; - for (size_t i=0 ;i<results.size(); i++) { - if (results[i].size() > 0) { - output.emplace_back(results[i][0].rec); - return output; - } - } - - return output; - } - - static void delete_query_state(void *state) { - } - - static void delete_buffer_query_state(void *state) { - } }; - - -template <RecordInterface R> -class PGMRangeQuery { -public: - constexpr static bool EARLY_ABORT=false; - constexpr static bool SKIP_DELETE_FILTER=false; - - static void *get_query_state(PGM<R> *ts, void *parms) { - auto res = new PGMState<R>(); - auto p = (pgm_range_query_parms<R> *) parms; - - res->start_idx = ts->get_lower_bound(p->lower_bound); - res->stop_idx = ts->get_record_count(); - - return res; - } - - static void* get_buffer_query_state(MutableBuffer<R> *buffer, void *parms) { - auto res = new PGMBufferState<R>(); - res->cutoff = buffer->get_record_count(); - - return res; - } - - static void process_query_states(void *query_parms, std::vector<void*> &shard_states, void *buff_state) { - return; - } - - static std::vector<Wrapped<R>> query(PGM<R> *ts, void *q_state, void *parms) { - size_t tot = 0; - //std::vector<Wrapped<R>> records; - auto p = (pgm_range_query_parms<R> *) parms; - auto s = (PGMState<R> *) q_state; - - // if the returned index is one past the end of the - // records for the PGM, then there are not records - // in the index falling into the specified range. - if (s->start_idx == ts->get_record_count()) { - return {}; - } - - auto ptr = ts->get_record_at(s->start_idx); - - // roll the pointer forward to the first record that is - // greater than or equal to the lower bound. - while(ptr->rec.key < p->lower_bound) { - ptr++; - } - - while (ptr->rec.key <= p->upper_bound && ptr < ts->m_data + s->stop_idx) { - if (ptr->is_tombstone()) --tot; - else if (!ptr->is_deleted()) ++tot; - //records.emplace_back(*ptr); - ptr++; - } - - return {Wrapped<R>{0, {tot, 0}}}; - //return records; - } - - static std::vector<Wrapped<R>> buffer_query(MutableBuffer<R> *buffer, void *state, void *parms) { - size_t tot = 0; - auto p = (pgm_range_query_parms<R> *) parms; - auto s = (PGMBufferState<R> *) state; - - //std::vector<Wrapped<R>> records; - for (size_t i=0; i<s->cutoff; i++) { - auto rec = buffer->get_data() + i; - if (rec->rec.key >= p->lower_bound && rec->rec.key <= p->upper_bound) { - if (rec->is_tombstone()) --tot; - else if (!rec->is_deleted()) ++tot; - //records.emplace_back(*rec); - } - } - - return {Wrapped<R>{0, {tot, 0}}}; - //return records; - } - - static std::vector<R> merge(std::vector<std::vector<Wrapped<R>>> &results, void *parms) { - /*std::vector<Cursor<Wrapped<R>>> cursors; - cursors.reserve(results.size()); - - PriorityQueue<Wrapped<R>> pq(results.size()); - size_t total = 0; - size_t tmp_n = results.size(); - - - for (size_t i = 0; i < tmp_n; ++i) - if (results[i].size() > 0){ - auto base = results[i].data(); - cursors.emplace_back(Cursor{base, base + results[i].size(), 0, results[i].size()}); - assert(i == cursors.size() - 1); - total += results[i].size(); - pq.push(cursors[i].ptr, tmp_n - i - 1); - } else { - cursors.emplace_back(Cursor<Wrapped<R>>{nullptr, nullptr, 0, 0}); - } - - if (total == 0) { - return std::vector<R>(); - } - - std::vector<R> output; - output.reserve(total); - - while (pq.size()) { - auto now = pq.peek(); - auto next = pq.size() > 1 ? pq.peek(1) : queue_record<Wrapped<R>>{nullptr, 0}; - if (!now.data->is_tombstone() && next.data != nullptr && - now.data->rec == next.data->rec && next.data->is_tombstone()) { - - pq.pop(); pq.pop(); - auto& cursor1 = cursors[tmp_n - now.version - 1]; - auto& cursor2 = cursors[tmp_n - next.version - 1]; - if (advance_cursor<Wrapped<R>>(cursor1)) pq.push(cursor1.ptr, now.version); - if (advance_cursor<Wrapped<R>>(cursor2)) pq.push(cursor2.ptr, next.version); - } else { - auto& cursor = cursors[tmp_n - now.version - 1]; - if (!now.data->is_tombstone()) output.push_back(cursor.ptr->rec); - pq.pop(); - - if (advance_cursor<Wrapped<R>>(cursor)) pq.push(cursor.ptr, now.version); - } - }*/ - - size_t tot = 0; - for (auto& result: results) - if (result.size() > 0) tot += result[0].rec.key; - - return {{tot, 0}}; - } - - static void delete_query_state(void *state) { - auto s = (PGMState<R> *) state; - delete s; - } - - static void delete_buffer_query_state(void *state) { - auto s = (PGMBufferState<R> *) state; - delete s; - } -}; - -; - } diff --git a/include/shard/TrieSpline.h b/include/shard/TrieSpline.h index a784a38..fdf8edb 100644 --- a/include/shard/TrieSpline.h +++ b/include/shard/TrieSpline.h @@ -30,32 +30,6 @@ using psudb::Alias; namespace de { -template <RecordInterface R> -struct ts_range_query_parms { - decltype(R::key) lower_bound; - decltype(R::key) upper_bound; -}; - -template <RecordInterface R> -class TrieSplineRangeQuery; - -template <RecordInterface R> -struct TrieSplineState { - size_t start_idx; - size_t stop_idx; -}; - -template <RecordInterface R> -struct TrieSplineBufferState { - size_t cutoff; - Alias* alias; - - ~TrieSplineBufferState() { - delete alias; - } - -}; - template <RecordInterface R, size_t E=1024> class TrieSpline { private: @@ -63,10 +37,6 @@ private: typedef decltype(R::value) V; public: - - // FIXME: there has to be a better way to do this - friend class TrieSplineRangeQuery<R>; - TrieSpline(MutableBuffer<R>* buffer) : m_reccnt(0), m_tombstone_cnt(0) { @@ -254,8 +224,6 @@ public: return 0; } -private: - size_t get_lower_bound(const K& key) const { auto bound = m_ts.GetSearchBound(key); size_t idx = bound.begin; @@ -293,6 +261,8 @@ private: return (m_data[idx].rec.key <= key) ? idx : m_reccnt; } +private: + Wrapped<R>* m_data; size_t m_reccnt; size_t m_tombstone_cnt; @@ -302,154 +272,4 @@ private: ts::TrieSpline<K> m_ts; BloomFilter<R> *m_bf; }; - - -template <RecordInterface R> -class TrieSplineRangeQuery { -public: - constexpr static bool EARLY_ABORT=false; - constexpr static bool SKIP_DELETE_FILTER=true; - - static void *get_query_state(TrieSpline<R> *ts, void *parms) { - auto res = new TrieSplineState<R>(); - auto p = (ts_range_query_parms<R> *) parms; - - res->start_idx = ts->get_lower_bound(p->lower_bound); - res->stop_idx = ts->get_record_count(); - - return res; - } - - static void* get_buffer_query_state(MutableBuffer<R> *buffer, void *parms) { - auto res = new TrieSplineBufferState<R>(); - res->cutoff = buffer->get_record_count(); - - return res; - } - - static void process_query_states(void *query_parms, std::vector<void*> &shard_states, void *buff_state) { - return; - } - - static std::vector<Wrapped<R>> query(TrieSpline<R> *ts, void *q_state, void *parms) { - //std::vector<Wrapped<R>> records; - size_t tot = 0; - auto p = (ts_range_query_parms<R> *) parms; - auto s = (TrieSplineState<R> *) q_state; - - // if the returned index is one past the end of the - // records for the TrieSpline, then there are not records - // in the index falling into the specified range. - if (s->start_idx == ts->get_record_count()) { - return {}; - } - - auto ptr = ts->get_record_at(s->start_idx); - - // roll the pointer forward to the first record that is - // greater than or equal to the lower bound. - while(ptr->rec.key < p->lower_bound) { - ptr++; - } - - - while (ptr->rec.key <= p->upper_bound && ptr < ts->m_data + s->stop_idx) { - if (ptr->is_tombstone()) --tot; - else if (!ptr->is_deleted()) ++tot; - //records.emplace_back(*ptr); - ptr++; - } - - return {Wrapped<R>{0, {tot, 0}}}; - //return records; - } - - static std::vector<Wrapped<R>> buffer_query(MutableBuffer<R> *buffer, void *state, void *parms) { - size_t tot = 0; - auto p = (ts_range_query_parms<R> *) parms; - auto s = (TrieSplineBufferState<R> *) state; - - //std::vector<Wrapped<R>> records; - for (size_t i=0; i<s->cutoff; i++) { - auto rec = buffer->get_data() + i; - if (rec->rec.key >= p->lower_bound && rec->rec.key <= p->upper_bound) { - if (rec->is_tombstone()) --tot; - else if (!rec->is_deleted()) ++tot; - //records.emplace_back(*rec); - } - - } - - return {Wrapped<R>{0, {tot, 0}}}; - //return records; - } - - static std::vector<R> merge(std::vector<std::vector<Wrapped<R>>> &results, void *parms) { -/* - std::vector<Cursor<Wrapped<R>>> cursors; - cursors.reserve(results.size()); - - PriorityQueue<Wrapped<R>> pq(results.size()); - size_t total = 0; - size_t tmp_n = results.size(); - - - for (size_t i = 0; i < tmp_n; ++i) - if (results[i].size() > 0){ - auto base = results[i].data(); - cursors.emplace_back(Cursor{base, base + results[i].size(), 0, results[i].size()}); - assert(i == cursors.size() - 1); - total += results[i].size(); - pq.push(cursors[i].ptr, tmp_n - i - 1); - } else { - cursors.emplace_back(Cursor<Wrapped<R>>{nullptr, nullptr, 0, 0}); - } - - if (total == 0) { - return std::vector<R>(); - } - - std::vector<R> output; - output.reserve(total); - - while (pq.size()) { - auto now = pq.peek(); - auto next = pq.size() > 1 ? pq.peek(1) : queue_record<Wrapped<R>>{nullptr, 0}; - if (!now.data->is_tombstone() && next.data != nullptr && - now.data->rec == next.data->rec && next.data->is_tombstone()) { - - pq.pop(); pq.pop(); - auto& cursor1 = cursors[tmp_n - now.version - 1]; - auto& cursor2 = cursors[tmp_n - next.version - 1]; - if (advance_cursor<Wrapped<R>>(cursor1)) pq.push(cursor1.ptr, now.version); - if (advance_cursor<Wrapped<R>>(cursor2)) pq.push(cursor2.ptr, next.version); - } else { - auto& cursor = cursors[tmp_n - now.version - 1]; - if (!now.data->is_tombstone()) output.push_back(cursor.ptr->rec); - pq.pop(); - - if (advance_cursor<Wrapped<R>>(cursor)) pq.push(cursor.ptr, now.version); - } - } - - return output;*/ - - size_t tot = 0; - for (auto& result: results) - if (result.size() > 0) tot += result[0].rec.key; - - return {{tot, 0}}; - } - - static void delete_query_state(void *state) { - auto s = (TrieSplineState<R> *) state; - delete s; - } - - static void delete_buffer_query_state(void *state) { - auto s = (TrieSplineBufferState<R> *) state; - delete s; - } -}; - } |