forked from vesoft-inc/nebula
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSchemaProviderIf.h
120 lines (93 loc) · 3.1 KB
/
SchemaProviderIf.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
/* 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.
*/
#ifndef META_SCHEMAPROVIDERIF_H_
#define META_SCHEMAPROVIDERIF_H_
#include "base/Base.h"
#include "gen-cpp2/common_constants.h"
namespace nebula {
using CommonConstants = nebula::cpp2::common_constants;
namespace meta {
class SchemaProviderIf {
public:
// This is an interface class
class Field {
public:
virtual ~Field() = default;
virtual const char* getName() const = 0;
virtual const nebula::cpp2::ValueType& getType() const = 0;
virtual bool isValid() const = 0;
};
// Inherited classes do not need to implement the Iterator
class Iterator final {
friend class SchemaProviderIf;
public:
const Field& operator*() const {
return *field_;
}
const Field* operator->() const {
return field_.get();
}
Iterator& operator++() {
if (field_) {
index_++;
field_ = schema_->field(index_);
}
return *this;
}
Iterator& operator+(uint16_t steps) {
if (field_) {
index_ += steps;
field_ = schema_->field(index_);
}
return *this;
}
operator bool() const {
return static_cast<bool>(field_);
}
bool operator==(const Iterator& rhs) const {
return schema_ == rhs.schema_ &&
(index_ == rhs.index_ || (!field_ && !rhs.field_));
}
private:
const SchemaProviderIf* schema_;
size_t numFields_;
int64_t index_;
std::shared_ptr<const Field> field_;
private:
explicit Iterator(const SchemaProviderIf* schema,
int64_t idx = 0)
: schema_(schema)
, numFields_(schema_->getNumFields())
, index_(idx) {
field_ = schema_->field(index_);
}
};
public:
virtual ~SchemaProviderIf() = default;
virtual SchemaVer getVersion() const noexcept = 0;
virtual size_t getNumFields() const noexcept = 0;
virtual int64_t getFieldIndex(const folly::StringPiece name) const = 0;
virtual const char* getFieldName(int64_t index) const = 0;
virtual const nebula::cpp2::ValueType& getFieldType(int64_t index) const = 0;
virtual const nebula::cpp2::ValueType& getFieldType(const folly::StringPiece name)
const = 0;
virtual std::shared_ptr<const Field> field(int64_t index) const = 0;
virtual std::shared_ptr<const Field> field(const folly::StringPiece name) const = 0;
/******************************************
*
* Iterator implementation
*
*****************************************/
Iterator begin() const {
return Iterator(this, 0);
}
Iterator end() const {
return Iterator(this, getNumFields());
}
};
} // namespace meta
} // namespace nebula
#endif // META_SCHEMAPROVIDERIF_H_