forked from mongodb/mongo-hhvm-driver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbson.cpp
1324 lines (1098 loc) · 39.7 KB
/
bson.cpp
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2015 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hphp/runtime/vm/native-data.h"
#include "hphp/runtime/base/array-init.h"
#include "hphp/runtime/base/array-iterator.h"
#include "hphp/runtime/base/builtin-functions.h"
#include "hphp/runtime/base/execution-context.h"
#include "hphp/runtime/base/type-string.h"
#include "hphp/util/logger.h"
#include "bson.h"
#include "utils.h"
#include "mongodb.h"
#include <iostream>
#include "src/MongoDB/BSON/Binary.h"
#include "src/MongoDB/BSON/Decimal128.h"
#include "src/MongoDB/BSON/Javascript.h"
#include "src/MongoDB/BSON/ObjectID.h"
#include "src/MongoDB/BSON/Regex.h"
#include "src/MongoDB/BSON/Timestamp.h"
#include "src/MongoDB/BSON/UTCDateTime.h"
#include "src/MongoDB/Driver/CursorId.h"
extern "C" {
#include "libbson/src/bson/bson.h"
#include "libmongoc/src/mongoc/mongoc.h"
}
namespace HPHP {
/* {{{ HHVM → BSON */
/* {{{ static strings used for conversions */
const StaticString
s_root("root"),
s_document("document"),
s_object("object"),
s_stdClass("stdClass"),
s_array("array"),
s_types("types");
/* }}} */
int VariantToBsonConverter::_isPackedArray(const Array &a)
{
int idx = 0, key_value = 0;
for (ArrayIter iter(a); iter; ++iter) {
Variant key(iter.first());
if (!key.isInteger()) {
return false;
}
key_value = key.toInt32();
if (idx != key_value) {
return false;
}
idx++;
}
return true;
}
VariantToBsonConverter::VariantToBsonConverter(const Variant& document, int flags)
{
m_document = document;
m_level = 0;
m_flags = flags;
m_out = Variant();
}
void VariantToBsonConverter::convert(bson_t *bson)
{
if (m_document.isObject() || m_document.isArray()) {
convertDocument(bson, NULL, m_document);
} else {
std::cout << "convert *unimplemented*: " << getDataTypeString(m_document.getType()).c_str() << "\n";
}
}
const StaticString s_MongoBSONTypeWrapper_className("MongoDB\\BSON\\TypeWrapper");
const StaticString s_MongoBSONTypeWrapper_createFromBSONType("createFromBSONType");
const StaticString s_MongoBSONTypeWrapper_toBSONType("toBSONType");
void VariantToBsonConverter::convertElement(bson_t *bson, const char *key, Variant v)
{
/* We need to check whether it is a type wrapped object (ie, the class
* implements TypeWrapper). If so, run the toBSONType() method on the
* object. The returned value then needs to be processed as normal. */
if (v.isObject() && v.toObject().instanceof(s_MongoBSONTypeWrapper_className)) {
Object obj = v.toObject();
Class *cls = obj.get()->getVMClass();
Func *m = cls->lookupMethod(s_MongoBSONTypeWrapper_toBSONType.get());
TypedValue args[0] = {};
Variant result;
#if HIPPO_HHVM_VERSION >= 31700
result = Variant::attach(
g_context->invokeFuncFew(
m, obj.get(),
nullptr, 0, args
)
);
#else
g_context->invokeFuncFew(
result.asTypedValue(),
m, obj.get(),
nullptr, 0, args
);
#endif
v = result;
}
switch (v.getType()) {
case KindOfUninit:
case KindOfNull:
convertNull(bson, key);
break;
case KindOfBoolean:
convertBoolean(bson, key, v.toBoolean());
break;
case KindOfInt64:
convertInt64(bson, key, v.toInt64());
break;
case KindOfDouble:
convertDouble(bson, key, v.toDouble());
break;
#if HIPPO_HHVM_VERSION >= 31200
case KindOfPersistentString:
#else
case KindOfStaticString:
#endif
case KindOfString:
convertString(bson, key, v.toString());
break;
case KindOfArray:
#if HIPPO_HHVM_VERSION >= 31100
case KindOfPersistentArray:
#endif
case KindOfObject:
convertDocument(bson, key, v);
break;
case KindOfRef:
convertElement(bson, key, *v.getRefData());
break;
#if HIPPO_HHVM_VERSION >= 31700
case KindOfPersistentVec:
case KindOfPersistentDict:
case KindOfPersistentKeyset:
case KindOfVec:
case KindOfDict:
case KindOfKeyset:
#endif
case KindOfResource: {
StringBuffer buf;
buf.printf(
"Got unsupported type '%s'",
HPHP::getDataTypeString(v.getType()).data()
);
Variant message = buf.detach();
throw MongoDriver::Utils::throwUnexpectedValueException((char*) message.toString().c_str());
return;
}
#if HIPPO_HHVM_VERSION < 31900
case KindOfClass:
not_reached();
#endif
}
}
void VariantToBsonConverter::convertNull(bson_t *bson, const char *key)
{
bson_append_null(bson, key, -1);
};
void VariantToBsonConverter::convertBoolean(bson_t *bson, const char *key, bool v)
{
bson_append_bool(bson, key, -1, v);
};
void VariantToBsonConverter::convertInt64(bson_t *bson, const char *key, int64_t v)
{
if (v > INT_MAX || v < INT_MIN) {
bson_append_int64(bson, key, -1, v);
} else {
bson_append_int32(bson, key, -1, (int32_t) v);
}
};
void VariantToBsonConverter::convertDouble(bson_t *bson, const char *key, double v)
{
bson_append_double(bson, key, -1, v);
};
void VariantToBsonConverter::convertString(bson_t *bson, const char *key, String v)
{
if (bson_utf8_validate(v.c_str(), v.size(), true)) {
bson_append_utf8(bson, key, -1, v.c_str(), v.size());
} else {
throw MongoDriver::Utils::throwUnexpectedValueException("Got invalid UTF-8 value serializing '" + String(v.c_str()) + "'");
}
}
char *VariantToBsonConverter::_getUnmangledPropertyName(String key)
{
const char *ckey = key.c_str();
if (ckey[0] == '\0' && key.length()) {
const char *cls = ckey + 1;
if (*cls == '*') { // protected
return strdup(ckey + 3);
} else {
int l = strlen(cls);
return strdup(cls + l + 1);
}
} else {
return strdup(ckey);
}
}
void VariantToBsonConverter::convertDocument(bson_t *bson, const char *property_name, Variant v)
{
bson_t child;
int unmangle = 0;
Array document;
/* if we are not at a top-level, we need to check (and convert) special
* BSON types too */
if (v.isObject()) {
if (convertSpecialObject(bson, property_name, v.toObject())) {
return;
}
/* The "convertSpecialObject" method didn't understand this type, so we
* will continue treating this as a normal document */
}
document = v.toObject()->o_toIterArray(null_string, ObjectData::PreserveRefs);
if (_isPackedArray(document) && !v.isObject()) {
if (property_name != NULL) {
bson_append_array_begin(bson, property_name, -1, &child);
}
} else {
unmangle = 1;
if (property_name != NULL) {
bson_append_document_begin(bson, property_name, -1, &child);
}
}
for (ArrayIter iter(document); iter; ++iter) {
Variant key(iter.first());
const Variant& data(iter.secondRef());
String s_key = key.toString();
if (m_level == 0 && (m_flags & HIPPO_BSON_ADD_ID)) {
/* If we have an ID, we don't need to add it. But we also need to
* set m_out to the value! */
if (strncmp(s_key.c_str(), "_id", s_key.length()) == 0) {
m_flags &= ~HIPPO_BSON_ADD_ID;
if (m_flags & HIPPO_BSON_RETURN_ID) {
/* FIXME: Should we add a ref here? */
m_out = data;
}
}
}
m_level++;
if (unmangle) {
const char *unmangledName;
unmangledName = _getUnmangledPropertyName(s_key);
if (strlen(s_key.c_str()) != s_key.size()) {
free((void*) unmangledName);
throw MongoDriver::Utils::throwUnexpectedValueException("BSON keys cannot contain null bytes. Unexpected null byte after \"" + String(s_key.c_str()) + "\".");
}
convertElement(property_name != NULL ? &child : bson, unmangledName, data);
free((void*) unmangledName);
} else {
if (strlen(s_key.c_str()) != s_key.size()) {
throw MongoDriver::Utils::throwUnexpectedValueException("BSON keys cannot contain null bytes. Unexpected null byte after \"" + String(s_key.c_str()) + "\".");
}
convertElement(property_name != NULL ? &child : bson, s_key.c_str(), data);
}
m_level--;
}
if (m_level == 0 && (m_flags & HIPPO_BSON_ADD_ID)) {
bson_oid_t oid;
bson_oid_init(&oid, NULL);
bson_append_oid(bson, "_id", strlen("_id"), &oid);
if (m_flags & HIPPO_BSON_RETURN_ID) {
Object obj = createMongoBsonObjectIDObject(&oid);
m_out = obj;
}
}
if (property_name != NULL) {
if (_isPackedArray(document)) {
bson_append_array_end(bson, &child);
} else {
bson_append_document_end(bson, &child);
}
}
}
/* {{{ Serialization of types */
const StaticString s_MongoDriverBsonType_className("MongoDB\\BSON\\Type");
const StaticString s_MongoDriverBsonPersistable_className("MongoDB\\BSON\\Persistable");
const StaticString s_MongoDriverBsonSerializable_className("MongoDB\\BSON\\Serializable");
const StaticString s_MongoDriverBsonUnserializable_className("MongoDB\\BSON\\Unserializable");
const StaticString s_MongoDriverBsonSerializable_functionName("bsonSerialize");
const StaticString s_MongoDriverBsonUnserializable_functionName("bsonUnserialize");
const StaticString s_MongoDriverBsonODM_fieldName("__pclass");
/* {{{ MongoDriver\BSON\Binary */
void VariantToBsonConverter::_convertBinary(bson_t *bson, const char *key, Object v)
{
String data = v->o_get(s_MongoBsonBinary_data, false, s_MongoBsonBinary_className).toString();
int64_t type = v->o_get(s_MongoBsonBinary_type, false, s_MongoBsonBinary_className).toInt64();
bson_append_binary(bson, key, -1, (bson_subtype_t) type, (const unsigned char*) data.c_str(), data.length());
}
/* }}} */
/* {{{ MongoDriver\BSON\Decimal128 */
void VariantToBsonConverter::_convertDecimal128(bson_t *bson, const char *key, Object v)
{
MongoDBBsonDecimal128Data* data = Native::data<MongoDBBsonDecimal128Data>(v.get());
bson_append_decimal128(bson, key, -1, &data->m_decimal);
}
/* }}} */
/* {{{ MongoDriver\BSON\Javascript */
void VariantToBsonConverter::_convertJavascript(bson_t *bson, const char *key, Object v)
{
bson_t *scope_bson;
String code = v->o_get(s_MongoBsonJavascript_code, false, s_MongoBsonJavascript_className).toString();
auto scope = v->o_get(s_MongoBsonJavascript_scope, false, s_MongoBsonJavascript_className);
if (scope.isObject() || scope.isArray()) {
/* Convert scope to document */
VariantToBsonConverter converter(scope, HIPPO_BSON_NO_FLAGS);
scope_bson = bson_new();
converter.convert(scope_bson);
bson_append_code_with_scope(bson, key, -1, (const char*) code.c_str(), scope_bson);
} else {
bson_append_code(bson, key, -1, (const char*) code.c_str());
}
}
/* }}} */
/* {{{ MongoDriver\BSON\MaxKey */
const StaticString s_MongoBsonMaxKey_className("MongoDB\\BSON\\MaxKey");
const StaticString s_MongoBsonMaxKey_shortName("MaxKey");
void VariantToBsonConverter::_convertMaxKey(bson_t *bson, const char *key, Object v)
{
bson_append_maxkey(bson, key, -1);
}
/* }}} */
/* {{{ MongoDriver\BSON\MinKey */
const StaticString s_MongoBsonMinKey_className("MongoDB\\BSON\\MinKey");
const StaticString s_MongoBsonMinKey_shortName("MinKey");
void VariantToBsonConverter::_convertMinKey(bson_t *bson, const char *key, Object v)
{
bson_append_minkey(bson, key, -1);
}
/* }}} */
/* {{{ MongoDriver\BSON\ObjectID */
void VariantToBsonConverter::_convertObjectID(bson_t *bson, const char *key, Object v)
{
MongoDBBsonObjectIDData* data = Native::data<MongoDBBsonObjectIDData>(v.get());
bson_append_oid(bson, key, -1, &data->m_oid);
}
/* }}} */
/* {{{ MongoDriver\BSON\Regex */
void VariantToBsonConverter::_convertRegex(bson_t *bson, const char *key, Object v)
{
String regex = v->o_get(s_MongoBsonRegex_pattern, false, s_MongoBsonRegex_className).toString();
String flags = v->o_get(s_MongoBsonRegex_flags, false, s_MongoBsonRegex_className).toString();
bson_append_regex(bson, key, -1, regex.c_str(), flags.c_str());
}
/* }}} */
/* {{{ MongoDriver\BSON\Timestamp */
void VariantToBsonConverter::_convertTimestamp(bson_t *bson, const char *key, Object v)
{
int32_t timestamp = v->o_get(s_MongoBsonTimestamp_timestamp, false, s_MongoBsonTimestamp_className).toInt32();
int32_t increment = v->o_get(s_MongoBsonTimestamp_increment, false, s_MongoBsonTimestamp_className).toInt32();
bson_append_timestamp(bson, key, -1, timestamp, increment);
}
/* {{{ MongoDriver\BSON\UTCDateTime */
void VariantToBsonConverter::_convertUTCDateTime(bson_t *bson, const char *key, Object v)
{
int64_t milliseconds = v->o_get(s_MongoBsonUTCDateTime_milliseconds, false, s_MongoBsonUTCDateTime_className).toInt64();
bson_append_date_time(bson, key, -1, milliseconds);
}
/* }}} */
/* {{{ MongoDriver\Driver\CursorId */
void VariantToBsonConverter::_convertCursorId(bson_t *bson, const char *key, Object v)
{
MongoDBDriverCursorIdData* data = Native::data<MongoDBDriverCursorIdData>(v.get());
bson_append_int64(bson, key, -1, data->id);
}
/* }}} */
/* {{{ Special objects that implement MongoDB\BSON\Serializable */
void VariantToBsonConverter::_convertSerializable(bson_t *bson, const char *key, Object v)
{
Variant result;
Array properties;
Class *cls;
Func *m;
cls = v.get()->getVMClass();
m = cls->lookupMethod(s_MongoDriverBsonSerializable_functionName.get());
#if HIPPO_HHVM_VERSION >= 31700
result = Variant::attach(
g_context->invokeFuncFew(
m,
v.get(),
nullptr
)
);
#else
g_context->invokeFuncFew(
result.asTypedValue(),
m,
v.get(),
nullptr
);
#endif
if ( ! (
result.isArray() ||
(result.isObject() && result.toObject().instanceof(s_stdClass))
)
) {
StringBuffer buf;
buf.printf(
"Expected %s::%s() to return an array or stdClass, %s given",
cls->nameStr().c_str(),
s_MongoDriverBsonSerializable_functionName.data(),
result.isObject() ? result.toObject()->getVMClass()->nameStr().c_str() : HPHP::getDataTypeString(result.getType()).data()
);
Variant full_name = buf.detach();
throw MongoDriver::Utils::throwUnexpectedValueException((char*) full_name.toString().c_str());
}
/* Convert to array so that we can handle it well */
properties = result.toArray();
if (v.instanceof(s_MongoDriverBsonPersistable_className)) {
const char *class_name = cls->nameStr().c_str();
Object obj = createMongoBsonBinaryObject(
(const uint8_t *) class_name,
strlen(class_name),
(bson_subtype_t) 0x80
);
properties.add(String(s_MongoDriverBsonODM_fieldName), obj);
}
convertDocument(bson, key, result.isObject() ? Variant(Variant(properties).toObject()) : Variant(properties));
}
/* }}} */
/* }}} */
bool VariantToBsonConverter::convertSpecialObject(bson_t *bson, const char *key, Object v)
{
if (v.instanceof(s_MongoDriverBsonType_className)) {
if (v.instanceof(s_MongoDriverBsonSerializable_className)) {
_convertSerializable(bson, key, v);
return true;
}
if (m_level == 0) {
throw MongoDriver::Utils::throwUnexpectedValueException("MongoDB\\BSON\\Type instance " + String(v->getClassName())+ " cannot be serialized as a root element");
}
if (v.instanceof(s_MongoBsonBinary_className)) {
_convertBinary(bson, key, v);
return true;
}
if (v.instanceof(s_MongoBsonDecimal128_className)) {
_convertDecimal128(bson, key, v);
return true;
}
if (v.instanceof(s_MongoBsonJavascript_className)) {
_convertJavascript(bson, key, v);
return true;
}
if (v.instanceof(s_MongoBsonMaxKey_className)) {
_convertMaxKey(bson, key, v);
return true;
}
if (v.instanceof(s_MongoBsonMinKey_className)) {
_convertMinKey(bson, key, v);
return true;
}
if (v.instanceof(s_MongoBsonObjectID_className)) {
_convertObjectID(bson, key, v);
return true;
}
if (v.instanceof(s_MongoBsonRegex_className)) {
_convertRegex(bson, key, v);
return true;
}
if (v.instanceof(s_MongoBsonTimestamp_className)) {
_convertTimestamp(bson, key, v);
return true;
}
if (v.instanceof(s_MongoBsonUTCDateTime_className)) {
_convertUTCDateTime(bson, key, v);
return true;
}
throw MongoDriver::Utils::throwUnexpectedValueException("Unexpected MongoDB\\BSON\\Type instance: " + String(v->getClassName()));
}
if (v.instanceof(s_MongoDriverCursorId_className)) {
_convertCursorId(bson, key, v);
return true;
}
return false;
}
/* }}} */
/* }}} */
/* {{{ BSON → HHVM */
BsonToVariantConverter::BsonToVariantConverter(const unsigned char *data, int data_len, hippo_bson_conversion_options_t options)
{
m_reader = bson_reader_new_from_data(data, data_len);
m_options = options;
}
/* {{{ TypeWrapping */
Variant wrapObject(String class_name, Object typeObject)
{
Variant result = invoke_static_method(class_name, s_MongoBSONTypeWrapper_createFromBSONType, Array::Create(typeObject));
return result;
}
/* }}} */
/* {{{ Visitors */
void hippo_bson_visit_corrupt(const bson_iter_t *iter __attribute__((unused)), void *data)
{
Logger::Verbose("[HIPPO] Corrupt BSON data detected!");
throw MongoDriver::Utils::throwUnexpectedValueException("Detected corrupt BSON data");
}
bool hippo_bson_visit_double(const bson_iter_t *iter __attribute__((unused)), const char *key, double v_double, void *data)
{
hippo_bson_state *state = (hippo_bson_state*) data;
state->zchild.add(String(key), Variant(v_double));
return false;
}
bool hippo_bson_visit_utf8(const bson_iter_t *iter __attribute__((unused)), const char *key, size_t v_utf8_len, const char *v_utf8, void *data)
{
hippo_bson_state *state = (hippo_bson_state*) data;
state->zchild.add(String(key), Variant(String(v_utf8)));
return false;
}
bool hippo_bson_visit_document(const bson_iter_t *iter __attribute__((unused)), const char *key, const bson_t *v_document, void *data)
{
hippo_bson_state *state = (hippo_bson_state*) data;
Variant document_v;
state->options.current_compound_type = HIPPO_BSONTYPE_DOCUMENT;
BsonToVariantConverter converter(bson_get_data(v_document), v_document->len, state->options);
converter.convert(&document_v);
state->zchild.add(String(key), document_v);
return false;
}
bool hippo_bson_visit_array(const bson_iter_t *iter __attribute__((unused)), const char *key, const bson_t *v_array, void *data)
{
hippo_bson_state *state = (hippo_bson_state*) data;
Variant array_v;
state->options.current_compound_type = HIPPO_BSONTYPE_ARRAY;
BsonToVariantConverter converter(bson_get_data(v_array), v_array->len, state->options);
converter.convert(&array_v);
state->zchild.add(String(key), array_v);
return false;
}
bool hippo_bson_visit_binary(const bson_iter_t *iter __attribute__((unused)), const char *key, bson_subtype_t v_subtype, size_t v_binary_len, const uint8_t *v_binary, void *data)
{
hippo_bson_state *state = (hippo_bson_state*) data;
Object obj;
obj = createMongoBsonBinaryObject(v_binary, v_binary_len, v_subtype);
if (! state->options.types.binary_class_name.empty()) {
/* We have a type wrapped class, so wrap it */
Variant result = wrapObject(state->options.types.binary_class_name, obj);
state->zchild.add(String(key), result);
} else {
state->zchild.add(String(key), Variant(obj));
}
return false;
}
bool hippo_bson_visit_oid(const bson_iter_t *iter __attribute__((unused)), const char *key, const bson_oid_t *v_oid, void *data)
{
hippo_bson_state *state = (hippo_bson_state*) data;
Object obj = createMongoBsonObjectIDObject(v_oid);
if (! state->options.types.objectid_class_name.empty()) {
/* We have a type wrapped class, so wrap it */
Variant result = wrapObject(state->options.types.objectid_class_name, obj);
state->zchild.add(String(key), result);
} else {
state->zchild.add(String(key), Variant(obj));
}
return false;
}
bool hippo_bson_visit_bool(const bson_iter_t *iter __attribute__((unused)), const char *key, bool v_bool, void *data)
{
hippo_bson_state *state = (hippo_bson_state*) data;
state->zchild.add(String(key), Variant(v_bool));
return false;
}
bool hippo_bson_visit_date_time(const bson_iter_t *iter __attribute__((unused)), const char *key, int64_t msec_since_epoch, void *data)
{
hippo_bson_state *state = (hippo_bson_state*) data;
static Class* c_datetime;
c_datetime = Unit::lookupClass(s_MongoBsonUTCDateTime_className.get());
assert(c_datetime);
Object obj = Object{c_datetime};
obj->o_set(s_MongoBsonUTCDateTime_milliseconds, Variant(msec_since_epoch), s_MongoBsonUTCDateTime_className);
if (! state->options.types.utcdatetime_class_name.empty()) {
/* We have a type wrapped class, so wrap it */
Variant result = wrapObject(state->options.types.utcdatetime_class_name, obj);
state->zchild.add(String(key), result);
} else {
state->zchild.add(String(key), Variant(obj));
}
return false;
}
bool hippo_bson_visit_null(const bson_iter_t *iter __attribute__((unused)), const char *key, void *data)
{
hippo_bson_state *state = (hippo_bson_state*) data;
state->zchild.add(String(key), Variant(Variant::NullInit()));
return false;
}
bool hippo_bson_visit_regex(const bson_iter_t *iter __attribute__((unused)), const char *key, const char *v_regex, const char *v_options, void *data)
{
hippo_bson_state *state = (hippo_bson_state*) data;
static Class* c_regex;
c_regex = Unit::lookupClass(s_MongoBsonRegex_className.get());
assert(c_regex);
Object obj = Object{c_regex};
/* Call __construct on the object */
static Class* c_class = Unit::getClass(s_MongoBsonRegex_className.get(), true);
#if HIPPO_HHVM_VERSION >= 31700
g_context->invokeFunc(
c_class->getCtor(),
HPHP::make_packed_array(v_regex, v_options),
obj.get()
);
#else
HPHP::TypedValue ret;
g_context->invokeFunc(
&ret,
c_class->getCtor(),
HPHP::make_packed_array(v_regex, v_options),
obj.get()
);
#endif
if (! state->options.types.regex_class_name.empty()) {
/* We have a type wrapped class, so wrap it */
Variant result = wrapObject(state->options.types.regex_class_name, obj);
state->zchild.add(String(key), result);
} else {
state->zchild.add(String(key), Variant(obj));
}
return false;
}
bool hippo_bson_visit_code(const bson_iter_t *iter __attribute__((unused)), const char *key, size_t v_code_len, const char *v_code, void *data)
{
hippo_bson_state *state = (hippo_bson_state*) data;
static Class* c_code;
String s;
unsigned char *data_s;
s = String(v_code_len, ReserveString);
data_s = (unsigned char*) s.bufferSlice().data();
memcpy(data_s, v_code, v_code_len);
s.setSize(v_code_len);
c_code = Unit::lookupClass(s_MongoBsonJavascript_className.get());
assert(c_code);
Object obj = Object{c_code};
obj->o_set(s_MongoBsonJavascript_code, s, s_MongoBsonJavascript_className);
if (! state->options.types.javascript_class_name.empty()) {
/* We have a type wrapped class, so wrap it */
Variant result = wrapObject(state->options.types.javascript_class_name, obj);
state->zchild.add(String(key), result);
} else {
state->zchild.add(String(key), Variant(obj));
}
return false;
}
bool hippo_bson_visit_codewscope(const bson_iter_t *iter __attribute__((unused)), const char *key, size_t v_code_len, const char *v_code, const bson_t *v_scope, void *data)
{
hippo_bson_state *state = (hippo_bson_state*) data;
static Class* c_code;
String s;
unsigned char *data_s;
Variant scope_v;
/* code */
s = String(v_code_len, ReserveString);
data_s = (unsigned char*) s.bufferSlice().data();
memcpy(data_s, v_code, v_code_len);
s.setSize(v_code_len);
/* scope */
BsonToVariantConverter converter(bson_get_data(v_scope), v_scope->len, state->options);
converter.convert(&scope_v);
/* create object */
c_code = Unit::lookupClass(s_MongoBsonJavascript_className.get());
assert(c_code);
Object obj = Object{c_code};
/* set properties */
obj->o_set(s_MongoBsonJavascript_code, s, s_MongoBsonJavascript_className);
obj->o_set(s_MongoBsonJavascript_scope, scope_v, s_MongoBsonJavascript_className);
/* add to array */
if (! state->options.types.javascript_class_name.empty()) {
/* We have a type wrapped class, so wrap it */
Variant result = wrapObject(state->options.types.javascript_class_name, obj);
state->zchild.add(String(key), result);
} else {
state->zchild.add(String(key), Variant(obj));
}
return false;
}
bool hippo_bson_visit_int32(const bson_iter_t *iter __attribute__((unused)), const char *key, int32_t v_int32, void *data)
{
hippo_bson_state *state = (hippo_bson_state*) data;
state->zchild.add(String(key), Variant(v_int32));
return false;
}
bool hippo_bson_visit_timestamp(const bson_iter_t *iter __attribute__((unused)), const char *key, uint32_t v_timestamp, uint32_t v_increment, void *data)
{
hippo_bson_state *state = (hippo_bson_state*) data;
static Class* c_timestamp;
c_timestamp = Unit::lookupClass(s_MongoBsonTimestamp_className.get());
assert(c_timestamp);
Object obj = Object{c_timestamp};
obj->o_set(s_MongoBsonTimestamp_timestamp, Variant((uint64_t) v_timestamp), s_MongoBsonTimestamp_className);
obj->o_set(s_MongoBsonTimestamp_increment, Variant((uint64_t) v_increment), s_MongoBsonTimestamp_className);
if (! state->options.types.timestamp_class_name.empty()) {
/* We have a type wrapped class, so wrap it */
Variant result = wrapObject(state->options.types.timestamp_class_name, obj);
state->zchild.add(String(key), result);
} else {
state->zchild.add(String(key), Variant(obj));
}
return false;
}
bool hippo_bson_visit_int64(const bson_iter_t *iter __attribute__((unused)), const char *key, int64_t v_int64, void *data)
{
hippo_bson_state *state = (hippo_bson_state*) data;
state->zchild.add(String(key), Variant(v_int64));
return false;
}
bool hippo_bson_visit_maxkey(const bson_iter_t *iter __attribute__((unused)), const char *key, void *data)
{
hippo_bson_state *state = (hippo_bson_state*) data;
static Class* c_objectId;
c_objectId = Unit::lookupClass(s_MongoBsonMaxKey_className.get());
assert(c_objectId);
Object obj = Object{c_objectId};
if (! state->options.types.maxkey_class_name.empty()) {
/* We have a type wrapped class, so wrap it */
Variant result = wrapObject(state->options.types.maxkey_class_name, obj);
state->zchild.add(String(key), result);
} else {
state->zchild.add(String(key), Variant(obj));
}
return false;
}
bool hippo_bson_visit_minkey(const bson_iter_t *iter __attribute__((unused)), const char *key, void *data)
{
hippo_bson_state *state = (hippo_bson_state*) data;
static Class* c_objectId;
c_objectId = Unit::lookupClass(s_MongoBsonMinKey_className.get());
assert(c_objectId);
Object obj = Object{c_objectId};
if (! state->options.types.minkey_class_name.empty()) {
/* We have a type wrapped class, so wrap it */
Variant result = wrapObject(state->options.types.minkey_class_name, obj);
state->zchild.add(String(key), result);
} else {
state->zchild.add(String(key), Variant(obj));
}
return false;
}
void hippo_bson_visit_unsupported_type(const bson_iter_t *iter __attribute__((unused)), const char *key, uint32_t v_type_code, void *data)
{
char *message;
message = (char*) malloc( 29 + 2 + 15 + strlen(key) + 35 + 1);
sprintf(message, "Detected unknown BSON type 0x%02hhx for fieldname \"%s\". Are you using the latest driver?", v_type_code, key);
throw MongoDriver::Utils::throwUnexpectedValueException(message);
}
bool hippo_bson_visit_decimal128(const bson_iter_t *iter __attribute__((unused)), const char *key, const bson_decimal128_t *v_decimal128, void *data)
{
hippo_bson_state *state = (hippo_bson_state*) data;
static Class* c_decimal128;
c_decimal128 = Unit::lookupClass(s_MongoBsonDecimal128_className.get());
assert(c_decimal128);
Object obj = Object{c_decimal128};
MongoDBBsonDecimal128Data* obj_data = Native::data<MongoDBBsonDecimal128Data>(obj);
memcpy(&obj_data->m_decimal, v_decimal128, sizeof(bson_decimal128_t));
if (! state->options.types.decimal128_class_name.empty()) {
/* We have a type wrapped class, so wrap it */
Variant result = wrapObject(state->options.types.decimal128_class_name, obj);
state->zchild.add(String(key), result);
} else {
state->zchild.add(String(key), Variant(obj));
}
return false;
}
static const bson_visitor_t hippo_bson_visitors = {
NULL /* hippo_phongo_bson_visit_before*/,
NULL /*hippo_phongo_bson_visit_after*/,
hippo_bson_visit_corrupt,
hippo_bson_visit_double,
hippo_bson_visit_utf8,
hippo_bson_visit_document,
hippo_bson_visit_array,
hippo_bson_visit_binary,
NULL /*hippo_bson_visit_undefined*/,
hippo_bson_visit_oid,
hippo_bson_visit_bool,
hippo_bson_visit_date_time,
hippo_bson_visit_null,
hippo_bson_visit_regex,
NULL /*hippo_bson_visit_dbpointer*/,
hippo_bson_visit_code,
NULL /*hippo_bson_visit_symbol*/,
hippo_bson_visit_codewscope,
hippo_bson_visit_int32,
hippo_bson_visit_timestamp,
hippo_bson_visit_int64,
hippo_bson_visit_maxkey,
hippo_bson_visit_minkey,
hippo_bson_visit_unsupported_type,
hippo_bson_visit_decimal128,
{ NULL }
};
/* }}} */
bool BsonToVariantConverter::convert(Variant *v)
{
bson_iter_t iter;
bool eof = false;
bool havePclass;
const bson_t *b;
int type_descriminator;
String named_class;
m_state.zchild = NULL;
/* Determine which descriminator to use */
switch (m_options.current_compound_type) {
case HIPPO_BSONTYPE_ARRAY:
type_descriminator = m_options.array_type;
named_class = m_options.array_class_name;
break;
case HIPPO_BSONTYPE_ROOT:
type_descriminator = m_options.root_type;
named_class = m_options.root_class_name;
break;
case HIPPO_BSONTYPE_DOCUMENT:
type_descriminator = m_options.document_type;
named_class = m_options.document_class_name;
break;
}
if (!(b = bson_reader_read(m_reader, &eof))) {
throw MongoDriver::Utils::throwUnexpectedValueException("Could not read document from BSON reader");
return false;
}
if (!bson_iter_init(&iter, b)) {
throw MongoDriver::Utils::throwUnexpectedValueException("Could not initialize BSON iterator");
return false;
}
m_state.zchild = Array::Create();