mongodb-mongo 0.12 → 0.13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. data/README.rdoc +12 -12
  2. data/Rakefile +1 -1
  3. data/bin/bson_benchmark.rb +1 -1
  4. data/bin/mongo_console +3 -3
  5. data/bin/run_test_script +2 -2
  6. data/bin/standard_benchmark +3 -3
  7. data/examples/admin.rb +3 -3
  8. data/examples/benchmarks.rb +2 -2
  9. data/examples/blog.rb +4 -4
  10. data/examples/capped.rb +3 -3
  11. data/examples/cursor.rb +3 -3
  12. data/examples/gridfs.rb +4 -4
  13. data/examples/index_test.rb +11 -11
  14. data/examples/info.rb +3 -3
  15. data/examples/queries.rb +3 -3
  16. data/examples/simple.rb +3 -3
  17. data/examples/strict.rb +3 -3
  18. data/examples/types.rb +4 -9
  19. data/lib/mongo.rb +35 -3
  20. data/lib/mongo/admin.rb +56 -60
  21. data/lib/mongo/collection.rb +368 -320
  22. data/lib/mongo/connection.rb +166 -0
  23. data/lib/mongo/cursor.rb +206 -209
  24. data/lib/mongo/db.rb +478 -489
  25. data/lib/mongo/errors.rb +8 -9
  26. data/lib/mongo/gridfs/chunk.rb +66 -70
  27. data/lib/mongo/gridfs/grid_store.rb +406 -410
  28. data/lib/mongo/message/get_more_message.rb +8 -13
  29. data/lib/mongo/message/insert_message.rb +7 -11
  30. data/lib/mongo/message/kill_cursors_message.rb +7 -12
  31. data/lib/mongo/message/message.rb +58 -62
  32. data/lib/mongo/message/message_header.rb +19 -24
  33. data/lib/mongo/message/msg_message.rb +5 -9
  34. data/lib/mongo/message/opcodes.rb +10 -15
  35. data/lib/mongo/message/query_message.rb +42 -46
  36. data/lib/mongo/message/remove_message.rb +8 -12
  37. data/lib/mongo/message/update_message.rb +9 -13
  38. data/lib/mongo/query.rb +84 -88
  39. data/lib/mongo/types/binary.rb +13 -17
  40. data/lib/mongo/types/code.rb +9 -13
  41. data/lib/mongo/types/dbref.rb +10 -14
  42. data/lib/mongo/types/objectid.rb +103 -107
  43. data/lib/mongo/types/regexp_of_holding.rb +18 -22
  44. data/lib/mongo/types/undefined.rb +7 -10
  45. data/lib/mongo/util/bson.rb +4 -9
  46. data/lib/mongo/util/xml_to_ruby.rb +1 -3
  47. data/mongo-ruby-driver.gemspec +33 -32
  48. data/{tests → test}/mongo-qa/_common.rb +1 -1
  49. data/{tests → test}/mongo-qa/admin +1 -1
  50. data/{tests → test}/mongo-qa/capped +1 -1
  51. data/{tests → test}/mongo-qa/count1 +4 -4
  52. data/{tests → test}/mongo-qa/dbs +1 -1
  53. data/{tests → test}/mongo-qa/find +1 -1
  54. data/{tests → test}/mongo-qa/find1 +1 -1
  55. data/{tests → test}/mongo-qa/gridfs_in +2 -2
  56. data/{tests → test}/mongo-qa/gridfs_out +2 -2
  57. data/{tests → test}/mongo-qa/indices +2 -2
  58. data/{tests → test}/mongo-qa/remove +1 -1
  59. data/{tests → test}/mongo-qa/stress1 +1 -1
  60. data/{tests → test}/mongo-qa/test1 +1 -1
  61. data/{tests → test}/mongo-qa/update +1 -1
  62. data/{tests → test}/test_admin.rb +3 -3
  63. data/{tests → test}/test_bson.rb +4 -4
  64. data/{tests → test}/test_byte_buffer.rb +0 -0
  65. data/{tests → test}/test_chunk.rb +4 -4
  66. data/{tests → test}/test_collection.rb +42 -4
  67. data/{tests/test_mongo.rb → test/test_connection.rb} +35 -11
  68. data/test/test_cursor.rb +223 -0
  69. data/{tests → test}/test_db.rb +12 -12
  70. data/{tests → test}/test_db_api.rb +28 -33
  71. data/{tests → test}/test_db_connection.rb +3 -3
  72. data/{tests → test}/test_grid_store.rb +4 -4
  73. data/{tests → test}/test_message.rb +1 -1
  74. data/{tests → test}/test_objectid.rb +3 -3
  75. data/{tests → test}/test_ordered_hash.rb +0 -0
  76. data/{tests → test}/test_round_trip.rb +6 -2
  77. data/{tests → test}/test_threading.rb +3 -3
  78. data/test/test_xgen.rb +73 -0
  79. metadata +33 -32
  80. data/lib/mongo/mongo.rb +0 -164
  81. data/tests/test_cursor.rb +0 -121
@@ -17,21 +17,16 @@
17
17
  require 'mongo/message/message'
18
18
  require 'mongo/message/opcodes'
19
19
 
20
- module XGen
21
- module Mongo
22
- module Driver
20
+ module Mongo
23
21
 
24
- class GetMoreMessage < Message
22
+ class GetMoreMessage < Message
25
23
 
26
- def initialize(db_name, collection_name, cursor)
27
- super(OP_GET_MORE)
28
- write_int(0)
29
- write_string("#{db_name}.#{collection_name}")
30
- write_int(0) # num to return; leave it up to the db for now
31
- write_long(cursor)
32
- end
33
- end
24
+ def initialize(db_name, collection_name, cursor)
25
+ super(OP_GET_MORE)
26
+ write_int(0)
27
+ write_string("#{db_name}.#{collection_name}")
28
+ write_int(0) # num to return; leave it up to the db for now
29
+ write_long(cursor)
34
30
  end
35
31
  end
36
32
  end
37
-
@@ -17,19 +17,15 @@
17
17
  require 'mongo/message/message'
18
18
  require 'mongo/message/opcodes'
19
19
 
20
- module XGen
21
- module Mongo
22
- module Driver
20
+ module Mongo
23
21
 
24
- class InsertMessage < Message
22
+ class InsertMessage < Message
25
23
 
26
- def initialize(db_name, collection_name, check_keys=true, *objs)
27
- super(OP_INSERT)
28
- write_int(0)
29
- write_string("#{db_name}.#{collection_name}")
30
- objs.each { |o| write_doc(o, check_keys) }
31
- end
32
- end
24
+ def initialize(db_name, collection_name, check_keys=true, *objs)
25
+ super(OP_INSERT)
26
+ write_int(0)
27
+ write_string("#{db_name}.#{collection_name}")
28
+ objs.each { |o| write_doc(o, check_keys) }
33
29
  end
34
30
  end
35
31
  end
@@ -17,20 +17,15 @@
17
17
  require 'mongo/message/message'
18
18
  require 'mongo/message/opcodes'
19
19
 
20
- module XGen
21
- module Mongo
22
- module Driver
20
+ module Mongo
23
21
 
24
- class KillCursorsMessage < Message
22
+ class KillCursorsMessage < Message
25
23
 
26
- def initialize(*cursors)
27
- super(OP_KILL_CURSORS)
28
- write_int(0)
29
- write_int(cursors.length)
30
- cursors.each { |c| write_long c }
31
- end
32
- end
24
+ def initialize(*cursors)
25
+ super(OP_KILL_CURSORS)
26
+ write_int(0)
27
+ write_int(cursors.length)
28
+ cursors.each { |c| write_long c }
33
29
  end
34
30
  end
35
31
  end
36
-
@@ -17,68 +17,64 @@
17
17
  require 'mongo/util/bson'
18
18
  require 'mongo/util/byte_buffer'
19
19
 
20
- module XGen
21
- module Mongo
22
- module Driver
23
-
24
- class Message
25
-
26
- HEADER_SIZE = 16 # size, id, response_to, opcode
27
-
28
- @@class_req_id = 0
29
-
30
- attr_reader :buf # for testing
31
-
32
- def initialize(op)
33
- @op = op
34
- @message_length = HEADER_SIZE
35
- @data_length = 0
36
- @request_id = (@@class_req_id += 1)
37
- @response_id = 0
38
- @buf = ByteBuffer.new
39
-
40
- @buf.put_int(16) # holder for length
41
- @buf.put_int(@request_id)
42
- @buf.put_int(0) # response_to
43
- @buf.put_int(op)
44
- end
45
-
46
- def write_int(i)
47
- @buf.put_int(i)
48
- update_message_length
49
- end
50
-
51
- def write_long(i)
52
- @buf.put_long(i)
53
- update_message_length
54
- end
55
-
56
- def write_string(s)
57
- BSON.serialize_cstr(@buf, s)
58
- update_message_length
59
- end
60
-
61
- def write_doc(hash, check_keys=false)
62
- @buf.put_array(BSON.new.serialize(hash, check_keys).to_a)
63
- update_message_length
64
- end
65
-
66
- def to_a
67
- @buf.to_a
68
- end
69
-
70
- def dump
71
- @buf.dump
72
- end
73
-
74
- # Do not call. Private, but kept public for testing.
75
- def update_message_length
76
- pos = @buf.position
77
- @buf.put_int(@buf.size, 0)
78
- @buf.position = pos
79
- end
80
-
81
- end
20
+ module Mongo
21
+
22
+ class Message
23
+
24
+ HEADER_SIZE = 16 # size, id, response_to, opcode
25
+
26
+ @@class_req_id = 0
27
+
28
+ attr_reader :buf # for testing
29
+
30
+ def initialize(op)
31
+ @op = op
32
+ @message_length = HEADER_SIZE
33
+ @data_length = 0
34
+ @request_id = (@@class_req_id += 1)
35
+ @response_id = 0
36
+ @buf = ByteBuffer.new
37
+
38
+ @buf.put_int(16) # holder for length
39
+ @buf.put_int(@request_id)
40
+ @buf.put_int(0) # response_to
41
+ @buf.put_int(op)
82
42
  end
43
+
44
+ def write_int(i)
45
+ @buf.put_int(i)
46
+ update_message_length
47
+ end
48
+
49
+ def write_long(i)
50
+ @buf.put_long(i)
51
+ update_message_length
52
+ end
53
+
54
+ def write_string(s)
55
+ BSON.serialize_cstr(@buf, s)
56
+ update_message_length
57
+ end
58
+
59
+ def write_doc(hash, check_keys=false)
60
+ @buf.put_array(BSON.new.serialize(hash, check_keys).to_a)
61
+ update_message_length
62
+ end
63
+
64
+ def to_a
65
+ @buf.to_a
66
+ end
67
+
68
+ def dump
69
+ @buf.dump
70
+ end
71
+
72
+ # Do not call. Private, but kept public for testing.
73
+ def update_message_length
74
+ pos = @buf.position
75
+ @buf.put_int(@buf.size, 0)
76
+ @buf.position = pos
77
+ end
78
+
83
79
  end
84
80
  end
@@ -16,35 +16,30 @@
16
16
 
17
17
  require 'mongo/util/byte_buffer'
18
18
 
19
- module XGen
20
- module Mongo
21
- module Driver
19
+ module Mongo
22
20
 
23
- class MessageHeader
21
+ class MessageHeader
24
22
 
25
- HEADER_SIZE = 16
23
+ HEADER_SIZE = 16
26
24
 
27
- def initialize()
28
- @buf = ByteBuffer.new
29
- end
25
+ def initialize()
26
+ @buf = ByteBuffer.new
27
+ end
30
28
 
31
- def read_header(db)
32
- @buf.rewind
33
- @buf.put_array(db.receive_full(HEADER_SIZE).unpack("C*"))
34
- raise "Short read for DB response header: expected #{HEADER_SIZE} bytes, saw #{@buf.size}" unless @buf.size == HEADER_SIZE
35
- @buf.rewind
36
- @size = @buf.get_int
37
- @request_id = @buf.get_int
38
- @response_to = @buf.get_int
39
- @op = @buf.get_int
40
- self
41
- end
29
+ def read_header(db)
30
+ @buf.rewind
31
+ @buf.put_array(db.receive_full(HEADER_SIZE).unpack("C*"))
32
+ raise "Short read for DB response header: expected #{HEADER_SIZE} bytes, saw #{@buf.size}" unless @buf.size == HEADER_SIZE
33
+ @buf.rewind
34
+ @size = @buf.get_int
35
+ @request_id = @buf.get_int
36
+ @response_to = @buf.get_int
37
+ @op = @buf.get_int
38
+ self
39
+ end
42
40
 
43
- def dump
44
- @buf.dump
45
- end
46
- end
41
+ def dump
42
+ @buf.dump
47
43
  end
48
44
  end
49
45
  end
50
-
@@ -17,17 +17,13 @@
17
17
  require 'mongo/message/message'
18
18
  require 'mongo/message/opcodes'
19
19
 
20
- module XGen
21
- module Mongo
22
- module Driver
20
+ module Mongo
23
21
 
24
- class MsgMessage < Message
22
+ class MsgMessage < Message
25
23
 
26
- def initialize(msg)
27
- super(OP_MSG)
28
- write_string(msg)
29
- end
30
- end
24
+ def initialize(msg)
25
+ super(OP_MSG)
26
+ write_string(msg)
31
27
  end
32
28
  end
33
29
  end
@@ -14,19 +14,14 @@
14
14
  # limitations under the License.
15
15
  # ++
16
16
 
17
- module XGen
18
- module Mongo
19
- module Driver
20
- OP_REPLY = 1 # reply. responseTo is set.
21
- OP_MSG = 1000 # generic msg command followed by a string
22
- OP_UPDATE = 2001 # update object
23
- OP_INSERT = 2002
24
- # GET_BY_OID = 2003
25
- OP_QUERY = 2004
26
- OP_GET_MORE = 2005
27
- OP_DELETE = 2006
28
- OP_KILL_CURSORS = 2007
29
- end
30
- end
17
+ module Mongo
18
+ OP_REPLY = 1 # reply. responseTo is set.
19
+ OP_MSG = 1000 # generic msg command followed by a string
20
+ OP_UPDATE = 2001 # update object
21
+ OP_INSERT = 2002
22
+ # GET_BY_OID = 2003
23
+ OP_QUERY = 2004
24
+ OP_GET_MORE = 2005
25
+ OP_DELETE = 2006
26
+ OP_KILL_CURSORS = 2007
31
27
  end
32
-
@@ -18,60 +18,56 @@ require 'mongo/message/message'
18
18
  require 'mongo/message/opcodes'
19
19
  require 'mongo/util/ordered_hash'
20
20
 
21
- module XGen
22
- module Mongo
23
- module Driver
21
+ module Mongo
24
22
 
25
- class QueryMessage < Message
23
+ class QueryMessage < Message
26
24
 
27
- attr_reader :query
25
+ attr_reader :query
28
26
 
29
- def initialize(db_name, collection_name, query)
30
- super(OP_QUERY)
31
- @query = query
32
- write_int(0)
33
- write_string("#{db_name}.#{collection_name}")
34
- write_int(query.number_to_skip)
35
- write_int(-query.number_to_return) # Negative means hard limit
36
- sel = query.selector
37
- if query.contains_special_fields
38
- sel = OrderedHash.new
39
- sel['query'] = query.selector
40
- if query.order_by && query.order_by.length > 0
41
- sel['orderby'] = case query.order_by
27
+ def initialize(db_name, collection_name, query)
28
+ super(OP_QUERY)
29
+ @query = query
30
+ write_int(0)
31
+ write_string("#{db_name}.#{collection_name}")
32
+ write_int(query.number_to_skip)
33
+ write_int(-query.number_to_return) # Negative means hard limit
34
+ sel = query.selector
35
+ if query.contains_special_fields
36
+ sel = OrderedHash.new
37
+ sel['query'] = query.selector
38
+ if query.order_by && query.order_by.length > 0
39
+ sel['orderby'] = case query.order_by
40
+ when String
41
+ {query.order_by => 1}
42
+ when Array
43
+ h = OrderedHash.new
44
+ query.order_by.each { |ob|
45
+ case ob
42
46
  when String
43
- {query.order_by => 1}
44
- when Array
45
- h = OrderedHash.new
46
- query.order_by.each { |ob|
47
- case ob
48
- when String
49
- h[ob] = 1
50
- when Hash # should have one entry; will handle all
51
- ob.each { |k,v| h[k] = v }
52
- else
53
- raise "illegal query order_by value #{query.order_by.inspect}"
54
- end
55
- }
56
- h
57
- when Hash # Should be an ordered hash, but this message doesn't care
58
- query.order_by
47
+ h[ob] = 1
48
+ when Hash # should have one entry; will handle all
49
+ ob.each { |k,v| h[k] = v }
59
50
  else
60
- raise "illegal order_by: is a #{query.order_by.class.name}, must be String, Array, Hash, or OrderedHash"
51
+ raise "illegal query order_by value #{query.order_by.inspect}"
61
52
  end
62
- end
63
- sel['$hint'] = query.hint if query.hint && query.hint.length > 0
64
- sel['$explain'] = true if query.explain
65
- sel['$snapshot'] = true if query.snapshot
66
- end
67
- write_doc(sel)
68
- write_doc(query.fields) if query.fields
69
- end
70
-
71
- def first_key(key)
72
- @first_key = key
53
+ }
54
+ h
55
+ when Hash # Should be an ordered hash, but this message doesn't care
56
+ query.order_by
57
+ else
58
+ raise "illegal order_by: is a #{query.order_by.class.name}, must be String, Array, Hash, or OrderedHash"
59
+ end
73
60
  end
61
+ sel['$hint'] = query.hint if query.hint && query.hint.length > 0
62
+ sel['$explain'] = true if query.explain
63
+ sel['$snapshot'] = true if query.snapshot
74
64
  end
65
+ write_doc(sel)
66
+ write_doc(query.fields) if query.fields
67
+ end
68
+
69
+ def first_key(key)
70
+ @first_key = key
75
71
  end
76
72
  end
77
73
  end
@@ -17,20 +17,16 @@
17
17
  require 'mongo/message/message'
18
18
  require 'mongo/message/opcodes'
19
19
 
20
- module XGen
21
- module Mongo
22
- module Driver
20
+ module Mongo
23
21
 
24
- class RemoveMessage < Message
22
+ class RemoveMessage < Message
25
23
 
26
- def initialize(db_name, collection_name, sel)
27
- super(OP_DELETE)
28
- write_int(0)
29
- write_string("#{db_name}.#{collection_name}")
30
- write_int(0) # flags?
31
- write_doc(sel)
32
- end
33
- end
24
+ def initialize(db_name, collection_name, sel)
25
+ super(OP_DELETE)
26
+ write_int(0)
27
+ write_string("#{db_name}.#{collection_name}")
28
+ write_int(0) # flags?
29
+ write_doc(sel)
34
30
  end
35
31
  end
36
32
  end