forked from vesoft-inc/nebula
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRowReader.cpp
635 lines (518 loc) · 17.2 KB
/
RowReader.cpp
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/* Copyright (c) 2018 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License,
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/
#include "base/Base.h"
#include "dataman/RowReader.h"
namespace nebula {
using nebula::meta::SchemaManager;
/*********************************************
*
* class RowReader::Cell
*
********************************************/
ResultType RowReader::Cell::getBool(bool& v) const noexcept {
RR_CELL_GET_VALUE(Bool);
}
ResultType RowReader::Cell::getFloat(float& v) const noexcept {
RR_CELL_GET_VALUE(Float);
}
ResultType RowReader::Cell::getDouble(double& v) const noexcept {
RR_CELL_GET_VALUE(Double);
}
ResultType RowReader::Cell::getString(folly::StringPiece& v) const noexcept {
RR_CELL_GET_VALUE(String);
}
ResultType RowReader::Cell::getVid(int64_t& v) const noexcept {
RR_CELL_GET_VALUE(Vid);
}
/*********************************************
*
* class RowReader::Iterator
*
********************************************/
RowReader::Iterator::Iterator(const RowReader* reader,
size_t numFields,
int64_t index)
: reader_(reader)
, numFields_(numFields)
, index_(index) {
cell_.reset(new Cell(reader_, this));
}
RowReader::Iterator::Iterator(Iterator&& iter)
: reader_(iter.reader_)
, numFields_(iter.numFields_)
, cell_(std::move(iter.cell_))
, index_(iter.index_)
, bytes_(iter.bytes_)
, offset_(iter.offset_) {}
const RowReader::Cell& RowReader::Iterator::operator*() const {
return *cell_;
}
const RowReader::Cell* RowReader::Iterator::operator->() const {
return cell_.get();
}
RowReader::Iterator& RowReader::Iterator::operator++() {
if (*this) {
if (bytes_ > 0) {
offset_ += bytes_;
} else {
offset_ = reader_->skipToNext(index_, offset_);
if (offset_ < 0) {
// Something is wrong
index_ = numFields_;
return *this;
}
}
bytes_ = 0;
index_++;
}
return *this;
}
bool RowReader::Iterator::operator==(const Iterator& rhs) const noexcept {
return reader_ == rhs.reader_ &&
numFields_ == rhs.numFields_ &&
index_ == rhs.index_;
}
RowReader::Iterator::operator bool() const {
return index_ != static_cast<int64_t>(numFields_);
}
/*********************************************
*
* class RowReader
*
********************************************/
// static
std::unique_ptr<RowReader> RowReader::getTagPropReader(
meta::SchemaManager* schemaMan,
folly::StringPiece row,
GraphSpaceID space,
TagID tag) {
CHECK_NOTNULL(schemaMan);
int32_t ver = getSchemaVer(row);
if (ver >= 0) {
return std::unique_ptr<RowReader>(new RowReader(
row,
schemaMan->getTagSchema(space, tag, ver)));
} else {
// Invalid data
// TODO We need a better error handler here
LOG(FATAL) << "Invalid schema version in the row data!";
return nullptr;
}
}
// static
std::unique_ptr<RowReader> RowReader::getEdgePropReader(
meta::SchemaManager* schemaMan,
folly::StringPiece row,
GraphSpaceID space,
EdgeType edge) {
CHECK_NOTNULL(schemaMan);
int32_t ver = getSchemaVer(row);
if (ver >= 0) {
return std::unique_ptr<RowReader>(new RowReader(
row,
schemaMan->getEdgeSchema(space, edge, ver)));
} else {
// Invalid data
// TODO We need a better error handler here
LOG(FATAL) << "Invalid schema version in the row data!";
return nullptr;
}
}
// static
std::unique_ptr<RowReader> RowReader::getRowReader(
folly::StringPiece row,
std::shared_ptr<const meta::SchemaProviderIf> schema) {
SchemaVer ver = getSchemaVer(row);
CHECK_EQ(ver, schema->getVersion());
return std::unique_ptr<RowReader>(new RowReader(row, std::move(schema)));
}
// static
int32_t RowReader::getSchemaVer(folly::StringPiece row) {
const uint8_t* it = reinterpret_cast<const uint8_t*>(row.begin());
if (reinterpret_cast<const char*>(it) == row.end()) {
LOG(ERROR) << "Row data is empty, so there is no schema version";
return 0;
}
// The first three bits indicate the number of bytes for the
// schema version. If the number is zero, no schema version
// presents
size_t verBytes = *(it++) >> 5;
int32_t ver = 0;
if (verBytes > 0) {
if (verBytes + 1 > row.size()) {
// Data is too short
LOG(ERROR) << "Row data is too short";
return 0;
}
// Schema Version is stored in Little Endian
for (size_t i = 0; i < verBytes; i++) {
ver |= (uint32_t(*(it++)) << (8 * i));
}
}
return ver;
}
RowReader::RowReader(folly::StringPiece row,
std::shared_ptr<const meta::SchemaProviderIf> schema)
: schema_{std::move(schema)} {
CHECK(!!schema_) << "A schema must be provided";
if (processHeader(row)) {
// data_.begin() points to the first field
data_.reset(row.begin() + headerLen_, row.size() - headerLen_);
} else {
// Invalid data
// TODO We need a better error handler here
LOG(FATAL) << "Invalid row data!";
}
}
bool RowReader::processHeader(folly::StringPiece row) {
const uint8_t* it = reinterpret_cast<const uint8_t*>(row.begin());
if (reinterpret_cast<const char*>(it) == row.end()) {
return false;
}
DCHECK(!!schema_) << "A schema must be provided";
// The last three bits indicate the number of bytes for offsets
// The first three bits indicate the number of bytes for the
// schena version. If the number is zero, no schema version
// presents
numBytesForOffset_ = (*it & 0x07) + 1;
int32_t verBytes = *(it++) >> 5;
it += verBytes;
// Process the block offsets
// Block offsets point to the start of every 16 fields, except the
// first 16 fields
// Block offsets are stored in Little Endian
uint32_t numFields = schema_->getNumFields();
uint32_t numOffsets = (numFields >> 4);
if (numBytesForOffset_ * numOffsets + verBytes + 1 > row.size()) {
// Data is too short
LOG(ERROR) << "Row data is too short";
return false;
}
offsets_.resize(numFields + 1, -1);
offsets_[0] = 0;
blockOffsets_.emplace_back(0, 0);
blockOffsets_.reserve(numOffsets);
for (uint32_t i = 0; i < numOffsets; i++) {
int64_t offset = 0;
for (int32_t j = 0; j < numBytesForOffset_; j++) {
offset |= (uint64_t(*(it++)) << (8 * j));
}
blockOffsets_.emplace_back(offset, 0);
offsets_[16 * (i + 1)] = offset;
}
// Now done with the header
headerLen_ = reinterpret_cast<const char*>(it) - row.begin();
offsets_[numFields] = row.size() - headerLen_;
return true;
}
int32_t RowReader::numFields() const noexcept {
return schema_->getNumFields();
}
SchemaVer RowReader::schemaVer() const noexcept {
return schema_->getVersion();
}
int64_t RowReader::skipToNext(int64_t index, int64_t offset) const noexcept {
const cpp2::ValueType& vType = schema_->getFieldType(index);
CHECK(vType != CommonConstants::kInvalidValueType())
<< "No schema for the index " << index;
if (offsets_[index + 1] >= 0) {
return offsets_[index + 1];
}
switch (vType.get_type()) {
case cpp2::SupportedType::BOOL: {
// One byte
offset++;
break;
}
case cpp2::SupportedType::INT:
case cpp2::SupportedType::TIMESTAMP: {
int64_t v;
int32_t len = readInteger(offset, v);
if (len <= 0) {
return static_cast<int64_t>(ResultType::E_DATA_INVALID);
}
offset += len;
break;
}
case cpp2::SupportedType::FLOAT: {
// Four bytes
offset += sizeof(float);
break;
}
case cpp2::SupportedType::DOUBLE: {
// Eight bytes
offset += sizeof(double);
break;
}
case cpp2::SupportedType::STRING: {
int64_t strLen;
int32_t intLen = readInteger(offset, strLen);
if (intLen <= 0) {
return static_cast<int64_t>(ResultType::E_DATA_INVALID);
}
offset += intLen + strLen;
break;
}
case cpp2::SupportedType::VID: {
// Eight bytes
offset += sizeof(int64_t);
break;
}
default: {
// TODO
LOG(FATAL) << "Unimplemented";
}
}
if (offset > static_cast<int64_t>(data_.size())) {
return static_cast<int64_t>(ResultType::E_DATA_INVALID);
}
// Update offsets
offsets_[index + 1] = offset;
// Update block offsets
int32_t base = (index + 1) >> 4;
blockOffsets_[base].second = ((index + 1) & 0x0F);
return offset;
}
int64_t RowReader::skipToField(int64_t index) const noexcept {
DCHECK_GE(index, 0);
if (index >= static_cast<int64_t>(schema_->getNumFields())) {
// Index is out of range
return static_cast<int64_t>(ResultType::E_INDEX_OUT_OF_RANGE);
}
int64_t base = index >> 4;
const auto& blockOffset = blockOffsets_[base];
base <<= 4;
int64_t maxVisitedIndex = base + blockOffset.second;
if (index <= maxVisitedIndex) {
return offsets_[index];
}
int64_t offset = offsets_[maxVisitedIndex];
for (int64_t i = maxVisitedIndex; i < base + (index & 0x0000000f); i++) {
offset = skipToNext(i, offset);
if (offset < 0) {
return static_cast<int64_t>(ResultType::E_DATA_INVALID);
}
}
return offset;
}
int32_t RowReader::readFloat(int64_t offset, float& v) const noexcept {
if (offset + sizeof(float) > data_.size()) {
return static_cast<int32_t>(ResultType::E_DATA_INVALID);
}
memcpy(reinterpret_cast<char*>(&v), &(data_[offset]), sizeof(float));
return sizeof(float);
}
int32_t RowReader::readDouble(int64_t offset, double& v) const noexcept {
if (offset + sizeof(double) > data_.size()) {
return static_cast<int32_t>(ResultType::E_DATA_INVALID);
}
memcpy(reinterpret_cast<char*>(&v), &(data_[offset]), sizeof(double));
return sizeof(double);
}
int32_t RowReader::readString(int64_t offset, folly::StringPiece& v)
const noexcept {
int64_t strLen;
int32_t intLen = readInteger(offset, strLen);
CHECK_GT(intLen, 0) << "Invalid string length";
if (offset + intLen + strLen > static_cast<int64_t>(data_.size())) {
return static_cast<int32_t>(ResultType::E_DATA_INVALID);
}
v = data_.subpiece(offset + intLen, strLen);
return intLen + strLen;
}
int32_t RowReader::readInt64(int64_t offset, int64_t& v) const noexcept {
if (offset + sizeof(int64_t) > data_.size()) {
return static_cast<int32_t>(ResultType::E_DATA_INVALID);
}
// VID is stored in Little Endian
memcpy(reinterpret_cast<char*>(&v), &(data_[offset]), sizeof(int64_t));
return sizeof(int64_t);
}
int32_t RowReader::readVid(int64_t offset, int64_t& v) const noexcept {
return readInt64(offset, v);
}
ResultType RowReader::getBool(int64_t index, int64_t& offset, bool& v)
const noexcept {
switch (schema_->getFieldType(index).get_type()) {
case cpp2::SupportedType::BOOL: {
v = intToBool(data_[offset]);
offset++;
break;
}
case cpp2::SupportedType::INT:
case cpp2::SupportedType::TIMESTAMP: {
int64_t intV;
int32_t numBytes = readInteger(offset, intV);
if (numBytes > 0) {
v = intToBool(intV);
offset += numBytes;
} else {
return static_cast<ResultType>(numBytes);
}
break;
}
case cpp2::SupportedType::STRING: {
folly::StringPiece strV;
int32_t numBytes = readString(offset, strV);
if (numBytes > 0) {
v = strToBool(strV);
offset += numBytes;
} else {
return static_cast<ResultType>(numBytes);
}
break;
}
default: {
return ResultType::E_INCOMPATIBLE_TYPE;
}
}
return ResultType::SUCCEEDED;
}
ResultType RowReader::getFloat(int64_t index, int64_t& offset, float& v)
const noexcept {
switch (schema_->getFieldType(index).get_type()) {
case cpp2::SupportedType::FLOAT: {
int32_t numBytes = readFloat(offset, v);
if (numBytes < 0) {
return static_cast<ResultType>(numBytes);
}
offset += numBytes;
break;
}
case cpp2::SupportedType::DOUBLE: {
double d;
int32_t numBytes = readDouble(offset, d);
if (numBytes < 0) {
return static_cast<ResultType>(numBytes);
}
v = static_cast<float>(d);
offset += numBytes;
break;
}
default: {
return ResultType::E_INCOMPATIBLE_TYPE;
}
}
return ResultType::SUCCEEDED;
}
ResultType RowReader::getDouble(int64_t index, int64_t& offset, double& v)
const noexcept {
switch (schema_->getFieldType(index).get_type()) {
case cpp2::SupportedType::FLOAT: {
float f;
int32_t numBytes = readFloat(offset, f);
if (numBytes < 0) {
return static_cast<ResultType>(numBytes);
}
v = static_cast<double>(f);
offset += numBytes;
break;
}
case cpp2::SupportedType::DOUBLE: {
int32_t numBytes = readDouble(offset, v);
if (numBytes < 0) {
return static_cast<ResultType>(numBytes);
}
offset += numBytes;
break;
}
default: {
return ResultType::E_INCOMPATIBLE_TYPE;
}
}
return ResultType::SUCCEEDED;
}
ResultType RowReader::getString(int64_t index,
int64_t& offset,
folly::StringPiece& v) const noexcept {
switch (schema_->getFieldType(index).get_type()) {
case cpp2::SupportedType::STRING: {
int32_t numBytes = readString(offset, v);
if (numBytes < 0) {
return static_cast<ResultType>(numBytes);
}
offset += numBytes;
break;
}
default: {
return ResultType::E_INCOMPATIBLE_TYPE;
}
}
return ResultType::SUCCEEDED;
}
ResultType RowReader::getInt64(int64_t index, int64_t& offset, int64_t& v)
const noexcept {
switch (schema_->getFieldType(index).get_type()) {
case cpp2::SupportedType::INT:
case cpp2::SupportedType::TIMESTAMP: {
int32_t numBytes = readInteger(offset, v);
if (numBytes < 0) {
return static_cast<ResultType>(numBytes);
}
offset += numBytes;
break;
}
case cpp2::SupportedType::VID: {
int32_t numBytes = readVid(offset, v);
if (numBytes < 0) {
return static_cast<ResultType>(numBytes);
}
offset += numBytes;
break;
}
default: {
return ResultType::E_INCOMPATIBLE_TYPE;
}
}
return ResultType::SUCCEEDED;
}
ResultType RowReader::getVid(int64_t index, int64_t& offset, int64_t& v)
const noexcept {
auto fieldType = schema_->getFieldType(index).get_type();
if (fieldType == cpp2::SupportedType::INT || fieldType == cpp2::SupportedType::VID)
return getInt64(index, offset, v);
else
return ResultType::E_INCOMPATIBLE_TYPE;
}
RowReader::Iterator RowReader::begin() const noexcept {
return Iterator(this, schema_->getNumFields(), 0);
}
RowReader::Iterator RowReader::end() const noexcept {
auto numFields = schema_->getNumFields();
return Iterator(this, numFields, numFields);
}
/***************************************************
*
* Field Accessors
*
**************************************************/
ResultType RR_GET_VALUE_BY_NAME(Bool, bool)
ResultType RowReader::getBool(int64_t index, bool& v) const noexcept {
RR_GET_OFFSET()
return getBool(index, offset, v);
}
ResultType RR_GET_VALUE_BY_NAME(Float, float)
ResultType RowReader::getFloat(int64_t index, float& v) const noexcept {
RR_GET_OFFSET()
return getFloat(index, offset, v);
}
ResultType RR_GET_VALUE_BY_NAME(Double, double)
ResultType RowReader::getDouble(int64_t index, double& v) const noexcept {
RR_GET_OFFSET()
return getDouble(index, offset, v);
}
ResultType RR_GET_VALUE_BY_NAME(String, folly::StringPiece)
ResultType RowReader::getString(int64_t index, folly::StringPiece& v)
const noexcept {
RR_GET_OFFSET()
return getString(index, offset, v);
}
ResultType RR_GET_VALUE_BY_NAME(Vid, int64_t)
ResultType RowReader::getVid(int64_t index, int64_t& v) const noexcept {
RR_GET_OFFSET()
return getVid(index, offset, v);
}
} // namespace nebula