0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SystemVariable.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
28 #include <Common/Compat.h>
29 #include "SystemVariable.h"
30 
31 #include <Common/Logger.h>
32 #include <Common/Serialization.h>
33 #include <Common/String.h>
34 #include <Common/StringExt.h>
35 
36 #include <string>
37 #include <unordered_map>
38 
39 using namespace Hypertable;
40 using namespace std;
41 
42 namespace {
43 
44  struct VariableInfo {
45  int code;
46  const char *text;
47  bool default_value;
48  };
49 
50  VariableInfo variable_info[] = {
51  { SystemVariable::READONLY, "READONLY", false },
52  { 0, 0, false }
53  };
54 
55  typedef std::unordered_map<int, const char *> CodeToStringMap;
56 
57  CodeToStringMap &build_code_to_string_map() {
58  CodeToStringMap *map = new CodeToStringMap();
59  for (int i=0; variable_info[i].text != 0; i++)
60  (*map)[variable_info[i].code] = variable_info[i].text;
61  HT_ASSERT(map->size() == SystemVariable::COUNT);
62  return *map;
63  }
64 
65  CodeToStringMap &code_to_string_map = build_code_to_string_map();
66 
67  typedef std::unordered_map<String, int> StringToCodeMap;
68 
69  StringToCodeMap &build_string_to_code_map() {
70  StringToCodeMap *map = new StringToCodeMap();
71  for (int i=0; variable_info[i].text != 0; i++)
72  (*map)[variable_info[i].text] = variable_info[i].code;
73  HT_ASSERT(map->size() == SystemVariable::COUNT);
74  return *map;
75  }
76 
77  StringToCodeMap &string_to_code_map = build_string_to_code_map();
78 
79  std::vector<bool> build_default_value_vector() {
80  std::vector<bool> vec;
81  for (int i=0; variable_info[i].text != 0; i++)
82  vec.push_back(variable_info[i].default_value);
83  HT_ASSERT(vec.size() == SystemVariable::COUNT);
84  return vec;
85  }
86 
87  std::vector<bool> defaults = build_default_value_vector();
88 
89 } // local namespace
90 
91 
92 const char *SystemVariable::code_to_string(int var_code) {
93  const char *text = code_to_string_map[var_code];
94  HT_ASSERT(text);
95  return text;
96 }
97 
98 int SystemVariable::string_to_code(const string &var_string) {
99  if (string_to_code_map.find(var_string) == string_to_code_map.end())
100  return -1;
101  return string_to_code_map[var_string];
102 }
103 
104 bool SystemVariable::default_value(int var_code) {
105  HT_ASSERT(var_code < (int)defaults.size());
106  return defaults[var_code];
107 }
108 
109 String SystemVariable::specs_to_string(const std::vector<Spec> &specs) {
110  bool first = true;
111  string str;
112  for (auto &spec : specs) {
113  if (!first)
114  str += ",";
115  if (spec.code < SystemVariable::COUNT)
116  str += code_to_string_map[spec.code];
117  else
118  str += String("") + spec.code;
119  str += String("=") + (spec.value ? "true" : "false");
120  first = false;
121  }
122  return str;
123 }
124 
125 
127  return 1;
128 }
129 
131  return 5;
132 }
133 
150 void SystemVariable::Spec::encode_internal(uint8_t **bufp) const {
151  Serialization::encode_i32(bufp, code);
152  Serialization::encode_bool(bufp, value);
153 }
154 
155 void SystemVariable::Spec::decode_internal(uint8_t version, const uint8_t **bufp,
156  size_t *remainp) {
157  code = Serialization::decode_i32(bufp, remainp);
158  value = Serialization::decode_bool(bufp, remainp);
159 }
160 
161 
162 
163 size_t SystemVariable::encoded_length_specs(const std::vector<Spec> &specs) {
164  return 4 + (5 * specs.size());
165 }
166 
167 void SystemVariable::encode_specs(const std::vector<Spec> &specs, uint8_t **bufp) {
168  Serialization::encode_i32(bufp, specs.size());
169  for (auto &spec : specs) {
170  Serialization::encode_i32(bufp, spec.code);
171  Serialization::encode_bool(bufp, spec.value);
172  }
173 }
174 
175 void SystemVariable::decode_specs(std::vector<Spec> &specs,
176  const uint8_t **bufp, size_t *remainp) {
177  Spec spec;
178  int count = Serialization::decode_i32(bufp, remainp);
179  for (int i=0; i<count; i++) {
180  spec.code = Serialization::decode_i32(bufp, remainp);
181  spec.value = Serialization::decode_bool(bufp, remainp);
182  specs.push_back(spec);
183  }
184 }
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
Declarations for SystemVariable.
int32_t code
Variable code.
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
bool default_value(int var_code)
Returns default value for given variable.
STL namespace.
std::string specs_to_string(const std::vector< Spec > &specs)
Returns a textual representation of variable specifications.
uint32_t decode_i32(const uint8_t **bufp, size_t *remainp)
Decode a 32-bit integer in little-endian order.
int string_to_code(const std::string &var_string)
Converts variable string to variable code.
#define HT_ASSERT(_e_)
Definition: Logger.h:396
void decode_specs(std::vector< Spec > &specs, const uint8_t **bufp, size_t *remainp)
Decodes a vector of variable specs.
bool decode_bool(const uint8_t **bufp, size_t *remainp)
Decodes a boolean value from the given buffer.
Definition: Serialization.h:96
Logging routines and macros.
void encode_i32(uint8_t **bufp, uint32_t val)
Encode a 32-bit integer in little-endian order.
Compatibility Macros for C/C++.
void encode_specs(const std::vector< Spec > &specs, uint8_t **bufp)
Encodes a vector of variable specs.
Functions to serialize/deserialize primitives to/from a memory buffer.
size_t encoded_length_internal() const override
Returns internal serialized length.
Holds a variable code and boolean value.
Hypertable definitions
void encode_internal(uint8_t **bufp) const override
Writes serialized representation of object to a buffer.
void encode_bool(uint8_t **bufp, bool bval)
Encodes a boolean into the given buffer.
Definition: Serialization.h:84
void decode_internal(uint8_t version, const uint8_t **bufp, size_t *remainp) override
Reads serialized representation of object from a buffer.
uint8_t encoding_version() const override
Returns encoding version.
A String class based on std::string.
String extensions and helpers: sets, maps, append operators etc.
size_t encoded_length_specs(const std::vector< Spec > &specs)
Returns encoded length of variable specs vector.
const char * code_to_string(int var_code)
Converts variable code to variable string.