/
gendb_if.h
213 lines (181 loc) · 6.36 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
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#ifndef __GENDB_IF_H__
#define __GENDB_IF_H__
//#include <stdint.h>
#include <string>
#include <vector>
#include <map>
#include <boost/function.hpp>
//#include <boost/asio/io_service.hpp>
#include <boost/uuid/uuid.hpp>
//#include <boost/uuid/uuid_generators.hpp>
#include <boost/variant.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/scoped_ptr.hpp>
#include "gendb_types.h"
namespace GenDb {
/* New stuff */
typedef boost::variant<boost::blank, std::string, uint64_t, uint32_t,
boost::uuids::uuid, uint8_t, uint16_t, double> 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,
};
typedef std::vector<DbDataValue> DbDataValueVec;
typedef std::vector<GenDb::DbDataType::type> DbDataTypeVec;
class DbDataValueTypeSizeVisitor : public boost::static_visitor<> {
public:
DbDataValueTypeSizeVisitor() :
size_(0) {
}
template<typename T>
void operator()(const T &t) {
size_ += sizeof(t);
}
void operator()(const std::string &str) {
size_ += str.length();
}
void operator()(const boost::blank &blank) {
size_ += 0;
}
void operator()(const boost::uuids::uuid &uuid) {
size_ += uuid.size();
}
size_t GetSize() const {
return size_;
}
size_t size_;
};
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_; // only one of these is expected to be filled
};
typedef boost::ptr_vector<ColList> ColListVec;
struct ColumnNameRange {
ColumnNameRange() : count(100) {
}
~ColumnNameRange() {
}
DbDataValueVec start_;
DbDataValueVec finish_;
uint32_t count;
};
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_AddTablespace(const std::string& tablespace,
const std::string& replication_factor) = 0;
virtual bool Db_SetTablespace(const std::string& tablespace) = 0;
virtual bool Db_AddSetTablespace(const std::string& tablespace,
const std::string& replication_factor="1") = 0;
virtual bool Db_FindTablespace(const std::string& tablespace) = 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,
GenDb::ColumnNameRange *crange_ptr = NULL) = 0;
virtual bool Db_GetRangeSlices(ColList& col_list,
const std::string& cfname, const ColumnNameRange& crange,
const DbDataValueVec& key) = 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::string Db_GetHost() const = 0;
virtual int Db_GetPort() const = 0;
static GenDbIf *GenDbIfImpl(DbErrorHandler hdlr,
const std::vector<std::string> &cassandra_ips,
const std::vector<int> &cassandra_ports,
std::string name, bool only_sync);
};
} // namespace GenDb
#endif // __GENDB_IF_H__