summaryrefslogtreecommitdiffstats
path: root/include/shard/WIRS.h
blob: f3696a4b2900d64ce206d28ee4dc1f9f88c11b97 (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*
    {s.get_tombstone_count()} -> std::convertible_to<size_t>;
 * include/shard/WIRS.h
 *
 * Copyright (C) 2023 Dong Xie <dongx@psu.edu>
 *
 * All rights reserved. Published under the Modified BSD License.
 *
 */
#pragma once


#include <vector>
#include <cassert>
#include <queue>
#include <memory>
#include <concepts>

#include "ds/PriorityQueue.h"
#include "util/Cursor.h"
#include "ds/Alias.h"
#include "ds/BloomFilter.h"
#include "util/bf_config.h"
#include "framework/MutableBuffer.h"
#include "framework/RecordInterface.h"
#include "framework/ShardInterface.h"
#include "framework/QueryInterface.h"

namespace de {

thread_local size_t wirs_cancelations = 0;

template <WeightedRecordInterface R>
struct wirs_query_parms {
    decltype(R::key) lower_bound;
    decltype(R::key) upper_bound;
    size_t sample_size;
    gsl_rng *rng;
};

template <WeightedRecordInterface R, bool Rejection>
class WIRSQuery;

template <WeightedRecordInterface R>
struct wirs_node {
    struct wirs_node<R> *left, *right;
    decltype(R::key) low, high;
    decltype(R::weight) weight;
    Alias* alias;
};

template <WeightedRecordInterface R>
struct WIRSState {
    decltype(R::weight) tot_weight;
    std::vector<wirs_node<R>*> nodes;
    Alias* top_level_alias;

    WIRSState() {
        tot_weight = 0;
        top_level_alias = nullptr;
    }

    ~WIRSState() {
        if (top_level_alias) delete top_level_alias;
    }
};

template <WeightedRecordInterface R>
struct WIRSBufferState {
    size_t cutoff;
    Alias* alias;
    std::vector<Wrapped<R>> records;
    decltype(R::weight) max_weight;

    ~WIRSBufferState() {
        delete alias;
    }

};

template <WeightedRecordInterface R>
class WIRS {
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 WIRSQuery<R, true>;
    friend class WIRSQuery<R, false>;

    WIRS(MutableBuffer<R>* buffer)
    : m_reccnt(0), m_tombstone_cnt(0), m_total_weight(0), m_root(nullptr) {

        size_t alloc_size = (buffer->get_record_count() * sizeof(Wrapped<R>)) + (CACHELINE_SIZE - (buffer->get_record_count() * sizeof(Wrapped<R>)) % CACHELINE_SIZE);
        assert(alloc_size % CACHELINE_SIZE == 0);
        m_data = (Wrapped<R>*)std::aligned_alloc(CACHELINE_SIZE, alloc_size);

        m_bf = new BloomFilter<K>(BF_FPR, buffer->get_tombstone_count(), BF_HASH_FUNCS);

        size_t offset = 0;
        m_reccnt = 0;
        auto base = buffer->get_data();
        auto stop = base + buffer->get_record_count();

        std::sort(base, stop, std::less<Wrapped<R>>());

        while (base < stop) {
            if (!(base->is_tombstone()) && (base + 1) < stop) {
                if (base->rec == (base + 1)->rec && (base + 1)->is_tombstone()) {
                    base += 2;
                    wirs_cancelations++;
                    continue;
                }
            } else if (base->is_deleted()) {
                base += 1;
                continue;
            }

            base->header &= 1;
            m_data[m_reccnt++] = *base;
            m_total_weight+= base->rec.weight;

            if (m_bf && base->is_tombstone()) {
                m_tombstone_cnt++;
                m_bf->insert(base->rec.key);
            }
            
            base++;
        }

        if (m_reccnt > 0) {
            build_wirs_structure();
        }
    }

    WIRS(WIRS** shards, size_t len)
    : m_reccnt(0), m_tombstone_cnt(0), m_total_weight(0), m_root(nullptr) {
        std::vector<Cursor<Wrapped<R>>> cursors;
        cursors.reserve(len);

        PriorityQueue<Wrapped<R>> pq(len);

        size_t attemp_reccnt = 0;
        size_t tombstone_count = 0;
        
        for (size_t i = 0; i < len; ++i) {
            if (shards[i]) {
                auto base = shards[i]->get_data();
                cursors.emplace_back(Cursor{base, base + shards[i]->get_record_count(), 0, shards[i]->get_record_count()});
                attemp_reccnt += shards[i]->get_record_count();
                tombstone_count += shards[i]->get_tombstone_count();
                pq.push(cursors[i].ptr, i);
            } else {
                cursors.emplace_back(Cursor<Wrapped<R>>{nullptr, nullptr, 0, 0});
            }
        }

        m_bf = new BloomFilter<K>(BF_FPR, tombstone_count, BF_HASH_FUNCS);

        size_t alloc_size = (attemp_reccnt * sizeof(Wrapped<R>)) + (CACHELINE_SIZE - (attemp_reccnt * sizeof(Wrapped<R>)) % CACHELINE_SIZE);
        assert(alloc_size % CACHELINE_SIZE == 0);
        m_data = (Wrapped<R>*)std::aligned_alloc(CACHELINE_SIZE, alloc_size);
        
        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[now.version];
                auto& cursor2 = cursors[next.version];
                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[now.version];
                if (!cursor.ptr->is_deleted()) {
                    m_data[m_reccnt++] = *cursor.ptr;
                    m_total_weight += cursor.ptr->rec.weight;
                    if (m_bf && cursor.ptr->is_tombstone()) {
                        ++m_tombstone_cnt;
                        if (m_bf) m_bf->insert(cursor.ptr->rec.key);
                    }
                }
                pq.pop();
                
                if (advance_cursor<Wrapped<R>>(cursor)) pq.push(cursor.ptr, now.version);
            }
        }

        if (m_reccnt > 0) {
            build_wirs_structure();
        }
   }

    ~WIRS() {
        if (m_data) free(m_data);
        for (size_t i=0; i<m_alias.size(); i++) {
            if (m_alias[i]) delete m_alias[i];
        }

        if (m_bf) delete m_bf;

        free_tree(m_root);
    }

    Wrapped<R> *point_lookup(const R &rec, bool filter=false) {
        if (filter && !m_bf->lookup(rec.key)) {
            return nullptr;
        }

        size_t idx = get_lower_bound(rec.key);
        if (idx >= m_reccnt) {
            return nullptr;
        }

        while (idx < m_reccnt && m_data[idx].rec < rec) ++idx;

        if (m_data[idx].rec == rec) {
            return m_data + idx;
        }

        return nullptr;
    }

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

    size_t get_tombstone_count() const {
        return m_tombstone_cnt;
    }

    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() {
        return 0;
    }

private:

    size_t get_lower_bound(const K& key) const {
        size_t min = 0;
        size_t max = m_reccnt - 1;

        const char * record_key;
        while (min < max) {
            size_t mid = (min + max) / 2;

            if (key > m_data[mid].rec.key) {
                min = mid + 1;
            } else {
                max = mid;
            }
        }

        return min;
    }

    bool covered_by(struct wirs_node<R>* node, const K& lower_key, const K& upper_key) {
        auto low_index = node->low * m_group_size;
        auto high_index = std::min((node->high + 1) * m_group_size - 1, m_reccnt - 1);
        return lower_key < m_data[low_index].rec.key && m_data[high_index].rec.key < upper_key;
    }

    bool intersects(struct wirs_node<R>* node, const K& lower_key, const K& upper_key) {
        auto low_index = node->low * m_group_size;
        auto high_index = std::min((node->high + 1) * m_group_size - 1, m_reccnt - 1);
        return lower_key < m_data[high_index].rec.key && m_data[low_index].rec.key < upper_key;
    }

    void build_wirs_structure() {
        m_group_size = std::ceil(std::log(m_reccnt));
        size_t n_groups = std::ceil((double) m_reccnt / (double) m_group_size);
        
        // Fat point construction + low level alias....
        double sum_weight = 0.0;
        std::vector<W> weights;
        std::vector<double> group_norm_weight;
        size_t i = 0;
        size_t group_no = 0;
        while (i < m_reccnt) {
            double group_weight = 0.0;
            group_norm_weight.clear();
            for (size_t k = 0; k < m_group_size && i < m_reccnt; ++k, ++i) {
                auto w = m_data[i].rec.weight;
                group_norm_weight.emplace_back(w);
                group_weight += w;
                sum_weight += w;
            }

            for (auto& w: group_norm_weight)
                if (group_weight) w /= group_weight;
                else w = 1.0 / group_norm_weight.size();
            m_alias.emplace_back(new Alias(group_norm_weight));

            
            weights.emplace_back(group_weight);
        }

        assert(weights.size() == n_groups);

        m_root = construct_wirs_node(weights, 0, n_groups-1);
    }

     struct wirs_node<R>* construct_wirs_node(const std::vector<W>& weights, size_t low, size_t high) {
        if (low == high) {
            return new wirs_node<R>{nullptr, nullptr, low, high, weights[low], new Alias({1.0})};
        } else if (low > high) return nullptr;

        std::vector<double> node_weights;
        W sum = 0;
        for (size_t i = low; i < high; ++i) {
            node_weights.emplace_back(weights[i]);
            sum += weights[i];
        }

        for (auto& w: node_weights)
            if (sum) w /= sum;
            else w = 1.0 / node_weights.size();
        
        
        size_t mid = (low + high) / 2;
        return new wirs_node<R>{construct_wirs_node(weights, low, mid),
                                construct_wirs_node(weights, mid + 1, high),
                                low, high, sum, new Alias(node_weights)};
    }

    void free_tree(struct wirs_node<R>* node) {
        if (node) {
            delete node->alias;
            free_tree(node->left);
            free_tree(node->right);
            delete node;
        }
    }

    Wrapped<R>* m_data;
    std::vector<Alias *> m_alias;
    wirs_node<R>* m_root;
    W m_total_weight;
    size_t m_reccnt;
    size_t m_tombstone_cnt;
    size_t m_group_size;
    BloomFilter<K> *m_bf;
};


template <WeightedRecordInterface R, bool Rejection=true>
class WIRSQuery {
public:
    static void *get_query_state(WIRS<R> *wirs, void *parms) {
        auto res = new WIRSState<R>();
        decltype(R::key) lower_key = ((wirs_query_parms<R> *) parms)->lower_bound;
        decltype(R::key) upper_key = ((wirs_query_parms<R> *) parms)->upper_bound;

        // Simulate a stack to unfold recursion.        
        double tot_weight = 0.0;
        struct wirs_node<R>* st[64] = {0};
        st[0] = wirs->m_root;
        size_t top = 1;
        while(top > 0) {
            auto now = st[--top];
            if (wirs->covered_by(now, lower_key, upper_key) ||
                (now->left == nullptr && now->right == nullptr && wirs->intersects(now, lower_key, upper_key))) {
                res->nodes.emplace_back(now);
                tot_weight += now->weight;
            } else {
                if (now->left && wirs->intersects(now->left, lower_key, upper_key)) st[top++] = now->left;
                if (now->right && wirs->intersects(now->right, lower_key, upper_key)) st[top++] = now->right;
            }
        }
        
        std::vector<double> weights;
        for (const auto& node: res->nodes) {
            weights.emplace_back(node->weight / tot_weight);
        }
        res->tot_weight = tot_weight;
        res->top_level_alias = new Alias(weights);

        return res;
    }

    static void* get_buffer_query_state(MutableBuffer<R> *buffer, void *parms) {
        WIRSBufferState<R> *state = new WIRSBufferState<R>();
        auto parameters = (wirs_query_parms<R>*) parms;
        if constexpr (Rejection) {
            state->cutoff = buffer->get_record_count() - 1;
            state->max_weight = buffer->get_max_weight();
            return state;
        }

        std::vector<double> weights;

        state->cutoff = buffer->get_record_count() - 1;
        double tot_weight = 0.0;

        for (size_t i = 0; i <= state->cutoff; i++) {
            auto rec = buffer->get_data() + i;

            if (rec->rec.key >= parameters->lower_bound && rec->rec.key <= parameters->upper_bound && !rec->is_tombstone() && !rec->is_deleted()) {
              weights.push_back(rec->rec.weight);
              state->records.push_back(*rec);
              tot_weight += rec->rec.weight;
            }
        }

        for (size_t i = 0; i < weights.size(); i++) {
            weights[i] = weights[i] / tot_weight;
        }

        state->alias = new Alias(weights);

        return state;
    }

    static std::vector<Wrapped<R>> query(WIRS<R> *wirs, void *q_state, void *parms) { 
        auto sample_sz = ((wirs_query_parms<R> *) parms)->sample_size;
        auto lower_key = ((wirs_query_parms<R> *) parms)->lower_bound;
        auto upper_key = ((wirs_query_parms<R> *) parms)->upper_bound;
        auto rng = ((wirs_query_parms<R> *) parms)->rng;

        auto state = (WIRSState<R> *) q_state;

        std::vector<Wrapped<R>> result_set;

        if (sample_sz == 0) {
            return result_set;
        }
        // k -> sampling: three levels. 1. select a node -> select a fat point -> select a record.
        size_t cnt = 0;
        size_t attempts = 0;
        do {
            ++attempts;
            // first level....
            auto node = state->nodes[state->top_level_alias->get(rng)];
            // second level...
            auto fat_point = node->low + node->alias->get(rng);
            // third level...
            size_t rec_offset = fat_point * wirs->m_group_size + wirs->m_alias[fat_point]->get(rng);
            auto record = wirs->m_data + rec_offset;

            // bounds rejection
            if (lower_key > record->rec.key || upper_key < record->rec.key) {
                continue;
            } 

            result_set.emplace_back(*record);
            cnt++;
        } while (attempts < sample_sz);

        return result_set;
    }

    static std::vector<Wrapped<R>> buffer_query(MutableBuffer<R> *buffer, void *state, void *parms) {
        auto st = (WIRSBufferState<R> *) state;
        auto p = (wirs_query_parms<R> *) parms;

        std::vector<Wrapped<R>> result;
        result.reserve(p->sample_size);

        if constexpr (Rejection) {
            for (size_t i=0; i<p->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 && rec->rec.key >= p->lower_bound && rec->rec.key <= p->upper_bound) {
                    result.emplace_back(*rec);
                }
            }
            return result;
        }

        for (size_t i=0; i<p->sample_size; i++) {
            auto idx = st->alias->get(p->rng);
            result.emplace_back(st->records[idx]);
        }

        return result;
    }

    static std::vector<R> merge(std::vector<std::vector<R>> &results) {
        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]);
            }
        }

        return output;
    }

    static void delete_query_state(void *state) {
        auto s = (WIRSState<R> *) state;
        delete s;
    }

    static void delete_buffer_query_state(void *state) {
        auto s = (WIRSBufferState<R> *) state;
        delete s;
    }


    //{q.get_buffer_query_state(p, p)};
    //{q.buffer_query(p, p)};

};

}