mongo 0.1.0 → 0.15

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.
Files changed (89) hide show
  1. data/README.rdoc +268 -71
  2. data/Rakefile +27 -62
  3. data/bin/bson_benchmark.rb +59 -0
  4. data/bin/mongo_console +3 -3
  5. data/bin/run_test_script +19 -0
  6. data/bin/standard_benchmark +109 -0
  7. data/examples/admin.rb +41 -0
  8. data/examples/benchmarks.rb +42 -0
  9. data/examples/blog.rb +76 -0
  10. data/examples/capped.rb +23 -0
  11. data/examples/cursor.rb +47 -0
  12. data/examples/gridfs.rb +87 -0
  13. data/examples/index_test.rb +125 -0
  14. data/examples/info.rb +30 -0
  15. data/examples/queries.rb +69 -0
  16. data/examples/simple.rb +23 -0
  17. data/examples/strict.rb +34 -0
  18. data/examples/types.rb +35 -0
  19. data/lib/mongo.rb +9 -2
  20. data/lib/mongo/admin.rb +65 -68
  21. data/lib/mongo/collection.rb +379 -117
  22. data/lib/mongo/connection.rb +151 -0
  23. data/lib/mongo/cursor.rb +271 -216
  24. data/lib/mongo/db.rb +500 -315
  25. data/lib/mongo/errors.rb +26 -0
  26. data/lib/mongo/gridfs.rb +16 -0
  27. data/lib/mongo/gridfs/chunk.rb +92 -0
  28. data/lib/mongo/gridfs/grid_store.rb +464 -0
  29. data/lib/mongo/message.rb +16 -0
  30. data/lib/mongo/message/get_more_message.rb +24 -13
  31. data/lib/mongo/message/insert_message.rb +29 -11
  32. data/lib/mongo/message/kill_cursors_message.rb +23 -12
  33. data/lib/mongo/message/message.rb +74 -62
  34. data/lib/mongo/message/message_header.rb +35 -24
  35. data/lib/mongo/message/msg_message.rb +21 -9
  36. data/lib/mongo/message/opcodes.rb +26 -15
  37. data/lib/mongo/message/query_message.rb +63 -43
  38. data/lib/mongo/message/remove_message.rb +29 -12
  39. data/lib/mongo/message/update_message.rb +30 -13
  40. data/lib/mongo/query.rb +97 -89
  41. data/lib/mongo/types/binary.rb +25 -21
  42. data/lib/mongo/types/code.rb +30 -0
  43. data/lib/mongo/types/dbref.rb +19 -23
  44. data/lib/mongo/types/objectid.rb +130 -116
  45. data/lib/mongo/types/regexp_of_holding.rb +27 -31
  46. data/lib/mongo/util/bson.rb +273 -160
  47. data/lib/mongo/util/byte_buffer.rb +32 -28
  48. data/lib/mongo/util/ordered_hash.rb +88 -42
  49. data/lib/mongo/util/xml_to_ruby.rb +18 -15
  50. data/mongo-ruby-driver.gemspec +103 -0
  51. data/test/mongo-qa/_common.rb +8 -0
  52. data/test/mongo-qa/admin +26 -0
  53. data/test/mongo-qa/capped +22 -0
  54. data/test/mongo-qa/count1 +18 -0
  55. data/test/mongo-qa/dbs +22 -0
  56. data/test/mongo-qa/find +10 -0
  57. data/test/mongo-qa/find1 +15 -0
  58. data/test/mongo-qa/gridfs_in +16 -0
  59. data/test/mongo-qa/gridfs_out +17 -0
  60. data/test/mongo-qa/indices +49 -0
  61. data/test/mongo-qa/remove +25 -0
  62. data/test/mongo-qa/stress1 +35 -0
  63. data/test/mongo-qa/test1 +11 -0
  64. data/test/mongo-qa/update +18 -0
  65. data/{tests → test}/test_admin.rb +25 -16
  66. data/test/test_bson.rb +268 -0
  67. data/{tests → test}/test_byte_buffer.rb +0 -0
  68. data/test/test_chunk.rb +84 -0
  69. data/test/test_collection.rb +282 -0
  70. data/test/test_connection.rb +101 -0
  71. data/test/test_cursor.rb +321 -0
  72. data/test/test_db.rb +196 -0
  73. data/test/test_db_api.rb +798 -0
  74. data/{tests → test}/test_db_connection.rb +4 -3
  75. data/test/test_grid_store.rb +284 -0
  76. data/{tests → test}/test_message.rb +1 -1
  77. data/test/test_objectid.rb +105 -0
  78. data/{tests → test}/test_ordered_hash.rb +55 -0
  79. data/{tests → test}/test_round_trip.rb +13 -9
  80. data/test/test_threading.rb +37 -0
  81. metadata +74 -32
  82. data/bin/validate +0 -51
  83. data/lib/mongo/mongo.rb +0 -74
  84. data/lib/mongo/types/undefined.rb +0 -31
  85. data/tests/test_bson.rb +0 -135
  86. data/tests/test_cursor.rb +0 -66
  87. data/tests/test_db.rb +0 -51
  88. data/tests/test_db_api.rb +0 -349
  89. data/tests/test_objectid.rb +0 -88
@@ -1,3 +1,19 @@
1
+ # --
2
+ # Copyright (C) 2008-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
+
1
17
  %w(get_more_message insert_message kill_cursors_message message_header
2
18
  msg_message query_message remove_message update_message).each { |f|
3
19
  require "mongo/message/#{f}"
@@ -1,21 +1,32 @@
1
+ # --
2
+ # Copyright (C) 2008-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
+
1
17
  require 'mongo/message/message'
2
18
  require 'mongo/message/opcodes'
3
19
 
4
- module XGen
5
- module Mongo
6
- module Driver
20
+ module Mongo
7
21
 
8
- class GetMoreMessage < Message
22
+ class GetMoreMessage < Message
9
23
 
10
- def initialize(db_name, collection_name, cursor)
11
- super(OP_GET_MORE)
12
- write_int(0)
13
- write_string("#{db_name}.#{collection_name}")
14
- write_int(0) # num to return; leave it up to the db for now
15
- write_long(cursor)
16
- end
17
- 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)
18
30
  end
19
31
  end
20
32
  end
21
-
@@ -1,19 +1,37 @@
1
+ # --
2
+ # Copyright (C) 2008-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
+
1
17
  require 'mongo/message/message'
2
18
  require 'mongo/message/opcodes'
3
19
 
4
- module XGen
5
- module Mongo
6
- module Driver
20
+ module Mongo
21
+
22
+ class InsertMessage < Message
7
23
 
8
- class InsertMessage < Message
24
+ def initialize(db_name, collection_name, check_keys=true, *objs)
25
+ @collection_name = collection_name
26
+ @objs = 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
9
32
 
10
- def initialize(db_name, collection_name, *objs)
11
- super(OP_INSERT)
12
- write_int(0)
13
- write_string("#{db_name}.#{collection_name}")
14
- objs.each { |o| write_doc(o) }
15
- end
16
- end
33
+ def to_s
34
+ "db.#{@collection_name}.insert(#{@objs.inspect})"
17
35
  end
18
36
  end
19
37
  end
@@ -1,20 +1,31 @@
1
+ # --
2
+ # Copyright (C) 2008-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
+
1
17
  require 'mongo/message/message'
2
18
  require 'mongo/message/opcodes'
3
19
 
4
- module XGen
5
- module Mongo
6
- module Driver
20
+ module Mongo
7
21
 
8
- class KillCursorsMessage < Message
22
+ class KillCursorsMessage < Message
9
23
 
10
- def initialize(*cursors)
11
- super(OP_KILL_CURSORS)
12
- write_int(0)
13
- write_int(cursors.length)
14
- cursors.each { |c| write_long c }
15
- end
16
- 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 }
17
29
  end
18
30
  end
19
31
  end
20
-
@@ -1,68 +1,80 @@
1
+ # --
2
+ # Copyright (C) 2008-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
+
1
17
  require 'mongo/util/bson'
2
18
  require 'mongo/util/byte_buffer'
3
19
 
4
- module XGen
5
- module Mongo
6
- module Driver
7
-
8
- class Message
9
-
10
- HEADER_SIZE = 16 # size, id, response_to, opcode
11
-
12
- @@class_req_id = 0
13
-
14
- attr_reader :buf # for testing
15
-
16
- def initialize(op)
17
- @op = op
18
- @message_length = HEADER_SIZE
19
- @data_length = 0
20
- @request_id = (@@class_req_id += 1)
21
- @response_id = 0
22
- @buf = ByteBuffer.new
23
-
24
- @buf.put_int(16) # holder for length
25
- @buf.put_int(@request_id)
26
- @buf.put_int(0) # response_to
27
- @buf.put_int(op)
28
- end
29
-
30
- def write_int(i)
31
- @buf.put_int(i)
32
- update_message_length
33
- end
34
-
35
- def write_long(i)
36
- @buf.put_long(i)
37
- update_message_length
38
- end
39
-
40
- def write_string(s)
41
- BSON.serialize_cstr(@buf, s)
42
- update_message_length
43
- end
44
-
45
- def write_doc(hash)
46
- @buf.put_array(BSON.new.serialize(hash).to_a)
47
- update_message_length
48
- end
49
-
50
- def to_a
51
- @buf.to_a
52
- end
53
-
54
- def dump
55
- @buf.dump
56
- end
57
-
58
- # Do not call. Private, but kept public for testing.
59
- def update_message_length
60
- pos = @buf.position
61
- @buf.put_int(@buf.size, 0)
62
- @buf.position = pos
63
- end
64
-
65
- 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)
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
66
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
+
67
79
  end
68
80
  end
@@ -1,34 +1,45 @@
1
+ # --
2
+ # Copyright (C) 2008-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
+
1
17
  require 'mongo/util/byte_buffer'
2
18
 
3
- module XGen
4
- module Mongo
5
- module Driver
19
+ module Mongo
6
20
 
7
- class MessageHeader
21
+ class MessageHeader
8
22
 
9
- HEADER_SIZE = 16
23
+ HEADER_SIZE = 16
10
24
 
11
- def initialize()
12
- @buf = ByteBuffer.new
13
- end
25
+ def initialize()
26
+ @buf = ByteBuffer.new
27
+ end
14
28
 
15
- def read_header(socket)
16
- @buf.rewind
17
- @buf.put_array(socket.recv(HEADER_SIZE).unpack("C*"))
18
- raise "Short read for DB response header: expected #{HEADER_SIZE} bytes, saw #{@buf.size}" unless @buf.size == HEADER_SIZE
19
- @buf.rewind
20
- @size = @buf.get_int
21
- @request_id = @buf.get_int
22
- @response_to = @buf.get_int
23
- @op = @buf.get_int
24
- self
25
- 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
26
40
 
27
- def dump
28
- @buf.dump
29
- end
30
- end
41
+ def dump
42
+ @buf.dump
31
43
  end
32
44
  end
33
45
  end
34
-
@@ -1,17 +1,29 @@
1
+ # --
2
+ # Copyright (C) 2008-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
+
1
17
  require 'mongo/message/message'
2
18
  require 'mongo/message/opcodes'
3
19
 
4
- module XGen
5
- module Mongo
6
- module Driver
20
+ module Mongo
7
21
 
8
- class MsgMessage < Message
22
+ class MsgMessage < Message
9
23
 
10
- def initialize(msg)
11
- super(OP_MSG)
12
- write_string(msg)
13
- end
14
- end
24
+ def initialize(msg)
25
+ super(OP_MSG)
26
+ write_string(msg)
15
27
  end
16
28
  end
17
29
  end
@@ -1,16 +1,27 @@
1
- module XGen
2
- module Mongo
3
- module Driver
4
- OP_REPLY = 1 # reply. responseTo is set.
5
- OP_MSG = 1000 # generic msg command followed by a string
6
- OP_UPDATE = 2001 # update object
7
- OP_INSERT = 2002
8
- # GET_BY_OID = 2003
9
- OP_QUERY = 2004
10
- OP_GET_MORE = 2005
11
- OP_DELETE = 2006
12
- OP_KILL_CURSORS = 2007
13
- end
14
- end
15
- end
1
+ # --
2
+ # Copyright (C) 2008-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
16
 
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
27
+ end