- Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathdocument.cc
541 lines (408 loc) · 11.2 KB
/
document.cc
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*
* Copyright (c) 2015, 2024, Oracle and/or its affiliates.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2.0, as
* published by the Free Software Foundation.
*
* This program is designed to work with certain software (including
* but not limited to OpenSSL) that is licensed under separate terms, as
* designated in a particular file or component or in included license
* documentation. The authors of MySQL hereby grant you an additional
* permission to link the program and your derivative works with the
* separately licensed software that they have either included with
* the program or referenced in the documentation.
*
* Without limiting anything contained in the foregoing, this file,
* which is part of Connector/C++, is also subject to the
* Universal FOSS Exception, version 1.0, a copy of which can be found at
* https://oss.oracle.com/licenses/universal-foss-exception.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License, version 2.0, for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include<mysql/cdk.h>
#include<mysqlx/xdevapi.h>
#include<json_parser.h>
/**
@file
Implementation of DbDoc and related classes.
*/
#include"impl.h"
#include<vector>
#include<sstream>
#include<iomanip>
#include<memory>
usingnamespace ::mysqlx;
using std::endl;
// Value::get specialization to allow convertion to common::Value type
// --------------------
template <>
common::Value Value::get<common::Value>() const {
if (getType() == DOCUMENT) {
returncommon::Value::Access::mk_json(m_doc.get_json());
}
return *this;
}
// DbDoc implementation
// --------------------
DbDoc::DbDoc(const std::string &json)
{
try {
m_impl = std::make_shared<Impl::JSONDoc>(json);
}
CATCH_AND_WRAP
}
DbDoc::DbDoc(std::string &&json)
{
try {
m_impl = std::make_shared<Impl::JSONDoc>(std::move(json));
}
CATCH_AND_WRAP
}
DbDoc::DbDoc(const std::shared_ptr<Impl> &impl)
: m_impl(impl)
{}
constchar* DbDoc::get_json() const
{
return m_impl ? m_impl->get_json() : "";
}
boolDbDoc::hasField(const Field &fld) const
{
try {
return m_impl && m_impl->has_field(fld);
}
CATCH_AND_WRAP
}
const Value& DbDoc::operator[](const Field &fld) const
{
if (!m_impl)
throwstd::out_of_range("empty document");
try {
return m_impl->get(fld);
}
catch (const std::out_of_range&)
{
throw;
}
catch (...)
{
try { throw; }
CATCH_AND_WRAP
}
}
voidDbDoc::print(std::ostream &out) const
{
try {
if (m_impl)
m_impl->print(out);
else
out << "{}";
}
CATCH_AND_WRAP
}
// JSON document
// -------------
/*
JSON processor which builds document implementation adding
key-value pairs to document's map.
*/
structDbDoc::Impl::Builder
: public cdk::JSON::Processor
, public cdk::JSON::Processor::Any_prc
, public cdk::JSON::Processor::Any_prc::Scalar_prc
{
Map &m_map;
mysqlx::string m_key;
public:
Builder(DbDoc::Impl &doc)
: m_map(doc.m_map)
{}
// JSON processor (to build the docuemnt)
voiddoc_begin()
{
m_map.clear();
}
voiddoc_end()
{}
cdk::JSON::Processor::Any_prc*
key_val(const cdk::string &key)
{
m_key = key;
// Return itself to process key value
returnthis;
}
/*
Builder for array values.
*/
structArr_builder
: cdk::JSON::Processor::Any_prc
, cdk::JSON::Processor::Any_prc::List_prc
, cdk::JSON::Processor::Any_prc::Scalar_prc
{
Value::Array *m_arr;
// List processor (to build the list)
voidlist_begin()
{
m_arr->clear();
}
voidlist_end() {}
Element_prc* list_el()
{
// Return itself to process list element.
returnthis;
}
// Any processor (to process elements of the list)
// Handle sub-arrray element.
std::unique_ptr<Arr_builder> m_arr_builder;
List_prc* arr()
{
// Create array value.
Value sub;
sub.m_type = Value::ARR;
sub.m_arr = std::make_shared<Value::Array>();
// Create builder for the sub-array.
m_arr_builder.reset(newArr_builder());
m_arr_builder->m_arr = sub.m_arr.get();
// Append the sub-array to the main array.
m_arr->emplace_back(sub);
return m_arr_builder.get();
}
// Handle a document element
std::unique_ptr<Builder> m_doc_builder;
Doc_prc* doc()
{
// Create document value and append it to the array.
Value sub;
sub.m_type = Value::DOC;
sub.m_doc.m_impl = std::make_shared<DbDoc::Impl>();
m_arr->emplace_back(sub);
// Create builder for the document and return it as the processor.
m_doc_builder.reset(newBuilder(*sub.m_doc.m_impl));
return m_doc_builder.get();
}
// Handle scalar values using itself as a processor.
Scalar_prc* scalar()
{ returnthis; }
// Sclar processor (to store scalar values in the list)
voidnull() { m_arr->emplace_back(Value()); }
voidstr(const cdk::string &val)
{
m_arr->emplace_back(mysqlx::string(val));
}
voidnum(uint64_t val) { m_arr->emplace_back(val); }
voidnum(int64_t val) { m_arr->emplace_back(val); }
voidnum(float val) { m_arr->emplace_back(val); }
voidnum(double val) { m_arr->emplace_back(val); }
voidyesno(bool val) { m_arr->emplace_back(val); }
}
m_arr_builder;
cdk::JSON::Processor::Any_prc::List_prc*
arr()
{
using mysqlx::Value;
Value &arr = m_map[m_key];
// Turn the value to one storing an array.
arr.m_type = Value::ARR;
arr.m_arr = std::make_shared<Value::Array>();
// Set up array builder for the new value.
m_arr_builder.m_arr = arr.m_arr.get();
return &m_arr_builder;
}
std::unique_ptr<Builder> m_doc_builder;
cdk::JSON::Processor::Any_prc::Doc_prc*
doc()
{
using mysqlx::Value;
Value &sub = m_map[m_key];
// Turn the value to one storing a document.
sub.m_type = Value::DOC;
sub.m_doc.m_impl = std::make_shared<DbDoc::Impl>();
// Use another builder to build the sub-document.
m_doc_builder.reset(newBuilder(*sub.m_doc.m_impl));
return m_doc_builder.get();
}
cdk::JSON::Processor::Any_prc::Scalar_prc*
scalar()
{
returnthis;
}
/*
Callbacks for scalar values store the value under
key given by m_key.
*/
voidnull() { m_map.emplace(m_key, Value()); }
voidstr(const cdk::string &val)
{
m_map.emplace(m_key, mysqlx::string(val));
}
voidnum(uint64_t val) { m_map.emplace(m_key, val); }
voidnum(int64_t val) { m_map.emplace(m_key, val); }
voidnum(float val) { m_map.emplace(m_key, val); }
voidnum(double val) { m_map.emplace(m_key, val); }
voidyesno(bool val) { m_map.emplace(m_key, val); }
};
voidDbDoc::Impl::JSONDoc::prepare()
{
if (m_parsed)
return;
cdk::Codec<cdk::TYPE_DOCUMENT> codec;
Builder bld(*this);
codec.from_bytes(cdk::bytes(m_json), bld);
m_parsed = true;
}
/*
Parse JSON string and build a corresponding Value.
*/
Value Value::Access::mk_from_json(const std::string &json)
{
/*
Define builder which acts as JSON value processor and
builds the corresponding Value object.
*/
structBuilder
: public cdk::JSON::Processor
, cdk::JSON_processor
, cdk::JSON::Processor::Any_prc
{
Value *m_val = NULL;
// JSON::Processor
Any_prc* key_val(const string&) override
{
returnthis;
}
// Any_prc
Scalar_prc *scalar() override
{ returnthis; }
std::unique_ptr<DbDoc::Impl::Builder> m_doc_builder;
Doc_prc *doc() override
{
m_val->m_type = DOC;
m_doc_builder.reset(newDbDoc::Impl::Builder(*m_val->m_doc.m_impl));
return m_doc_builder.get();
}
DbDoc::Impl::Builder::Arr_builder m_arr_builder;
List_prc *arr() override
{
m_val->m_type = ARR;
m_val->m_arr = std::make_shared<Array>();
m_arr_builder.m_arr = m_val->m_arr.get();
return &m_arr_builder;
}
// JSON_processor
voidnull() override
{}
voidstr(const cdk::string &val) override
{
*m_val = (mysqlx::string)val;
}
voidnum(uint64_t val) override
{ *m_val = val; }
voidnum(int64_t val) override
{ *m_val = val; }
voidnum(float val) override
{ *m_val = val; }
voidnum(double val) override
{ *m_val = val; }
voidyesno(bool val) override
{ *m_val = val; }
}
builder;
// Invoke parser to build the value.
Value val;
builder.m_val = &val;
/*
Note: json can be not only an object, but also scalar or array. Since
JSON_parser can parse only documents, we do a trick of parsing document
of the form { "doc": json } and builder ignores the top-level "doc"
field.
*/
parser::JSON_parser parser(std::string("{ \"doc\":") + json + "}");
parser.process(builder);
return val;
}
/*
Iterating over document fields
------------------------------
Iterator functionality is implemented by document implementation
object in forms of these methods:
- reset() - restart iteration form the beginning,
- next() - move to next document field,
- at_end() - true if all fields have been enumerated,
- get_current_fld() - return current field in the sequence.
Note: Since document implementation acts as an iterator, only one
iterator can be used at a time. Creating new iterator will invalidate
other iterators.
Note: Iterator takes shared ownership of document implementation
so it can be used even if original document was destroyed.
TODO: Use common::Iterator<> template instead?
*/
DbDoc::Iterator DbDoc::begin()
{
try {
Iterator it;
m_impl->reset();
it.m_impl = m_impl;
it.m_end = false;
return it;
}
CATCH_AND_WRAP
}
DbDoc::Iterator DbDoc::end()
{
try {
/*
Iterator that points one-past-the-end-of-sequence has no
real representation - we simply set m_end flag in it.
*/
Iterator it;
it.m_end = true;
return it;
}
CATCH_AND_WRAP
}
const Field& DbDoc::Iterator::operator*()
{
if (m_end)
THROW("dereferencing past-the-end iterator");
try {
return m_impl->get_current_fld();
}
CATCH_AND_WRAP
}
DbDoc::Iterator& DbDoc::Iterator::operator++()
{
try {
// only non-end iterator can be incremented.
if (!m_end)
m_impl->next();
return *this;
}
CATCH_AND_WRAP
}
bool DbDoc::Iterator::operator==(const Iterator &other) const
{
try {
/*
if this is end iterator, other is equal if it is also end
iterator or it is at the end of sequence. And vice-versa.
*/
if (m_end)
return other.m_end || other.m_impl->at_end();
if (other.m_end)
return m_end || m_impl->at_end();
/*
Otherwise two iterators are equal if they use the same
document implementation (but such two iterators should not
be used at the same time).
*/
return m_impl == other.m_impl;
}
CATCH_AND_WRAP
}