kbaum-mongo_ext 0.18.3.2 → 0.19

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.
data/ext/cbson/cbson.c CHANGED
@@ -141,17 +141,14 @@ static int cmp_char(const void* a, const void* b) {
141
141
  }
142
142
 
143
143
  static void write_doc(buffer_t buffer, VALUE hash, VALUE check_keys, VALUE move_id);
144
- static int write_element(VALUE key, VALUE value, VALUE extra);
144
+ static int write_element_with_id(VALUE key, VALUE value, VALUE extra);
145
+ static int write_element_without_id(VALUE key, VALUE value, VALUE extra);
145
146
  static VALUE elements_to_hash(const char* buffer, int max);
146
147
 
147
148
  static VALUE pack_extra(buffer_t buffer, VALUE check_keys) {
148
149
  return rb_ary_new3(2, LL2NUM((long long)buffer), check_keys);
149
150
  }
150
151
 
151
- static VALUE pack_triple(buffer_t buffer, VALUE check_keys, int allow_id) {
152
- return rb_ary_new3(3, LL2NUM((long long)buffer), check_keys, allow_id);
153
- }
154
-
155
152
  static void write_name_and_type(buffer_t buffer, VALUE name, char type) {
156
153
  SAFE_WRITE(buffer, &type, 1);
157
154
  name = TO_UTF8(name);
@@ -159,7 +156,7 @@ static void write_name_and_type(buffer_t buffer, VALUE name, char type) {
159
156
  SAFE_WRITE(buffer, &zero, 1);
160
157
  }
161
158
 
162
- static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow_id) {
159
+ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
163
160
  buffer_t buffer = (buffer_t)NUM2LL(rb_ary_entry(extra, 0));
164
161
  VALUE check_keys = rb_ary_entry(extra, 1);
165
162
 
@@ -173,7 +170,7 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
173
170
  rb_raise(rb_eTypeError, "keys must be strings or symbols");
174
171
  }
175
172
 
176
- if (!allow_id && strcmp("_id", RSTRING_PTR(key)) == 0) {
173
+ if (allow_id == 0 && strcmp("_id", RSTRING_PTR(key)) == 0) {
177
174
  return ST_CONTINUE;
178
175
  }
179
176
 
@@ -266,7 +263,7 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
266
263
  VALUE key;
267
264
  INT2STRING(&name, i);
268
265
  key = rb_str_new2(name);
269
- write_element(key, values[i], pack_extra(buffer, check_keys));
266
+ write_element_with_id(key, values[i], pack_extra(buffer, check_keys));
270
267
  free(name);
271
268
  }
272
269
 
@@ -366,9 +363,9 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
366
363
  }
367
364
 
368
365
  ns = rb_funcall(value, rb_intern("namespace"), 0);
369
- write_element(rb_str_new2("$ref"), ns, pack_extra(buffer, Qfalse));
366
+ write_element_with_id(rb_str_new2("$ref"), ns, pack_extra(buffer, Qfalse));
370
367
  oid = rb_funcall(value, rb_intern("object_id"), 0);
371
- write_element(rb_str_new2("$id"), oid, pack_extra(buffer, Qfalse));
368
+ write_element_with_id(rb_str_new2("$id"), oid, pack_extra(buffer, Qfalse));
372
369
 
373
370
  // write null byte and fill in length
374
371
  SAFE_WRITE(buffer, &zero, 1);
@@ -464,8 +461,12 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
464
461
  return ST_CONTINUE;
465
462
  }
466
463
 
467
- static int write_element(VALUE key, VALUE value, VALUE extra) {
468
- return write_element_allow_id(key, value, extra, 0);
464
+ static int write_element_without_id(VALUE key, VALUE value, VALUE extra) {
465
+ return write_element(key, value, extra, 0);
466
+ }
467
+
468
+ static int write_element_with_id(VALUE key, VALUE value, VALUE extra) {
469
+ return write_element(key, value, extra, 1);
469
470
  }
470
471
 
471
472
  static void write_doc(buffer_t buffer, VALUE hash, VALUE check_keys, VALUE move_id) {
@@ -473,6 +474,7 @@ static void write_doc(buffer_t buffer, VALUE hash, VALUE check_keys, VALUE move_
473
474
  buffer_position length_location = buffer_save_space(buffer, 4);
474
475
  buffer_position length;
475
476
  int allow_id;
477
+ int (*write_function)(VALUE, VALUE, VALUE) = NULL;
476
478
  VALUE id_str = rb_str_new2("_id");
477
479
  VALUE id_sym = ID2SYM(rb_intern("_id"));
478
480
 
@@ -480,37 +482,47 @@ static void write_doc(buffer_t buffer, VALUE hash, VALUE check_keys, VALUE move_
480
482
  rb_raise(rb_eNoMemError, "failed to allocate memory in buffer.c");
481
483
  }
482
484
 
483
- // write '_id' first if move_id is true
485
+ // write '_id' first if move_id is true. then don't allow an id to be written.
484
486
  if(move_id == Qtrue) {
485
487
  allow_id = 0;
486
488
  if (rb_funcall(hash, rb_intern("has_key?"), 1, id_str) == Qtrue) {
487
489
  VALUE id = rb_hash_aref(hash, id_str);
488
- write_element_allow_id(id_str, id, pack_extra(buffer, check_keys), 1);
490
+ write_element_with_id(id_str, id, pack_extra(buffer, check_keys));
489
491
  } else if (rb_funcall(hash, rb_intern("has_key?"), 1, id_sym) == Qtrue) {
490
492
  VALUE id = rb_hash_aref(hash, id_sym);
491
- write_element_allow_id(id_sym, id, pack_extra(buffer, check_keys), 1);
493
+ write_element_with_id(id_sym, id, pack_extra(buffer, check_keys));
492
494
  }
493
495
  }
494
496
  else {
495
497
  allow_id = 1;
496
- if ((rb_funcall(hash, rb_intern("has_key?"), 1, id_str) == Qtrue) &&
497
- (rb_funcall(hash, rb_intern("has_key?"), 1, id_sym) == Qtrue)) {
498
- VALUE obj = rb_hash_delete(hash, id_str);
498
+ if (strcmp(rb_class2name(RBASIC(hash)->klass), "HashWithIndifferentAccess") != 0) {
499
+ if ((rb_funcall(hash, rb_intern("has_key?"), 1, id_str) == Qtrue) &&
500
+ (rb_funcall(hash, rb_intern("has_key?"), 1, id_sym) == Qtrue)) {
501
+ VALUE oid_sym = rb_hash_delete(hash, id_sym);
502
+ rb_funcall(hash, rb_intern("[]="), 2, id_str, oid_sym);
503
+ }
499
504
  }
500
505
  }
501
506
 
507
+ if(allow_id == 1) {
508
+ write_function = write_element_with_id;
509
+ }
510
+ else {
511
+ write_function = write_element_without_id;
512
+ }
513
+
502
514
  // we have to check for an OrderedHash and handle that specially
503
515
  if (strcmp(rb_class2name(RBASIC(hash)->klass), "OrderedHash") == 0) {
504
516
  VALUE keys = rb_funcall(hash, rb_intern("keys"), 0);
505
517
  int i;
506
- for(i = 0; i < RARRAY_LEN(keys); i++) {
518
+ for(i = 0; i < RARRAY_LEN(keys); i++) {
507
519
  VALUE key = RARRAY_PTR(keys)[i];
508
520
  VALUE value = rb_hash_aref(hash, key);
509
521
 
510
- write_element_allow_id(key, value, pack_extra(buffer, check_keys), allow_id);
522
+ write_function(key, value, pack_extra(buffer, check_keys));
511
523
  }
512
524
  } else {
513
- rb_hash_foreach(hash, write_element_allow_id, pack_triple(buffer, check_keys, allow_id));
525
+ rb_hash_foreach(hash, write_function, pack_extra(buffer, check_keys));
514
526
  }
515
527
 
516
528
  // write null byte and fill in length
data/ext/cbson/version.h CHANGED
@@ -14,4 +14,4 @@
14
14
  * limitations under the License.
15
15
  */
16
16
 
17
- #define VERSION "0.18.3.2"
17
+ #define VERSION "0.19"
@@ -1,6 +1,6 @@
1
1
  require 'lib/mongo'
2
- #VERSION_HEADER = File.open(File.join(File.dirname(__FILE__), 'ext', 'cbson', 'version.h'), "r")
3
- VERSION = '0.18.3.2'
2
+ VERSION_HEADER = File.open(File.join(File.dirname(__FILE__), 'ext', 'cbson', 'version.h'), "r")
3
+ VERSION = VERSION_HEADER.read.scan(/VERSION\s+"(\d+\.\d+(\.\d+\w*)?)\"/)[0][0]
4
4
  Gem::Specification.new do |s|
5
5
  s.name = 'kbaum-mongo_ext'
6
6
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kbaum-mongo_ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.3.2
4
+ version: "0.19"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Dirolf
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-26 00:00:00 -05:00
12
+ date: 2010-03-01 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15