pahagon-mongo-abd 0.14.1

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 +353 -0
  2. data/Rakefile +62 -0
  3. data/bin/bson_benchmark.rb +59 -0
  4. data/bin/mongo_console +21 -0
  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 +19 -0
  20. data/lib/mongo/admin.rb +83 -0
  21. data/lib/mongo/collection.rb +415 -0
  22. data/lib/mongo/connection.rb +151 -0
  23. data/lib/mongo/cursor.rb +279 -0
  24. data/lib/mongo/db.rb +560 -0
  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 +20 -0
  30. data/lib/mongo/message/get_more_message.rb +32 -0
  31. data/lib/mongo/message/insert_message.rb +37 -0
  32. data/lib/mongo/message/kill_cursors_message.rb +31 -0
  33. data/lib/mongo/message/message.rb +80 -0
  34. data/lib/mongo/message/message_header.rb +45 -0
  35. data/lib/mongo/message/msg_message.rb +29 -0
  36. data/lib/mongo/message/opcodes.rb +27 -0
  37. data/lib/mongo/message/query_message.rb +78 -0
  38. data/lib/mongo/message/remove_message.rb +37 -0
  39. data/lib/mongo/message/update_message.rb +38 -0
  40. data/lib/mongo/query.rb +118 -0
  41. data/lib/mongo/types/binary.rb +38 -0
  42. data/lib/mongo/types/code.rb +30 -0
  43. data/lib/mongo/types/dbref.rb +33 -0
  44. data/lib/mongo/types/objectid.rb +143 -0
  45. data/lib/mongo/types/regexp_of_holding.rb +40 -0
  46. data/lib/mongo/util/bson.rb +546 -0
  47. data/lib/mongo/util/byte_buffer.rb +167 -0
  48. data/lib/mongo/util/ordered_hash.rb +113 -0
  49. data/lib/mongo/util/xml_to_ruby.rb +105 -0
  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/test/test_admin.rb +69 -0
  66. data/test/test_bson.rb +268 -0
  67. data/test/test_byte_buffer.rb +69 -0
  68. data/test/test_chunk.rb +84 -0
  69. data/test/test_collection.rb +249 -0
  70. data/test/test_connection.rb +101 -0
  71. data/test/test_cursor.rb +331 -0
  72. data/test/test_db.rb +185 -0
  73. data/test/test_db_api.rb +798 -0
  74. data/test/test_db_connection.rb +18 -0
  75. data/test/test_grid_store.rb +284 -0
  76. data/test/test_message.rb +35 -0
  77. data/test/test_objectid.rb +105 -0
  78. data/test/test_ordered_hash.rb +138 -0
  79. data/test/test_round_trip.rb +120 -0
  80. data/test/test_threading.rb +37 -0
  81. metadata +135 -0
@@ -0,0 +1,20 @@
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
+
17
+ %w(get_more_message insert_message kill_cursors_message message_header
18
+ msg_message query_message remove_message update_message).each { |f|
19
+ require "mongo/message/#{f}"
20
+ }
@@ -0,0 +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
+
17
+ require 'mongo/message/message'
18
+ require 'mongo/message/opcodes'
19
+
20
+ module Mongo
21
+
22
+ class GetMoreMessage < Message
23
+
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)
30
+ end
31
+ end
32
+ end
@@ -0,0 +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
+
17
+ require 'mongo/message/message'
18
+ require 'mongo/message/opcodes'
19
+
20
+ module Mongo
21
+
22
+ class InsertMessage < Message
23
+
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
32
+
33
+ def to_s
34
+ "db.#{@collection_name}.insert(#{@objs.inspect})"
35
+ end
36
+ end
37
+ end
@@ -0,0 +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
+
17
+ require 'mongo/message/message'
18
+ require 'mongo/message/opcodes'
19
+
20
+ module Mongo
21
+
22
+ class KillCursorsMessage < Message
23
+
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 }
29
+ end
30
+ end
31
+ end
@@ -0,0 +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
+
17
+ require 'mongo/util/bson'
18
+ require 'mongo/util/byte_buffer'
19
+
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
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
+
79
+ end
80
+ end
@@ -0,0 +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
+
17
+ require 'mongo/util/byte_buffer'
18
+
19
+ module Mongo
20
+
21
+ class MessageHeader
22
+
23
+ HEADER_SIZE = 16
24
+
25
+ def initialize()
26
+ @buf = ByteBuffer.new
27
+ end
28
+
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
40
+
41
+ def dump
42
+ @buf.dump
43
+ end
44
+ end
45
+ end
@@ -0,0 +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
+
17
+ require 'mongo/message/message'
18
+ require 'mongo/message/opcodes'
19
+
20
+ module Mongo
21
+
22
+ class MsgMessage < Message
23
+
24
+ def initialize(msg)
25
+ super(OP_MSG)
26
+ write_string(msg)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,27 @@
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
+
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
@@ -0,0 +1,78 @@
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
+
17
+ require 'mongo/message/message'
18
+ require 'mongo/message/opcodes'
19
+ require 'mongo/util/ordered_hash'
20
+
21
+ module Mongo
22
+
23
+ class QueryMessage < Message
24
+
25
+ attr_reader :query
26
+
27
+ def initialize(db_name, collection_name, query)
28
+ super(OP_QUERY)
29
+ @query = query
30
+ @collection_name = collection_name
31
+ write_int(0)
32
+ write_string("#{db_name}.#{collection_name}")
33
+ write_int(query.number_to_skip)
34
+ write_int(query.number_to_return)
35
+ sel = query.selector
36
+ if query.contains_special_fields
37
+ sel = OrderedHash.new
38
+ sel['query'] = query.selector
39
+ if query.order_by && query.order_by.length > 0
40
+ sel['orderby'] = case query.order_by
41
+ when String
42
+ {query.order_by => 1}
43
+ when Array
44
+ h = OrderedHash.new
45
+ query.order_by.each { |ob|
46
+ case ob
47
+ when String
48
+ h[ob] = 1
49
+ when Hash # should have one entry; will handle all
50
+ ob.each { |k,v| h[k] = v }
51
+ else
52
+ raise "illegal query order_by value #{query.order_by.inspect}"
53
+ end
54
+ }
55
+ h
56
+ when Hash # Should be an ordered hash, but this message doesn't care
57
+ query.order_by
58
+ else
59
+ raise "illegal order_by: is a #{query.order_by.class.name}, must be String, Array, Hash, or OrderedHash"
60
+ end
61
+ end
62
+ sel['$hint'] = query.hint if query.hint && query.hint.length > 0
63
+ sel['$explain'] = true if query.explain
64
+ sel['$snapshot'] = true if query.snapshot
65
+ end
66
+ write_doc(sel)
67
+ write_doc(query.fields) if query.fields
68
+ end
69
+
70
+ def first_key(key)
71
+ @first_key = key
72
+ end
73
+
74
+ def to_s
75
+ "db.#{@collection_name}.#{@query}"
76
+ end
77
+ end
78
+ end
@@ -0,0 +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
+
17
+ require 'mongo/message/message'
18
+ require 'mongo/message/opcodes'
19
+
20
+ module Mongo
21
+
22
+ class RemoveMessage < Message
23
+
24
+ def initialize(db_name, collection_name, sel)
25
+ @collection_name = collection_name
26
+ super(OP_DELETE)
27
+ write_int(0)
28
+ write_string("#{db_name}.#{collection_name}")
29
+ write_int(0) # flags?
30
+ write_doc(sel)
31
+ end
32
+
33
+ def to_s
34
+ "#{@collection_name}.clear()"
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,38 @@
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
+
17
+ require 'mongo/message/message'
18
+ require 'mongo/message/opcodes'
19
+
20
+ module Mongo
21
+
22
+ class UpdateMessage < Message
23
+
24
+ def initialize(db_name, collection_name, sel, obj, repsert)
25
+ @collection_name = collection_name
26
+ super(OP_UPDATE)
27
+ write_int(0)
28
+ write_string("#{db_name}.#{collection_name}")
29
+ write_int(repsert ? 1 : 0) # 1 if a repsert operation (upsert)
30
+ write_doc(sel)
31
+ write_doc(obj)
32
+ end
33
+
34
+ def to_s
35
+ "db.#{@collection_name}.update(#{@sel.inspect}, #{@obj.inspect})"
36
+ end
37
+ end
38
+ end