summaryrefslogtreecommitdiffstats
path: root/include/framework/structure/MutableBuffer.h
blob: 7bec2193b50a74fd554b2d6fc500882a54d8f0c6 (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
/*
 * include/framework/structure/MutableBuffer.h
 *
 * Copyright (C) 2023 Douglas B. Rumbaugh <drumbaugh@psu.edu>
 *                    Dong Xie <dongx@psu.edu>
 *
 * Distributed under the Modified BSD License.
 *
 */
#pragma once

#include <cstdlib>
#include <atomic>
#include <condition_variable>
#include <cassert>
#include <numeric>
#include <algorithm>
#include <type_traits>

#include "psu-util/alignment.h"
#include "util/bf_config.h"
#include "psu-ds/BloomFilter.h"
#include "psu-ds/Alias.h"
#include "psu-util/timer.h"
#include "framework/interface/Record.h"
#include "framework/structure/BufferView.h"

using psudb::CACHELINE_SIZE;

namespace de {

template <RecordInterface R>
class MutableBuffer {
    friend class BufferView<R>;
public:
    MutableBuffer(size_t low_watermark, size_t high_watermark, size_t capacity=0) 
    : m_lwm(low_watermark), m_hwm(high_watermark), m_cap(capacity), m_head(0), m_tail(0) {
        /* 
         * default capacity is twice the high water mark, to account for the worst-case
         * memory requirements.
         */
        if (m_cap == 0) {
            m_cap = m_hwm * 2;
        }

        m_data = (Wrapped<R> *) psudb::sf_aligned_alloc(CACHELINE_SIZE, m_cap * sizeof(Wrapped<R>));
        
        // FIXME: need to figure out how to detail with tombstones at some point...
        m_tombstone_filter = new psudb::BloomFilter<R>(BF_FPR, m_hwm, BF_HASH_FUNCS);
    }

    ~MutableBuffer() {
        assert(m_refcnt.load() == 0);

        if (m_data) free(m_data);
        if (m_tombstone_filter) delete m_tombstone_filter;
    }

    template <typename R_ = R>
    int append(const R &rec, bool tombstone=false) {
        int32_t pos = 0;
        if ((pos = try_advance_tail()) == -1) return 0;

        Wrapped<R> wrec;
        wrec.rec = rec;
        wrec.header = 0;
        if (tombstone) wrec.set_tombstone();

        m_data[pos] = wrec;
        m_data[pos].header |= (pos << 2);

        if (tombstone) {
            m_tombstonecnt.fetch_add(1);
            if (m_tombstone_filter) m_tombstone_filter->insert(rec);
        }

        return 1;     
    }

    bool truncate() {
        m_tombstonecnt.store(0);
        m_tail.store(0);
        if (m_tombstone_filter) m_tombstone_filter->clear();

        return true;
    }

    size_t get_record_count() {
        return (m_tail - m_head) % m_cap;
    }
    
    size_t get_capacity() {
        return m_cap;
    }

    bool is_full() {
        return (m_tail % m_cap) >= m_hwm;
    }

    size_t get_tombstone_count() {
        return m_tombstonecnt.load();
    }

    bool delete_record(const R& rec) {
        for (size_t i=0; i<get_record_count(); i++) {
            if (m_data[to_idx(i)].rec == rec) {
                m_data[to_idx(i)].set_delete();
                return true;
            }
        }

        return false;
    }

    bool check_tombstone(const R& rec) {
        if (m_tombstone_filter && !m_tombstone_filter->lookup(rec)) return false;

        for (size_t i=0; i<get_record_count(); i++) {
            if (m_data[to_idx(i)].rec == rec && m_data[to_idx(i)].is_tombstone()) {
                return true;
            }
        }

        return false;
    }

    size_t get_memory_usage() {
        return m_cap * sizeof(R);
    }

    size_t get_aux_memory_usage() {
        return m_tombstone_filter->get_memory_usage();
    }

    size_t get_tombstone_capacity() {
        // FIXME: tombstone capacity needs figured out again
        return m_cap;
    }

    /*
     * Concurrency-related operations
     */
    bool take_reference() {
        m_refcnt.fetch_add(1);
        return true;
    }

    bool release_reference() {
        assert(m_refcnt > 0);
        m_refcnt.fetch_add(-1);
        return true;
    }

    size_t get_reference_count() {
        return m_refcnt.load();
    }

private:
    int64_t try_advance_tail() {
        int64_t new_tail = m_tail.fetch_add(1) % m_cap;

        if (new_tail < m_hwm) {
            return new_tail;
        }

        m_tail.fetch_add(-1);
        return -1;
    }

    size_t to_idx(size_t i) {
        return (m_head + i) % m_cap;
    }

    size_t m_cap;

    size_t m_lwm;
    size_t m_hwm;
    
    alignas(64) std::atomic<size_t> m_tail;
    alignas(64) std::atomic<size_t> m_head;
    
    Wrapped<R>* m_data;

    psudb::BloomFilter<R>* m_tombstone_filter;

    alignas(64) std::atomic<size_t> m_tombstonecnt;
    alignas(64) std::atomic<size_t> m_refcnt;
};

}