forked from TimesGraph/TimesGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitmap_index_utils.h
300 lines (257 loc) · 8.6 KB
/
bitmap_index_utils.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
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
/*******************************************************************************
*
* Copyright (c) 2019-2022 TimesGraph
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifndef TimesGraph_BITMAP_INDEX_UTILS_H
#define TimesGraph_BITMAP_INDEX_UTILS_H
#include <utility>
#include <atomic>
#include "util.h"
ATTR_UNUSED
inline static int64_t to_local_row_id(int64_t row_id)
{
return row_id & 0xFFFFFFFFFFFL;
}
ATTR_UNUSED
inline static int32_t to_partition_index(int64_t row_id)
{
return static_cast<int32_t>(row_id >> 44);
}
ATTR_UNUSED
inline static int64_t to_row_id(int32_t partition_index, int64_t local_row_id)
{
return (static_cast<int64_t>(partition_index) << 44) + local_row_id;
}
struct out_arguments
{
int64_t key_lo;
int64_t key_hi;
int64_t *rows_address;
int64_t rows_capacity;
int64_t rows_size;
int64_t hashes_address;
int64_t filtered_size;
};
struct key_header
{
int8_t signature;
int64_t sequence;
int64_t value_mem_size;
int32_t block_value_count;
int64_t key_count;
int64_t sequence_check;
ATTR_UNUSED int8_t padding[27];
} __attribute__((packed));
struct key_entry
{
int64_t value_count;
int64_t first_value_block_offset;
int64_t last_value_block_offset;
int64_t count_check;
} __attribute__((packed));
struct value_block_link
{
int64_t prev;
int64_t next;
} __attribute__((packed));
struct fl_record
{
int64_t first_row_id;
int64_t last_row_id;
int64_t timestamp_index;
private:
ATTR_UNUSED int8_t padding[8];
} __attribute__((packed));
class keys_reader
{
public:
//local copy
struct key_entry_proxy : public key_entry
{
bool is_block_consistent;
};
explicit keys_reader(const uint8_t *base_ptr, size_t memory_size)
: proxy_(),
header_ptr_(reinterpret_cast<const key_header *>(base_ptr)),
keys_ptr_(reinterpret_cast<const key_entry *>(base_ptr + sizeof(key_header))),
memory_size_(memory_size) {}
const key_entry_proxy &operator[](size_t index) const noexcept
{
auto retries_count = 10;
proxy_.is_block_consistent = false;
while (retries_count--)
{
proxy_.value_count = keys_ptr_[index].value_count;
std::atomic_thread_fence(std::memory_order_acquire);
if (keys_ptr_[index].count_check == proxy_.value_count)
{
proxy_.first_value_block_offset = keys_ptr_[index].first_value_block_offset;
proxy_.last_value_block_offset = keys_ptr_[index].last_value_block_offset;
std::atomic_thread_fence(std::memory_order_acquire);
if (keys_ptr_[index].value_count == proxy_.value_count)
{
proxy_.is_block_consistent = true;
break;
}
}
}
return proxy_;
}
[[nodiscard]] ATTR_UNUSED size_t memory_size() const noexcept { return memory_size_; };
[[nodiscard]] size_t key_count() const noexcept { return header_ptr_->key_count; }
[[nodiscard]] ATTR_UNUSED size_t values_total_count() const noexcept { return header_ptr_->block_value_count; }
[[nodiscard]] ATTR_UNUSED size_t value_memory_size() const noexcept { return header_ptr_->value_mem_size; }
[[nodiscard]] ATTR_UNUSED size_t key_count_in_memory() const noexcept
{
return (memory_size_ - sizeof(key_header)) / sizeof(int64_t);
};
private:
mutable key_entry_proxy proxy_;
const key_header *header_ptr_;
const key_entry *keys_ptr_;
size_t memory_size_;
};
template <typename T>
class block
{
public:
explicit block(const uint8_t *base_ptr, size_t offset, size_t cap) noexcept
: base_ptr_(base_ptr), offset_(offset), cap_(cap), msk_(cap - 1) {}
const T &operator[](size_t index) const noexcept { return data()[index & msk_]; }
[[nodiscard]] const T *data() const noexcept { return reinterpret_cast<const T *>(memory()); }
[[nodiscard]] const uint8_t *memory() const noexcept { return base_ptr_ + offset_; }
[[nodiscard]] size_t offset() const noexcept { return offset_; }
[[nodiscard]] size_t capacity() const noexcept { return cap_; }
[[nodiscard]] size_t memory_size() const noexcept { return cap_ * sizeof(T) + sizeof(value_block_link); }
[[nodiscard]] const uint8_t *next() const noexcept { return base_ptr_ + next_offset(); }
[[nodiscard]] const uint8_t *prev() const noexcept { return base_ptr_ + prev_offset(); }
[[nodiscard]] uint64_t next_offset() const noexcept { return link()->next; }
[[nodiscard]] uint64_t prev_offset() const noexcept { return link()->prev; }
void move_next() noexcept { offset_ = next_offset(); }
void move_prev() noexcept { offset_ = prev_offset(); }
private:
[[nodiscard]] const value_block_link *link() const
{
return reinterpret_cast<const value_block_link *>(memory() + memory_size() - sizeof(value_block_link));
}
const uint8_t *base_ptr_;
size_t offset_;
size_t cap_;
size_t msk_;
};
template <typename T>
int64_t search_in_block(const T *memory, int64_t count, T value)
{
// when block is "small", we just scan it linearly
if (count < 64)
{
// this will definitely exit because we had checked that at least the last value is greater than value
for (long i = 0; i < count; ++i)
{
if (memory[i] > value)
{
return i;
}
}
return count;
}
else
{
// use binary search on larger block
return branch_free_search_upper(memory, count, value);
}
}
template <typename T>
int64_t scan_blocks_backward(block<T> ¤t_block, int64_t value_count, T max_value)
{
int64_t stored;
do
{
// check block range by peeking at first and last value
auto lo = current_block[0]; // first value in the block
stored = (value_count - 1 & static_cast<int64_t>(current_block.capacity()) - 1) + 1;
// can we skip this block ?
if (lo > max_value)
{
value_count -= stored;
// do we have previous block?
if (value_count > 0)
{
current_block.move_prev();
continue;
}
}
break;
} while (true);
if (value_count > 0)
{
// do we need to search this block?
const auto hi = current_block[stored - 1]; // last value in the block
if (max_value < hi)
{
// yes, we do
auto index = search_in_block(current_block.data(), stored, max_value);
value_count -= stored - index;
}
}
return value_count;
}
template <typename T>
ATTR_UNUSED int64_t scan_blocks_forward(block<T> ¤t_block, int64_t initial_count, T min_value)
{
int64_t value_count = initial_count;
int64_t stored;
do
{
// check block range by peeking at first and last value
stored = value_count > current_block.capacity() - 1 ? current_block.capacity() : value_count;
const auto hi = current_block[stored - 1]; // last value in the block
if (hi < min_value)
{
value_count -= stored;
// do we have previous block?
if (value_count > 0)
{
current_block.move_next();
continue;
}
}
break;
} while (true);
if (value_count > 0)
{
// do we need to search this block?
const auto lo = current_block[0]; // first value in the block
if (min_value > lo)
{
// yes, we do
value_count -= search_in_block(current_block.data(), stored, min_value - 1);
}
}
return value_count;
}
void latest_scan_backward(
uint64_t keys_memory_addr,
size_t keys_memory_size,
uint64_t values_memory_addr,
size_t value_memory_size,
uint64_t args_memory_addr,
int64_t unindexed_null_count,
int64_t max_value,
int64_t min_value,
int32_t partition_index,
uint32_t vblock_capacity_mask);
#endif //TimesGraph_BITMAP_INDEX_UTILS_H