OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gendb_if.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include <boost/foreach.hpp>
6 
7 #include <base/string_util.h>
8 #include <database/gendb_if.h>
9 
10 using namespace GenDb;
11 
12 class DbDataValueSizeVisitor : public boost::static_visitor<> {
13  public:
15  size_(0) {
16  }
17  template<typename T>
18  void operator()(const T &t) {
19  size_ += sizeof(t);
20  }
21  void operator()(const std::string &str) {
22  size_ += str.length();
23  }
24  void operator()(const boost::blank &blank) {
25  size_ += 0;
26  }
28  size_ += uuid.size();
29  }
30  size_t GetSize() const {
31  return size_;
32  }
33  size_t size_;
34 };
35 
36 size_t NewCol::GetSize() const {
37  DbDataValueSizeVisitor size_visitor;
38  // Name
39  std::for_each(name->begin(), name->end(),
40  boost::apply_visitor(size_visitor));
41  // Value
42  std::for_each(value->begin(), value->end(),
43  boost::apply_visitor(size_visitor));
44  return size_visitor.GetSize();
45 }
46 
47 size_t ColList::GetSize() const {
48  DbDataValueSizeVisitor size_visitor;
49  // Rowkey
50  std::for_each(rowkey_.begin(), rowkey_.end(),
51  boost::apply_visitor(size_visitor));
52  size_t size = size_visitor.GetSize();
53  // Columns
54  BOOST_FOREACH(const NewCol &col, columns_) {
55  size += col.GetSize();
56  }
57  return size;
58 }
59 
60 std::string ColumnNameRange::ToString() const {
61  std::ostringstream ss;
62  ss << "ColumnNameRange: ";
63  if (IsEmpty()) {
64  ss << "Empty";
65  return ss.str();
66  }
67  if (!start_.empty()) {
68  ss << "Start: " << DbDataValueVecToString(start_);;
69  }
70  ss << "Start Op: " << Op::ToString(start_op_);
71  if (!finish_.empty()) {
72  ss << "Finish: " << DbDataValueVecToString(finish_);
73  }
74  ss << "Finish Op: " << Op::ToString(finish_op_);
75  if (count_) {
76  ss << "Count: " << count_;
77  }
78  return ss.str();
79 }
80 
81 std::string Op::ToString(Op::type op) {
82  switch (op) {
83  case Op::GE:
84  return ">=";
85  case Op::GT:
86  return ">";
87  case Op::LE:
88  return "<=";
89  case Op::LT:
90  return "<";
91  case Op::EQ:
92  return "=";
93  case Op::LIKE:
94  return "LIKE";
95  case Op::CONTAINS:
96  return "CONTAINS";
97  default:
98  assert(0);
99  }
100 }
101 
103  const GenDb::DbDataValueVec &v_db_value) {
104  std::ostringstream ss;
105  ss << "[";
106  for (int i = 0; i < (int)v_db_value.size(); i++) {
107  if (i) {
108  ss << " ";
109  }
110  ss << DbDataValueToString(v_db_value[i]);
111  }
112  ss << "]";
113  return ss.str();
114 }
115 
116 std::string GenDb::bytes_to_hex(const uint8_t *byte_array, size_t size) {
117  std::stringstream value;
118  value << std::hex << std::setfill('0');
119  for (unsigned int n = 0; n < size; ++n) {
120  value << std::setw(2) << static_cast<unsigned>(byte_array[n]);
121  }
122  if (value.str().size() == 0) {
123  value << "00";
124  }
125  return "0x" + value.str();
126 }
127 
128 std::ostream& GenDb::operator<<(std::ostream &out, const Blob &value) {
129  out << bytes_to_hex(value.data(), value.size());
130  return out;
131 }
132 
133 // DbDataValue Printer
134 class DbDataValuePrinter : public boost::static_visitor<std::string> {
135  public:
137  }
138  template <typename IntegerType>
139  std::string operator()(const IntegerType &tint) const {
140  return integerToString(tint);
141  }
142  std::string operator()(const std::string &tstring) const {
143  return tstring;
144  }
145  std::string operator()(const boost::uuids::uuid &tuuid) const {
146  return to_string(tuuid);
147  }
148  std::string operator()(const IpAddress &tipaddr) const {
149  boost::system::error_code ec;
150  std::string ips(tipaddr.to_string(ec));
151  assert(!ec);
152  return ips;
153  }
154  std::string operator()(const GenDb::Blob &tblob) const {
155  std::stringstream value;
156  value << tblob;
157  return value.str();
158  }
159  std::string operator()(const boost::blank &tblank) const {
160  assert(false && "Empty Db Value");
161  return "EMPTY DB VALUE";
162  }
163 };
164 
165 std::string GenDb::DbDataValueToString(const GenDb::DbDataValue &db_value) {
166  DbDataValuePrinter vprinter;
167  return boost::apply_visitor(vprinter, db_value);
168 }
std::string operator()(const boost::uuids::uuid &tuuid) const
Definition: gendb_if.cc:145
void operator()(const boost::uuids::uuid &uuid)
Definition: gendb_if.cc:27
DbDataValueVec rowkey_
Definition: gendb_if.h:198
boost::asio::ip::address IpAddress
Definition: address.h:13
const uint8_t * data() const
Definition: gendb_if.h:60
size_t GetSize() const
Definition: gendb_if.cc:47
boost::uuids::uuid uuid
size_t GetSize() const
Definition: gendb_if.cc:36
std::string ToString() const
Definition: gendb_if.cc:60
bool IsEmpty() const
Definition: gendb_if.h:217
std::string operator()(const IntegerType &tint) const
Definition: gendb_if.cc:139
DbDataValueVec start_
Definition: gendb_if.h:225
void operator()(const std::string &str)
Definition: gendb_if.cc:21
static std::string ToString(Op::type op)
Definition: gendb_if.cc:81
static const std::string integerToString(const NumberType &num)
Definition: string_util.h:19
std::vector< DbDataValue > DbDataValueVec
Definition: gendb_if.h:100
std::string bytes_to_hex(const uint8_t *byte_array, size_t size)
Definition: gendb_if.cc:116
size_t GetSize() const
Definition: gendb_if.cc:30
std::string DbDataValueVecToString(const GenDb::DbDataValueVec &v_db_value)
Definition: gendb_if.cc:102
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
std::string operator()(const GenDb::Blob &tblob) const
Definition: gendb_if.cc:154
NewColVec columns_
Definition: gendb_if.h:199
std::string operator()(const std::string &tstring) const
Definition: gendb_if.cc:142
void operator()(const boost::blank &blank)
Definition: gendb_if.cc:24
size_t size() const
Definition: gendb_if.h:63
std::string DbDataValueToString(const GenDb::DbDataValue &db_value)
Definition: gendb_if.cc:165
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
void operator()(const T &t)
Definition: gendb_if.cc:18
std::string operator()(const boost::blank &tblank) const
Definition: gendb_if.cc:159
std::string operator()(const IpAddress &tipaddr) const
Definition: gendb_if.cc:148