Flex  0.17.9
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 inline int64_t GetCurrentTimeStamp() {
50  return std::chrono::duration_cast<std::chrono::milliseconds>(
51  std::chrono::system_clock::now().time_since_epoch())
52  .count();
53 }
54 
55 inline std::string rapidjson_stringify(const rapidjson::Value& value,
56  int indent = -1) {
57  rapidjson::StringBuffer buffer;
58  if (indent == -1) {
59  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
60  value.Accept(writer);
61  return buffer.GetString();
62  } else {
63  rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
64  writer.SetIndent(' ', indent);
65  value.Accept(writer);
66  return buffer.GetString();
67  }
68 }
69 
70 inline std::string toUpper(const std::string str) {
71  std::string upper_str = str;
72  std::transform(upper_str.begin(), upper_str.end(), upper_str.begin(),
73  ::toupper);
74  return upper_str;
75 }
76 
77 // With the help of the following functions, we can serialize and deserialize
78 // by json.get<PropertyType>() and operator <</operator =;
79 // These two functions are inlined to avoid linking library in codegen.
80 inline bool to_json(rapidjson::Document& j, const PropertyType& p) {
81  if (p == PropertyType::Empty()) {
82  j.AddMember("empty", "empty", j.GetAllocator());
83  } else if (p == PropertyType::Bool() || p == PropertyType::UInt8() ||
84  p == PropertyType::UInt16() || p == PropertyType::Int32() ||
85  p == PropertyType::UInt32() || p == PropertyType::Float() ||
86  p == PropertyType::Int64() || p == PropertyType::UInt64() ||
87  p == PropertyType::Double()) {
88  j.AddMember("primitive_type",
90  j.GetAllocator());
91  } else if (p == PropertyType::Date()) {
92  rapidjson::Document temporal(rapidjson::kObjectType, &j.GetAllocator());
93  temporal.AddMember("timestamp", "", j.GetAllocator());
94  j.AddMember("temporal", temporal, j.GetAllocator());
95  } else if (p == PropertyType::Day()) {
96  rapidjson::Document temporal(rapidjson::kObjectType, &j.GetAllocator());
97  temporal.AddMember("date32", "", j.GetAllocator());
98  j.AddMember("temporal", temporal, j.GetAllocator());
99  } else if (p == PropertyType::StringView() ||
100  p == PropertyType::StringMap()) {
101  rapidjson::Document long_text(rapidjson::kObjectType, &j.GetAllocator());
102  long_text.AddMember("long_text", "", j.GetAllocator());
103  j.AddMember("string", long_text, j.GetAllocator());
104  } else if (p.IsVarchar()) {
105  rapidjson::Document string(rapidjson::kObjectType, &j.GetAllocator());
106  rapidjson::Document var_char(rapidjson::kObjectType, &j.GetAllocator());
107  var_char.AddMember("max_length", p.additional_type_info.max_length,
108  j.GetAllocator());
109  string.AddMember("var_char", var_char, j.GetAllocator());
110  j.AddMember("string", string, j.GetAllocator());
111  } else {
112  LOG(ERROR) << "Unknown property type";
113  return false;
114  }
115  return true;
116 }
117 
118 inline rapidjson::Document to_json(
119  const PropertyType& p,
120  rapidjson::Document::AllocatorType* allocator = nullptr) {
121  rapidjson::Document j;
122  if (allocator) {
123  j = rapidjson::Document(rapidjson::kObjectType, allocator);
124  } else {
125  j = rapidjson::Document(rapidjson::kObjectType);
126  }
127  if (!to_json(j, p)) {
128  LOG(ERROR) << "Failed to convert PropertyType to json";
129  }
130  return j;
131 }
132 
133 inline bool from_json(const rapidjson::Value& j, PropertyType& p) {
134  if (j.HasMember("primitive_type")) {
136  j["primitive_type"].GetString());
137  } else if (j.HasMember("string")) {
138  if (j["string"].HasMember("long_text")) {
139  p = PropertyType::String();
140  } else if (j.HasMember("string") && j["string"].HasMember("var_char")) {
141  if (j["string"]["var_char"].HasMember("max_length")) {
143  j["string"]["var_char"]["max_length"].GetInt());
144  } else {
146  }
147  } else {
148  throw std::invalid_argument("Unknown string type: " +
150  }
151  } else if (j.HasMember("temporal")) {
152  if (j["temporal"].HasMember("timestamp")) {
153  p = PropertyType::Date();
154  } else if (j["temporal"].HasMember("date32")) {
155  p = PropertyType::Day();
156  } else {
157  throw std::invalid_argument("Unknown temporal type");
158  }
159  } else {
160  LOG(ERROR) << "Unknown property type";
161  return false;
162  }
163  return true;
164 }
165 
166 inline PropertyType from_json(const rapidjson::Value& j) {
167  PropertyType p;
168  if (!from_json(j, p)) {
169  LOG(ERROR) << "Failed to convert json to PropertyType";
170  }
171  return p;
172 }
173 
174 inline boost::filesystem::path get_current_binary_directory() {
175  return boost::filesystem::canonical("/proc/self/exe").parent_path();
176 }
177 
178 inline std::string jsonToString(const rapidjson::Value& json) {
179  if (json.IsString()) {
180  return json.GetString();
181  } else {
182  return rapidjson_stringify(json);
183  }
184 }
185 
186 // Get the directory of the current executable
187 std::string get_current_dir();
188 
189 std::string find_codegen_bin();
190 
191 std::pair<uint64_t, uint64_t> get_total_physical_memory_usage();
192 
193 void init_cpu_usage_watch();
194 
195 std::pair<double, double> get_current_cpu_usage();
196 
197 std::string memory_to_mb_str(uint64_t mem_bytes);
198 
199 } // namespace gs
200 
201 #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:80
gs::rapidjson_stringify
std::string rapidjson_stringify(const rapidjson::Value &value, int indent=-1)
Definition: service_utils.h:55
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:49
gs::CODEGEN_BIN
static constexpr const char * CODEGEN_BIN
Definition: service_utils.h:46
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:70
gs::find_codegen_bin
std::string find_codegen_bin()
Definition: service_utils.cc:41
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:174
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:178
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:133