|
1 | 1 | #include "json_facade.h" |
2 | 2 |
|
3 | | -#include <optional> |
| 3 | +#include <cstring> |
4 | 4 | #include <sstream> |
5 | 5 | #include <stdexcept> |
6 | 6 | #include <string> |
@@ -36,47 +36,47 @@ std::ostream& operator<<(std::ostream& os, const JsonQuery& query) { |
36 | 36 | return os; |
37 | 37 | } |
38 | 38 |
|
39 | | -void RapidJsonFacade::Parse(std::string_view jsonStr) { |
40 | | - rapidjson::ParseResult ok = doc_.Parse(jsonStr.data(), jsonStr.size()); |
41 | | - if (!ok || !doc_.IsObject()) { |
42 | | - throw std::runtime_error("Failed to parse JSON string"); |
43 | | - } |
44 | | - key_value_map_.clear(); |
| 39 | +bool RapidJsonFacade::EvaluateQuery(std::string_view jsonStr, const JsonQuery& query) { |
| 40 | + rapidjson::Document doc; |
| 41 | + rapidjson::ParseResult ok = doc.Parse(jsonStr.data(), jsonStr.size()); |
45 | 42 |
|
46 | | - for (auto it = doc_.MemberBegin(); it != doc_.MemberEnd(); ++it) { |
47 | | - const char* key = it->name.GetString(); |
48 | | - if (it->value.IsString()) { |
49 | | - key_value_map_[key] = it->value.GetString(); |
50 | | - } |
| 43 | + if (!ok || !doc.IsObject()) { |
| 44 | +#ifndef NDEBUG |
| 45 | + throw std::runtime_error("Failed to parse JSON string"); |
| 46 | +#endif |
| 47 | + return false; |
51 | 48 | } |
52 | | -} |
53 | 49 |
|
54 | | -std::optional<std::string_view> RapidJsonFacade::GetString(std::string_view key) const { |
55 | | - auto it = key_value_map_.find(key); |
56 | | - if (it == key_value_map_.end()) { |
57 | | - return std::nullopt; |
58 | | - } |
59 | | - return it->second; |
60 | | -} |
61 | | - |
62 | | -bool JsonQueryDriver::RunQuery(std::string_view buffer, const JsonQuery& query) { |
63 | | - json_facade_->Parse(buffer); |
| 50 | + bool ans = false; |
64 | 51 |
|
65 | 52 | for (const auto& conjunction : query.GetDisjunction().conjunctions) { |
66 | 53 | bool all_predicates_satisfied = true; |
67 | 54 |
|
68 | 55 | for (const auto& predicate : conjunction.predicates) { |
69 | | - auto value = json_facade_->GetString(predicate.key); |
70 | | - if (!value.has_value() || !value.value().contains(predicate.value)) { |
| 56 | + auto itr = doc.FindMember(predicate.key.c_str()); |
| 57 | + |
| 58 | + if (itr == doc.MemberEnd()) { |
| 59 | +#ifndef NDEBUG |
| 60 | + throw std::runtime_error("Key not found: " + predicate.key); |
| 61 | +#endif |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + if (!itr->value.IsString() || !strstr(itr->value.GetString(), predicate.value.c_str())) { |
71 | 66 | all_predicates_satisfied = false; |
72 | 67 | break; |
73 | 68 | } |
74 | 69 | } |
75 | 70 |
|
76 | 71 | if (all_predicates_satisfied) { |
77 | | - return true; |
| 72 | + ans = true; |
| 73 | + break; |
78 | 74 | } |
79 | 75 | } |
80 | 76 |
|
81 | | - return false; |
| 77 | + return ans; |
| 78 | +} |
| 79 | + |
| 80 | +bool JsonQueryDriver::RunQuery(std::string_view buffer, const JsonQuery& query) { |
| 81 | + return json_facade_->EvaluateQuery(buffer, query); |
82 | 82 | } |
0 commit comments