-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu_auxiliary_range_index.h
184 lines (156 loc) · 5.32 KB
/
cpu_auxiliary_range_index.h
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
//
// Created by Shujian Qian on 2024-03-31.
//
#ifndef CPU_AUXILIARY_RANGE_INDEX_H
#define CPU_AUXILIARY_RANGE_INDEX_H
#include <limits>
#include <thread>
#include <unistd.h>
#include <type_traits>
#include <build/config.h>
#include <kvthread.hh>
#include <masstree_insert.hh>
#include <masstree_remove.hh>
#include <masstree_tcursor.hh>
#include <masstree_print.hh>
#include <masstree_scan.hh>
#include <kvthread.hh>
#include <timestamp.hh>
#include <masstree.hh>
#include <util_endianness.h>
#include <util_log.h>
namespace epic {
template<typename ValueType>
struct CpuAuxRangeIndexParam : public Masstree::nodeparams<>
{
typedef ValueType value_type;
typedef threadinfo threadinfo_type;
};
extern thread_local threadinfo *ti;
template<typename KeyType, typename ValueType>
class CpuAuxRangeIndex : protected Masstree::basic_table<CpuAuxRangeIndexParam<ValueType>>
{
using base_table_type = Masstree::basic_table<CpuAuxRangeIndexParam<ValueType>>;
public:
using key_type = KeyType;
using value_type = ValueType;
static_assert(std::is_integral_v<KeyType>, "KeyType must be an integral type");
static_assert(std::is_integral_v<ValueType>, "ValueType must be an integral type");
static_assert(std::is_unsigned_v<KeyType>, "KeyType must be an unsigned type");
static constexpr ValueType invalid_value = std::numeric_limits<ValueType>::max();
CpuAuxRangeIndex()
: base_table_type()
{
auto &logger = Logger::GetInstance();
logger.Info("initializing CpuAuxRangeIndex");
this->initialize(*GetThreadInfo());
}
static threadinfo *GetThreadInfo()
{
if (ti == nullptr)
{
auto &logger = Logger::GetInstance();
logger.Info("getting thread id {}", gettid());
ti = threadinfo::make(threadinfo::TI_MAIN, gettid());
}
return ti;
}
template<typename BaseIteratorType>
class iterator : public BaseIteratorType
{
KeyType curr_key;
ValueType curr_value;
KeyType end_key;
public:
iterator(typename BaseIteratorType::node_type *root, lcdf::Str firstkey, threadinfo &ti)
: BaseIteratorType(root, firstkey, ti)
, end_key(std::numeric_limits<KeyType>::max())
{}
void adapt()
{
if (!this->terminated)
{
KeyType bs_curr_key = *reinterpret_cast<const KeyType *>(this->key.full_string().data());
curr_key = endian_swap(bs_curr_key);
curr_value = this->entry.value();
}
}
void get_next()
{
this->next(*GetThreadInfo());
adapt();
}
bool is_valid() const
{
static_assert(std::is_same_v<BaseIteratorType, typename base_table_type::forward_scan_iterator_impl> ||
std::is_same_v<BaseIteratorType, typename base_table_type::reverse_scan_iterator_impl>,
"iterator type not supported");
if constexpr (std::is_same_v<BaseIteratorType, typename base_table_type::forward_scan_iterator_impl>)
{
return !this->terminated && !(end_key < curr_key);
}
else
{
return !this->terminated && !(curr_key < end_key);
}
}
void set_iterator_end_key(KeyType end)
{
end_key = end;
}
key_type getKey() const
{
return curr_key;
}
value_type getValue() const
{
return curr_value;
}
};
using forward_iterator = iterator<typename base_table_type::forward_scan_iterator_impl>;
using reverse_iterator = iterator<typename base_table_type::reverse_scan_iterator_impl>;
ValueType searchOrInsert(KeyType key, ValueType value)
{
KeyType bs_key = endian_swap(key);
typename base_table_type::cursor_type cursor(*this, reinterpret_cast<char *>(&bs_key), sizeof(KeyType));
auto &ti = *GetThreadInfo();
bool found = cursor.find_insert(ti);
if (!found)
{
cursor.value() = value;
}
ValueType retval = cursor.value();
cursor.finish(1, ti);
return retval;
}
ValueType search(KeyType key)
{
auto &ti = *GetThreadInfo();
KeyType bs_key = endian_swap(key);
ValueType result = invalid_value;
this->get(lcdf::Str(reinterpret_cast<char *>(&bs_key), sizeof(KeyType)), result, ti);
return result;
}
forward_iterator *searchForwardIterator(KeyType start, KeyType end)
{
KeyType bs_start = endian_swap(start);
auto &ti = *GetThreadInfo();
auto it = this->template find_iterator<forward_iterator>(
lcdf::Str(reinterpret_cast<char *>(&bs_start), sizeof(KeyType)), ti);
it->set_iterator_end_key(end);
it->adapt();
return it;
}
reverse_iterator *searchReverseIterator(KeyType start, KeyType end)
{
KeyType bs_start = endian_swap(start);
auto &ti = *GetThreadInfo();
auto it = this->template find_iterator<reverse_iterator>(
lcdf::Str(reinterpret_cast<char *>(&bs_start), sizeof(KeyType)), ti);
it->set_iterator_end_key(end);
it->adapt();
return it;
}
};
} // namespace epic
#endif // CPU_AUXILIARY_RANGE_INDEX_H