LCOV - code coverage report
Current view: top level - root/contrail/src/contrail-common/database - gendb_if.h (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 62 64 96.9 %
Date: 2026-08-03 02:19:58 Functions: 19 21 90.5 %
Legend: Lines: hit not hit

          Line data    Source code
       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 {
      27             :         UNKNOWN,
      28             :         ANY,
      29             :         ONE,
      30             :         TWO,
      31             :         THREE,
      32             :         QUORUM,
      33             :         ALL,
      34             :         LOCAL_QUORUM,
      35             :         EACH_QUORUM,
      36             :         SERIAL,
      37             :         LOCAL_SERIAL,
      38             :         LOCAL_ONE,
      39             :     };
      40             : };
      41             : 
      42             : struct Op {
      43             :     enum type {
      44             :         INVALID,
      45             :         GE,
      46             :         GT,
      47             :         LE,
      48             :         LT,
      49             :         EQ,
      50             :         LIKE,
      51             :         CONTAINS,
      52             :     };
      53             :     static std::string ToString(Op::type op);
      54             : };
      55             : 
      56             : struct Blob {
      57      210440 :     Blob(const uint8_t *data, size_t size) :
      58      210440 :         data_(reinterpret_cast<const char *>(data), size) {
      59      210444 :     }
      60      189691 :     const uint8_t* data() const {
      61      189691 :         return reinterpret_cast<const uint8_t *>(data_.c_str());
      62             :     }
      63      189689 :     size_t size() const {
      64      189689 :         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           0 : inline bool operator==(const Blob &lhs, const Blob &rhs) {
      73           0 :     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>
      85             :     DbDataValue;
      86             : 
      87             : enum DbDataValueType {
      88             :     DB_VALUE_BLANK = 0,
      89             :     DB_VALUE_STRING = 1,
      90             :     DB_VALUE_UINT64 = 2,
      91             :     DB_VALUE_UINT32 = 3,
      92             :     DB_VALUE_UUID = 4,
      93             :     DB_VALUE_UINT8 = 5,
      94             :     DB_VALUE_UINT16 = 6,
      95             :     DB_VALUE_DOUBLE = 7,
      96             :     DB_VALUE_INET = 8,
      97             :     DB_VALUE_BLOB = 9,
      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 {
     108             :     enum ColumnFamilyType {
     109             :         COLUMN_FAMILY_INVALID = 0,
     110             :         COLUMN_FAMILY_SQL = 1,
     111             :         COLUMN_FAMILY_NOSQL = 2,
     112             :     };
     113             :     typedef std::map<std::string, GenDb::DbDataType::type> ColumnMap; /* columns meta-data */
     114             : 
     115           3 :     NewCf(const std::string& cfname, const DbDataTypeVec& keys,
     116           3 :             const ColumnMap& cfcolumns) :
     117           3 :         cfname_(cfname),
     118           3 :         cftype_(COLUMN_FAMILY_SQL),
     119           3 :         partition_keys_(keys),
     120           3 :         cfcolumns_(cfcolumns) {
     121           3 :     }
     122             : 
     123           7 :     NewCf(const std::string& cfname, const DbDataTypeVec& keys,
     124             :             const DbDataTypeVec& clustering_columns,
     125             :             const DbDataTypeVec& columns,
     126           7 :             const DbDataTypeVec& values) :
     127           7 :         cfname_(cfname),
     128           7 :         cftype_(COLUMN_FAMILY_NOSQL),
     129           7 :         partition_keys_(keys),
     130           7 :         clustering_columns_(clustering_columns),
     131           7 :         columns_(columns),
     132          14 :         value_(values) {
     133           7 :     }
     134             : 
     135          10 :     ~NewCf() {}
     136             : 
     137             :     std::string cfname_;
     138             :     ColumnFamilyType cftype_;
     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 {
     147       25789 :     NewCol(DbDataValueVec* n, DbDataValueVec* v, int ttl) :
     148       25789 :         cftype_(NewCf::COLUMN_FAMILY_NOSQL), name(n), value(v), ttl(ttl),
     149       25789 :     timestamp(new DbDataValueVec()) {}
     150             : 
     151      126480 :     NewCol(DbDataValueVec* n, DbDataValueVec* v, int ttl,
     152      126480 :            DbDataValueVec* t) :
     153      126480 :         cftype_(NewCf::COLUMN_FAMILY_NOSQL), name(n), value(v), ttl(ttl),
     154      126480 :     timestamp(t) {}
     155             : 
     156         259 :     NewCol(const std::string& n, const DbDataValue& v, int ttl) :
     157         259 :         cftype_(NewCf::COLUMN_FAMILY_SQL), name(new DbDataValueVec(1, n)),
     158         259 :         value(new DbDataValueVec(1, v)), ttl(ttl),
     159         259 :         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          88 :     NewCol(const NewCol &rhs) :
     166          88 :         cftype_(rhs.cftype_), name(new DbDataValueVec(*rhs.name)),
     167          88 :         value(new DbDataValueVec(*rhs.value)), ttl(rhs.ttl),
     168          88 :         timestamp(new DbDataValueVec(*rhs.timestamp)) {}
     169             : 
     170          16 :     bool operator==(const NewCol &rhs) const {
     171          16 :         return (*rhs.name == *name &&
     172          16 :                 *rhs.value == *value &&
     173          48 :                 rhs.ttl == ttl &&
     174          32 :                 *rhs.timestamp == *timestamp);
     175             :     }
     176             : 
     177             :     size_t GetSize() const;
     178             : 
     179             :     NewCf::ColumnFamilyType cftype_;
     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 {
     189       37971 :     ColList() {
     190       37971 :     }
     191             : 
     192       37996 :     ~ColList() {
     193       37996 :     }
     194             : 
     195             :     size_t GetSize() const;
     196             : 
     197             :     std::string cfname_; /* column family name */
     198             :     DbDataValueVec rowkey_; /* rowkey-value */
     199             :     NewColVec columns_;
     200             : };
     201             : 
     202           5 : inline bool operator==(const ColList &lhs, const ColList &rhs) {
     203           5 :     return (lhs.cfname_ == rhs.cfname_ &&
     204          10 :         lhs.rowkey_ == rhs.rowkey_ &&
     205          10 :         lhs.columns_ == rhs.columns_);
     206             : }
     207             : 
     208             : typedef boost::ptr_vector<ColList> ColListVec;
     209             : 
     210             : struct ColumnNameRange {
     211       27093 :     ColumnNameRange() : count_(0), start_op_(Op::GE), finish_op_(Op::LE) {
     212       27093 :     }
     213             : 
     214       27095 :     ~ColumnNameRange() {
     215       27095 :     }
     216             : 
     217      108287 :     bool IsEmpty() const {
     218      128063 :         return start_.empty() &&
     219      128041 :             finish_.empty() &&
     220      108431 :             count_ == 0;
     221             :     }
     222             : 
     223             :     std::string ToString() const;
     224             : 
     225             :     DbDataValueVec start_;
     226             :     DbDataValueVec finish_;
     227             :     uint32_t count_;
     228             :     Op::type start_op_;
     229             :     Op::type finish_op_;
     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,
     245             :         BACK_PRESSURE,
     246             :         ERROR,
     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        3725 :     GenDbIf() {}
     259        3725 :     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,
     277             :         const GenDb::ColIndexMode::type index_mode = GenDb::ColIndexMode::NONE) = 0;
     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,
     317             :         GenDb::GenDbIf::DbGetRowCb cb) = 0;
     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_

Generated by: LCOV version 1.14