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>
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>
47 static constexpr
const char*
CODEGEN_BIN =
"load_plan_and_gen.sh";
51 return std::chrono::duration_cast<std::chrono::milliseconds>(
52 std::chrono::system_clock::now().time_since_epoch())
58 rapidjson::StringBuffer buffer;
60 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
62 return buffer.GetString();
64 rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
65 writer.SetIndent(
' ', indent);
67 return buffer.GetString();
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(),
83 j.AddMember(
"empty",
"empty", j.GetAllocator());
89 j.AddMember(
"primitive_type",
93 rapidjson::Document temporal(rapidjson::kObjectType, &j.GetAllocator());
94 temporal.AddMember(
"timestamp",
"", j.GetAllocator());
95 j.AddMember(
"temporal", temporal, j.GetAllocator());
97 rapidjson::Document temporal(rapidjson::kObjectType, &j.GetAllocator());
98 temporal.AddMember(
"date32",
"", j.GetAllocator());
99 j.AddMember(
"temporal", temporal, j.GetAllocator());
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());
106 rapidjson::Document string(rapidjson::kObjectType, &j.GetAllocator());
107 rapidjson::Document var_char(rapidjson::kObjectType, &j.GetAllocator());
110 string.AddMember(
"var_char", var_char, j.GetAllocator());
111 j.AddMember(
"string",
string, j.GetAllocator());
113 LOG(ERROR) <<
"Unknown property type";
121 rapidjson::Document::AllocatorType* allocator =
nullptr) {
122 rapidjson::Document j;
124 j = rapidjson::Document(rapidjson::kObjectType, allocator);
126 j = rapidjson::Document(rapidjson::kObjectType);
129 LOG(ERROR) <<
"Failed to convert PropertyType to json";
135 if (j.HasMember(
"primitive_type")) {
137 j[
"primitive_type"].GetString());
138 }
else if (j.HasMember(
"string")) {
139 if (j[
"string"].HasMember(
"long_text")) {
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());
149 throw std::invalid_argument(
"Unknown string type: " +
152 }
else if (j.HasMember(
"temporal")) {
153 if (j[
"temporal"].HasMember(
"timestamp")) {
155 }
else if (j[
"temporal"].HasMember(
"date32")) {
158 throw std::invalid_argument(
"Unknown temporal type");
161 LOG(ERROR) <<
"Unknown property type";
170 LOG(ERROR) <<
"Failed to convert json to PropertyType";
176 return boost::filesystem::canonical(
"/proc/self/exe").parent_path();
180 if (json.IsString()) {
181 return json.GetString();
202 #endif // SERVICE_UTILS_H
static PropertyType UInt64()
Definition: types.cc:302
static PropertyType String()
Definition: types.cc:314
static PropertyType Empty()
Definition: types.cc:278
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:81
std::string rapidjson_stringify(const rapidjson::Value &value, int indent=-1)
Definition: service_utils.h:56
void init_cpu_usage_watch()
Definition: service_utils.cc:99
static PropertyType StringView()
Definition: types.cc:317
static PropertyType Varchar(uint16_t max_length)
Definition: types.cc:323
static PropertyType UInt32()
Definition: types.cc:293
static PropertyType Bool()
Definition: types.cc:281
Definition: adj_list.h:23
std::string memory_to_mb_str(uint64_t mem_bytes)
Definition: service_utils.cc:137
int64_t GetCurrentTimeStamp()
Util functions.
Definition: service_utils.h:50
static constexpr const char * CODEGEN_BIN
Definition: service_utils.h:47
PropertyType StringToPrimitivePropertyType(const std::string &str)
Definition: types.cc:55
static PropertyType Date()
Definition: types.cc:308
static constexpr const uint16_t STRING_DEFAULT_MAX_LENGTH
Definition: types.h:96
static PropertyType Int64()
Definition: types.cc:299
static PropertyType StringMap()
Definition: types.cc:320
std::string toUpper(const std::string str)
Definition: service_utils.h:71
std::string find_codegen_bin()
Definition: service_utils.cc:41
static PropertyType Day()
Definition: types.cc:311
static PropertyType Double()
Definition: types.cc:305
boost::filesystem::path get_current_binary_directory()
Definition: service_utils.h:175
bool IsVarchar() const
Definition: types.cc:225
std::string PrimitivePropertyTypeToString(PropertyType type)
Definition: types.cc:25
static PropertyType UInt8()
Definition: types.cc:284
std::pair< double, double > get_current_cpu_usage()
Definition: service_utils.cc:107
impl::AdditionalTypeInfo additional_type_info
Definition: types.h:98
std::string jsonToString(const rapidjson::Value &json)
Definition: service_utils.h:179
static PropertyType Int32()
Definition: types.cc:290
uint16_t max_length
Definition: types.h:91
static PropertyType UInt16()
Definition: types.cc:287
static PropertyType Float()
Definition: types.cc:296
bool from_json(const rapidjson::Value &j, PropertyType &p)
Definition: service_utils.h:134