animehunter-mongo 0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/README.rdoc +311 -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 +40 -0
  19. data/lib/mongo.rb +19 -0
  20. data/lib/mongo/admin.rb +87 -0
  21. data/lib/mongo/collection.rb +235 -0
  22. data/lib/mongo/cursor.rb +227 -0
  23. data/lib/mongo/db.rb +538 -0
  24. data/lib/mongo/gridfs.rb +16 -0
  25. data/lib/mongo/gridfs/chunk.rb +96 -0
  26. data/lib/mongo/gridfs/grid_store.rb +468 -0
  27. data/lib/mongo/message.rb +20 -0
  28. data/lib/mongo/message/get_more_message.rb +37 -0
  29. data/lib/mongo/message/insert_message.rb +35 -0
  30. data/lib/mongo/message/kill_cursors_message.rb +36 -0
  31. data/lib/mongo/message/message.rb +84 -0
  32. data/lib/mongo/message/message_header.rb +50 -0
  33. data/lib/mongo/message/msg_message.rb +33 -0
  34. data/lib/mongo/message/opcodes.rb +32 -0
  35. data/lib/mongo/message/query_message.rb +77 -0
  36. data/lib/mongo/message/remove_message.rb +36 -0
  37. data/lib/mongo/message/update_message.rb +37 -0
  38. data/lib/mongo/mongo.rb +164 -0
  39. data/lib/mongo/query.rb +119 -0
  40. data/lib/mongo/types/binary.rb +42 -0
  41. data/lib/mongo/types/code.rb +34 -0
  42. data/lib/mongo/types/dbref.rb +37 -0
  43. data/lib/mongo/types/objectid.rb +137 -0
  44. data/lib/mongo/types/regexp_of_holding.rb +44 -0
  45. data/lib/mongo/types/undefined.rb +31 -0
  46. data/lib/mongo/util/bson.rb +534 -0
  47. data/lib/mongo/util/byte_buffer.rb +167 -0
  48. data/lib/mongo/util/ordered_hash.rb +96 -0
  49. data/lib/mongo/util/xml_to_ruby.rb +107 -0
  50. data/mongo-ruby-driver.gemspec +99 -0
  51. data/tests/mongo-qa/_common.rb +8 -0
  52. data/tests/mongo-qa/admin +26 -0
  53. data/tests/mongo-qa/capped +22 -0
  54. data/tests/mongo-qa/count1 +18 -0
  55. data/tests/mongo-qa/dbs +22 -0
  56. data/tests/mongo-qa/find +10 -0
  57. data/tests/mongo-qa/find1 +15 -0
  58. data/tests/mongo-qa/gridfs_in +16 -0
  59. data/tests/mongo-qa/gridfs_out +17 -0
  60. data/tests/mongo-qa/indices +49 -0
  61. data/tests/mongo-qa/remove +25 -0
  62. data/tests/mongo-qa/stress1 +35 -0
  63. data/tests/mongo-qa/test1 +11 -0
  64. data/tests/mongo-qa/update +18 -0
  65. data/tests/test_admin.rb +69 -0
  66. data/tests/test_bson.rb +246 -0
  67. data/tests/test_byte_buffer.rb +69 -0
  68. data/tests/test_chunk.rb +84 -0
  69. data/tests/test_cursor.rb +121 -0
  70. data/tests/test_db.rb +160 -0
  71. data/tests/test_db_api.rb +701 -0
  72. data/tests/test_db_connection.rb +18 -0
  73. data/tests/test_grid_store.rb +284 -0
  74. data/tests/test_message.rb +35 -0
  75. data/tests/test_mongo.rb +78 -0
  76. data/tests/test_objectid.rb +98 -0
  77. data/tests/test_ordered_hash.rb +129 -0
  78. data/tests/test_round_trip.rb +116 -0
  79. metadata +133 -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,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 XGen
21
+ module Mongo
22
+ module Driver
23
+
24
+ class GetMoreMessage < Message
25
+
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
34
+ end
35
+ end
36
+ end
37
+
@@ -0,0 +1,35 @@
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 XGen
21
+ module Mongo
22
+ module Driver
23
+
24
+ class InsertMessage < Message
25
+
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
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,36 @@
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 XGen
21
+ module Mongo
22
+ module Driver
23
+
24
+ class KillCursorsMessage < Message
25
+
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
33
+ end
34
+ end
35
+ end
36
+
@@ -0,0 +1,84 @@
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 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
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,50 @@
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 XGen
20
+ module Mongo
21
+ module Driver
22
+
23
+ class MessageHeader
24
+
25
+ HEADER_SIZE = 16
26
+
27
+ def initialize()
28
+ @buf = ByteBuffer.new
29
+ end
30
+
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
42
+
43
+ def dump
44
+ @buf.dump
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+
@@ -0,0 +1,33 @@
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 XGen
21
+ module Mongo
22
+ module Driver
23
+
24
+ class MsgMessage < Message
25
+
26
+ def initialize(msg)
27
+ super(OP_MSG)
28
+ write_string(msg)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -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
+ 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
31
+ end
32
+
@@ -0,0 +1,77 @@
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 XGen
22
+ module Mongo
23
+ module Driver
24
+
25
+ class QueryMessage < Message
26
+
27
+ attr_reader :query
28
+
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
42
+ 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
59
+ else
60
+ raise "illegal order_by: is a #{query.order_by.class.name}, must be String, Array, Hash, or OrderedHash"
61
+ end
62
+ end
63
+ sel['$hint'] = query.hint if query.hint && query.hint.length > 0
64
+ sel['$explain'] = true if query.explain
65
+
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
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,36 @@
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 XGen
21
+ module Mongo
22
+ module Driver
23
+
24
+ class RemoveMessage < Message
25
+
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
34
+ end
35
+ end
36
+ end