JODA  0.13.1 (59b41972)
JSON On-Demand Analysis
DocumentCostHandler.h
Go to the documentation of this file.
1 //
2 // Created by Nico on 08/11/2019.
3 //
4 
5 #ifndef JODA_DOCUMENTCOSTHANDLER_H
6 #define JODA_DOCUMENTCOSTHANDLER_H
7 #include <glob.h>
8 #include <joda/misc/RJFwd.h>
9 #include <rapidjson/encodings.h>
10 #include <rapidjson/rapidjson.h>
11 #include <cstdint>
12 #include <vector>
13 
15  typedef rapidjson::UTF8<>::Ch Ch;
16 
17  public:
19 
20  bool Default() { return true; };
21 
22  bool Null() {
23  theoreticalCost += sizeof(RJValue);
24  return true;
25  };
26 
27  bool Bool(bool b) {
28  theoreticalCost += sizeof(RJValue);
29  return true;
30  };
31 
32  bool Int(int i) {
33  theoreticalCost += sizeof(RJValue);
34  return true;
35  };
36 
37  bool Uint(unsigned int i) {
38  theoreticalCost += sizeof(RJValue);
39  return true;
40  };
41 
42  bool Int64(int64_t int64) {
43  theoreticalCost += sizeof(RJValue);
44  return true;
45  };
46 
47  bool Uint64(uint64_t uint64) {
48  theoreticalCost += sizeof(RJValue);
49  return true;
50  };
51 
52  bool Double(double d) {
53  theoreticalCost += sizeof(RJValue);
54  return true;
55  };
56 
57  bool RawNumber(const Ch *str, rapidjson::SizeType len, bool copy) {
58  return String(str, len, copy);
59  };
60 
61  bool String(const Ch *ch, rapidjson::SizeType len, bool copy) {
62  theoreticalCost += sizeof(RJValue);
63  if (copy && !canBeShortString(len))
64  theoreticalCost += sizeof(RJValue::Ch) * len;
65  return true;
66  };
67 
68  bool StartObject() { return true; };
69 
70  bool Key(const Ch *str, rapidjson::SizeType len, bool copy) {
71  return String(str, len, copy);
72  };
73 
74  bool EndObject(rapidjson::SizeType len) {
75  theoreticalCost += sizeof(RJValue);
76 
77  return true;
78  };
79 
80  bool StartArray() { return true; };
81 
82  bool EndArray(rapidjson::SizeType len) {
83  theoreticalCost += sizeof(RJValue);
84  return true;
85  };
86 
87  void checkDocument(const RJDocument &doc) {
88  doc.Accept(*this);
89  cost += sizeof(RJDocument) - sizeof(RJValue);
90  checkValue(doc);
91  }
92 
93  void checkValue(const RJValue &doc) {
94  switch (doc.GetType()) {
95  case rapidjson::kObjectType: {
96  cost += sizeof(RJValue::Member) *
97  (doc.MemberCapacity() - doc.MemberCount());
98  for (const auto &item : doc.GetObject()) {
99  checkValue(item.name);
100  checkValue(item.value);
101  }
102  } break;
103  case rapidjson::kArrayType: {
104  cost += sizeof(RJValue) * (doc.Capacity() - doc.Size());
105  for (const auto &item : doc.GetArray()) {
106  checkValue(item);
107  }
108  } break;
109  case rapidjson::kStringType: {
110  cost += sizeof(RJValue);
111  auto len = doc.GetStringLength();
112  if (!isShortString(doc)) {
113  cost += sizeof(RJValue::Ch) * len;
114  }
115  } break;
116  default:
117  cost += sizeof(RJValue);
118  }
119  }
120 
121  void addViewPaths(const std::vector<std::string> &paths) {
122  cost += sizeof(std::vector<std::string>) + paths.size();
123  theoreticalCost += sizeof(std::vector<std::string>) + paths.size();
124  for (const auto &path : paths) {
125  cost += path.size() * sizeof(char);
126  theoreticalCost += path.size() * sizeof(char);
127  }
128  }
129 
130  size_t getCost() const { return cost; }
131 
132  size_t getTheoreticalCost() const { return theoreticalCost; }
133 
134  private:
135  size_t cost = 0;
136  size_t theoreticalCost = 0;
137 
138  // Checks against the rules in
139  // https://rapidjson.org/structrapidjson_1_1_generic_value_1_1_short_string.html
140  bool canBeShortString(size_t len) const {
141  if (RAPIDJSON_64BIT == 0 || RAPIDJSON_48BITPOINTER_OPTIMIZATION == 1)
142  return len <= 13;
143  return len <= 21;
144  }
145 
146  bool isShortString(const RJValue &doc) const {
147  return canBeShortString(doc.GetStringLength());
148  }
149 };
150 
151 #endif // JODA_DOCUMENTCOSTHANDLER_H
rapidjson::GenericDocument< RJChar, RJMemoryPoolAlloc, RJBaseAlloc > RJDocument
Definition: RJFwd.h:28
rapidjson::GenericValue< RJChar, RJMemoryPoolAlloc > RJValue
Definition: RJFwd.h:29
Definition: DocumentCostHandler.h:14
bool StartArray()
Definition: DocumentCostHandler.h:80
bool String(const Ch *ch, rapidjson::SizeType len, bool copy)
Definition: DocumentCostHandler.h:61
void addViewPaths(const std::vector< std::string > &paths)
Definition: DocumentCostHandler.h:121
void checkValue(const RJValue &doc)
Definition: DocumentCostHandler.h:93
bool Uint(unsigned int i)
Definition: DocumentCostHandler.h:37
bool Int(int i)
Definition: DocumentCostHandler.h:32
bool Null()
Definition: DocumentCostHandler.h:22
bool EndArray(rapidjson::SizeType len)
Definition: DocumentCostHandler.h:82
size_t getTheoreticalCost() const
Definition: DocumentCostHandler.h:132
bool Int64(int64_t int64)
Definition: DocumentCostHandler.h:42
size_t getCost() const
Definition: DocumentCostHandler.h:130
bool Uint64(uint64_t uint64)
Definition: DocumentCostHandler.h:47
DocumentCostHandler()
Definition: DocumentCostHandler.h:18
bool Default()
Definition: DocumentCostHandler.h:20
void checkDocument(const RJDocument &doc)
Definition: DocumentCostHandler.h:87
bool Double(double d)
Definition: DocumentCostHandler.h:52
bool StartObject()
Definition: DocumentCostHandler.h:68
bool EndObject(rapidjson::SizeType len)
Definition: DocumentCostHandler.h:74
bool RawNumber(const Ch *str, rapidjson::SizeType len, bool copy)
Definition: DocumentCostHandler.h:57
bool Bool(bool b)
Definition: DocumentCostHandler.h:27
bool Key(const Ch *str, rapidjson::SizeType len, bool copy)
Definition: DocumentCostHandler.h:70