mongo_ext 0.18.3 → 0.19

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -47,7 +47,7 @@ namespace :test do
47
47
  end
48
48
 
49
49
  Rake::TestTask.new(:functional) do |t|
50
- t.test_files = FileList['test/test*.rb']
50
+ t.test_files = FileList['test/*_test.rb']
51
51
  t.verbose = true
52
52
  end
53
53
 
@@ -76,6 +76,16 @@ namespace :test do
76
76
  t.verbose = true
77
77
  end
78
78
 
79
+ Rake::TestTask.new(:auto_reconnect) do |t|
80
+ t.test_files = FileList['test/auxillary/autoreconnect_test.rb']
81
+ t.verbose = true
82
+ end
83
+
84
+ Rake::TestTask.new(:authentication) do |t|
85
+ t.test_files = FileList['test/auxillary/authentication_test.rb']
86
+ t.verbose = true
87
+ end
88
+
79
89
  task :drop_databases do |t|
80
90
  puts "Dropping test database..."
81
91
  require File.join(File.dirname(__FILE__), 'lib', 'mongo')
@@ -96,10 +106,10 @@ end
96
106
 
97
107
  desc "Generate YARD documentation"
98
108
  task :ydoc do
99
- version = eval(File.read("mongo-ruby-driver.gemspec")).version
100
- out = File.join('ydoc', version.to_s)
109
+ require File.join(File.dirname(__FILE__), 'lib', 'mongo')
110
+ out = File.join('ydoc', Mongo::VERSION)
101
111
  FileUtils.rm_rf('ydoc')
102
- system "yardoc lib/**/*.rb lib/mongo/**/*.rb -o #{out} --title MongoRuby-#{version}"
112
+ system "yardoc lib/**/*.rb lib/mongo/**/*.rb -e docs/yard_ext.rb -p docs/templates -o #{out} --title MongoRuby-#{Mongo::VERSION}"
103
113
  end
104
114
 
105
115
  desc "Publish documentation to mongo.rubyforge.org"
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright 2009 10gen, Inc.
2
+ * Copyright 2009-2010 10gen, Inc.
3
3
  *
4
4
  * Licensed under the Apache License, Version 2.0 (the "License");
5
5
  * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright 2009 10gen, Inc.
2
+ * Copyright 2009-2010 10gen, Inc.
3
3
  *
4
4
  * Licensed under the Apache License, Version 2.0 (the "License");
5
5
  * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright 2009 10gen, Inc.
2
+ * Copyright 2009-2010 10gen, Inc.
3
3
  *
4
4
  * Licensed under the Apache License, Version 2.0 (the "License");
5
5
  * you may not use this file except in compliance with the License.
@@ -62,6 +62,7 @@ static VALUE DBRef;
62
62
  static VALUE Code;
63
63
  static VALUE MinKey;
64
64
  static VALUE MaxKey;
65
+ static VALUE Regexp;
65
66
  static VALUE RegexpOfHolding;
66
67
  static VALUE OrderedHash;
67
68
  static VALUE InvalidName;
@@ -139,8 +140,9 @@ static int cmp_char(const void* a, const void* b) {
139
140
  return *(char*)a - *(char*)b;
140
141
  }
141
142
 
142
- static void write_doc(buffer_t buffer, VALUE hash, VALUE check_keys);
143
- static int write_element(VALUE key, VALUE value, VALUE extra);
143
+ static void write_doc(buffer_t buffer, VALUE hash, VALUE check_keys, VALUE move_id);
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);
144
146
  static VALUE elements_to_hash(const char* buffer, int max);
145
147
 
146
148
  static VALUE pack_extra(buffer_t buffer, VALUE check_keys) {
@@ -154,7 +156,7 @@ static void write_name_and_type(buffer_t buffer, VALUE name, char type) {
154
156
  SAFE_WRITE(buffer, &zero, 1);
155
157
  }
156
158
 
157
- 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) {
158
160
  buffer_t buffer = (buffer_t)NUM2LL(rb_ary_entry(extra, 0));
159
161
  VALUE check_keys = rb_ary_entry(extra, 1);
160
162
 
@@ -168,7 +170,7 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
168
170
  rb_raise(rb_eTypeError, "keys must be strings or symbols");
169
171
  }
170
172
 
171
- if (!allow_id && strcmp("_id", RSTRING_PTR(key)) == 0) {
173
+ if (allow_id == 0 && strcmp("_id", RSTRING_PTR(key)) == 0) {
172
174
  return ST_CONTINUE;
173
175
  }
174
176
 
@@ -236,7 +238,7 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
236
238
  case T_HASH:
237
239
  {
238
240
  write_name_and_type(buffer, key, 0x03);
239
- write_doc(buffer, value, check_keys);
241
+ write_doc(buffer, value, check_keys, Qfalse);
240
242
  break;
241
243
  }
242
244
  case T_ARRAY:
@@ -261,7 +263,7 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
261
263
  VALUE key;
262
264
  INT2STRING(&name, i);
263
265
  key = rb_str_new2(name);
264
- write_element(key, values[i], pack_extra(buffer, check_keys));
266
+ write_element_with_id(key, values[i], pack_extra(buffer, check_keys));
265
267
  free(name);
266
268
  }
267
269
 
@@ -289,7 +291,7 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
289
291
  SAFE_WRITE(buffer, (char*)&length, 4);
290
292
  SAFE_WRITE(buffer, RSTRING_PTR(value), length - 1);
291
293
  SAFE_WRITE(buffer, &zero, 1);
292
- write_doc(buffer, rb_funcall(value, rb_intern("scope"), 0), Qfalse);
294
+ write_doc(buffer, rb_funcall(value, rb_intern("scope"), 0), Qfalse, Qfalse);
293
295
 
294
296
  total_length = buffer_get_position(buffer) - start_position;
295
297
  SAFE_WRITE_AT_POS(buffer, length_location, (const char*)&total_length, 4);
@@ -361,9 +363,9 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
361
363
  }
362
364
 
363
365
  ns = rb_funcall(value, rb_intern("namespace"), 0);
364
- 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));
365
367
  oid = rb_funcall(value, rb_intern("object_id"), 0);
366
- 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));
367
369
 
368
370
  // write null byte and fill in length
369
371
  SAFE_WRITE(buffer, &zero, 1);
@@ -459,14 +461,20 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
459
461
  return ST_CONTINUE;
460
462
  }
461
463
 
462
- static int write_element(VALUE key, VALUE value, VALUE extra) {
463
- 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);
464
466
  }
465
467
 
466
- static void write_doc(buffer_t buffer, VALUE hash, VALUE check_keys) {
468
+ static int write_element_with_id(VALUE key, VALUE value, VALUE extra) {
469
+ return write_element(key, value, extra, 1);
470
+ }
471
+
472
+ static void write_doc(buffer_t buffer, VALUE hash, VALUE check_keys, VALUE move_id) {
467
473
  buffer_position start_position = buffer_get_position(buffer);
468
474
  buffer_position length_location = buffer_save_space(buffer, 4);
469
475
  buffer_position length;
476
+ int allow_id;
477
+ int (*write_function)(VALUE, VALUE, VALUE) = NULL;
470
478
  VALUE id_str = rb_str_new2("_id");
471
479
  VALUE id_sym = ID2SYM(rb_intern("_id"));
472
480
 
@@ -474,26 +482,47 @@ static void write_doc(buffer_t buffer, VALUE hash, VALUE check_keys) {
474
482
  rb_raise(rb_eNoMemError, "failed to allocate memory in buffer.c");
475
483
  }
476
484
 
477
- if (rb_funcall(hash, rb_intern("has_key?"), 1, id_str) == Qtrue) {
478
- VALUE id = rb_hash_aref(hash, id_str);
479
- write_element_allow_id(id_str, id, pack_extra(buffer, check_keys), 1);
480
- } else if (rb_funcall(hash, rb_intern("has_key?"), 1, id_sym) == Qtrue) {
481
- VALUE id = rb_hash_aref(hash, id_sym);
482
- write_element_allow_id(id_sym, id, pack_extra(buffer, check_keys), 1);
485
+ // write '_id' first if move_id is true. then don't allow an id to be written.
486
+ if(move_id == Qtrue) {
487
+ allow_id = 0;
488
+ if (rb_funcall(hash, rb_intern("has_key?"), 1, id_str) == Qtrue) {
489
+ VALUE id = rb_hash_aref(hash, id_str);
490
+ write_element_with_id(id_str, id, pack_extra(buffer, check_keys));
491
+ } else if (rb_funcall(hash, rb_intern("has_key?"), 1, id_sym) == Qtrue) {
492
+ VALUE id = rb_hash_aref(hash, id_sym);
493
+ write_element_with_id(id_sym, id, pack_extra(buffer, check_keys));
494
+ }
495
+ }
496
+ else {
497
+ allow_id = 1;
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
+ }
504
+ }
505
+ }
506
+
507
+ if(allow_id == 1) {
508
+ write_function = write_element_with_id;
509
+ }
510
+ else {
511
+ write_function = write_element_without_id;
483
512
  }
484
513
 
485
514
  // we have to check for an OrderedHash and handle that specially
486
515
  if (strcmp(rb_class2name(RBASIC(hash)->klass), "OrderedHash") == 0) {
487
516
  VALUE keys = rb_funcall(hash, rb_intern("keys"), 0);
488
517
  int i;
489
- for(i = 0; i < RARRAY_LEN(keys); i++) {
518
+ for(i = 0; i < RARRAY_LEN(keys); i++) {
490
519
  VALUE key = RARRAY_PTR(keys)[i];
491
520
  VALUE value = rb_hash_aref(hash, key);
492
521
 
493
- write_element(key, value, pack_extra(buffer, check_keys));
522
+ write_function(key, value, pack_extra(buffer, check_keys));
494
523
  }
495
524
  } else {
496
- rb_hash_foreach(hash, write_element, pack_extra(buffer, check_keys));
525
+ rb_hash_foreach(hash, write_function, pack_extra(buffer, check_keys));
497
526
  }
498
527
 
499
528
  // write null byte and fill in length
@@ -509,14 +538,14 @@ static void write_doc(buffer_t buffer, VALUE hash, VALUE check_keys) {
509
538
  SAFE_WRITE_AT_POS(buffer, length_location, (const char*)&length, 4);
510
539
  }
511
540
 
512
- static VALUE method_serialize(VALUE self, VALUE doc, VALUE check_keys) {
541
+ static VALUE method_serialize(VALUE self, VALUE doc, VALUE check_keys, VALUE move_id) {
513
542
  VALUE result;
514
543
  buffer_t buffer = buffer_new();
515
544
  if (buffer == NULL) {
516
545
  rb_raise(rb_eNoMemError, "failed to allocate memory in buffer.c");
517
546
  }
518
547
 
519
- write_doc(buffer, doc, check_keys);
548
+ write_doc(buffer, doc, check_keys, move_id);
520
549
 
521
550
  result = rb_str_new(buffer_get_buffer(buffer), buffer_get_position(buffer));
522
551
  if (buffer_free(buffer) != 0) {
@@ -677,8 +706,13 @@ static VALUE get_value(const char* buffer, int* position, int type) {
677
706
  }
678
707
  argv[0] = pattern;
679
708
  argv[1] = INT2FIX(flags);
680
- argv[2] = rb_str_new2(extra);
681
- value = rb_class_new_instance(3, argv, RegexpOfHolding);
709
+ if(extra[0] == 0) {
710
+ value = rb_class_new_instance(2, argv, Regexp);
711
+ }
712
+ else { // Deserializing a RegexpOfHolding
713
+ argv[2] = rb_str_new2(extra);
714
+ value = rb_class_new_instance(3, argv, RegexpOfHolding);
715
+ }
682
716
  *position += flags_length + 1;
683
717
  break;
684
718
  }
@@ -861,6 +895,7 @@ void Init_cbson() {
861
895
  MinKey = rb_const_get(mongo, rb_intern("MinKey"));
862
896
  MaxKey = rb_const_get(mongo, rb_intern("MaxKey"));
863
897
  rb_require("mongo/types/regexp_of_holding");
898
+ Regexp = rb_const_get(rb_cObject, rb_intern("Regexp"));
864
899
  RegexpOfHolding = rb_const_get(mongo, rb_intern("RegexpOfHolding"));
865
900
  rb_require("mongo/exceptions");
866
901
  InvalidName = rb_const_get(mongo, rb_intern("InvalidName"));
@@ -872,7 +907,7 @@ void Init_cbson() {
872
907
  CBson = rb_define_module("CBson");
873
908
  ext_version = rb_str_new2(VERSION);
874
909
  rb_define_const(CBson, "VERSION", ext_version);
875
- rb_define_module_function(CBson, "serialize", method_serialize, 2);
910
+ rb_define_module_function(CBson, "serialize", method_serialize, 3);
876
911
  rb_define_module_function(CBson, "deserialize", method_deserialize, 1);
877
912
 
878
913
  rb_require("digest/md5");
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright 2009 10gen, Inc.
2
+ * Copyright 2009-2010 10gen, Inc.
3
3
  *
4
4
  * Licensed under the Apache License, Version 2.0 (the "License");
5
5
  * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright 2009 10gen, Inc.
2
+ * Copyright 2009-2010 10gen, Inc.
3
3
  *
4
4
  * Licensed under the Apache License, Version 2.0 (the "License");
5
5
  * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright 2009 10gen, Inc.
2
+ * Copyright 2009-2010 10gen, Inc.
3
3
  *
4
4
  * Licensed under the Apache License, Version 2.0 (the "License");
5
5
  * you may not use this file except in compliance with the License.
@@ -14,4 +14,4 @@
14
14
  * limitations under the License.
15
15
  */
16
16
 
17
- #define VERSION "0.18.3"
17
+ #define VERSION "0.19"
@@ -1,6 +1,6 @@
1
1
  require 'lib/mongo'
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+)?)\"/)[0][0]
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 = 'mongo_ext'
6
6
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.3
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-01-25 00:00:00 -05:00
12
+ date: 2010-03-01 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -31,7 +31,7 @@ files:
31
31
  - ext/cbson/buffer.h
32
32
  - ext/cbson/encoding_helpers.h
33
33
  - ext/cbson/version.h
34
- has_rdoc: false
34
+ has_rdoc: true
35
35
  homepage: http://www.mongodb.org
36
36
  licenses: []
37
37