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