summaryrefslogtreecommitdiffstats
path: root/include/shard/FSTrie.h
blob: d720aadff527df696c710d391f4b2d1a5acae33f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * include/shard/FSTrie.h
 *
 * Copyright (C) 2024 Douglas B. Rumbaugh <drumbaugh@psu.edu>
 *
 * Distributed under the Modified BSD License.
 *
 * A shard shim around the FSTrie learned index.
 */
#pragma once


#include <vector>

#include "framework/ShardRequirements.h"
#include "fst.hpp"
#include "util/SortedMerge.h"

using psudb::CACHELINE_SIZE;
using psudb::BloomFilter;
using psudb::PriorityQueue;
using psudb::queue_record;
using psudb::byte;

namespace de {

template <KVPInterface R>
class FSTrie {
public:
    typedef R RECORD;
private:

    typedef decltype(R::key) K;
    typedef decltype(R::value) V;
    static_assert(std::is_same_v<K, const char*>, "FST requires const char* keys.");

public:
    FSTrie(BufferView<R> buffer)
        : m_data(nullptr)
        , m_reccnt(0)
        , m_alloc_size(0)
    {
        m_data = new Wrapped<R>[buffer.get_record_count()]();
        m_alloc_size = sizeof(Wrapped<R>) * buffer.get_record_count();

        size_t cnt = 0;
        std::vector<std::string> keys;
        keys.reserve(buffer.get_record_count());

        /*
         * Copy the contents of the buffer view into a temporary buffer, and
         * sort them. We still need to iterate over these temporary records to 
         * apply tombstone/deleted record filtering, as well as any possible
         * per-record processing that is required by the shard being built.
         */
        auto temp_buffer = new Wrapped<R>[buffer.get_record_count()]();
        for (size_t i=0; i<buffer.get_record_count(); i++) {
            temp_buffer[i] = *(buffer.get(i));
        }

        auto base = temp_buffer;
        auto stop = base + buffer.get_record_count();
        std::sort(base, stop, std::less<Wrapped<R>>());

        for (size_t i=0; i<buffer.get_record_count(); i++) {
            if (temp_buffer[i].is_deleted() || !temp_buffer[i].is_visible() || temp_buffer[i].rec.key == "") {
                continue;
            }

            m_data[cnt] = temp_buffer[i];
            m_data[cnt].clear_timestamp();

            keys.push_back(std::string(m_data[cnt].rec.key));
            cnt++;
        }

        m_reccnt = cnt;
        if (m_reccnt > 0) {
            m_fst = new fst::Trie(keys, true, 1);
        }

        delete[] temp_buffer;
    }

    FSTrie(std::vector<const FSTrie*> const &shards) 
        : m_data(nullptr)
        , m_reccnt(0)
        , m_alloc_size(0)
    {
        size_t attemp_reccnt = 0;
        size_t tombstone_count = 0;
        auto cursors = build_cursor_vec<R, FSTrie>(shards, &attemp_reccnt, &tombstone_count);
        
        m_data = new Wrapped<R>[attemp_reccnt]();
        m_alloc_size = attemp_reccnt * sizeof(Wrapped<R>);

        std::vector<std::string> keys;
        keys.reserve(attemp_reccnt);

        // FIXME: For smaller cursor arrays, it may be more efficient to skip
        //        the priority queue and just do a scan.
        PriorityQueue<Wrapped<R>> pq(cursors.size());
        for (size_t i=0; i<cursors.size(); i++) {
            pq.push(cursors[i].ptr, i);
        }

        while (pq.size()) {
            auto now = pq.peek();
            auto next = pq.size() > 1 ? pq.peek(1) : queue_record<Wrapped<R>>{nullptr, 0};
            /* 
             * if the current record is not a tombstone, and the next record is
             * a tombstone that matches the current one, then the current one
             * has been deleted, and both it and its tombstone can be skipped
             * over.
             */
            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[now.version];
                auto& cursor2 = cursors[next.version];
                if (advance_cursor(cursor1)) pq.push(cursor1.ptr, now.version);
                if (advance_cursor(cursor2)) pq.push(cursor2.ptr, next.version);
            } else {
                auto& cursor = cursors[now.version];
                /* skip over records that have been deleted via tagging */
                if (!cursor.ptr->is_deleted() && cursor.ptr->rec.key != "") {
                    m_data[m_reccnt] = *cursor.ptr;
                    keys.push_back(std::string(m_data[m_reccnt].rec.key));

                    m_reccnt++;
                }
                pq.pop();
                
                if (advance_cursor(cursor)) pq.push(cursor.ptr, now.version);
            }
        }

        if (m_reccnt > 0) {
            m_fst = new fst::Trie(keys, true, 1);
        }
    }

    ~FSTrie() {
        delete[] m_data;
        delete m_fst;
    }

    Wrapped<R> *point_lookup(const R &rec, bool filter=false) {

        auto idx = m_fst->exactSearch(rec.key);

        if (idx == fst::kNotFound) {
            return nullptr;
        }

        // FIXME: for convenience, I'm treating this Trie as a unique index
        // for now, so no need to scan forward and/or check values. This
        // also makes the point lookup query class a lot easier to make.
        // Ultimately, though, we can support non-unique indexes with some
        // extra work.

        return m_data + idx;
    }

    Wrapped<R>* get_data() const {
        return m_data;
    }
    
    size_t get_record_count() const {
        return m_reccnt;
    }

    size_t get_tombstone_count() const {
        return 0;
    }

    const Wrapped<R>* get_record_at(size_t idx) const {
        if (idx >= m_reccnt) return nullptr;
        return m_data + idx;
    }


    size_t get_memory_usage() const {
        return m_fst->getMemoryUsage();
    }

    size_t get_aux_memory_usage() const {
        return m_alloc_size;
    }

    size_t get_lower_bound(R &rec) {return 0;}
    size_t get_upper_bound(R &rec) {return 0;}

private:

    Wrapped<R>* m_data;
    size_t m_reccnt;
    size_t m_alloc_size;
    fst::Trie *m_fst;
};
}