forked from TuGraph-family/tugraph-db
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvertex_index.h
More file actions
483 lines (404 loc) · 15.6 KB
/
vertex_index.h
File metadata and controls
483 lines (404 loc) · 15.6 KB
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
/**
* Copyright 2022 AntGroup CO., Ltd.
*
* 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.
*/
#pragma once
#include <atomic>
#include <exception>
#include <unordered_map>
#include "core/field_data_helper.h"
#include "core/data_type.h"
#include "core/iterator_base.h"
#include "core/kv_store.h"
#include "core/kv_table_comparators.h"
#include "core/type_convert.h"
#include "core/value.h"
int TestVertexIndexImpl();
int CURDVertexWithTooLongKey();
int TestVRefreshContentIfKvIteratorModified();
namespace lgraph {
class Transaction;
class VertexIndex;
class VertexIndexIterator;
class Schema;
class CompositeIndex;
class CompositeIndexIterator;
/**
* An VertexIndexValue packs multiple vids into a single value.
*/
class VertexIndexValue {
/**
* uint8_t count
* [5 bytes vid] * count
*/
friend class VertexIndex;
friend class VertexIndexIterator;
friend class CompositeIndex;
friend class CompositeIndexIterator;
Value v_;
VertexIndexValue();
explicit VertexIndexValue(const Value& v);
explicit VertexIndexValue(Value&& v);
template <typename IT>
VertexIndexValue(const IT& beg, const IT& end) {
size_t n = end - beg;
FMA_DBG_ASSERT(n < std::numeric_limits<uint8_t>::max());
v_.Resize(1 + _detail::VID_SIZE * n);
char* p = v_.Data();
*(uint8_t*)p = (uint8_t)n;
p++;
for (auto it = beg; it < end; it++) {
_detail::WriteVid(p, *it);
p += _detail::VID_SIZE;
}
}
int GetVidCount() const { return static_cast<int>(*(uint8_t*)v_.Data()); }
int64_t GetNthVid(int n) const {
return _detail::GetVid(v_.Data() + 1 + _detail::VID_SIZE * n);
}
/**
* Search for the specified vertex id
*
* \param vid The vid.
* \param [in,out] found Is this vid found?
*
* \return Position where the vid is found.
* list.
*/
int SearchVid(int64_t vid, bool& found) const;
/**
* Insert the specified vid.
*
* \param vid The vid.
* \return The status of the operation.
*
* return value:
* 0, vid already exists
* 1, the key does not change
* 2, the key changes
*/
uint8_t InsertVid(int64_t vid);
void InsertVidAtNthPos(int pos, int64_t vid);
/**
* Delete the specified vid.
*
* \param vid The vid.
* \return The status of the operation.
*
* return value:
* 0, vid does not exist
* 1, the key does not change
* 2, the key changes
*/
uint8_t DeleteVidIfExist(int64_t vid);
void DeleteNthVid(int pos);
bool TooLarge() { return v_.Size() > _detail::NODE_SPLIT_THRESHOLD; }
VertexIndexValue SplitRightHalf();
const Value& GetBuf() const { return v_; }
Value& GetBuf() { return v_; }
/**
* Patch the key with the last vid in this VertexIndexValue.
*/
Value CreateKey(const Value& key) const;
};
/**
* The iterator for indices
*/
class VertexIndexIterator : public ::lgraph::IteratorBase {
friend class VertexIndex;
VertexIndex* index_;
std::unique_ptr<KvIterator> it_;
Value key_end_;
Value curr_key_; // current indexed key, excluding vid
VertexIndexValue iv_; // VertexIndexValue, if this is non-unique index
bool valid_;
int pos_;
VertexId vid_; // current vid
IndexType type_;
/**
* The constructor for VertexIndexIterator
*
* \param [in,out] idx The VertexIndex object this iterator will point to.
* \param [in,out] txn The transaction.
* \param [in,out] table The index table.
* \param key_start The start key.
* \param key_end The end key.
* \param vid The vid from which to start searching.
* \param unique Whether the index is a unique index.
*/
VertexIndexIterator(VertexIndex* idx, Transaction* txn, KvTable& table,
const Value& key_start,
const Value& key_end,
VertexId vid, IndexType type);
VertexIndexIterator(VertexIndex* idx, KvTransaction* txn, KvTable& table,
const Value& key_start,
const Value& key_end,
VertexId vid, IndexType type);
bool KeyOutOfRange();
/**
* Traverse to the previous key/value pair. Used when an inserted vid is
* greater than all existing vids. Should be used together with KeyEquals.
*
* \return True if it succeeds, false if it fails.
*/
bool PrevKV();
/**
* Check whether the iterator's current key starts with key.
*
* \param key The key.
* \return Whether the iterator's current key starts with key.
*/
bool KeyEquals(const Value& key);
/** Loads content from iterator, assuming iterator is already at the right
* position. */
void LoadContentFromIt();
DISABLE_COPY(VertexIndexIterator);
VertexIndexIterator& operator=(VertexIndexIterator&&) = delete;
protected:
void CloseImpl() override;
public:
VertexIndexIterator(VertexIndexIterator&& rhs);
/**
* Query if this iterator is valid, i.e. the Key and Vid can be queried.
*
* \return True if valid, false if not.
*/
bool IsValid() const { return valid_; }
/**
* Move to the next vertex id in the list, which consists of all the valid
* vertex ids of the iterator and is sorted from small to large.
*
* \return True if it succeeds, otherwise false.
*/
bool Next();
bool Goto(VertexId vid);
/**
* Gets the current key.
*
* \return The current key.
*/
Value GetKey() const;
FieldData GetKeyData() const {
return field_data_helper::ValueToFieldData(GetKey(), KeyType());
}
/**
* Gets the current vertex id.
*
* \return The current vertex id.
*/
int64_t GetVid() const { return vid_; }
FieldType KeyType() const;
/**
* Determines if we can refresh content if kv iterator modified
*
* @return True if KvIterator was modified.
*/
void RefreshContentIfKvIteratorModified() override;
};
/**
* The indices
*/
class VertexIndex {
friend class Schema;
friend class IndexManager;
friend class LightningGraph;
friend class Transaction;
friend int ::TestVertexIndexImpl();
friend int ::CURDVertexWithTooLongKey();
friend int ::TestVRefreshContentIfKvIteratorModified();
std::shared_ptr<KvTable> table_;
FieldType key_type_;
std::atomic<bool> ready_;
std::atomic<bool> disabled_;
IndexType type_;
public:
VertexIndex(std::shared_ptr<KvTable> table, FieldType key_type, IndexType type);
VertexIndex(const VertexIndex& rhs);
VertexIndex(VertexIndex&& rhs) = delete;
VertexIndex& operator=(const VertexIndex& rhs) = delete;
VertexIndex& operator=(VertexIndex&& rhs) = delete;
static std::unique_ptr<KvTable> OpenTable(KvTransaction& txn, KvStore& store,
const std::string& name, FieldType dt, IndexType type);
void _AppendVertexIndexEntry(KvTransaction& txn, const Value& k, VertexId vid);
/**
* Append an entry to non-unique vertex index.
*
* \param txn The transaction.
* \param k The key, raw format.
* \param vids All the vids corresponding to k. The vids must be sorted.
*/
void _AppendNonUniqueVertexIndexEntry(KvTransaction& txn, const Value& k,
const std::vector<VertexId>& vids);
#define _GET_UNM_INDEX_ITER_WITH_EMPTY_START_KEY(ft) \
do { \
auto d = field_data_helper::MinStoreValue<FieldType::ft>(); \
return VertexIndexIterator(this, &txn, *table_, Value::ConstRef(d), \
std::forward<V2>(key_end), \
vid, type_); \
} while (0)
/**
* Get an Vertex index iterator.
*
* \param [in,out] txn The transaction.
* \param key_start The start key.
* \param key_end The end key.
* \param vid The vid to start from.
*
* \return The index iterator.
*/
template <typename V1, typename V2>
VertexIndexIterator GetUnmanagedIterator(KvTransaction& txn, V1&& key_start, V2&& key_end,
VertexId vid = 0) {
if (key_start.Empty()) {
switch (key_type_) {
case FieldType::BOOL:
_GET_UNM_INDEX_ITER_WITH_EMPTY_START_KEY(BOOL);
case FieldType::INT8:
_GET_UNM_INDEX_ITER_WITH_EMPTY_START_KEY(INT8);
case FieldType::INT16:
_GET_UNM_INDEX_ITER_WITH_EMPTY_START_KEY(INT16);
case FieldType::INT32:
_GET_UNM_INDEX_ITER_WITH_EMPTY_START_KEY(INT32);
case FieldType::INT64:
_GET_UNM_INDEX_ITER_WITH_EMPTY_START_KEY(INT64);
case FieldType::DATE:
_GET_UNM_INDEX_ITER_WITH_EMPTY_START_KEY(DATE);
case FieldType::DATETIME:
_GET_UNM_INDEX_ITER_WITH_EMPTY_START_KEY(DATETIME);
case FieldType::FLOAT:
_GET_UNM_INDEX_ITER_WITH_EMPTY_START_KEY(FLOAT);
case FieldType::DOUBLE:
_GET_UNM_INDEX_ITER_WITH_EMPTY_START_KEY(DOUBLE);
case FieldType::STRING:
{
return VertexIndexIterator(this, &txn, *table_, std::forward<V1>(key_start),
CutKeyIfLongOnlyForNonUniqueIndex(std::forward<V2>(key_end)), vid, type_);
}
case FieldType::BLOB:
FMA_ASSERT(false) << "Blob fields must not be indexed.";
default:
FMA_ASSERT(false);
return VertexIndexIterator(this, &txn, *table_, std::forward<V1>(key_start),
std::forward<V2>(key_end), vid, type_);
}
} else {
return VertexIndexIterator(this, &txn, *table_,
CutKeyIfLongOnlyForNonUniqueIndex(std::forward<V1>(key_start)),
CutKeyIfLongOnlyForNonUniqueIndex(std::forward<V2>(key_end)),
vid, type_);
}
}
#define _GET_INDEX_ITER_WITH_EMPTY_START_KEY(ft) \
do { \
auto d = field_data_helper::MinStoreValue<FieldType::ft>(); \
return VertexIndexIterator(this, txn, *table_, Value::ConstRef(d), \
std::forward<V2>(key_end), \
vid, type_); \
} while (0)
template <typename V1, typename V2>
VertexIndexIterator GetIterator(Transaction* txn, V1&& key_start,
V2&& key_end, VertexId vid = 0) {
if (key_start.Empty()) {
switch (key_type_) {
case FieldType::BOOL:
_GET_INDEX_ITER_WITH_EMPTY_START_KEY(BOOL);
case FieldType::INT8:
_GET_INDEX_ITER_WITH_EMPTY_START_KEY(INT8);
case FieldType::INT16:
_GET_INDEX_ITER_WITH_EMPTY_START_KEY(INT16);
case FieldType::INT32:
_GET_INDEX_ITER_WITH_EMPTY_START_KEY(INT32);
case FieldType::INT64:
_GET_INDEX_ITER_WITH_EMPTY_START_KEY(INT64);
case FieldType::DATE:
_GET_INDEX_ITER_WITH_EMPTY_START_KEY(DATE);
case FieldType::DATETIME:
_GET_INDEX_ITER_WITH_EMPTY_START_KEY(DATETIME);
case FieldType::FLOAT:
_GET_INDEX_ITER_WITH_EMPTY_START_KEY(FLOAT);
case FieldType::DOUBLE:
_GET_INDEX_ITER_WITH_EMPTY_START_KEY(DOUBLE);
case FieldType::STRING:
{
return VertexIndexIterator(this, txn, *table_, std::forward<V1>(key_start),
CutKeyIfLongOnlyForNonUniqueIndex(std::forward<V2>(key_end)),
vid, type_);
}
case FieldType::BLOB:
FMA_ASSERT(false) << "Blob fields must not be indexed.";
default:
FMA_ASSERT(false);
return VertexIndexIterator(this, txn, *table_, std::forward<V1>(key_start),
std::forward<V2>(key_end), vid, type_);
}
} else {
return VertexIndexIterator(this, txn, *table_,
CutKeyIfLongOnlyForNonUniqueIndex(std::forward<V1>(key_start)),
CutKeyIfLongOnlyForNonUniqueIndex(std::forward<V2>(key_end)),
vid, type_);
}
}
bool IsReady() const {
return ready_.load(std::memory_order_acquire) && !disabled_.load(std::memory_order_acquire);
}
bool IsUnique() const { return type_ == IndexType::GlobalUniqueIndex; }
IndexType GetType() const { return type_; }
FieldType KeyType() { return key_type_; }
void Dump(KvTransaction& txn,
const std::function<std::string(const char* p, size_t s)>& key_to_string);
size_t GetNumKeys(KvTransaction& txn) { return table_->GetKeyCount(txn); }
private:
void Clear(KvTransaction& txn) { table_->Drop(txn); }
void SetReady() { ready_.store(true, std::memory_order_release); }
void Disable() { disabled_.store(true, std::memory_order_release); }
void Enable() { disabled_.store(false, std::memory_order_release); }
bool IsDisabled() const { return disabled_.load(std::memory_order_acquire); }
/**
* Delete a specified vertex under a specified key.
*
* \param [in,out] txn The transaction.
* \param key The key.
* \param vid The vid.
*
* \return Whether the operation succeeds or not.
*/
bool Delete(KvTransaction& txn, const Value& k, int64_t vid);
/**
* Move a specified vertex from an old key to a new key.
*
* \exception IndexException Thrown when an VertexIndex error condition occurs.
*
* \param [in,out] txn The transaction.
* \param old_key The old key.
* \param new_key The new key.
* \param vid The vid.
*
* \return Whether the operation succeeds or not.
*/
bool Update(KvTransaction& txn, const Value& old_key, const Value& new_key, int64_t vid);
/**
* Add a specified vertex under a specified key.
*
* \exception IndexException Thrown when an VertexIndex error condition occurs.
*
* \param [in,out] txn The transaction.
* \param key The key.
* \param vid The vid.
*
* \return Whether the operation succeeds or not.
*/
bool Add(KvTransaction& txn, const Value& k, int64_t vid);
bool UniqueIndexConflict(KvTransaction& txn, const Value& k);
size_t GetMaxVertexIndexKeySize();
Value CutKeyIfLongOnlyForNonUniqueIndex(const Value& k);
};
} // namespace lgraph