/
gendb_if.h
236 lines (202 loc) · 6.96 KB
/
gendb_if.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
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#ifndef DATABASE_GENDB_IF_H_
#define DATABASE_GENDB_IF_H_
#include <string>
#include <vector>
#include <map>
#include <boost/function.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/variant.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/scoped_ptr.hpp>
#include <database/gendb_types.h>
#include <net/address.h>
#include <boost/asio/ip/tcp.hpp>
namespace GenDb {
/* New stuff */
typedef boost::variant<boost::blank, std::string, uint64_t, uint32_t,
boost::uuids::uuid, uint8_t, uint16_t, double, IpAddress> DbDataValue;
enum DbDataValueType {
DB_VALUE_BLANK = 0,
DB_VALUE_STRING = 1,
DB_VALUE_UINT64 = 2,
DB_VALUE_UINT32 = 3,
DB_VALUE_UUID = 4,
DB_VALUE_UINT8 = 5,
DB_VALUE_UINT16 = 6,
DB_VALUE_DOUBLE = 7,
DB_VALUE_INET = 8
};
typedef std::vector<DbDataValue> DbDataValueVec;
typedef std::vector<GenDb::DbDataType::type> DbDataTypeVec;
std::string DbDataValueVecToString(const GenDb::DbDataValueVec &v_db_value);
std::string DbDataValueToString(const GenDb::DbDataValue &db_value);
class DbDataValueCqlPrinter : public boost::static_visitor<> {
public:
DbDataValueCqlPrinter(std::ostream &os, bool quote_strings) :
os_(os),
quote_strings_(quote_strings) {
}
DbDataValueCqlPrinter(std::ostream &os) :
os_(os),
quote_strings_(true) {
}
template<typename T>
void operator()(const T &t) const {
os_ << t;
}
void operator()(const boost::uuids::uuid &tuuid) const {
os_ << to_string(tuuid);
}
// uint8_t must be handled specially because ostream sees
// uint8_t as a text type instead of an integer type
void operator()(const uint8_t &t8) const {
os_ << (uint16_t)t8;
}
void operator()(const std::string &tstring) const {
if (quote_strings_) {
os_ << "'" << tstring << "'";
} else {
os_ << tstring;
}
}
// CQL int is 32 bit signed integer
void operator()(const uint32_t &tu32) const {
os_ << (int32_t)tu32;
}
// CQL bigint is 64 bit signed long
void operator()(const uint64_t &tu64) const {
os_ << (int64_t)tu64;
}
void operator()(const IpAddress &tipaddr) const {
os_ << "'" << tipaddr << "'";
}
std::ostream &os_;
bool quote_strings_;
};
struct NewCf {
enum ColumnFamilyType {
COLUMN_FAMILY_INVALID = 0,
COLUMN_FAMILY_SQL = 1,
COLUMN_FAMILY_NOSQL = 2,
};
typedef std::map<std::string, GenDb::DbDataType::type> SqlColumnMap; /* columns meta-data */
NewCf(const std::string& cfname, const DbDataTypeVec& key_type,
const SqlColumnMap& cfcolumns) :
cfname_(cfname),
cftype_(COLUMN_FAMILY_SQL),
key_validation_class(key_type),
cfcolumns_(cfcolumns) {
}
NewCf(const std::string& cfname, const DbDataTypeVec& key_type,
const DbDataTypeVec& comp_type,
const DbDataTypeVec& valid_class) :
cfname_(cfname),
cftype_(COLUMN_FAMILY_NOSQL),
key_validation_class(key_type),
comparator_type(comp_type),
default_validation_class(valid_class) {
}
~NewCf() {}
std::string cfname_;
ColumnFamilyType cftype_;
DbDataTypeVec key_validation_class; /* for key-value comparison */
SqlColumnMap cfcolumns_; /* columns meta-data */
DbDataTypeVec comparator_type; /* for column-name comparison */
DbDataTypeVec default_validation_class; /* for column-value comparison */
};
struct NewCol {
NewCol(DbDataValueVec* n, DbDataValueVec* v, int ttl) :
cftype_(NewCf::COLUMN_FAMILY_NOSQL), name(n), value(v), ttl(ttl) {}
NewCol(const std::string& n, const DbDataValue& v, int ttl) :
cftype_(NewCf::COLUMN_FAMILY_SQL), name(new DbDataValueVec(1, n)),
value(new DbDataValueVec(1, v)), ttl(ttl) {}
NewCol(const NewCol &rhs) :
cftype_(rhs.cftype_), name(new DbDataValueVec(*rhs.name)),
value(new DbDataValueVec(*rhs.value)), ttl(rhs.ttl) {}
bool operator==(const NewCol &rhs) const {
return (*rhs.name == *name &&
*rhs.value == *value &&
rhs.ttl == ttl);
}
size_t GetSize() const;
NewCf::ColumnFamilyType cftype_;
boost::scoped_ptr<DbDataValueVec> name;
boost::scoped_ptr<DbDataValueVec> value;
int ttl;
};
typedef boost::ptr_vector<NewCol> NewColVec;
struct ColList {
ColList() {
}
~ColList() {
}
size_t GetSize() const;
std::string cfname_; /* column family name */
DbDataValueVec rowkey_; /* rowkey-value */
NewColVec columns_;
};
typedef boost::ptr_vector<ColList> ColListVec;
struct ColumnNameRange {
ColumnNameRange() : count_(0) {
}
~ColumnNameRange() {
}
bool IsEmpty() const {
return start_.empty() &&
finish_.empty() &&
count_ == 0;
}
std::string ToString() const;
DbDataValueVec start_;
DbDataValueVec finish_;
uint32_t count_;
};
typedef boost::asio::ip::tcp::endpoint Endpoint;
class GenDbIf {
public:
typedef boost::function<void(void)> DbErrorHandler;
typedef boost::function<void(size_t)> DbQueueWaterMarkCb;
GenDbIf() {}
virtual ~GenDbIf() {}
// Init/Uninit
virtual bool Db_Init(const std::string& task_id, int task_instance) = 0;
virtual void Db_Uninit(const std::string& task_id, int task_instance) = 0;
virtual void Db_UninitUnlocked(const std::string& task_id,
int task_instance) = 0;
virtual void Db_SetInitDone(bool init_done) = 0;
// Tablespace
virtual bool Db_SetTablespace(const std::string& tablespace) = 0;
virtual bool Db_AddSetTablespace(const std::string& tablespace,
const std::string& replication_factor="1") = 0;
// Column family
virtual bool Db_AddColumnfamily(const NewCf& cf) = 0;
virtual bool Db_UseColumnfamily(const NewCf& cf) = 0;
// Column
virtual bool Db_AddColumn(std::auto_ptr<ColList> cl) = 0;
virtual bool Db_AddColumnSync(std::auto_ptr<GenDb::ColList> cl) = 0;
// Read/Get
virtual bool Db_GetRow(ColList *ret, const std::string& cfname,
const DbDataValueVec& rowkey) = 0;
virtual bool Db_GetMultiRow(ColListVec *ret,
const std::string& cfname, const std::vector<DbDataValueVec>& key) = 0;
virtual bool Db_GetMultiRow(ColListVec *ret,
const std::string& cfname, const std::vector<DbDataValueVec>& key,
const GenDb::ColumnNameRange& crange) = 0;
// Queue
virtual bool Db_GetQueueStats(uint64_t *queue_count,
uint64_t *enqueues) const = 0;
virtual void Db_SetQueueWaterMark(bool high, size_t queue_count,
DbQueueWaterMarkCb cb) = 0;
virtual void Db_ResetQueueWaterMarks() = 0;
// Stats
virtual bool Db_GetStats(std::vector<DbTableInfo> *vdbti,
DbErrors *dbe) = 0;
// Connection
virtual std::vector<Endpoint> Db_GetEndpoints() const = 0;
};
} // namespace GenDb
#endif // DATABASE_GENDB_IF_H_