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/yaml_utils.h"
36 
37 #include <glog/logging.h>
38 #include <rapidjson/document.h>
39 #include <rapidjson/prettywriter.h>
40 #include <rapidjson/stringbuffer.h>
41 #include <rapidjson/writer.h>
42 #include <boost/filesystem.hpp>
43 
44 namespace gs {
45 
46 static constexpr const char* CODEGEN_BIN = "load_plan_and_gen.sh";
47 
49 
50 inline void blockSignal(int sig) {
51  sigset_t set;
52  sigemptyset(&set);
53  sigaddset(&set, sig);
54  if (pthread_sigmask(SIG_BLOCK, &set, NULL) != 0) {
55  perror("pthread_sigmask");
56  }
57 }
58 
59 inline int64_t GetCurrentTimeStamp() {
60  return std::chrono::duration_cast<std::chrono::milliseconds>(
61  std::chrono::system_clock::now().time_since_epoch())
62  .count();
63 }
64 
65 inline std::string rapidjson_stringify(const rapidjson::Value& value,
66  int indent = -1) {
67  rapidjson::StringBuffer buffer;
68  if (indent == -1) {
69  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
70  value.Accept(writer);
71  return buffer.GetString();
72  } else {
73  rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
74  writer.SetIndent(' ', indent);
75  value.Accept(writer);
76  return buffer.GetString();
77  }
78 }
79 
80 inline std::string toUpper(const std::string str) {
81  std::string upper_str = str;
82  std::transform(upper_str.begin(), upper_str.end(), upper_str.begin(),
83  ::toupper);
84  return upper_str;
85 }
86 
87 // With the help of the following functions, we can serialize and deserialize
88 // by json.get<PropertyType>() and operator <</operator =;
89 // These two functions are inlined to avoid linking library in codegen.
90 inline bool to_json(rapidjson::Document& j, const PropertyType& p) {
91  if (p == PropertyType::Empty()) {
92  j.AddMember("empty", "empty", j.GetAllocator());
93  } else if (p == PropertyType::Bool() || p == PropertyType::UInt8() ||
94  p == PropertyType::UInt16() || p == PropertyType::Int32() ||
95  p == PropertyType::UInt32() || p == PropertyType::Float() ||
96  p == PropertyType::Int64() || p == PropertyType::UInt64() ||
97  p == PropertyType::Double()) {
98  j.AddMember("primitive_type",
100  j.GetAllocator());
101  } else if (p == PropertyType::Date()) {
102  rapidjson::Document temporal(rapidjson::kObjectType, &j.GetAllocator());
103  temporal.AddMember("timestamp", "", j.GetAllocator());
104  j.AddMember("temporal", temporal, j.GetAllocator());
105  } else if (p == PropertyType::Day()) {
106  rapidjson::Document temporal(rapidjson::kObjectType, &j.GetAllocator());
107  temporal.AddMember("date32", "", j.GetAllocator());
108  j.AddMember("temporal", temporal, j.GetAllocator());
109  } else if (p == PropertyType::StringView() ||
110  p == PropertyType::StringMap()) {
111  rapidjson::Document long_text(rapidjson::kObjectType, &j.GetAllocator());
112  long_text.AddMember("long_text", "", j.GetAllocator());
113  j.AddMember("string", long_text, j.GetAllocator());
114  } else if (p.IsVarchar()) {
115  rapidjson::Document string(rapidjson::kObjectType, &j.GetAllocator());
116  rapidjson::Document var_char(rapidjson::kObjectType, &j.GetAllocator());
117  var_char.AddMember("max_length", p.additional_type_info.max_length,
118  j.GetAllocator());
119  string.AddMember("var_char", var_char, j.GetAllocator());
120  j.AddMember("string", string, j.GetAllocator());
121  } else {
122  LOG(ERROR) << "Unknown property type";
123  return false;
124  }
125  return true;
126 }
127 
128 inline rapidjson::Document to_json(
129  const PropertyType& p,
130  rapidjson::Document::AllocatorType* allocator = nullptr) {
131  rapidjson::Document j;
132  if (allocator) {
133  j = rapidjson::Document(rapidjson::kObjectType, allocator);
134  } else {
135  j = rapidjson::Document(rapidjson::kObjectType);
136  }
137  if (!to_json(j, p)) {
138  LOG(ERROR) << "Failed to convert PropertyType to json";
139  }
140  return j;
141 }
142 
143 inline bool from_json(const rapidjson::Value& j, PropertyType& p) {
144  if (j.HasMember("primitive_type")) {
146  j["primitive_type"].GetString());
147  } else if (j.HasMember("string")) {
148  if (j["string"].HasMember("long_text")) {
149  p = PropertyType::String();
150  } else if (j.HasMember("string") && j["string"].HasMember("var_char")) {
151  if (j["string"]["var_char"].HasMember("max_length")) {
153  j["string"]["var_char"]["max_length"].GetInt());
154  } else {
156  }
157  } else {
158  throw std::invalid_argument("Unknown string type: " +
160  }
161  } else if (j.HasMember("temporal")) {
162  if (j["temporal"].HasMember("timestamp")) {
163  p = PropertyType::Date();
164  } else if (j["temporal"].HasMember("date32")) {
165  p = PropertyType::Day();
166  } else {
167  throw std::invalid_argument("Unknown temporal type");
168  }
169  } else {
170  LOG(ERROR) << "Unknown property type";
171  return false;
172  }
173  return true;
174 }
175 
176 inline PropertyType from_json(const rapidjson::Value& j) {
177  PropertyType p;
178  if (!from_json(j, p)) {
179  LOG(ERROR) << "Failed to convert json to PropertyType";
180  }
181  return p;
182 }
183 
184 inline boost::filesystem::path get_current_binary_directory() {
185  return boost::filesystem::canonical("/proc/self/exe").parent_path();
186 }
187 
188 inline std::string jsonToString(const rapidjson::Value& json) {
189  if (json.IsString()) {
190  return json.GetString();
191  } else {
192  return rapidjson_stringify(json);
193  }
194 }
195 
196 // Get the directory of the current executable
197 std::string get_current_dir();
198 
199 std::string find_codegen_bin();
200 
201 std::pair<uint64_t, uint64_t> get_total_physical_memory_usage();
202 
203 void init_cpu_usage_watch();
204 
205 std::pair<double, double> get_current_cpu_usage();
206 
207 std::string memory_to_mb_str(uint64_t mem_bytes);
208 
209 size_t human_readable_to_bytes(const std::string& human_readable);
210 
211 } // namespace gs
212 
213 #endif // SERVICE_UTILS_H
gs::PropertyType::UInt64
static PropertyType UInt64()
Definition: types.cc:336
gs::PropertyType::String
static PropertyType String()
Definition: types.cc:348
gs::PropertyType::GetStringDefaultMaxLength
static uint16_t GetStringDefaultMaxLength()
Definition: types.cc:103
gs::PropertyType::Empty
static PropertyType Empty()
Definition: types.cc:312
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:90
gs::rapidjson_stringify
std::string rapidjson_stringify(const rapidjson::Value &value, int indent=-1)
Definition: service_utils.h:65
gs::init_cpu_usage_watch
void init_cpu_usage_watch()
Definition: service_utils.cc:99
gs::PropertyType::StringView
static PropertyType StringView()
Definition: types.cc:351
gs::PropertyType::Varchar
static PropertyType Varchar(uint16_t max_length)
Definition: types.cc:357
gs::PropertyType::UInt32
static PropertyType UInt32()
Definition: types.cc:327
gs::PropertyType::Bool
static PropertyType Bool()
Definition: types.cc:315
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()
Definition: service_utils.h:59
gs::CODEGEN_BIN
static constexpr const char * CODEGEN_BIN
Definition: service_utils.h:46
gs::human_readable_to_bytes
size_t human_readable_to_bytes(const std::string &human_readable_bytes)
Definition: service_utils.cc:143
gs::config_parsing::StringToPrimitivePropertyType
PropertyType StringToPrimitivePropertyType(const std::string &str)
Definition: types.cc:55
gs::PropertyType::Date
static PropertyType Date()
Definition: types.cc:342
gs::PropertyType::Int64
static PropertyType Int64()
Definition: types.cc:333
yaml_utils.h
gs::PropertyType::StringMap
static PropertyType StringMap()
Definition: types.cc:354
gs::toUpper
std::string toUpper(const std::string str)
Definition: service_utils.h:80
gs::find_codegen_bin
std::string find_codegen_bin()
Definition: service_utils.cc:41
gs::PropertyType::Day
static PropertyType Day()
Definition: types.cc:345
gs::PropertyType::Double
static PropertyType Double()
Definition: types.cc:339
gs::get_current_binary_directory
boost::filesystem::path get_current_binary_directory()
Definition: service_utils.h:184
gs::PropertyType::IsVarchar
bool IsVarchar() const
Definition: types.cc:259
gs::config_parsing::PrimitivePropertyTypeToString
std::string PrimitivePropertyTypeToString(PropertyType type)
Definition: types.cc:25
gs::PropertyType::UInt8
static PropertyType UInt8()
Definition: types.cc:318
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:102
gs::jsonToString
std::string jsonToString(const rapidjson::Value &json)
Definition: service_utils.h:188
gs::PropertyType::Int32
static PropertyType Int32()
Definition: types.cc:324
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:321
gs::PropertyType::Float
static PropertyType Float()
Definition: types.cc:330
gs::from_json
bool from_json(const rapidjson::Value &j, PropertyType &p)
Definition: service_utils.h:143
gs::blockSignal
void blockSignal(int sig)
Util functions.
Definition: service_utils.h:50