1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// cs/net/proto/db/query_view.gpt.hh
#ifndef CS_NET_PROTO_DB_QUERY_VIEW_GPT_HH
#define CS_NET_PROTO_DB_QUERY_VIEW_GPT_HH
#include <algorithm>
#include <functional>
#include <memory>
#include <sstream>
#include <string>
#include <type_traits>
#include <vector>
#include "cs/log.hh"
#include "cs/net/json/object.hh"
#include "cs/net/json/parsers.hh"
#include "cs/net/proto/db/client.gpt.hh"
#include "cs/net/proto/db/field_path_builder.gpt.hh"
#include "cs/net/proto/db/protos/database.proto.hh"
#include "cs/net/proto/db/query_helpers.gpt.hh"
#include "cs/result.hh"
namespace cs::net::proto::db {
template <typename T>
class QueryView {
typedef std::vector<T> Ts;
public:
// Default constructor for ResultOr compatibility
QueryView() : collection_(""), materialized_(false) {}
// Constructor for remote query (takes collection name
// and non-null client). Client must not be null for
// remote queries.
explicit QueryView(
const std::string& collection,
std::shared_ptr<IDatabaseClient> client)
: collection_(collection),
materialized_(false),
client_(std::move(client)) {}
// Constructor for in-memory query (takes pre-loaded
// values) Useful for testing or when you already have
// values in memory. Values are pre-loaded but steps need
// to be applied during materialize()
explicit QueryView(const Ts& values)
: collection_(""),
materialized_(false),
materialized_values_(values) {}
std::string query_string() {
return BuildQueryString(proto_steps_);
}
ResultOr<Ts> values() {
OK_OR_RET(this->materialize());
return materialized_values_;
}
QueryView<T> where(
const cs::net::proto::database::Condition&
condition) {
cs::net::proto::database::Predicate predicate;
predicate.condition = condition.Serialize(0);
cs::net::proto::database::QueryStep step;
step.type = "WHERE";
step.predicate = predicate;
proto_steps_.push_back(step);
return *this;
}
template <typename FieldPathType>
QueryView<T> order_by(
FieldPathType field_path,
const std::string& direction = "desc") {
cs::net::proto::database::OrderBy order_by;
order_by.field_path = GetFieldPathOrConvert(
std::forward<FieldPathType>(field_path));
order_by.direction = direction;
cs::net::proto::database::QueryStep step;
step.type = "ORDER_BY";
step.order_by = order_by;
proto_steps_.push_back(step);
return *this;
}
QueryView<T> limit(unsigned int n) {
cs::net::proto::database::QueryStep step;
step.type = "LIMIT";
step.limit = static_cast<int>(n);
proto_steps_.push_back(step);
return *this;
}
cs::ResultOr<T> first() {
limit(1);
OK_OR_RET(this->materialize());
if (materialized_values_.empty()) {
return TRACE(cs::NotFoundError("No values found"));
}
if (materialized_values_.size() > 1) {
return TRACE(
cs::InvalidArgument("More than one value found"));
}
return materialized_values_[0];
}
cs::ResultOr<bool> any() {
OK_OR_RET(this->materialize());
return !materialized_values_.empty();
}
cs::Result materialize() {
if (materialized_) {
return cs::Ok();
}
// For in-memory queries (collection_ empty), apply
// steps locally
if (collection_.empty()) {
// Apply WHERE filters
std::vector<cs::net::proto::database::Condition>
where_conditions;
for (const auto& step : proto_steps_) {
if (step.type == "WHERE") {
cs::net::proto::database::Condition condition;
SET_OR_RET(
condition,
condition.Parse(step.predicate.condition));
where_conditions.push_back(condition);
}
}
// Filter records
if (!where_conditions.empty()) {
Ts filtered;
for (const auto& value : materialized_values_) {
std::string json = value.Serialize(0);
SET_OR_RET(
auto json_obj,
cs::net::json::parsers::ParseObject(json));
bool matches = true;
for (const auto& condition : where_conditions) {
SET_OR_RET(bool matches_condition,
EvaluateConditionLocal(json_obj,
condition));
if (!matches_condition) {
matches = false;
break;
}
}
if (matches) {
filtered.push_back(value);
}
}
materialized_values_ = filtered;
}
// Apply ORDER BY
cs::net::proto::database::OrderBy* order_by_step =
nullptr;
for (const auto& step : proto_steps_) {
if (step.type == "ORDER_BY") {
order_by_step = const_cast<
cs::net::proto::database::OrderBy*>(
&step.order_by);
break;
}
}
if (order_by_step != nullptr &&
!materialized_values_.empty()) {
std::sort(materialized_values_.begin(),
materialized_values_.end(),
[order_by_step](const T& a, const T& b) {
std::string json_a = a.Serialize(0);
std::string json_b = b.Serialize(0);
auto obj_a_or =
cs::net::json::parsers::ParseObject(
json_a);
auto obj_b_or =
cs::net::json::parsers::ParseObject(
json_b);
if (!obj_a_or.ok() || !obj_b_or.ok()) {
return false;
}
auto val_a_or = ExtractFieldValueLocal(
obj_a_or.value(),
order_by_step->field_path);
auto val_b_or = ExtractFieldValueLocal(
obj_b_or.value(),
order_by_step->field_path);
if (!val_a_or.ok() || !val_b_or.ok()) {
return false;
}
auto val_a = val_a_or.value();
auto val_b = val_b_or.value();
bool a_less = false;
if (val_a.is(std::string()) &&
val_b.is(std::string())) {
a_less = val_a.as(std::string()) <
val_b.as(std::string());
} else if (val_a.is(int()) &&
val_b.is(int())) {
a_less =
val_a.as(int()) < val_b.as(int());
} else if (val_a.is(float()) &&
val_b.is(float())) {
a_less = val_a.as(float()) <
val_b.as(float());
}
return order_by_step->direction == "asc"
? a_less
: !a_less;
});
}
// Apply LIMIT
int limit_value = -1;
for (const auto& step : proto_steps_) {
if (step.type == "LIMIT") {
limit_value = step.limit;
break;
}
}
if (limit_value > 0 &&
materialized_values_.size() >
static_cast<size_t>(limit_value)) {
materialized_values_.resize(limit_value);
}
materialized_ = true;
return cs::Ok();
}
cs::net::proto::database::QueryRequest request;
request.collection = collection_;
request.steps = proto_steps_;
SET_OR_RET(auto response, client_->Query(request));
materialized_values_.clear();
for (const auto& record : response.records) {
SET_OR_RET(auto parsed,
T().Parse(record.payload_json));
materialized_values_.push_back(parsed);
}
materialized_ = true;
return cs::Ok();
}
private:
static cs::ResultOr<bool> EvaluateConditionLocal(
const cs::net::json::Object& json_obj,
const cs::net::proto::database::Condition&
condition) {
SET_OR_RET(auto field_value,
ExtractFieldValueLocal(
json_obj, condition.field_path));
return CompareValuesLocal(field_value,
condition.operator_type,
condition.value);
}
static cs::ResultOr<cs::net::json::Object>
ExtractFieldValueLocal(const cs::net::json::Object& obj,
const std::string& field_path) {
auto parts = SplitFieldPathLocal(field_path);
cs::net::json::Object current = obj;
for (const auto& part : parts) {
SET_OR_RET(current, current.get(part));
}
return current;
}
static std::vector<std::string> SplitFieldPathLocal(
const std::string& path) {
std::vector<std::string> parts;
std::istringstream iss(path);
std::string part;
while (std::getline(iss, part, '.')) {
parts.push_back(part);
}
return parts;
}
static cs::ResultOr<bool> CompareValuesLocal(
const cs::net::json::Object& field_value,
const std::string& operator_type,
const cs::net::proto::database::Value& expected) {
if (operator_type == "eq") {
if (expected.type == "string") {
return field_value.as(std::string()) ==
expected.string_value;
} else if (expected.type == "int") {
return field_value.as(int()) == expected.int_value;
} else if (expected.type == "float") {
return field_value.as(float()) ==
expected.float_value;
} else if (expected.type == "bool") {
return field_value.as(bool()) ==
expected.bool_value;
}
} else if (operator_type == "contains") {
if (expected.type == "string") {
std::string field_str =
field_value.as(std::string());
return field_str.find(expected.string_value) !=
std::string::npos;
}
} else if (operator_type == "not_contains") {
if (expected.type == "string") {
std::string field_str =
field_value.as(std::string());
return field_str.find(expected.string_value) ==
std::string::npos;
}
}
return TRACE(cs::Error("Unsupported operator: " +
operator_type));
}
private:
std::vector<cs::net::proto::database::QueryStep>
proto_steps_;
std::string collection_;
bool materialized_ = false;
std::shared_ptr<IDatabaseClient> client_;
Ts materialized_values_;
};
} // namespace cs::net::proto::db
#endif // CS_NET_PROTO_DB_QUERY_VIEW_GPT_HH