animehunter-mongo 0.9

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 (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,167 @@
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
+ # A byte buffer.
18
+ class ByteBuffer
19
+
20
+ attr_reader :order
21
+
22
+ def initialize(initial_data=[])
23
+ @buf = initial_data
24
+ @cursor = 0
25
+ self.order = :little_endian
26
+ end
27
+
28
+ # +endianness+ should be :little_endian or :big_endian. Default is :little_endian
29
+ def order=(endianness)
30
+ @order = endianness
31
+ @int_pack_order = endianness == :little_endian ? 'V' : 'N'
32
+ @double_pack_order = endianness == :little_endian ? 'E' : 'G'
33
+ end
34
+
35
+ def rewind
36
+ @cursor = 0
37
+ end
38
+
39
+ def position
40
+ @cursor
41
+ end
42
+
43
+ def position=(val)
44
+ @cursor = val
45
+ end
46
+
47
+ def clear
48
+ @buf = []
49
+ rewind
50
+ end
51
+
52
+ def size
53
+ @buf.size
54
+ end
55
+ alias_method :length, :size
56
+
57
+ def put(byte, offset=nil)
58
+ @cursor = offset if offset
59
+ @buf[@cursor] = byte
60
+ @cursor += 1
61
+ end
62
+
63
+ def put_array(array, offset=nil)
64
+ @cursor = offset if offset
65
+ @buf[@cursor, array.length] = array
66
+ @cursor += array.length
67
+ end
68
+
69
+ def put_int(i, offset=nil)
70
+ a = []
71
+ [i].pack(@int_pack_order).each_byte { |b| a << b }
72
+ put_array(a, offset)
73
+ end
74
+
75
+ def put_long(i, offset=nil)
76
+ offset = @cursor unless offset
77
+ if @int_pack_order == 'N'
78
+ put_int(i >> 32, offset)
79
+ put_int(i & 0xffffffff, offset + 4)
80
+ else
81
+ put_int(i & 0xffffffff, offset)
82
+ put_int(i >> 32, offset + 4)
83
+ end
84
+ end
85
+
86
+ def put_double(d, offset=nil)
87
+ a = []
88
+ [d].pack(@double_pack_order).each_byte { |b| a << b }
89
+ put_array(a, offset)
90
+ end
91
+
92
+ # If +size+ == nil, returns one byte. Else returns array of bytes of length
93
+ # # +size+.
94
+ def get(len=nil)
95
+ one_byte = len.nil?
96
+ len ||= 1
97
+ check_read_length(len)
98
+ start = @cursor
99
+ @cursor += len
100
+ if one_byte
101
+ @buf[start]
102
+ else
103
+ if @buf.respond_to? "unpack"
104
+ @buf[start, len].unpack("C*")
105
+ else
106
+ @buf[start, len]
107
+ end
108
+ end
109
+ end
110
+
111
+ def get_int
112
+ check_read_length(4)
113
+ vals = ""
114
+ (@cursor..@cursor+3).each { |i| vals << @buf[i].chr }
115
+ @cursor += 4
116
+ vals.unpack(@int_pack_order)[0]
117
+ end
118
+
119
+ def get_long
120
+ i1 = get_int
121
+ i2 = get_int
122
+ if @int_pack_order == 'N'
123
+ (i1 << 32) + i2
124
+ else
125
+ (i2 << 32) + i1
126
+ end
127
+ end
128
+
129
+ def get_double
130
+ check_read_length(8)
131
+ vals = ""
132
+ (@cursor..@cursor+7).each { |i| vals << @buf[i].chr }
133
+ @cursor += 8
134
+ vals.unpack(@double_pack_order)[0]
135
+ end
136
+
137
+ def more?
138
+ @cursor < @buf.size
139
+ end
140
+
141
+ def to_a
142
+ if @buf.respond_to? "unpack"
143
+ @buf.unpack("C*")
144
+ else
145
+ @buf
146
+ end
147
+ end
148
+
149
+ def to_s
150
+ if @buf.respond_to? "pack"
151
+ @buf.pack("C*")
152
+ else
153
+ @buf
154
+ end
155
+ end
156
+
157
+ def dump
158
+ @buf.each_with_index { |c, i| $stderr.puts "#{'%04d' % i}: #{'%02x' % c} #{'%03o' % c} #{'%s' % c.chr} #{'%3d' % c}" }
159
+ end
160
+
161
+ private
162
+
163
+ def check_read_length(len)
164
+ raise "attempt to read past end of buffer" if @cursor + len > @buf.length
165
+ end
166
+
167
+ end
@@ -0,0 +1,96 @@
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
+ # A hash in which the order of keys are preserved.
18
+ #
19
+ # Under Ruby 1.9 and greater, this class has no added methods because Ruby's
20
+ # Hash already keeps its keys ordered by order of insertion.
21
+ class OrderedHash < Hash
22
+
23
+ def ==(other)
24
+ begin
25
+ !other.nil? &&
26
+ keys == other.keys &&
27
+ values == other.values
28
+ rescue
29
+ false
30
+ end
31
+ end
32
+
33
+ # We only need the body of this class if the RUBY_VERSION is before 1.9
34
+ if RUBY_VERSION < '1.9'
35
+
36
+ attr_accessor :ordered_keys
37
+
38
+ def keys
39
+ @ordered_keys || []
40
+ end
41
+
42
+ def []=(key, value)
43
+ @ordered_keys ||= []
44
+ @ordered_keys << key unless @ordered_keys.include?(key)
45
+ super(key, value)
46
+ end
47
+
48
+ def each
49
+ @ordered_keys ||= []
50
+ @ordered_keys.each { |k| yield k, self[k] }
51
+ end
52
+
53
+ def values
54
+ collect { |k, v| v }
55
+ end
56
+
57
+ def merge(other)
58
+ oh = self.dup
59
+ oh.merge!(other)
60
+ oh
61
+ end
62
+
63
+ def merge!(other)
64
+ @ordered_keys ||= []
65
+ @ordered_keys += other.keys # unordered if not an OrderedHash
66
+ @ordered_keys.uniq!
67
+ super(other)
68
+ end
69
+
70
+ def inspect
71
+ str = '{'
72
+ str << (@ordered_keys || []).collect { |k| "\"#{k}\"=>#{self.[](k).inspect}" }.join(", ")
73
+ str << '}'
74
+ end
75
+
76
+ def delete(key, &block)
77
+ @ordered_keys.delete(key) if @ordered_keys
78
+ super
79
+ end
80
+
81
+ def delete_if(&block)
82
+ self.each { |k,v|
83
+ if yield k, v
84
+ delete(k)
85
+ end
86
+ }
87
+ end
88
+
89
+ def clear
90
+ super
91
+ @ordered_keys = []
92
+ end
93
+
94
+ end # Ruby before 1.9
95
+
96
+ end
@@ -0,0 +1,107 @@
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 'rexml/document'
18
+ require 'mongo'
19
+
20
+ # Converts a .xson file (an XML file that describes a Mongo-type document) to
21
+ # an OrderedHash.
22
+ class XMLToRuby
23
+
24
+ include XGen::Mongo::Driver
25
+
26
+ def xml_to_ruby(io)
27
+ doc = REXML::Document.new(io)
28
+ doc_to_ruby(doc.root.elements['doc'])
29
+ end
30
+
31
+ protected
32
+
33
+ def element_to_ruby(e)
34
+ type = e.name
35
+ child = e.elements[1]
36
+ case type
37
+ when 'oid'
38
+ ObjectID.from_string(e.text)
39
+ when 'ref'
40
+ dbref_to_ruby(e.elements)
41
+ when 'int'
42
+ e.text.to_i
43
+ when 'number'
44
+ e.text.to_f
45
+ when 'string'
46
+ e.text.to_s
47
+ when 'code'
48
+ Code.new(e.text.to_s)
49
+ when 'binary'
50
+ bin = Binary.new
51
+ decoded = Base64.decode64(e.text.to_s)
52
+ decoded.each_byte { |b| bin.put(b) }
53
+ bin
54
+ when 'symbol'
55
+ e.text.to_s.intern
56
+ when 'boolean'
57
+ e.text.to_s == 'true'
58
+ when 'array'
59
+ array_to_ruby(e.elements)
60
+ when 'date'
61
+ Time.at(e.text.to_f / 1000.0)
62
+ when 'regex'
63
+ regex_to_ruby(e.elements)
64
+ when 'null'
65
+ nil
66
+ when 'undefined'
67
+ Undefined.new
68
+ when 'doc'
69
+ doc_to_ruby(e)
70
+ else
71
+ raise "Unknown type #{type} in element with name #{e.attributes['name']}"
72
+ end
73
+ end
74
+
75
+ def doc_to_ruby(element)
76
+ oh = OrderedHash.new
77
+ element.elements.each { |e| oh[e.attributes['name']] = element_to_ruby(e) }
78
+ oh
79
+ end
80
+
81
+ def array_to_ruby(elements)
82
+ a = []
83
+ elements.each { |e|
84
+ index_str = e.attributes['name']
85
+ a[index_str.to_i] = element_to_ruby(e)
86
+ }
87
+ a
88
+ end
89
+
90
+ def regex_to_ruby(elements)
91
+ pattern = elements['pattern'].text
92
+ options_str = elements['options'].text || ''
93
+
94
+ options = 0
95
+ options |= Regexp::IGNORECASE if options_str.include?('i')
96
+ options |= Regexp::MULTILINE if options_str.include?('m')
97
+ options |= Regexp::EXTENDED if options_str.include?('x')
98
+ Regexp.new(pattern, options)
99
+ end
100
+
101
+ def dbref_to_ruby(elements)
102
+ ns = elements['ns'].text
103
+ oid_str = elements['oid'].text
104
+ DBRef.new(ns, ObjectID.from_string(oid_str))
105
+ end
106
+
107
+ end
@@ -0,0 +1,99 @@
1
+ # We need to list all of the included files because we aren't allowed to use
2
+ # Dir[...] in the github sandbox.
3
+ PACKAGE_FILES = ['README.rdoc', 'Rakefile', 'mongo-ruby-driver.gemspec',
4
+ 'bin/bson_benchmark.rb',
5
+ 'bin/mongo_console',
6
+ 'bin/run_test_script',
7
+ 'bin/standard_benchmark',
8
+ 'examples/admin.rb',
9
+ 'examples/benchmarks.rb',
10
+ 'examples/blog.rb',
11
+ 'examples/capped.rb',
12
+ 'examples/cursor.rb',
13
+ 'examples/gridfs.rb',
14
+ 'examples/index_test.rb',
15
+ 'examples/info.rb',
16
+ 'examples/queries.rb',
17
+ 'examples/simple.rb',
18
+ 'examples/strict.rb',
19
+ 'examples/types.rb',
20
+ 'lib/mongo/admin.rb',
21
+ 'lib/mongo/collection.rb',
22
+ 'lib/mongo/cursor.rb',
23
+ 'lib/mongo/db.rb',
24
+ 'lib/mongo/gridfs/chunk.rb',
25
+ 'lib/mongo/gridfs/grid_store.rb',
26
+ 'lib/mongo/gridfs.rb',
27
+ 'lib/mongo/message/get_more_message.rb',
28
+ 'lib/mongo/message/insert_message.rb',
29
+ 'lib/mongo/message/kill_cursors_message.rb',
30
+ 'lib/mongo/message/message.rb',
31
+ 'lib/mongo/message/message_header.rb',
32
+ 'lib/mongo/message/msg_message.rb',
33
+ 'lib/mongo/message/opcodes.rb',
34
+ 'lib/mongo/message/query_message.rb',
35
+ 'lib/mongo/message/remove_message.rb',
36
+ 'lib/mongo/message/update_message.rb',
37
+ 'lib/mongo/message.rb',
38
+ 'lib/mongo/mongo.rb',
39
+ 'lib/mongo/query.rb',
40
+ 'lib/mongo/types/binary.rb',
41
+ 'lib/mongo/types/code.rb',
42
+ 'lib/mongo/types/dbref.rb',
43
+ 'lib/mongo/types/objectid.rb',
44
+ 'lib/mongo/types/regexp_of_holding.rb',
45
+ 'lib/mongo/types/undefined.rb',
46
+ 'lib/mongo/util/bson.rb',
47
+ 'lib/mongo/util/byte_buffer.rb',
48
+ 'lib/mongo/util/ordered_hash.rb',
49
+ 'lib/mongo/util/xml_to_ruby.rb',
50
+ 'lib/mongo.rb']
51
+ TEST_FILES = ['tests/mongo-qa/_common.rb',
52
+ 'tests/mongo-qa/admin',
53
+ 'tests/mongo-qa/capped',
54
+ 'tests/mongo-qa/count1',
55
+ 'tests/mongo-qa/dbs',
56
+ 'tests/mongo-qa/find',
57
+ 'tests/mongo-qa/find1',
58
+ 'tests/mongo-qa/gridfs_in',
59
+ 'tests/mongo-qa/gridfs_out',
60
+ 'tests/mongo-qa/indices',
61
+ 'tests/mongo-qa/remove',
62
+ 'tests/mongo-qa/stress1',
63
+ 'tests/mongo-qa/test1',
64
+ 'tests/mongo-qa/update',
65
+ 'tests/test_admin.rb',
66
+ 'tests/test_bson.rb',
67
+ 'tests/test_byte_buffer.rb',
68
+ 'tests/test_chunk.rb',
69
+ 'tests/test_cursor.rb',
70
+ 'tests/test_db.rb',
71
+ 'tests/test_db_api.rb',
72
+ 'tests/test_db_connection.rb',
73
+ 'tests/test_grid_store.rb',
74
+ 'tests/test_message.rb',
75
+ 'tests/test_mongo.rb',
76
+ 'tests/test_objectid.rb',
77
+ 'tests/test_ordered_hash.rb',
78
+ 'tests/test_round_trip.rb']
79
+
80
+ Gem::Specification.new do |s|
81
+ s.name = 'mongo'
82
+ s.version = '0.9'
83
+ s.platform = Gem::Platform::RUBY
84
+ s.summary = 'Ruby driver for the 10gen Mongo DB'
85
+ s.description = 'A Ruby driver for the 10gen Mongo DB. For more information about Mongo, see http://www.mongodb.org.'
86
+
87
+ s.require_paths = ['lib']
88
+
89
+ s.files = PACKAGE_FILES
90
+ s.test_files = TEST_FILES
91
+
92
+ s.has_rdoc = true
93
+ s.rdoc_options = ['--main', 'README.rdoc', '--inline-source']
94
+ s.extra_rdoc_files = ['README.rdoc']
95
+
96
+ s.authors = ['Jim Menard', 'Mike Dirolf']
97
+ s.email = 'mongodb-dev@googlegroups.com'
98
+ s.homepage = 'http://www.mongodb.org'
99
+ end