-
Notifications
You must be signed in to change notification settings - Fork 14
/
bson.cc
370 lines (310 loc) · 9.98 KB
/
bson.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
#include <v8.h>
#include <node.h>
#include <node_object_wrap.h>
#include <sstream>
extern "C" {
#define MONGO_HAVE_STDINT
#include <bson.h>
}
#include "bson.h"
using namespace std;
using namespace v8;
Persistent<FunctionTemplate> ObjectID::constructor_template;
void ObjectID::Initialize(Handle<Object> target) {
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(ObjectID::New);
constructor_template = Persistent<FunctionTemplate>::New(t);
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
constructor_template->SetClassName(String::NewSymbol("ObjectID"));
NODE_SET_PROTOTYPE_METHOD(ObjectID::constructor_template, "toString", ObjectID::ToString);
target->Set(String::NewSymbol("ObjectID"), constructor_template->GetFunction());
}
Handle<Value> ObjectID::New(const Arguments &args) {
HandleScope scope;
if (args.Length() < 1
|| !args[0]->IsString()
|| (args[0]->IsString()
&& args[0]->ToString()->Length() != 24)) {
return ThrowException(Exception::Error(
String::New("Argument must be 24 character hex string")));
}
String::Utf8Value hex(args[0]->ToString());
// XXX where should this be deleted?
ObjectID *o = new ObjectID((const char *) *hex);
o->Wrap(args.This());
return args.This();
}
void ObjectID::str(char *str) {
bson_oid_to_string(&oid, str);
}
Handle<Value>
ObjectID::ToString(const Arguments &args) {
ObjectID *o = ObjectWrap::Unwrap<ObjectID>(args.This());
HandleScope scope;
char hex[25];
o->str(hex);
return String::New(hex);
}
const char *
ToCString(const String::Utf8Value& value) {
return *value ? *value : "<string conversion failed>";
}
inline void
encodeString(bson_buffer *bb, const char *name, const Local<Value> element) {
String::Utf8Value v(element);
const char *value(ToCString(v));
bson_append_string(bb, name, value);
}
inline void
encodeNumber(bson_buffer *bb, const char *name, const Local<Value> element) {
double value(element->NumberValue());
bson_append_double(bb, name, value);
}
inline void
encodeInteger(bson_buffer *bb, const char *name, const Local<Value> element) {
int value(element->NumberValue());
bson_append_int(bb, name, value);
}
inline void
encodeBoolean(bson_buffer *bb, const char *name, const Local<Value> element) {
bool value(element->IsTrue());
bson_append_bool(bb, name, value);
}
void
encodeObjectID(bson_buffer *bb, const char *name, const Local<Value> element) {
// get at the delicious wrapped object centre
Local<Object> obj = element->ToObject();
assert(!obj.IsEmpty());
assert(obj->InternalFieldCount() > 0);
ObjectID *o = static_cast<ObjectID*>(Handle<External>::Cast(
obj->GetInternalField(0))->Value());
bson_oid_t oid;
char oid_hex[25];
o->str(oid_hex);
bson_oid_from_string(&oid, oid_hex);
bson_append_oid(bb, name, &oid);
}
void
encodeArray(bson_buffer *bb, const char *name, const Local<Value> element) {
Local<Array> a = Array::Cast(*element);
bson_buffer *arr = bson_append_start_array(bb, name);
for (int i = 0, l=a->Length(); i < l; i++) {
Local<Value> val = a->Get(Number::New(i));
stringstream keybuf;
string keyval;
keybuf << i << endl;
keybuf >> keyval;
if (val->IsString()) {
encodeString(arr, keyval.c_str(), val);
}
else if (val->IsInt32()) {
encodeInteger(arr, keyval.c_str(), val);
}
else if (val->IsNumber()) {
encodeNumber(arr, keyval.c_str(), val);
}
else if (val->IsBoolean()) {
encodeBoolean(arr, keyval.c_str(), val);
}
else if (val->IsArray()) {
encodeArray(arr, keyval.c_str(), val);
}
else if (val->IsObject()) {
bson bson(encodeObject(val));
bson_append_bson(arr, keyval.c_str(), &bson);
bson_destroy(&bson);
}
}
bson_append_finish_object(arr);
}
bson encodeObject(const Local<Value> element) {
HandleScope scope;
bson_buffer bb;
bson_buffer_init(&bb);
Local<Object> object = element->ToObject();
Local<Array> properties = object->GetPropertyNames();
for (int i = 0; i < properties->Length(); i++) {
// get the property name and value
Local<Value> prop_name = properties->Get(Integer::New(i));
Local<Value> prop_val = object->Get(prop_name->ToString());
// convert the property name to a c string
String::Utf8Value n(prop_name);
const char *pname = ToCString(n);
// append property using appropriate appender
if (prop_val->IsString()) {
encodeString(&bb, pname, prop_val);
}
else if (prop_val->IsInt32()) {
encodeInteger(&bb, pname, prop_val);
}
else if (prop_val->IsNumber()) {
encodeNumber(&bb, pname, prop_val);
}
else if (prop_val->IsBoolean()) {
encodeBoolean(&bb, pname, prop_val);
}
else if (prop_val->IsArray()) {
encodeArray(&bb, pname, prop_val);
}
else if (prop_val->IsObject()
&& ObjectID::constructor_template->HasInstance(prop_val)) {
encodeObjectID(&bb, pname, prop_val);
}
else if (prop_val->IsObject()) {
bson bson(encodeObject(prop_val));
bson_append_bson(&bb, pname, &bson);
bson_destroy(&bson);
}
}
bson bson;
bson_from_buffer(&bson, &bb);
return bson;
}
Handle<Value>
encode(const Arguments &args) {
// TODO assert args.length > 0
// TODO assert args.type == Object
HandleScope scope;
bson bson(encodeObject(args[0]));
Handle<Value> ret = node::Encode(bson.data, bson_size(&bson), node::BINARY);
bson_destroy(&bson);
return ret;
}
// Decoding functions
Handle<Value>
decodeString(bson_iterator *i) {
HandleScope scope;
const char *val = bson_iterator_string(i);
Local<Value> str = String::New(val);
return scope.Close(str);
}
Handle<Value>
decodeObject(bson_iterator *i) {
HandleScope scope;
bson bson;
bson_iterator_subobject(i, &bson);
Handle<Value> sub = decodeObjectStr(bson.data);
return scope.Close(sub);
}
Handle<Value>
decodeObjectID(bson_iterator *i) {
HandleScope scope;
char hex_oid[25];
bson_oid_t *oid = bson_iterator_oid(i);
bson_oid_to_string(oid, hex_oid);
Handle<Value> argv[1];
argv[0] = String::New(hex_oid);
Handle<Value> obj =
ObjectID::constructor_template->GetFunction()->NewInstance(1, argv);
return scope.Close(obj);
}
Handle<Value>
decodeDouble(bson_iterator *i) {
HandleScope scope;
double val = bson_iterator_double_raw(i);
Local<Value> obj = Number::New(val);
return scope.Close(obj);
}
Handle<Value>
decodeInteger(bson_iterator *i) {
HandleScope scope;
double val = bson_iterator_int_raw(i);
Local<Value> obj = Integer::New(val);
return scope.Close(obj);
}
Handle<Value>
decodeBool(bson_iterator *i) {
HandleScope scope;
bson_bool_t val = bson_iterator_bool(i);
Handle<Value> obj = Boolean::New(val);
return scope.Close(obj);
}
Handle<Value>
decodeArray(bson_iterator *it) {
HandleScope scope;
bson_iterator sub;
bson_iterator_subiterator(it, &sub);
Local<Array> obj = Array::New();
for (int i = 0; bson_iterator_next(&sub); i++) {
bson_type type = bson_iterator_type(&sub);
const char *key = bson_iterator_key(&sub);
switch (type) {
case bson_string:
obj->Set(Number::New(i), decodeString(&sub));
break;
case bson_array:
obj->Set(Number::New(i), decodeArray(&sub));
break;
case bson_object:
obj->Set(Number::New(i), decodeObject(&sub));
break;
case bson_oid:
obj->Set(Number::New(i), decodeObjectID(&sub));
break;
case bson_double:
obj->Set(Number::New(i), decodeDouble(&sub));
break;
case bson_int:
obj->Set(Number::New(i), decodeInteger(&sub));
break;
case bson_bool:
obj->Set(Number::New(i), decodeBool(&sub));
break;
}
}
return scope.Close(obj);
}
Local<Value>
decodeObjectIterator(bson_iterator *it) {
HandleScope scope;
Local<Object> obj = Object::New();
while (bson_iterator_next(it)) {
bson_type type = bson_iterator_type(it);
const char *key = bson_iterator_key(it);
switch (type) {
case bson_string:
obj->Set(String::New(key), decodeString(it));
break;
case bson_array:
obj->Set(String::New(key), decodeArray(it));
break;
case bson_object:
obj->Set(String::New(key), decodeObject(it));
break;
case bson_oid:
obj->Set(String::New(key), decodeObjectID(it));
break;
case bson_double:
obj->Set(String::New(key), decodeDouble(it));
break;
case bson_int:
obj->Set(String::New(key), decodeInteger(it));
break;
case bson_bool:
obj->Set(String::New(key), decodeBool(it));
break;
}
}
return scope.Close(obj);
}
Local<Value>
decodeObjectStr(const char *buf) {
HandleScope scope;
bson_iterator it;
bson_iterator_init(&it, buf);
Handle<Value> obj = decodeObjectIterator(&it);
return scope.Close(obj);
}
Handle<Value>
decodeObject(const Local<Value> str) {
HandleScope scope;
size_t buflen = str->ToString()->Length();
char buf[buflen];
node::DecodeWrite(buf, buflen, str, node::BINARY);
return decodeObjectStr(buf);
}
Handle<Value>
decode(const Arguments &args) {
HandleScope scope;
return decodeObject(args[0]);
}