15 #ifndef SERVICE_UTILS_H 
   16 #define SERVICE_UTILS_H 
   19 #include <rapidjson/pointer.h> 
   20 #include <rapidjson/rapidjson.h> 
   22 #include <sys/sysinfo.h> 
   23 #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";
 
   55   if (pthread_sigmask(SIG_BLOCK, &set, NULL) != 0) {
 
   56     perror(
"pthread_sigmask");
 
   61   return std::chrono::duration_cast<std::chrono::milliseconds>(
 
   62              std::chrono::system_clock::now().time_since_epoch())
 
   68   rapidjson::StringBuffer buffer;
 
   70     rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
 
   72     return buffer.GetString();
 
   74     rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
 
   75     writer.SetIndent(
' ', indent);
 
   77     return buffer.GetString();
 
   81 inline std::string 
toUpper(
const std::string str) {
 
   82   std::string upper_str = str;
 
   83   std::transform(upper_str.begin(), upper_str.end(), upper_str.begin(),
 
   93     j.AddMember(
"empty", 
"empty", j.GetAllocator());
 
   99     j.AddMember(
"primitive_type",
 
  103     rapidjson::Document temporal(rapidjson::kObjectType, &j.GetAllocator());
 
  104     temporal.AddMember(
"timestamp", 
"", j.GetAllocator());
 
  105     j.AddMember(
"temporal", temporal, j.GetAllocator());
 
  107     rapidjson::Document temporal(rapidjson::kObjectType, &j.GetAllocator());
 
  108     temporal.AddMember(
"date32", 
"", j.GetAllocator());
 
  109     j.AddMember(
"temporal", temporal, j.GetAllocator());
 
  112     rapidjson::Document long_text(rapidjson::kObjectType, &j.GetAllocator());
 
  113     long_text.AddMember(
"long_text", 
"", j.GetAllocator());
 
  114     j.AddMember(
"string", long_text, j.GetAllocator());
 
  116     rapidjson::Document string(rapidjson::kObjectType, &j.GetAllocator());
 
  117     rapidjson::Document var_char(rapidjson::kObjectType, &j.GetAllocator());
 
  120     string.AddMember(
"var_char", var_char, j.GetAllocator());
 
  121     j.AddMember(
"string", 
string, j.GetAllocator());
 
  123     LOG(ERROR) << 
"Unknown property type";
 
  131     rapidjson::Document::AllocatorType* allocator = 
nullptr) {
 
  132   rapidjson::Document j;
 
  134     j = rapidjson::Document(rapidjson::kObjectType, allocator);
 
  136     j = rapidjson::Document(rapidjson::kObjectType);
 
  139     LOG(ERROR) << 
"Failed to convert PropertyType to json";
 
  145   if (j.HasMember(
"primitive_type")) {
 
  147         j[
"primitive_type"].GetString());
 
  148   } 
else if (j.HasMember(
"string")) {
 
  149     if (j[
"string"].HasMember(
"long_text")) {
 
  151     } 
else if (j.HasMember(
"string") && j[
"string"].HasMember(
"var_char")) {
 
  152       if (j[
"string"][
"var_char"].HasMember(
"max_length")) {
 
  154             j[
"string"][
"var_char"][
"max_length"].GetInt());
 
  159       throw std::invalid_argument(
"Unknown string type: " +
 
  162   } 
else if (j.HasMember(
"temporal")) {
 
  163     if (j[
"temporal"].HasMember(
"timestamp")) {
 
  165     } 
else if (j[
"temporal"].HasMember(
"date32")) {
 
  168       throw std::invalid_argument(
"Unknown temporal type");
 
  171     LOG(ERROR) << 
"Unknown property type";
 
  180     LOG(ERROR) << 
"Failed to convert json to PropertyType";
 
  186   return boost::filesystem::canonical(
"/proc/self/exe").parent_path();
 
  190   if (json.IsString()) {
 
  191     return json.GetString();
 
PropertyType StringToPrimitivePropertyType(const std::string &str)
Definition: types.cc:55
 
std::string PrimitivePropertyTypeToString(PropertyType type)
Definition: types.cc:25
 
Definition: adj_list.h:23
 
std::pair< double, double > get_current_cpu_usage()
Definition: service_utils.cc:107
 
int64_t GetCurrentTimeStamp()
Definition: service_utils.h:60
 
static constexpr const char * CODEGEN_BIN
Definition: service_utils.h:47
 
void blockSignal(int sig)
Util functions.
Definition: service_utils.h:51
 
size_t human_readable_to_bytes(const std::string &human_readable_bytes)
Definition: service_utils.cc:143
 
std::string rapidjson_stringify(const rapidjson::Value &value, int indent=-1)
Definition: service_utils.h:66
 
std::string jsonToString(const rapidjson::Value &json)
Definition: service_utils.h:189
 
bool from_json(const rapidjson::Value &j, PropertyType &p)
Definition: service_utils.h:144
 
bool to_json(rapidjson::Document &j, const PropertyType &p)
Definition: service_utils.h:91
 
void init_cpu_usage_watch()
Definition: service_utils.cc:99
 
std::pair< uint64_t, uint64_t > get_total_physical_memory_usage()
Definition: service_utils.cc:87
 
std::string find_codegen_bin()
Definition: service_utils.cc:41
 
boost::filesystem::path get_current_binary_directory()
Definition: service_utils.h:185
 
std::string memory_to_mb_str(uint64_t mem_bytes)
Definition: service_utils.cc:137
 
std::string toUpper(const std::string str)
Definition: service_utils.h:81
 
std::string get_current_dir()
Definition: service_utils.cc:24
 
static PropertyType String()
Definition: types.cc:348
 
static PropertyType Bool()
Definition: types.cc:315
 
static PropertyType UInt64()
Definition: types.cc:336
 
static PropertyType Int64()
Definition: types.cc:333
 
static PropertyType Day()
Definition: types.cc:345
 
static PropertyType UInt16()
Definition: types.cc:321
 
static PropertyType Double()
Definition: types.cc:339
 
static PropertyType Date()
Definition: types.cc:342
 
static PropertyType Float()
Definition: types.cc:330
 
static PropertyType StringMap()
Definition: types.cc:354
 
static PropertyType Varchar(uint16_t max_length)
Definition: types.cc:357
 
static uint16_t GetStringDefaultMaxLength()
Definition: types.cc:103
 
impl::AdditionalTypeInfo additional_type_info
Definition: types.h:102
 
static PropertyType UInt8()
Definition: types.cc:318
 
static PropertyType StringView()
Definition: types.cc:351
 
static PropertyType Int32()
Definition: types.cc:324
 
bool IsVarchar() const
Definition: types.cc:259
 
static PropertyType UInt32()
Definition: types.cc:327
 
static PropertyType Empty()
Definition: types.cc:312
 
uint16_t max_length
Definition: types.h:91