OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gendb_if.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #ifndef DATABASE_GENDB_IF_H_
6 #define DATABASE_GENDB_IF_H_
7 
8 #include <string>
9 #include <vector>
10 #include <map>
11 #include <boost/function.hpp>
12 #include <boost/uuid/uuid.hpp>
13 #include <boost/uuid/uuid_io.hpp>
14 #include <boost/variant.hpp>
15 #include <boost/ptr_container/ptr_vector.hpp>
16 #include <boost/scoped_ptr.hpp>
17 #include <boost/tuple/tuple.hpp>
18 #include <boost/asio/ip/tcp.hpp>
19 
20 #include <base/address.h>
21 #include <database/gendb_types.h>
22 
23 namespace GenDb {
24 
25 struct DbConsistency {
26  enum type {
28  ANY,
29  ONE,
30  TWO,
33  ALL,
39  };
40 };
41 
42 struct Op {
43  enum type {
45  GE,
46  GT,
47  LE,
48  LT,
49  EQ,
52  };
53  static std::string ToString(Op::type op);
54 };
55 
56 struct Blob {
57  Blob(const uint8_t *data, size_t size) :
58  data_(reinterpret_cast<const char *>(data), size) {
59  }
60  const uint8_t* data() const {
61  return reinterpret_cast<const uint8_t *>(data_.c_str());
62  }
63  size_t size() const {
64  return data_.length();
65  }
66  private:
67  friend inline bool operator==(const Blob &lhs, const Blob &rhs);
68  friend inline bool operator<(const Blob &lhs, const Blob &rhs);
69  std::string data_;
70 };
71 
72 inline bool operator==(const Blob &lhs, const Blob &rhs) {
73  return lhs.data_ == rhs.data_;
74 }
75 
76 inline bool operator<(const Blob &lhs, const Blob &rhs) {
77  return lhs.data_ < rhs.data_;
78 }
79 
80 std::ostream& operator<<(std::ostream &out, const Blob &value);
81 
82 /* New stuff */
83 typedef boost::variant<boost::blank, std::string, uint64_t, uint32_t,
84  boost::uuids::uuid, uint8_t, uint16_t, double, IpAddress, Blob>
86 
98 };
99 
100 typedef std::vector<DbDataValue> DbDataValueVec;
101 typedef std::vector<GenDb::DbDataType::type> DbDataTypeVec;
102 
103 std::string DbDataValueVecToString(const GenDb::DbDataValueVec &v_db_value);
104 std::string DbDataValueToString(const GenDb::DbDataValue &db_value);
105 std::string bytes_to_hex(const uint8_t *byte_array, size_t size);
106 
107 struct NewCf {
112  };
113  typedef std::map<std::string, GenDb::DbDataType::type> ColumnMap; /* columns meta-data */
114 
115  NewCf(const std::string& cfname, const DbDataTypeVec& keys,
116  const ColumnMap& cfcolumns) :
117  cfname_(cfname),
119  partition_keys_(keys),
120  cfcolumns_(cfcolumns) {
121  }
122 
123  NewCf(const std::string& cfname, const DbDataTypeVec& keys,
124  const DbDataTypeVec& clustering_columns,
125  const DbDataTypeVec& columns,
126  const DbDataTypeVec& values) :
127  cfname_(cfname),
129  partition_keys_(keys),
130  clustering_columns_(clustering_columns),
131  columns_(columns),
132  value_(values) {
133  }
134 
135  ~NewCf() {}
136 
137  std::string cfname_;
139  DbDataTypeVec partition_keys_; /* for key-value comparison */
140  ColumnMap cfcolumns_; /* column-name:datatype - static tables */
141  DbDataTypeVec clustering_columns_; /* clustering column datatype - dynamic tables */
142  DbDataTypeVec columns_; /* column datatype - dynamic tables */
143  DbDataTypeVec value_; /* actual data - used for dynamic tables */
144 };
145 
146 struct NewCol {
148  cftype_(NewCf::COLUMN_FAMILY_NOSQL), name(n), value(v), ttl(ttl),
149  timestamp(new DbDataValueVec()) {}
150 
152  DbDataValueVec* t) :
153  cftype_(NewCf::COLUMN_FAMILY_NOSQL), name(n), value(v), ttl(ttl),
154  timestamp(t) {}
155 
156  NewCol(const std::string& n, const DbDataValue& v, int ttl) :
157  cftype_(NewCf::COLUMN_FAMILY_SQL), name(new DbDataValueVec(1, n)),
158  value(new DbDataValueVec(1, v)), ttl(ttl),
159  timestamp(new DbDataValueVec()) {}
160 
161  NewCol(const std::string& n, const DbDataValue& v, int ttl, DbDataValueVec *t) :
162  cftype_(NewCf::COLUMN_FAMILY_SQL), name(new DbDataValueVec(1, n)),
163  value(new DbDataValueVec(1, v)), ttl(ttl), timestamp(t) {}
164 
165  NewCol(const NewCol &rhs) :
166  cftype_(rhs.cftype_), name(new DbDataValueVec(*rhs.name)),
167  value(new DbDataValueVec(*rhs.value)), ttl(rhs.ttl),
168  timestamp(new DbDataValueVec(*rhs.timestamp)) {}
169 
170  bool operator==(const NewCol &rhs) const {
171  return (*rhs.name == *name &&
172  *rhs.value == *value &&
173  rhs.ttl == ttl &&
174  *rhs.timestamp == *timestamp);
175  }
176 
177  size_t GetSize() const;
178 
180  boost::scoped_ptr<DbDataValueVec> name;
181  boost::scoped_ptr<DbDataValueVec> value;
182  int ttl;
183  boost::scoped_ptr<DbDataValueVec> timestamp;
184 };
185 
186 typedef boost::ptr_vector<NewCol> NewColVec;
187 
188 struct ColList {
190  }
191 
193  }
194 
195  size_t GetSize() const;
196 
197  std::string cfname_; /* column family name */
198  DbDataValueVec rowkey_; /* rowkey-value */
200 };
201 
202 inline bool operator==(const ColList &lhs, const ColList &rhs) {
203  return (lhs.cfname_ == rhs.cfname_ &&
204  lhs.rowkey_ == rhs.rowkey_ &&
205  lhs.columns_ == rhs.columns_);
206 }
207 
208 typedef boost::ptr_vector<ColList> ColListVec;
209 
212  }
213 
215  }
216 
217  bool IsEmpty() const {
218  return start_.empty() &&
219  finish_.empty() &&
220  count_ == 0;
221  }
222 
223  std::string ToString() const;
224 
227  uint32_t count_;
230 };
231 
232 typedef boost::tuple<std::string, GenDb::Op::type, DbDataValue> WhereIndexInfo;
233 typedef std::vector<WhereIndexInfo> WhereIndexInfoVec;
234 
235 // fields to read, whether the field is rowkey, whether the field is column,
236 // whether to read writetime
237 typedef boost::tuple<std::string, bool, bool, bool> FieldNamesToReadInfo;
238 typedef std::vector<FieldNamesToReadInfo> FieldNamesToReadVec;
239 
240 typedef boost::asio::ip::tcp::endpoint Endpoint;
241 
242 struct DbOpResult {
243  enum type {
244  OK,
247  };
248 };
249 
250 class GenDbIf {
251 public:
252  typedef boost::function<void(void)> DbErrorHandler;
253  typedef boost::function<void(size_t)> DbQueueWaterMarkCb;
254  typedef boost::function<void(DbOpResult::type)> DbAddColumnCb;
255  typedef boost::function<void(DbOpResult::type,
256  std::auto_ptr<ColList>)> DbGetRowCb;
257 
258  GenDbIf() {}
259  virtual ~GenDbIf() {}
260 
261  // Init/Uninit
262  virtual bool Db_Init() = 0;
263  virtual void Db_Uninit() = 0;
264  virtual void Db_SetInitDone(bool init_done) = 0;
265  // Tablespace
266  virtual bool Db_SetTablespace(const std::string& tablespace) = 0;
267  virtual bool Db_AddSetTablespace(const std::string& tablespace,
268  const std::string& replication_factor="1") = 0;
269  // Column family
270  virtual bool Db_AddColumnfamily(const NewCf& cf,
271  const std::string &compaction_strategy) = 0;
272  virtual bool Db_UseColumnfamily(const NewCf& cf) = 0;
273  virtual bool Db_UseColumnfamily(const std::string& cfname) = 0;
274  // Index
275  virtual bool Db_CreateIndex(const std::string &cfname,
276  const std::string &column, const std::string &indexname,
278  // Column
279  virtual bool Db_AddColumn(std::auto_ptr<ColList> cl,
280  DbConsistency::type dconsistency, DbAddColumnCb cb) = 0;
281  virtual bool Db_AddColumnSync(std::auto_ptr<ColList> cl,
282  DbConsistency::type dconsistency) = 0;
283  // Read/Get
284  virtual bool Db_GetRow(ColList *ret, const std::string& cfname,
285  const DbDataValueVec& rowkey, DbConsistency::type dconsistency) = 0;
286  virtual bool Db_GetRow(ColList *ret, const std::string& cfname,
287  const DbDataValueVec& rowkey, DbConsistency::type dconsistency,
288  const ColumnNameRange& crange,
289  const FieldNamesToReadVec &read_vec) = 0;
290  virtual bool Db_GetMultiRow(ColListVec *ret,
291  const std::string& cfname, const std::vector<DbDataValueVec>& key) = 0;
292  virtual bool Db_GetMultiRow(ColListVec *ret,
293  const std::string& cfname, const std::vector<DbDataValueVec>& key,
294  const ColumnNameRange& crange) = 0;
295  virtual bool Db_GetMultiRow(ColListVec *ret,
296  const std::string& cfname, const std::vector<DbDataValueVec>& key,
297  const ColumnNameRange& crange,
298  const FieldNamesToReadVec &read_vec,
299  DbConsistency::type dconsistency = DbConsistency::ONE) = 0;
300  virtual bool Db_GetRowAsync(const std::string& cfname,
301  const DbDataValueVec& rowkey, DbConsistency::type dconsistency,
302  int task_id, int task_instance, DbGetRowCb cb) = 0;
303  virtual bool Db_GetRowAsync(const std::string& cfname,
304  const DbDataValueVec& rowkey, DbConsistency::type dconsistency,
305  DbGetRowCb cb) = 0;
306  virtual bool Db_GetRowAsync(const std::string& cfname,
307  const DbDataValueVec& rowkey, const ColumnNameRange &crange,
308  DbConsistency::type dconsistency, DbGetRowCb cb) = 0;
309  virtual bool Db_GetRowAsync(const std::string& cfname,
310  const DbDataValueVec& rowkey, const ColumnNameRange &crange,
311  DbConsistency::type dconsistency, int task_id, int task_instance,
312  DbGetRowCb cb) = 0;
313  virtual bool Db_GetRowAsync(const std::string &cfname,
314  const GenDb::DbDataValueVec &rowkey, const GenDb::ColumnNameRange &crange,
315  const GenDb::WhereIndexInfoVec &where_vec,
316  GenDb::DbConsistency::type dconsistency,
318  virtual bool Db_GetAllRows(ColListVec *ret,
319  const std::string& cfname, DbConsistency::type dconsistency) = 0;
320  // Queue
321  virtual bool Db_GetQueueStats(uint64_t *queue_count,
322  uint64_t *enqueues) const = 0;
323  virtual void Db_SetQueueWaterMark(bool high, size_t queue_count,
324  DbQueueWaterMarkCb cb) = 0;
325  virtual void Db_ResetQueueWaterMarks() = 0;
326  // Stats
327  virtual bool Db_GetStats(std::vector<DbTableInfo> *vdbti,
328  DbErrors *dbe) = 0;
329  virtual bool Db_GetCumulativeStats(std::vector<DbTableInfo> *vdbti,
330  DbErrors *dbe) const= 0;
331  // Connection
332  virtual std::vector<Endpoint> Db_GetEndpoints() const = 0;
333 };
334 
335 } // namespace GenDb
336 
337 #endif // DATABASE_GENDB_IF_H_
boost::function< void(void)> DbErrorHandler
Definition: gendb_if.h:252
virtual bool Db_SetTablespace(const std::string &tablespace)=0
std::vector< GenDb::DbDataType::type > DbDataTypeVec
Definition: gendb_if.h:101
virtual bool Db_GetCumulativeStats(std::vector< DbTableInfo > *vdbti, DbErrors *dbe) const =0
std::vector< FieldNamesToReadInfo > FieldNamesToReadVec
Definition: gendb_if.h:238
bool operator==(const Blob &lhs, const Blob &rhs)
Definition: gendb_if.h:72
boost::asio::ip::tcp::endpoint Endpoint
Definition: gendb_if.h:240
boost::tuple< std::string, bool, bool, bool > FieldNamesToReadInfo
Definition: gendb_if.h:237
virtual bool Db_AddSetTablespace(const std::string &tablespace, const std::string &replication_factor="1")=0
DbDataValueVec rowkey_
Definition: gendb_if.h:198
virtual bool Db_Init()=0
boost::asio::ip::address IpAddress
Definition: address.h:13
ColumnMap cfcolumns_
Definition: gendb_if.h:140
DbDataTypeVec partition_keys_
Definition: gendb_if.h:139
const uint8_t * data() const
Definition: gendb_if.h:60
size_t GetSize() const
Definition: gendb_if.cc:47
virtual void Db_Uninit()=0
boost::uuids::uuid uuid
friend bool operator<(const Blob &lhs, const Blob &rhs)
Definition: gendb_if.h:76
size_t GetSize() const
Definition: gendb_if.cc:36
std::string ToString() const
Definition: gendb_if.cc:60
std::string cfname_
Definition: gendb_if.h:197
virtual void Db_SetInitDone(bool init_done)=0
boost::ptr_vector< NewCol > NewColVec
Definition: gendb_if.h:186
bool IsEmpty() const
Definition: gendb_if.h:217
virtual bool Db_UseColumnfamily(const NewCf &cf)=0
Blob(const uint8_t *data, size_t size)
Definition: gendb_if.h:57
DbDataValueVec start_
Definition: gendb_if.h:225
static std::string ToString(Op::type op)
Definition: gendb_if.cc:81
bool operator==(const NewCol &rhs) const
Definition: gendb_if.h:170
virtual void Db_SetQueueWaterMark(bool high, size_t queue_count, DbQueueWaterMarkCb cb)=0
uint8_t type
Definition: load_balance.h:109
NewCol(DbDataValueVec *n, DbDataValueVec *v, int ttl, DbDataValueVec *t)
Definition: gendb_if.h:151
std::vector< DbDataValue > DbDataValueVec
Definition: gendb_if.h:100
DbDataTypeVec clustering_columns_
Definition: gendb_if.h:141
virtual bool Db_CreateIndex(const std::string &cfname, const std::string &column, const std::string &indexname, const GenDb::ColIndexMode::type index_mode=GenDb::ColIndexMode::NONE)=0
std::string bytes_to_hex(const uint8_t *byte_array, size_t size)
Definition: gendb_if.cc:116
boost::scoped_ptr< DbDataValueVec > timestamp
Definition: gendb_if.h:183
std::string DbDataValueVecToString(const GenDb::DbDataValueVec &v_db_value)
Definition: gendb_if.cc:102
virtual bool Db_GetRow(ColList *ret, const std::string &cfname, const DbDataValueVec &rowkey, DbConsistency::type dconsistency)=0
boost::variant< boost::blank, std::string, uint64_t, uint32_t, boost::uuids::uuid, uint8_t, uint16_t, double, IpAddress, Blob > DbDataValue
Definition: gendb_if.h:85
boost::scoped_ptr< DbDataValueVec > value
Definition: gendb_if.h:181
boost::function< void(DbOpResult::type, std::auto_ptr< ColList >)> DbGetRowCb
Definition: gendb_if.h:256
virtual bool Db_GetStats(std::vector< DbTableInfo > *vdbti, DbErrors *dbe)=0
std::map< std::string, GenDb::DbDataType::type > ColumnMap
Definition: gendb_if.h:113
virtual ~GenDbIf()
Definition: gendb_if.h:259
std::string data_
Definition: gendb_if.h:69
virtual bool Db_GetRowAsync(const std::string &cfname, const DbDataValueVec &rowkey, DbConsistency::type dconsistency, int task_id, int task_instance, DbGetRowCb cb)=0
std::vector< WhereIndexInfo > WhereIndexInfoVec
Definition: gendb_if.h:233
NewColVec columns_
Definition: gendb_if.h:199
NewCf(const std::string &cfname, const DbDataTypeVec &keys, const ColumnMap &cfcolumns)
Definition: gendb_if.h:115
virtual bool Db_GetQueueStats(uint64_t *queue_count, uint64_t *enqueues) const =0
virtual std::vector< Endpoint > Db_GetEndpoints() const =0
boost::ptr_vector< ColList > ColListVec
Definition: gendb_if.h:208
NewCol(DbDataValueVec *n, DbDataValueVec *v, int ttl)
Definition: gendb_if.h:147
std::string cfname_
Definition: gendb_if.h:137
size_t size() const
Definition: gendb_if.h:63
virtual bool Db_GetAllRows(ColListVec *ret, const std::string &cfname, DbConsistency::type dconsistency)=0
NewCf(const std::string &cfname, const DbDataTypeVec &keys, const DbDataTypeVec &clustering_columns, const DbDataTypeVec &columns, const DbDataTypeVec &values)
Definition: gendb_if.h:123
boost::function< void(size_t)> DbQueueWaterMarkCb
Definition: gendb_if.h:253
NewCol(const NewCol &rhs)
Definition: gendb_if.h:165
DbDataTypeVec columns_
Definition: gendb_if.h:142
std::string DbDataValueToString(const GenDb::DbDataValue &db_value)
Definition: gendb_if.cc:165
boost::tuple< std::string, GenDb::Op::type, DbDataValue > WhereIndexInfo
Definition: gendb_if.h:232
virtual bool Db_GetMultiRow(ColListVec *ret, const std::string &cfname, const std::vector< DbDataValueVec > &key)=0
virtual bool Db_AddColumn(std::auto_ptr< ColList > cl, DbConsistency::type dconsistency, DbAddColumnCb cb)=0
virtual bool Db_AddColumnfamily(const NewCf &cf, const std::string &compaction_strategy)=0
NewCf::ColumnFamilyType cftype_
Definition: gendb_if.h:179
boost::function< void(DbOpResult::type)> DbAddColumnCb
Definition: gendb_if.h:254
NewCol(const std::string &n, const DbDataValue &v, int ttl, DbDataValueVec *t)
Definition: gendb_if.h:161
std::ostream & operator<<(std::ostream &out, const Blob &value)
Definition: gendb_if.cc:128
boost::scoped_ptr< DbDataValueVec > name
Definition: gendb_if.h:180
DbDataValueVec finish_
Definition: gendb_if.h:226
bool operator<(const Blob &lhs, const Blob &rhs)
Definition: gendb_if.h:76
virtual void Db_ResetQueueWaterMarks()=0
NewCol(const std::string &n, const DbDataValue &v, int ttl)
Definition: gendb_if.h:156
DbDataValueType
Definition: gendb_if.h:87
friend bool operator==(const Blob &lhs, const Blob &rhs)
Definition: gendb_if.h:72
DbDataTypeVec value_
Definition: gendb_if.h:143
ColumnFamilyType cftype_
Definition: gendb_if.h:138
virtual bool Db_AddColumnSync(std::auto_ptr< ColList > cl, DbConsistency::type dconsistency)=0