OoOJoin  0.0.1
The next generation of out-of-order join operator
ConfigMap.hpp
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef _UTILS_CONFIGMAP_HPP_
4 #define _UTILS_CONFIGMAP_HPP_
5 #include <map>
6 #include <vector>
7 #include <memory>
8 #include <string>
9 #include <fstream>
10 #include <iostream>
11 #include <sstream>
12 #include <assert.h>
13 #include <Utils/IntelliLog.h>
22 using namespace std;
23 namespace INTELLI {
30 class ConfigMap {
31  protected:
32  std::map<std::string, uint64_t> u64Map;
33  std::map<std::string, int64_t> i64Map;
34  std::map<std::string, double> doubleMap;
35  std::map<std::string, std::string> strMap;
36 
37  void spilt(const std::string s, const std::string &c, vector<std::string> &v) {
38  std::string::size_type pos1, pos2;
39  pos2 = s.find(c);
40  pos1 = 0;
41  while (std::string::npos != pos2) {
42  v.push_back(s.substr(pos1, pos2 - pos1));
43 
44  pos1 = pos2 + c.size();
45  pos2 = s.find(c, pos1);
46  }
47  if (pos1 != s.length())
48  v.push_back(s.substr(pos1));
49  }
50  public:
51  ConfigMap() {}
52  ~ConfigMap() {}
58  void edit(std::string key, uint64_t value) {
59  u64Map[key] = value;
60  }
66  void edit(std::string key, int64_t value) {
67  i64Map[key] = value;
68  }
74  void edit(std::string key, double value) {
75  doubleMap[key] = value;
76  }
82  void edit(std::string key, std::string value) {
83  strMap[key] = value;
84  }
90  bool existU64(std::string key) {
91  return (u64Map.count(key) == 1);
92  }
98  bool existI64(std::string key) {
99  return (i64Map.count(key) == 1);
100  }
106  bool existDouble(std::string key) {
107  return (doubleMap.count(key) == 1);
108  }
114  bool existString(std::string key) {
115  return (strMap.count(key) == 1);
116  }
122  bool exist(std::string key) {
123  return existU64(key) || existI64(key) || existDouble(key) || existString(key);
124  }
131  uint64_t getU64(std::string key) {
132  return u64Map.at(key);
133  }
140  int64_t getI64(std::string key) {
141  return i64Map.at(key);
142  }
149  double getDouble(std::string key) {
150  return doubleMap.at(key);
151  }
158  std::string getString(std::string key) {
159  return strMap.at(key);
160  }
167  std::string toString(std::string separator = "\t", std::string newLine = "\n") {
168  std::string str = "key" + separator + "value" + separator + "type" + newLine;
169  for (auto &iter : u64Map) {
170  std::string col = iter.first + separator + to_string(iter.second) + separator + "U64" + newLine;
171  str += col;
172  }
173  for (auto &iter : i64Map) {
174  std::string col = iter.first + separator + to_string(iter.second) + separator + "I64" + newLine;
175  str += col;
176  }
177  for (auto &iter : doubleMap) {
178  std::string col = iter.first + separator + to_string(iter.second) + separator + "Double" + newLine;
179  str += col;
180  }
181  for (auto &iter : strMap) {
182  std::string col = iter.first + separator + (iter.second) + separator + "String" + newLine;
183  str += col;
184  }
185  return str;
186  }
191  void cloneInto(ConfigMap &dest) {
192  for (auto &iter : u64Map) {
193  dest.edit(iter.first, (uint64_t) iter.second);
194  }
195  for (auto &iter : i64Map) {
196  dest.edit(iter.first, (int64_t) iter.second);
197  }
198  for (auto &iter : doubleMap) {
199  dest.edit(iter.first, (double) iter.second);
200  }
201  for (auto &iter : strMap) {
202  dest.edit(iter.first, (std::string) iter.second);
203  }
204  }
212  bool toFile(std::string fname, std::string separator = ",", std::string newLine = "\n") {
213  ofstream of;
214  of.open(fname);
215  if (of.fail()) {
216  return false;
217  }
218  of << toString(separator, newLine);
219  of.close();
220  return true;
221  }
229  bool fromFile(std::string fname, std::string separator = ",", std::string newLine = "\n") {
230  ifstream ins;
231  ins.open(fname);
232  assert(separator.data());
233  assert(newLine.data());
234  if (ins.fail()) {
235  return false;
236  }
237  std::string readStr;
238  // cout << "read file\r\n";
239  while (std::getline(ins, readStr, newLine.data()[0])) {
240  vector<std::string> cols;
241  // readStr.erase(readStr.size()-1);
242  spilt(readStr, separator, cols);
243  // cout<<readStr+"\n";
244  if (cols.size() >= 3) {
245  istringstream iss(cols[1]);
246  if (cols[2] == "U64") {
247  uint64_t value;
248  iss >> value;
249  edit(cols[0], (uint64_t) value);
250  } else if (cols[2] == "I64") {
251  int64_t value;
252  iss >> value;
253  edit(cols[0], (int64_t) value);
254  } else if (cols[2] == "Double") {
255  double value;
256  iss >> value;
257  edit(cols[0], (double) value);
258  } else if (cols[2] == "String") {
259  edit(cols[0], (std::string) cols[1]);
260  }
261  }
262  }
263  //ins>>readStr;
264  ins.close();
265  return true;
266  }
267 
275  int64_t tryI64(string key, int64_t defaultValue = 0, bool showWarning = false) {
276  int64_t ru = defaultValue;
277  if (this->existI64(key)) {
278  ru = this->getI64(key);
279  // INTELLI_INFO(key + " = " + to_string(ru));
280  } else {
281  if (showWarning) {
282  INTELLI_WARNING("Leaving " + key + " as blank, will use " + to_string(defaultValue) + " instead");
283  }
284  // WM_WARNNING("Leaving " + key + " as blank, will use " + to_string(defaultValue) + " instead");
285  }
286  return ru;
287  }
295  uint64_t tryU64(string key, uint64_t defaultValue = 0, bool showWarning = false) {
296  uint64_t ru = defaultValue;
297  if (this->existU64(key)) {
298  ru = this->getU64(key);
299  // INTELLI_INFO(key + " = " + to_string(ru));
300  } else {
301  if (showWarning) {
302  INTELLI_WARNING("Leaving " + key + " as blank, will use " + to_string(defaultValue) + " instead");
303  }
304  // WM_WARNNING("Leaving " + key + " as blank, will use " + to_string(defaultValue) + " instead");
305  }
306  return ru;
307  }
308 
316  double tryDouble(string key, double defaultValue = 0, bool showWarning = false) {
317  double ru = defaultValue;
318  if (this->existDouble(key)) {
319  ru = this->getDouble(key);
320  // INTELLI_INFO(key + " = " + to_string(ru));
321  } else {
322  if (showWarning) {
323  INTELLI_WARNING("Leaving " + key + " as blank, will use " + to_string(defaultValue) + " instead");
324  }
325  // WM_WARNNING("Leaving " + key + " as blank, will use " + to_string(defaultValue) + " instead");
326  }
327  return ru;
328  }
336  string tryString(string key, string defaultValue = "", bool showWarning = false) {
337  string ru = defaultValue;
338  if (this->existString(key)) {
339  ru = this->getString(key);
340  //INTELLI_INFO(key + " = " + (ru));
341  } else {
342  if (showWarning) {
343  INTELLI_WARNING("Leaving " + key + " as blank, will use " + (defaultValue) + " instead");
344  }
345  // WM_WARNNING("Leaving " + key + " as blank, will use " + (defaultValue) + " instead");
346  }
347  return ru;
348  }
349 
350 };
356 typedef std::shared_ptr<ConfigMap> ConfigMapPtr;
362 #define newConfigMap make_shared<INTELLI::ConfigMap>
363 }
364 
371 #endif
The unified map structure to store configurations in a key-value style.
Definition: ConfigMap.hpp:30
std::shared_ptr< ConfigMap > ConfigMapPtr
The class to describe a shared pointer to ConfigMap.
Definition: ConfigMap.hpp:356
void edit(std::string key, std::string value)
Edit the config map. If not exit the config, will create new, or will overwrite.
Definition: ConfigMap.hpp:82
std::string toString(std::string separator="\t", std::string newLine="\n")
convert the whole map to std::string and retuen
Definition: ConfigMap.hpp:167
bool fromFile(std::string fname, std::string separator=",", std::string newLine="\n")
update the whole map from file
Definition: ConfigMap.hpp:229
bool existI64(std::string key)
To detect whether the key exists and related to a I64.
Definition: ConfigMap.hpp:98
void edit(std::string key, int64_t value)
Edit the config map. If not exit the config, will create new, or will overwrite.
Definition: ConfigMap.hpp:66
bool toFile(std::string fname, std::string separator=",", std::string newLine="\n")
convert the whole map to file
Definition: ConfigMap.hpp:212
std::string getString(std::string key)
To get a std::string value by key.
Definition: ConfigMap.hpp:158
double getDouble(std::string key)
To get a double value by key.
Definition: ConfigMap.hpp:149
void cloneInto(ConfigMap &dest)
clone this config into destination
Definition: ConfigMap.hpp:191
int64_t tryI64(string key, int64_t defaultValue=0, bool showWarning=false)
Try to get an I64 from config map, if not exist, use default value instead.
Definition: ConfigMap.hpp:275
void edit(std::string key, double value)
Edit the config map. If not exit the config, will create new, or will overwrite.
Definition: ConfigMap.hpp:74
uint64_t getU64(std::string key)
To get a U64 value by key.
Definition: ConfigMap.hpp:131
bool existU64(std::string key)
To detect whether the key exists and related to a U64.
Definition: ConfigMap.hpp:90
int64_t getI64(std::string key)
To get a I64 value by key.
Definition: ConfigMap.hpp:140
uint64_t tryU64(string key, uint64_t defaultValue=0, bool showWarning=false)
Try to get an U64 from config map, if not exist, use default value instead.
Definition: ConfigMap.hpp:295
bool existDouble(std::string key)
To detect whether the key exists and related to a double.
Definition: ConfigMap.hpp:106
bool existString(std::string key)
To detect whether the key exists and related to a std::string.
Definition: ConfigMap.hpp:114
double tryDouble(string key, double defaultValue=0, bool showWarning=false)
Try to get a double from config map, if not exist, use default value instead.
Definition: ConfigMap.hpp:316
string tryString(string key, string defaultValue="", bool showWarning=false)
Try to get an String from config map, if not exist, use default value instead.
Definition: ConfigMap.hpp:336
bool exist(std::string key)
To detect whether the key exists.
Definition: ConfigMap.hpp:122
void edit(std::string key, uint64_t value)
Edit the config map. If not exit the config, will create new, or will overwrite.
Definition: ConfigMap.hpp:58