ramsingla-mongo_ext 0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,62 @@
1
+ require 'rubygems'
2
+ require 'rubygems/specification'
3
+ require 'fileutils'
4
+ require 'rake'
5
+ require 'rake/testtask'
6
+ require 'rake/gempackagetask'
7
+ begin
8
+ require 'rake/contrib/rubyforgepublisher'
9
+ rescue LoadError
10
+ end
11
+ require 'rbconfig'
12
+ include Config
13
+
14
+ gem_command = "gem"
15
+ gem_command = "gem1.9" if CONFIG["MAJOR"] == "1" && CONFIG["MINOR"] == "9"
16
+
17
+ # NOTE: some of the tests assume Mongo is running
18
+ Rake::TestTask.new do |t|
19
+ t.test_files = FileList['tests/test*.rb']
20
+ end
21
+
22
+ desc "Generate documentation"
23
+ task :rdoc do
24
+ version = eval(File.read("mongo-ruby-driver.gemspec")).version
25
+ out = File.join('html', version.to_s)
26
+ FileUtils.rm_rf('html')
27
+ system "rdoc --main README.rdoc --op #{out} --inline-source --quiet README.rdoc `find lib -name '*.rb'`"
28
+ end
29
+
30
+ desc "Publish documentation to mongo.rubyforge.org"
31
+ task :publish => [:rdoc] do
32
+ # Assumes docs are in ./html
33
+ Rake::RubyForgePublisher.new(GEM, RUBYFORGE_USER).upload
34
+ end
35
+
36
+ namespace :gem do
37
+
38
+ desc "Install the gem locally"
39
+ task :install do
40
+ sh <<EOS
41
+ #{gem_command} build mongo-ruby-driver.gemspec &&
42
+ sudo #{gem_command} install mongo-*.gem &&
43
+ rm mongo-*.gem
44
+ EOS
45
+ end
46
+
47
+ desc "Install the optional c extensions"
48
+ task :install_extensions do
49
+ sh <<EOS
50
+ #{gem_command} build mongo-extensions.gemspec &&
51
+ sudo #{gem_command} install mongo_ext-*.gem &&
52
+ rm mongo_ext-*.gem
53
+ EOS
54
+ end
55
+
56
+ end
57
+
58
+ task :default => :list
59
+
60
+ task :list do
61
+ system 'rake -T'
62
+ end
@@ -0,0 +1,744 @@
1
+ /*
2
+ * Copyright 2009 10gen, Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ /*
18
+ * This file contains C implementations of some of the functions needed by the
19
+ * bson module. If possible, these implementations should be used to speed up
20
+ * BSON encoding and decoding.
21
+ */
22
+
23
+ #include "ruby.h"
24
+ #include "ruby/st.h"
25
+ #include "ruby/regex.h"
26
+ #include <assert.h>
27
+ #include <math.h>
28
+
29
+ #define INITIAL_BUFFER_SIZE 256
30
+
31
+ static VALUE Binary;
32
+ static VALUE Undefined;
33
+ static VALUE Time;
34
+ static VALUE ObjectID;
35
+ static VALUE DBRef;
36
+ static VALUE Code;
37
+ static VALUE RegexpOfHolding;
38
+ static VALUE OrderedHash;
39
+
40
+ // this sucks. but for some reason these moved around between 1.8 and 1.9
41
+ #ifdef ONIGURUMA_H
42
+ #define IGNORECASE ONIG_OPTION_IGNORECASE
43
+ #define MULTILINE ONIG_OPTION_MULTILINE
44
+ #define EXTENDED ONIG_OPTION_EXTEND
45
+ #else
46
+ #define IGNORECASE RE_OPTION_IGNORECASE
47
+ #define MULTILINE RE_OPTION_MULTILINE
48
+ #define EXTENDED RE_OPTION_EXTENDED
49
+ #endif
50
+
51
+ /* TODO we ought to check that the malloc or asprintf was successful
52
+ * and raise an exception if not. */
53
+ #ifdef _MSC_VER
54
+ #define INT2STRING(buffer, i) \
55
+ { \
56
+ int vslength = _scprintf("%d", i) + 1; \
57
+ *buffer = malloc(vslength); \
58
+ _snprintf(*buffer, vslength, "%d", i); \
59
+ }
60
+ #else
61
+ #define INT2STRING(buffer, i) asprintf(buffer, "%d", i);
62
+ #endif
63
+
64
+ // this sucks too.
65
+ #ifndef RREGEXP_SRC_PTR
66
+ #define RREGEXP_SRC_PTR(r) RREGEXP(r)->str
67
+ #define RREGEXP_SRC_LEN(r) RREGEXP(r)->len
68
+ #endif
69
+
70
+ typedef struct {
71
+ char* buffer;
72
+ int size;
73
+ int position;
74
+ } bson_buffer;
75
+
76
+ static char zero = 0;
77
+ static char one = 1;
78
+
79
+ static int cmp_char(const void* a, const void* b) {
80
+ return *(char*)a - *(char*)b;
81
+ }
82
+
83
+ static void write_doc(bson_buffer* buffer, VALUE hash, VALUE check_keys);
84
+ static int write_element(VALUE key, VALUE value, VALUE extra);
85
+ static VALUE elements_to_hash(const char* buffer, int max);
86
+
87
+ static bson_buffer* buffer_new(void) {
88
+ bson_buffer* buffer;
89
+ buffer = ALLOC(bson_buffer);
90
+ assert(buffer);
91
+
92
+ buffer->size = INITIAL_BUFFER_SIZE;
93
+ buffer->position = 0;
94
+ buffer->buffer = ALLOC_N(char, INITIAL_BUFFER_SIZE);
95
+ assert(buffer->buffer);
96
+
97
+ return buffer;
98
+ }
99
+
100
+ static void buffer_free(bson_buffer* buffer) {
101
+ assert(buffer);
102
+ assert(buffer->buffer);
103
+
104
+ free(buffer->buffer);
105
+ free(buffer);
106
+ }
107
+
108
+ static void buffer_resize(bson_buffer* buffer, int min_length) {
109
+ int size = buffer->size;
110
+ if (size >= min_length) {
111
+ return;
112
+ }
113
+ while (size < min_length) {
114
+ size *= 2;
115
+ }
116
+ buffer->buffer = REALLOC_N(buffer->buffer, char, size);
117
+ assert(buffer->buffer);
118
+ buffer->size = size;
119
+ }
120
+
121
+ static void buffer_assure_space(bson_buffer* buffer, int size) {
122
+ if (buffer->position + size <= buffer->size) {
123
+ return;
124
+ }
125
+ buffer_resize(buffer, buffer->position + size);
126
+ }
127
+
128
+ /* returns offset for writing */
129
+ static int buffer_save_bytes(bson_buffer* buffer, int size) {
130
+ int position = buffer->position;
131
+ buffer_assure_space(buffer, size);
132
+ buffer->position += size;
133
+ return position;
134
+ }
135
+
136
+ static void buffer_write_bytes(bson_buffer* buffer, const char* bytes, int size) {
137
+ buffer_assure_space(buffer, size);
138
+
139
+ memcpy(buffer->buffer + buffer->position, bytes, size);
140
+ buffer->position += size;
141
+ }
142
+
143
+ static VALUE pack_extra(bson_buffer* buffer, VALUE check_keys) {
144
+ return rb_ary_new3(2, INT2NUM((int)buffer), check_keys);
145
+ }
146
+
147
+ static void write_name_and_type(bson_buffer* buffer, VALUE name, char type) {
148
+ buffer_write_bytes(buffer, &type, 1);
149
+ buffer_write_bytes(buffer, RSTRING_PTR(name), RSTRING_LEN(name));
150
+ buffer_write_bytes(buffer, &zero, 1);
151
+ }
152
+
153
+ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow_id) {
154
+ bson_buffer* buffer = (bson_buffer*)NUM2INT(rb_ary_entry(extra, 0));
155
+ VALUE check_keys = rb_ary_entry(extra, 1);
156
+
157
+ if (TYPE(key) == T_SYMBOL) {
158
+ // TODO better way to do this... ?
159
+ key = rb_str_new2(rb_id2name(SYM2ID(key)));
160
+ }
161
+
162
+ if (TYPE(key) != T_STRING) {
163
+ rb_raise(rb_eTypeError, "keys must be strings or symbols");
164
+ }
165
+
166
+ if (!allow_id && strcmp("_id", RSTRING_PTR(key)) == 0) {
167
+ return ST_CONTINUE;
168
+ }
169
+
170
+ if (check_keys == Qtrue) {
171
+ int i;
172
+ if (RSTRING_LEN(key) > 0 && RSTRING_PTR(key)[0] == '$') {
173
+ rb_raise(rb_eRuntimeError, "key must not start with '$'");
174
+ }
175
+ for (i = 0; i < RSTRING_LEN(key); i++) {
176
+ if (RSTRING_PTR(key)[i] == '.') {
177
+ rb_raise(rb_eRuntimeError, "key must not contain '.'");
178
+ }
179
+ }
180
+ }
181
+
182
+ switch(TYPE(value)) {
183
+ case T_BIGNUM:
184
+ {
185
+ VALUE as_f;
186
+ int int_value;
187
+ if (rb_funcall(value, rb_intern(">"), 1, INT2NUM(2147483647)) == Qtrue ||
188
+ rb_funcall(value, rb_intern("<"), 1, INT2NUM(-2147483648)) == Qtrue) {
189
+ rb_raise(rb_eRangeError, "MongoDB can only handle 4-byte ints"
190
+ " - try converting to a double before saving");
191
+ }
192
+ write_name_and_type(buffer, key, 0x10);
193
+ as_f = rb_funcall(value, rb_intern("to_f"), 0);
194
+ int_value = NUM2LL(as_f);
195
+ buffer_write_bytes(buffer, (char*)&int_value, 4);
196
+ break;
197
+ }
198
+ case T_FIXNUM:
199
+ {
200
+ int int_value = FIX2INT(value);
201
+ write_name_and_type(buffer, key, 0x10);
202
+ buffer_write_bytes(buffer, (char*)&int_value, 4);
203
+ break;
204
+ }
205
+ case T_TRUE:
206
+ {
207
+ write_name_and_type(buffer, key, 0x08);
208
+ buffer_write_bytes(buffer, &one, 1);
209
+ break;
210
+ }
211
+ case T_FALSE:
212
+ {
213
+ write_name_and_type(buffer, key, 0x08);
214
+ buffer_write_bytes(buffer, &zero, 1);
215
+ break;
216
+ }
217
+ case T_FLOAT:
218
+ {
219
+ double d = NUM2DBL(value);
220
+ write_name_and_type(buffer, key, 0x01);
221
+ buffer_write_bytes(buffer, (char*)&d, 8);
222
+ break;
223
+ }
224
+ case T_NIL:
225
+ {
226
+ write_name_and_type(buffer, key, 0x0A);
227
+ break;
228
+ }
229
+ case T_HASH:
230
+ {
231
+ write_name_and_type(buffer, key, 0x03);
232
+ write_doc(buffer, value, check_keys);
233
+ break;
234
+ }
235
+ case T_ARRAY:
236
+ {
237
+ int start_position, length_location, items, i, obj_length;
238
+ VALUE* values;
239
+
240
+ write_name_and_type(buffer, key, 0x04);
241
+ start_position = buffer->position;
242
+
243
+ // save space for length
244
+ length_location = buffer_save_bytes(buffer, 4);
245
+
246
+ items = RARRAY_LEN(value);
247
+ values = RARRAY_PTR(value);
248
+ for(i = 0; i < items; i++) {
249
+ char* name;
250
+ VALUE key;
251
+ INT2STRING(&name, i);
252
+ key = rb_str_new2(name);
253
+ write_element(key, values[i], pack_extra(buffer, check_keys));
254
+ free(name);
255
+ }
256
+
257
+ // write null byte and fill in length
258
+ buffer_write_bytes(buffer, &zero, 1);
259
+ obj_length = buffer->position - start_position;
260
+ memcpy(buffer->buffer + length_location, &obj_length, 4);
261
+ break;
262
+ }
263
+ case T_STRING:
264
+ {
265
+ if (strcmp(rb_class2name(RBASIC(value)->klass),
266
+ "XGen::Mongo::Driver::Code") == 0) {
267
+ int start_position, length_location, length, total_length;
268
+ write_name_and_type(buffer, key, 0x0F);
269
+
270
+ start_position = buffer->position;
271
+ length_location = buffer_save_bytes(buffer, 4);
272
+
273
+ length = RSTRING_LEN(value) + 1;
274
+ buffer_write_bytes(buffer, (char*)&length, 4);
275
+ buffer_write_bytes(buffer, RSTRING_PTR(value), length - 1);
276
+ buffer_write_bytes(buffer, &zero, 1);
277
+ write_doc(buffer, rb_funcall(value, rb_intern("scope"), 0), Qfalse);
278
+
279
+ total_length = buffer->position - start_position;
280
+ memcpy(buffer->buffer + length_location, &total_length, 4);
281
+
282
+ break;
283
+ } else {
284
+ int length = RSTRING_LEN(value) + 1;
285
+ write_name_and_type(buffer, key, 0x02);
286
+ buffer_write_bytes(buffer, (char*)&length, 4);
287
+ buffer_write_bytes(buffer, RSTRING_PTR(value), length - 1);
288
+ buffer_write_bytes(buffer, &zero, 1);
289
+ break;
290
+ }
291
+ }
292
+ case T_SYMBOL:
293
+ {
294
+ const char* str_value = rb_id2name(SYM2ID(value));
295
+ int length = strlen(str_value) + 1;
296
+ write_name_and_type(buffer, key, 0x0E);
297
+ buffer_write_bytes(buffer, (char*)&length, 4);
298
+ buffer_write_bytes(buffer, str_value, length);
299
+ break;
300
+ }
301
+ case T_OBJECT:
302
+ {
303
+ // TODO there has to be a better way to do these checks...
304
+ const char* cls = rb_class2name(RBASIC(value)->klass);
305
+ if (strcmp(cls, "XGen::Mongo::Driver::Binary") == 0 ||
306
+ strcmp(cls, "ByteBuffer") == 0) {
307
+ const char subtype = strcmp(cls, "ByteBuffer") ?
308
+ (const char)FIX2INT(rb_funcall(value, rb_intern("subtype"), 0)) : 2;
309
+ VALUE string_data = rb_funcall(value, rb_intern("to_s"), 0);
310
+ int length = RSTRING_LEN(string_data);
311
+ write_name_and_type(buffer, key, 0x05);
312
+ if (subtype == 2) {
313
+ const int other_length = length + 4;
314
+ buffer_write_bytes(buffer, (const char*)&other_length, 4);
315
+ buffer_write_bytes(buffer, &subtype, 1);
316
+ }
317
+ buffer_write_bytes(buffer, (const char*)&length, 4);
318
+ if (subtype != 2) {
319
+ buffer_write_bytes(buffer, &subtype, 1);
320
+ }
321
+ buffer_write_bytes(buffer, RSTRING_PTR(string_data), length);
322
+ break;
323
+ }
324
+ if (strcmp(cls, "XGen::Mongo::Driver::ObjectID") == 0) {
325
+ VALUE as_array = rb_funcall(value, rb_intern("to_a"), 0);
326
+ int i;
327
+ write_name_and_type(buffer, key, 0x07);
328
+ for (i = 0; i < 12; i++) {
329
+ char byte = (char)FIX2INT(RARRAY_PTR(as_array)[i]);
330
+ buffer_write_bytes(buffer, &byte, 1);
331
+ }
332
+ break;
333
+ }
334
+ if (strcmp(cls, "XGen::Mongo::Driver::DBRef") == 0) {
335
+ int start_position, length_location, obj_length;
336
+ VALUE ns, oid;
337
+ write_name_and_type(buffer, key, 0x03);
338
+
339
+ start_position = buffer->position;
340
+
341
+ // save space for length
342
+ length_location = buffer_save_bytes(buffer, 4);
343
+
344
+ ns = rb_funcall(value, rb_intern("namespace"), 0);
345
+ write_element(rb_str_new2("$ref"), ns, pack_extra(buffer, Qfalse));
346
+ oid = rb_funcall(value, rb_intern("object_id"), 0);
347
+ write_element(rb_str_new2("$id"), oid, pack_extra(buffer, Qfalse));
348
+
349
+ // write null byte and fill in length
350
+ buffer_write_bytes(buffer, &zero, 1);
351
+ obj_length = buffer->position - start_position;
352
+ memcpy(buffer->buffer + length_location, &obj_length, 4);
353
+ break;
354
+ }
355
+ if (strcmp(cls, "XGen::Mongo::Driver::Undefined") == 0) {
356
+ write_name_and_type(buffer, key, 0x06);
357
+ break;
358
+ }
359
+ }
360
+ case T_DATA:
361
+ {
362
+ // TODO again, is this really the only way to do this?
363
+ const char* cls = rb_class2name(RBASIC(value)->klass);
364
+ if (strcmp(cls, "Time") == 0) {
365
+ double t = NUM2DBL(rb_funcall(value, rb_intern("to_f"), 0));
366
+ long long time_since_epoch = (long long)round(t * 1000);
367
+ write_name_and_type(buffer, key, 0x09);
368
+ buffer_write_bytes(buffer, (const char*)&time_since_epoch, 8);
369
+ break;
370
+ }
371
+ }
372
+ case T_REGEXP:
373
+ {
374
+ int length = RREGEXP_SRC_LEN(value);
375
+ char* pattern = (char*)RREGEXP_SRC_PTR(value);
376
+ long flags = RREGEXP(value)->ptr->options;
377
+ VALUE has_extra;
378
+
379
+ write_name_and_type(buffer, key, 0x0B);
380
+
381
+ buffer_write_bytes(buffer, pattern, length);
382
+ buffer_write_bytes(buffer, &zero, 1);
383
+
384
+ if (flags & IGNORECASE) {
385
+ char ignorecase = 'i';
386
+ buffer_write_bytes(buffer, &ignorecase, 1);
387
+ }
388
+ if (flags & MULTILINE) {
389
+ char multiline = 'm';
390
+ buffer_write_bytes(buffer, &multiline, 1);
391
+ }
392
+ if (flags & EXTENDED) {
393
+ char extended = 'x';
394
+ buffer_write_bytes(buffer, &extended, 1);
395
+ }
396
+
397
+ has_extra = rb_funcall(value, rb_intern("respond_to?"), 1, rb_str_new2("extra_options_str"));
398
+ if (TYPE(has_extra) == T_TRUE) {
399
+ VALUE extra = rb_funcall(value, rb_intern("extra_options_str"), 0);
400
+ int old_position = buffer->position;
401
+ buffer_write_bytes(buffer, RSTRING_PTR(extra), RSTRING_LEN(extra));
402
+ qsort(buffer->buffer + old_position, RSTRING_LEN(extra), sizeof(char), cmp_char);
403
+ }
404
+ buffer_write_bytes(buffer, &zero, 1);
405
+
406
+ break;
407
+ }
408
+ default:
409
+ {
410
+ rb_raise(rb_eTypeError, "no c encoder for this type yet (%d)", TYPE(value));
411
+ break;
412
+ }
413
+ }
414
+ return ST_CONTINUE;
415
+ }
416
+
417
+ static int write_element(VALUE key, VALUE value, VALUE extra) {
418
+ return write_element_allow_id(key, value, extra, 0);
419
+ }
420
+
421
+ static void write_doc(bson_buffer* buffer, VALUE hash, VALUE check_keys) {
422
+ int start_position = buffer->position;
423
+ int length_location = buffer_save_bytes(buffer, 4);
424
+ int length;
425
+
426
+ VALUE key = rb_str_new2("_id");
427
+ if (rb_funcall(hash, rb_intern("has_key?"), 1, key) == Qtrue) {
428
+ VALUE id = rb_hash_aref(hash, key);
429
+ write_element_allow_id(key, id, pack_extra(buffer, check_keys), 1);
430
+ }
431
+ key = ID2SYM(rb_intern("_id"));
432
+ if (rb_funcall(hash, rb_intern("has_key?"), 1, key) == Qtrue) {
433
+ VALUE id = rb_hash_aref(hash, key);
434
+ write_element_allow_id(key, id, pack_extra(buffer, check_keys), 1);
435
+ }
436
+
437
+ // we have to check for an OrderedHash and handle that specially
438
+ if (strcmp(rb_class2name(RBASIC(hash)->klass), "OrderedHash") == 0) {
439
+ VALUE keys = rb_funcall(hash, rb_intern("keys"), 0);
440
+ int i;
441
+ for(i = 0; i < RARRAY_LEN(keys); i++) {
442
+ VALUE key = RARRAY_PTR(keys)[i];
443
+ VALUE value = rb_hash_aref(hash, key);
444
+
445
+ write_element(key, value, pack_extra(buffer, check_keys));
446
+ }
447
+ } else {
448
+ rb_hash_foreach(hash, write_element, pack_extra(buffer, check_keys));
449
+ }
450
+
451
+ // write null byte and fill in length
452
+ buffer_write_bytes(buffer, &zero, 1);
453
+ length = buffer->position - start_position;
454
+ memcpy(buffer->buffer + length_location, &length, 4);
455
+ }
456
+
457
+ static VALUE method_serialize(VALUE self, VALUE doc, VALUE check_keys) {
458
+ VALUE result;
459
+ bson_buffer* buffer = buffer_new();
460
+ assert(buffer);
461
+
462
+ write_doc(buffer, doc, check_keys);
463
+
464
+ result = rb_str_new(buffer->buffer, buffer->position);
465
+ buffer_free(buffer);
466
+ return result;
467
+ }
468
+
469
+ static VALUE get_value(const char* buffer, int* position, int type) {
470
+ VALUE value;
471
+ switch (type) {
472
+ case 1:
473
+ {
474
+ double d;
475
+ memcpy(&d, buffer + *position, 8);
476
+ value = rb_float_new(d);
477
+ *position += 8;
478
+ break;
479
+ }
480
+ case 2:
481
+ case 13:
482
+ {
483
+ int value_length;
484
+ *position += 4;
485
+ value_length = strlen(buffer + *position);
486
+ value = rb_str_new(buffer+ *position, value_length);
487
+ *position += value_length + 1;
488
+ break;
489
+ }
490
+ case 3:
491
+ {
492
+ int size;
493
+ memcpy(&size, buffer + *position, 4);
494
+ if (strcmp(buffer + *position + 5, "$ref") == 0) { // DBRef
495
+ int offset = *position + 14;
496
+ VALUE argv[2];
497
+ int collection_length = strlen(buffer + offset);
498
+ char id_type;
499
+
500
+ argv[0] = rb_str_new(buffer + offset, collection_length);
501
+ offset += collection_length + 1;
502
+ id_type = buffer[offset];
503
+ offset += 5;
504
+ argv[1] = get_value(buffer, &offset, (int)id_type);
505
+ value = rb_class_new_instance(2, argv, DBRef);
506
+ } else {
507
+ value = elements_to_hash(buffer + *position + 4, size - 5);
508
+ }
509
+ *position += size;
510
+ break;
511
+ }
512
+ case 4:
513
+ {
514
+ int size, end;
515
+ memcpy(&size, buffer + *position, 4);
516
+ end = *position + size - 1;
517
+ *position += 4;
518
+
519
+ value = rb_ary_new();
520
+ while (*position < end) {
521
+ int type = (int)buffer[(*position)++];
522
+ int key_size = strlen(buffer + *position);
523
+ VALUE to_append;
524
+
525
+ *position += key_size + 1; // just skip the key, they're in order.
526
+ to_append = get_value(buffer, position, type);
527
+ rb_ary_push(value, to_append);
528
+ }
529
+ (*position)++;
530
+ break;
531
+ }
532
+ case 5:
533
+ {
534
+ int length, subtype;
535
+ VALUE data, st;
536
+ VALUE argv[2];
537
+ memcpy(&length, buffer + *position, 4);
538
+ subtype = (unsigned char)buffer[*position + 4];
539
+ if (subtype == 2) {
540
+ data = rb_str_new(buffer + *position + 9, length - 4);
541
+ } else {
542
+ data = rb_str_new(buffer + *position + 5, length);
543
+ }
544
+ st = INT2FIX(subtype);
545
+ argv[0] = data;
546
+ argv[1] = st;
547
+ value = rb_class_new_instance(2, argv, Binary);
548
+ *position += length + 5;
549
+ break;
550
+ }
551
+ case 6:
552
+ {
553
+ value = rb_class_new_instance(0, NULL, Undefined);
554
+ break;
555
+ }
556
+ case 7:
557
+ {
558
+ VALUE str = rb_str_new(buffer + *position, 12);
559
+ VALUE oid = rb_funcall(str, rb_intern("unpack"), 1, rb_str_new2("C*"));
560
+ value = rb_class_new_instance(1, &oid, ObjectID);
561
+ *position += 12;
562
+ break;
563
+ }
564
+ case 8:
565
+ {
566
+ value = buffer[(*position)++] ? Qtrue : Qfalse;
567
+ break;
568
+ }
569
+ case 9:
570
+ {
571
+ long long millis;
572
+ VALUE seconds, microseconds;
573
+ memcpy(&millis, buffer + *position, 8);
574
+ seconds = INT2NUM(millis / 1000);
575
+ microseconds = INT2NUM((millis % 1000) * 1000);
576
+
577
+ value = rb_funcall(Time, rb_intern("at"), 2, seconds, microseconds);
578
+ value = rb_funcall(value, rb_intern("utc"), 0);
579
+ *position += 8;
580
+ break;
581
+ }
582
+ case 10:
583
+ {
584
+ value = Qnil;
585
+ break;
586
+ }
587
+ case 11:
588
+ {
589
+ int pattern_length = strlen(buffer + *position);
590
+ VALUE pattern = rb_str_new(buffer + *position, pattern_length);
591
+ int flags_length, flags = 0, i = 0;
592
+ char extra[10];
593
+ VALUE argv[3];
594
+ *position += pattern_length + 1;
595
+
596
+ flags_length = strlen(buffer + *position);
597
+ extra[0] = 0;
598
+ for (i = 0; i < flags_length; i++) {
599
+ char flag = buffer[*position + i];
600
+ if (flag == 'i') {
601
+ flags |= IGNORECASE;
602
+ }
603
+ else if (flag == 'm') {
604
+ flags |= MULTILINE;
605
+ }
606
+ else if (flag == 'x') {
607
+ flags |= EXTENDED;
608
+ }
609
+ else if (strlen(extra) < 9) {
610
+ strncat(extra, &flag, 1);
611
+ }
612
+ }
613
+ argv[0] = pattern;
614
+ argv[1] = INT2FIX(flags);
615
+ argv[2] = rb_str_new2(extra);
616
+ value = rb_class_new_instance(3, argv, RegexpOfHolding);
617
+ *position += flags_length + 1;
618
+ break;
619
+ }
620
+ case 12:
621
+ {
622
+ int collection_length;
623
+ VALUE collection, str, oid, id, argv[2];
624
+ *position += 4;
625
+ collection_length = strlen(buffer + *position);
626
+ collection = rb_str_new(buffer + *position, collection_length);
627
+ *position += collection_length + 1;
628
+
629
+ str = rb_str_new(buffer + *position, 12);
630
+ oid = rb_funcall(str, rb_intern("unpack"), 1, rb_str_new2("C*"));
631
+ id = rb_class_new_instance(1, &oid, ObjectID);
632
+ *position += 12;
633
+
634
+ argv[0] = collection;
635
+ argv[1] = id;
636
+ value = rb_class_new_instance(2, argv, DBRef);
637
+ break;
638
+ }
639
+ case 14:
640
+ {
641
+ int value_length;
642
+ memcpy(&value_length, buffer + *position, 4);
643
+ value = ID2SYM(rb_intern(buffer + *position + 4));
644
+ *position += value_length + 4;
645
+ break;
646
+ }
647
+ case 15:
648
+ {
649
+ int code_length, scope_size;
650
+ VALUE code, scope, argv[2];
651
+ *position += 8;
652
+ code_length = strlen(buffer + *position);
653
+ code = rb_str_new(buffer + *position, code_length);
654
+ *position += code_length + 1;
655
+
656
+ memcpy(&scope_size, buffer + *position, 4);
657
+ scope = elements_to_hash(buffer + *position + 4, scope_size - 5);
658
+ *position += scope_size;
659
+
660
+ argv[0] = code;
661
+ argv[1] = scope;
662
+ value = rb_class_new_instance(2, argv, Code);
663
+ break;
664
+ }
665
+ case 16:
666
+ {
667
+ int i;
668
+ memcpy(&i, buffer + *position, 4);
669
+ value = LL2NUM(i);
670
+ *position += 4;
671
+ break;
672
+ }
673
+ case 17:
674
+ {
675
+ int i;
676
+ int j;
677
+ memcpy(&i, buffer + *position, 4);
678
+ memcpy(&j, buffer + *position + 4, 4);
679
+ value = rb_ary_new3(2, LL2NUM(i), LL2NUM(j));
680
+ *position += 8;
681
+ break;
682
+ }
683
+ default:
684
+ {
685
+ rb_raise(rb_eTypeError, "no c decoder for this type yet (%d)", type);
686
+ break;
687
+ }
688
+ }
689
+ return value;
690
+ }
691
+
692
+ static VALUE elements_to_hash(const char* buffer, int max) {
693
+ VALUE hash = rb_class_new_instance(0, NULL, OrderedHash);
694
+ int position = 0;
695
+ while (position < max) {
696
+ int type = (int)buffer[position++];
697
+ int name_length = strlen(buffer + position);
698
+ VALUE name = rb_str_new(buffer + position, name_length);
699
+ VALUE value;
700
+ position += name_length + 1;
701
+ value = get_value(buffer, &position, type);
702
+ rb_funcall(hash, rb_intern("[]="), 2, name, value);
703
+ }
704
+ return hash;
705
+ }
706
+
707
+ static VALUE method_deserialize(VALUE self, VALUE bson) {
708
+ const char* buffer = RSTRING_PTR(bson);
709
+ int remaining = RSTRING_LEN(bson);
710
+
711
+ // NOTE we just swallow the size and end byte here
712
+ buffer += 4;
713
+ remaining -= 5;
714
+
715
+ return elements_to_hash(buffer, remaining);
716
+ }
717
+
718
+ void Init_cbson() {
719
+ VALUE driver, CBson;
720
+ Time = rb_const_get(rb_cObject, rb_intern("Time"));
721
+
722
+ driver = rb_const_get(rb_const_get(rb_const_get(rb_cObject,
723
+ rb_intern("XGen")),
724
+ rb_intern("Mongo")),
725
+ rb_intern("Driver"));
726
+ rb_require("mongo/types/binary");
727
+ Binary = rb_const_get(driver, rb_intern("Binary"));
728
+ rb_require("mongo/types/undefined");
729
+ Undefined = rb_const_get(driver, rb_intern("Undefined"));
730
+ rb_require("mongo/types/objectid");
731
+ ObjectID = rb_const_get(driver, rb_intern("ObjectID"));
732
+ rb_require("mongo/types/dbref");
733
+ DBRef = rb_const_get(driver, rb_intern("DBRef"));
734
+ rb_require("mongo/types/code");
735
+ Code = rb_const_get(driver, rb_intern("Code"));
736
+ rb_require("mongo/types/regexp_of_holding");
737
+ RegexpOfHolding = rb_const_get(driver, rb_intern("RegexpOfHolding"));
738
+ rb_require("mongo/util/ordered_hash");
739
+ OrderedHash = rb_const_get(rb_cObject, rb_intern("OrderedHash"));
740
+
741
+ CBson = rb_define_module("CBson");
742
+ rb_define_module_function(CBson, "serialize", method_serialize, 2);
743
+ rb_define_module_function(CBson, "deserialize", method_deserialize, 1);
744
+ }
@@ -0,0 +1,6 @@
1
+ require 'mkmf'
2
+
3
+ find_header("st.h")
4
+ find_header("regex.h")
5
+ dir_config('cbson')
6
+ create_makefile('mongo_ext/cbson')
@@ -0,0 +1,26 @@
1
+ # We need to list all of the included files because we aren't allowed to use
2
+ # Dir[...] in the github sandbox.
3
+ PACKAGE_FILES = ['Rakefile', 'mongo-extensions.gemspec',
4
+ 'ext/cbson/cbson.c',
5
+ 'ext/cbson/extconf.rb']
6
+ TEST_FILES = []
7
+
8
+ Gem::Specification.new do |s|
9
+ s.name = 'mongo_ext'
10
+ s.version = '0.3'
11
+ s.platform = Gem::Platform::RUBY
12
+ s.summary = 'C extensions for the MongoDB Ruby driver'
13
+ s.description = 'C extensions to accelerate the MondoDB Ruby driver. For more information about Mongo, see http://www.mongodb.org.'
14
+
15
+ s.require_paths = ['ext']
16
+
17
+ s.files = PACKAGE_FILES
18
+ s.test_files = TEST_FILES
19
+
20
+ s.has_rdoc = false
21
+ s.extensions << 'ext/cbson/extconf.rb'
22
+
23
+ s.author = 'Mike Dirolf'
24
+ s.email = 'mongodb-dev@googlegroups.com'
25
+ s.homepage = 'http://www.mongodb.org'
26
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ramsingla-mongo_ext
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.3"
5
+ platform: ruby
6
+ authors:
7
+ - Mike Dirolf
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-16 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: C extensions to accelerate the MondoDB Ruby driver. For more information about Mongo, see http://www.mongodb.org.
17
+ email: mongodb-dev@googlegroups.com
18
+ executables: []
19
+
20
+ extensions:
21
+ - ext/cbson/extconf.rb
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - Rakefile
26
+ - mongo-extensions.gemspec
27
+ - ext/cbson/cbson.c
28
+ - ext/cbson/extconf.rb
29
+ has_rdoc: false
30
+ homepage: http://www.mongodb.org
31
+ post_install_message:
32
+ rdoc_options: []
33
+
34
+ require_paths:
35
+ - ext
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: "0"
41
+ version:
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ requirements: []
49
+
50
+ rubyforge_project:
51
+ rubygems_version: 1.2.0
52
+ signing_key:
53
+ specification_version: 2
54
+ summary: C extensions for the MongoDB Ruby driver
55
+ test_files: []
56
+