Go to the documentation of this file.
15 #ifndef SERVICE_UTILS_H
16 #define SERVICE_UTILS_H
19 #include <rapidjson/pointer.h>
20 #include <rapidjson/rapidjson.h>
21 #include <sys/sysinfo.h>
22 #include <sys/types.h>
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>
46 static constexpr
const char*
CODEGEN_BIN =
"load_plan_and_gen.sh";
54 if (pthread_sigmask(SIG_BLOCK, &set, NULL) != 0) {
55 perror(
"pthread_sigmask");
60 return std::chrono::duration_cast<std::chrono::milliseconds>(
61 std::chrono::system_clock::now().time_since_epoch())
67 rapidjson::StringBuffer buffer;
69 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
71 return buffer.GetString();
73 rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
74 writer.SetIndent(
' ', indent);
76 return buffer.GetString();
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(),
92 j.AddMember(
"empty",
"empty", j.GetAllocator());
98 j.AddMember(
"primitive_type",
102 rapidjson::Document temporal(rapidjson::kObjectType, &j.GetAllocator());
103 temporal.AddMember(
"timestamp",
"", j.GetAllocator());
104 j.AddMember(
"temporal", temporal, j.GetAllocator());
106 rapidjson::Document temporal(rapidjson::kObjectType, &j.GetAllocator());
107 temporal.AddMember(
"date32",
"", j.GetAllocator());
108 j.AddMember(
"temporal", temporal, j.GetAllocator());
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());
115 rapidjson::Document string(rapidjson::kObjectType, &j.GetAllocator());
116 rapidjson::Document var_char(rapidjson::kObjectType, &j.GetAllocator());
119 string.AddMember(
"var_char", var_char, j.GetAllocator());
120 j.AddMember(
"string",
string, j.GetAllocator());
122 LOG(ERROR) <<
"Unknown property type";
130 rapidjson::Document::AllocatorType* allocator =
nullptr) {
131 rapidjson::Document j;
133 j = rapidjson::Document(rapidjson::kObjectType, allocator);
135 j = rapidjson::Document(rapidjson::kObjectType);
138 LOG(ERROR) <<
"Failed to convert PropertyType to json";
144 if (j.HasMember(
"primitive_type")) {
146 j[
"primitive_type"].GetString());
147 }
else if (j.HasMember(
"string")) {
148 if (j[
"string"].HasMember(
"long_text")) {
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());
158 throw std::invalid_argument(
"Unknown string type: " +
161 }
else if (j.HasMember(
"temporal")) {
162 if (j[
"temporal"].HasMember(
"timestamp")) {
164 }
else if (j[
"temporal"].HasMember(
"date32")) {
167 throw std::invalid_argument(
"Unknown temporal type");
170 LOG(ERROR) <<
"Unknown property type";
179 LOG(ERROR) <<
"Failed to convert json to PropertyType";
185 return boost::filesystem::canonical(
"/proc/self/exe").parent_path();
189 if (json.IsString()) {
190 return json.GetString();
213 #endif // SERVICE_UTILS_H
static PropertyType UInt64()
Definition: types.cc:336
static PropertyType String()
Definition: types.cc:348
static uint16_t GetStringDefaultMaxLength()
Definition: types.cc:103
static PropertyType Empty()
Definition: types.cc:312
std::string get_current_dir()
Definition: service_utils.cc:24
std::pair< uint64_t, uint64_t > get_total_physical_memory_usage()
Definition: service_utils.cc:87
bool to_json(rapidjson::Document &j, const PropertyType &p)
Definition: service_utils.h:90
std::string rapidjson_stringify(const rapidjson::Value &value, int indent=-1)
Definition: service_utils.h:65
void init_cpu_usage_watch()
Definition: service_utils.cc:99
static PropertyType StringView()
Definition: types.cc:351
static PropertyType Varchar(uint16_t max_length)
Definition: types.cc:357
static PropertyType UInt32()
Definition: types.cc:327
static PropertyType Bool()
Definition: types.cc:315
Definition: adj_list.h:23
std::string memory_to_mb_str(uint64_t mem_bytes)
Definition: service_utils.cc:137
int64_t GetCurrentTimeStamp()
Definition: service_utils.h:59
static constexpr const char * CODEGEN_BIN
Definition: service_utils.h:46
size_t human_readable_to_bytes(const std::string &human_readable_bytes)
Definition: service_utils.cc:143
PropertyType StringToPrimitivePropertyType(const std::string &str)
Definition: types.cc:55
static PropertyType Date()
Definition: types.cc:342
static PropertyType Int64()
Definition: types.cc:333
static PropertyType StringMap()
Definition: types.cc:354
std::string toUpper(const std::string str)
Definition: service_utils.h:80
std::string find_codegen_bin()
Definition: service_utils.cc:41
static PropertyType Day()
Definition: types.cc:345
static PropertyType Double()
Definition: types.cc:339
boost::filesystem::path get_current_binary_directory()
Definition: service_utils.h:184
bool IsVarchar() const
Definition: types.cc:259
std::string PrimitivePropertyTypeToString(PropertyType type)
Definition: types.cc:25
static PropertyType UInt8()
Definition: types.cc:318
std::pair< double, double > get_current_cpu_usage()
Definition: service_utils.cc:107
impl::AdditionalTypeInfo additional_type_info
Definition: types.h:102
std::string jsonToString(const rapidjson::Value &json)
Definition: service_utils.h:188
static PropertyType Int32()
Definition: types.cc:324
uint16_t max_length
Definition: types.h:91
static PropertyType UInt16()
Definition: types.cc:321
static PropertyType Float()
Definition: types.cc:330
bool from_json(const rapidjson::Value &j, PropertyType &p)
Definition: service_utils.h:143
void blockSignal(int sig)
Util functions.
Definition: service_utils.h:50