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