Flex  0.17.9
service_utils.h
Go to the documentation of this file.
1 
15 #ifndef SERVICE_UTILS_H
16 #define SERVICE_UTILS_H
17 
18 #include <fcntl.h>
19 #include <rapidjson/pointer.h>
20 #include <rapidjson/rapidjson.h>
21 #include <sys/sysinfo.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <cctype>
25 #include <cstdio>
26 #include <cstdlib>
27 #include <cstring>
28 #include <exception>
29 #include <filesystem>
30 #include <iostream>
31 #include <string>
32 #include <vector>
33 
35 #include "flex/utils/result.h"
36 #include "flex/utils/yaml_utils.h"
37 
38 #include <glog/logging.h>
39 #include <rapidjson/document.h>
40 #include <rapidjson/prettywriter.h>
41 #include <rapidjson/stringbuffer.h>
42 #include <rapidjson/writer.h>
43 #include <boost/filesystem.hpp>
44 
45 namespace gs {
46 
47 static constexpr const char* CODEGEN_BIN = "load_plan_and_gen.sh";
48 
50 inline int64_t GetCurrentTimeStamp() {
51  return std::chrono::duration_cast<std::chrono::milliseconds>(
52  std::chrono::system_clock::now().time_since_epoch())
53  .count();
54 }
55 
56 inline std::string rapidjson_stringify(const rapidjson::Value& value,
57  int indent = -1) {
58  rapidjson::StringBuffer buffer;
59  if (indent == -1) {
60  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
61  value.Accept(writer);
62  return buffer.GetString();
63  } else {
64  rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
65  writer.SetIndent(' ', indent);
66  value.Accept(writer);
67  return buffer.GetString();
68  }
69 }
70 
71 inline std::string toUpper(const std::string str) {
72  std::string upper_str = str;
73  std::transform(upper_str.begin(), upper_str.end(), upper_str.begin(),
74  ::toupper);
75  return upper_str;
76 }
77 
78 // With the help of the following functions, we can serialize and deserialize
79 // by json.get<PropertyType>() and operator <</operator =;
80 // These two functions are inlined to avoid linking library in codegen.
81 inline bool to_json(rapidjson::Document& j, const PropertyType& p) {
82  if (p == PropertyType::Empty()) {
83  j.AddMember("empty", "empty", j.GetAllocator());
84  } else if (p == PropertyType::Bool() || p == PropertyType::UInt8() ||
85  p == PropertyType::UInt16() || p == PropertyType::Int32() ||
86  p == PropertyType::UInt32() || p == PropertyType::Float() ||
87  p == PropertyType::Int64() || p == PropertyType::UInt64() ||
88  p == PropertyType::Double()) {
89  j.AddMember("primitive_type",
91  j.GetAllocator());
92  } else if (p == PropertyType::Date()) {
93  rapidjson::Document temporal(rapidjson::kObjectType, &j.GetAllocator());
94  temporal.AddMember("timestamp", "", j.GetAllocator());
95  j.AddMember("temporal", temporal, j.GetAllocator());
96  } else if (p == PropertyType::Day()) {
97  rapidjson::Document temporal(rapidjson::kObjectType, &j.GetAllocator());
98  temporal.AddMember("date32", "", j.GetAllocator());
99  j.AddMember("temporal", temporal, j.GetAllocator());
100  } else if (p == PropertyType::StringView() ||
101  p == PropertyType::StringMap()) {
102  rapidjson::Document long_text(rapidjson::kObjectType, &j.GetAllocator());
103  long_text.AddMember("long_text", "", j.GetAllocator());
104  j.AddMember("string", long_text, j.GetAllocator());
105  } else if (p.IsVarchar()) {
106  rapidjson::Document string(rapidjson::kObjectType, &j.GetAllocator());
107  rapidjson::Document var_char(rapidjson::kObjectType, &j.GetAllocator());
108  var_char.AddMember("max_length", p.additional_type_info.max_length,
109  j.GetAllocator());
110  string.AddMember("var_char", var_char, j.GetAllocator());
111  j.AddMember("string", string, j.GetAllocator());
112  } else {
113  LOG(ERROR) << "Unknown property type";
114  return false;
115  }
116  return true;
117 }
118 
119 inline rapidjson::Document to_json(
120  const PropertyType& p,
121  rapidjson::Document::AllocatorType* allocator = nullptr) {
122  rapidjson::Document j;
123  if (allocator) {
124  j = rapidjson::Document(rapidjson::kObjectType, allocator);
125  } else {
126  j = rapidjson::Document(rapidjson::kObjectType);
127  }
128  if (!to_json(j, p)) {
129  LOG(ERROR) << "Failed to convert PropertyType to json";
130  }
131  return j;
132 }
133 
134 inline bool from_json(const rapidjson::Value& j, PropertyType& p) {
135  if (j.HasMember("primitive_type")) {
137  j["primitive_type"].GetString());
138  } else if (j.HasMember("string")) {
139  if (j["string"].HasMember("long_text")) {
140  p = PropertyType::String();
141  } else if (j.HasMember("string") && j["string"].HasMember("var_char")) {
142  if (j["string"]["var_char"].HasMember("max_length")) {
144  j["string"]["var_char"]["max_length"].GetInt());
145  } else {
147  }
148  } else {
149  throw std::invalid_argument("Unknown string type: " +
151  }
152  } else if (j.HasMember("temporal")) {
153  if (j["temporal"].HasMember("timestamp")) {
154  p = PropertyType::Date();
155  } else if (j["temporal"].HasMember("date32")) {
156  p = PropertyType::Day();
157  } else {
158  throw std::invalid_argument("Unknown temporal type");
159  }
160  } else {
161  LOG(ERROR) << "Unknown property type";
162  return false;
163  }
164  return true;
165 }
166 
167 inline PropertyType from_json(const rapidjson::Value& j) {
168  PropertyType p;
169  if (!from_json(j, p)) {
170  LOG(ERROR) << "Failed to convert json to PropertyType";
171  }
172  return p;
173 }
174 
175 inline boost::filesystem::path get_current_binary_directory() {
176  return boost::filesystem::canonical("/proc/self/exe").parent_path();
177 }
178 
179 inline std::string jsonToString(const rapidjson::Value& json) {
180  if (json.IsString()) {
181  return json.GetString();
182  } else {
183  return rapidjson_stringify(json);
184  }
185 }
186 
187 // Get the directory of the current executable
188 std::string get_current_dir();
189 
190 std::string find_codegen_bin();
191 
192 std::pair<uint64_t, uint64_t> get_total_physical_memory_usage();
193 
194 void init_cpu_usage_watch();
195 
196 std::pair<double, double> get_current_cpu_usage();
197 
198 std::string memory_to_mb_str(uint64_t mem_bytes);
199 
200 } // namespace gs
201 
202 #endif // SERVICE_UTILS_H
gs::PropertyType::UInt64
static PropertyType UInt64()
Definition: types.cc:302
gs::PropertyType::String
static PropertyType String()
Definition: types.cc:314
gs::PropertyType::Empty
static PropertyType Empty()
Definition: types.cc:278
gs::get_current_dir
std::string get_current_dir()
Definition: service_utils.cc:24
gs::get_total_physical_memory_usage
std::pair< uint64_t, uint64_t > get_total_physical_memory_usage()
Definition: service_utils.cc:87
types.h
gs::to_json
bool to_json(rapidjson::Document &j, const PropertyType &p)
Definition: service_utils.h:81
gs::rapidjson_stringify
std::string rapidjson_stringify(const rapidjson::Value &value, int indent=-1)
Definition: service_utils.h:56
gs::init_cpu_usage_watch
void init_cpu_usage_watch()
Definition: service_utils.cc:99
gs::PropertyType::StringView
static PropertyType StringView()
Definition: types.cc:317
gs::PropertyType::Varchar
static PropertyType Varchar(uint16_t max_length)
Definition: types.cc:323
gs::PropertyType::UInt32
static PropertyType UInt32()
Definition: types.cc:293
gs::PropertyType::Bool
static PropertyType Bool()
Definition: types.cc:281
gs
Definition: adj_list.h:23
gs::memory_to_mb_str
std::string memory_to_mb_str(uint64_t mem_bytes)
Definition: service_utils.cc:137
gs::GetCurrentTimeStamp
int64_t GetCurrentTimeStamp()
Util functions.
Definition: service_utils.h:50
gs::CODEGEN_BIN
static constexpr const char * CODEGEN_BIN
Definition: service_utils.h:47
gs::config_parsing::StringToPrimitivePropertyType
PropertyType StringToPrimitivePropertyType(const std::string &str)
Definition: types.cc:55
gs::PropertyType::Date
static PropertyType Date()
Definition: types.cc:308
gs::PropertyType::STRING_DEFAULT_MAX_LENGTH
static constexpr const uint16_t STRING_DEFAULT_MAX_LENGTH
Definition: types.h:96
gs::PropertyType::Int64
static PropertyType Int64()
Definition: types.cc:299
yaml_utils.h
gs::PropertyType::StringMap
static PropertyType StringMap()
Definition: types.cc:320
gs::toUpper
std::string toUpper(const std::string str)
Definition: service_utils.h:71
gs::find_codegen_bin
std::string find_codegen_bin()
Definition: service_utils.cc:41
result.h
gs::PropertyType::Day
static PropertyType Day()
Definition: types.cc:311
gs::PropertyType::Double
static PropertyType Double()
Definition: types.cc:305
gs::get_current_binary_directory
boost::filesystem::path get_current_binary_directory()
Definition: service_utils.h:175
gs::PropertyType::IsVarchar
bool IsVarchar() const
Definition: types.cc:225
gs::config_parsing::PrimitivePropertyTypeToString
std::string PrimitivePropertyTypeToString(PropertyType type)
Definition: types.cc:25
gs::PropertyType::UInt8
static PropertyType UInt8()
Definition: types.cc:284
gs::get_current_cpu_usage
std::pair< double, double > get_current_cpu_usage()
Definition: service_utils.cc:107
gs::PropertyType::additional_type_info
impl::AdditionalTypeInfo additional_type_info
Definition: types.h:98
gs::jsonToString
std::string jsonToString(const rapidjson::Value &json)
Definition: service_utils.h:179
gs::PropertyType::Int32
static PropertyType Int32()
Definition: types.cc:290
gs::PropertyType
Definition: types.h:95
gs::impl::AdditionalTypeInfo::max_length
uint16_t max_length
Definition: types.h:91
gs::PropertyType::UInt16
static PropertyType UInt16()
Definition: types.cc:287
gs::PropertyType::Float
static PropertyType Float()
Definition: types.cc:296
gs::from_json
bool from_json(const rapidjson::Value &j, PropertyType &p)
Definition: service_utils.h:134