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,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,113 @@
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
+ attr_accessor :ordered_keys
36
+
37
+ def self.[] *args
38
+ oh = OrderedHash.new
39
+ if Hash === args[0]
40
+ oh.merge! args[0]
41
+ elsif (args.size % 2) != 0
42
+ raise ArgumentError, "odd number of elements for Hash"
43
+ else
44
+ 0.step(args.size - 1, 2) do |key|
45
+ value = key + 1
46
+ oh[args[key]] = args[value]
47
+ end
48
+ end
49
+ oh
50
+ end
51
+
52
+ def initialize(*a, &b)
53
+ super
54
+ @ordered_keys = []
55
+ end
56
+
57
+ def keys
58
+ @ordered_keys || []
59
+ end
60
+
61
+ def []=(key, value)
62
+ @ordered_keys ||= []
63
+ @ordered_keys << key unless @ordered_keys.include?(key)
64
+ super(key, value)
65
+ end
66
+
67
+ def each
68
+ @ordered_keys ||= []
69
+ @ordered_keys.each { |k| yield k, self[k] }
70
+ end
71
+
72
+ def values
73
+ collect { |k, v| v }
74
+ end
75
+
76
+ def merge(other)
77
+ oh = self.dup
78
+ oh.merge!(other)
79
+ oh
80
+ end
81
+
82
+ def merge!(other)
83
+ @ordered_keys ||= []
84
+ @ordered_keys += other.keys # unordered if not an OrderedHash
85
+ @ordered_keys.uniq!
86
+ super(other)
87
+ end
88
+
89
+ def inspect
90
+ str = '{'
91
+ str << (@ordered_keys || []).collect { |k| "\"#{k}\"=>#{self.[](k).inspect}" }.join(", ")
92
+ str << '}'
93
+ end
94
+
95
+ def delete(key, &block)
96
+ @ordered_keys.delete(key) if @ordered_keys
97
+ super
98
+ end
99
+
100
+ def delete_if(&block)
101
+ self.each { |k,v|
102
+ if yield k, v
103
+ delete(k)
104
+ end
105
+ }
106
+ end
107
+
108
+ def clear
109
+ super
110
+ @ordered_keys = []
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,105 @@
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 Mongo
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 'doc'
67
+ doc_to_ruby(e)
68
+ else
69
+ raise "Unknown type #{type} in element with name #{e.attributes['name']}"
70
+ end
71
+ end
72
+
73
+ def doc_to_ruby(element)
74
+ oh = OrderedHash.new
75
+ element.elements.each { |e| oh[e.attributes['name']] = element_to_ruby(e) }
76
+ oh
77
+ end
78
+
79
+ def array_to_ruby(elements)
80
+ a = []
81
+ elements.each { |e|
82
+ index_str = e.attributes['name']
83
+ a[index_str.to_i] = element_to_ruby(e)
84
+ }
85
+ a
86
+ end
87
+
88
+ def regex_to_ruby(elements)
89
+ pattern = elements['pattern'].text
90
+ options_str = elements['options'].text || ''
91
+
92
+ options = 0
93
+ options |= Regexp::IGNORECASE if options_str.include?('i')
94
+ options |= Regexp::MULTILINE if options_str.include?('m')
95
+ options |= Regexp::EXTENDED if options_str.include?('x')
96
+ Regexp.new(pattern, options)
97
+ end
98
+
99
+ def dbref_to_ruby(elements)
100
+ ns = elements['ns'].text
101
+ oid_str = elements['oid'].text
102
+ DBRef.new(ns, ObjectID.from_string(oid_str))
103
+ end
104
+
105
+ end
@@ -0,0 +1,103 @@
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/connection.rb',
23
+ 'lib/mongo/cursor.rb',
24
+ 'lib/mongo/db.rb',
25
+ 'lib/mongo/gridfs/chunk.rb',
26
+ 'lib/mongo/gridfs/grid_store.rb',
27
+ 'lib/mongo/gridfs.rb',
28
+ 'lib/mongo/errors.rb',
29
+ 'lib/mongo/message/get_more_message.rb',
30
+ 'lib/mongo/message/insert_message.rb',
31
+ 'lib/mongo/message/kill_cursors_message.rb',
32
+ 'lib/mongo/message/message.rb',
33
+ 'lib/mongo/message/message_header.rb',
34
+ 'lib/mongo/message/msg_message.rb',
35
+ 'lib/mongo/message/opcodes.rb',
36
+ 'lib/mongo/message/query_message.rb',
37
+ 'lib/mongo/message/remove_message.rb',
38
+ 'lib/mongo/message/update_message.rb',
39
+ 'lib/mongo/message.rb',
40
+ 'lib/mongo/query.rb',
41
+ 'lib/mongo/types/binary.rb',
42
+ 'lib/mongo/types/code.rb',
43
+ 'lib/mongo/types/dbref.rb',
44
+ 'lib/mongo/types/objectid.rb',
45
+ 'lib/mongo/types/regexp_of_holding.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 = ['test/mongo-qa/_common.rb',
52
+ 'test/mongo-qa/admin',
53
+ 'test/mongo-qa/capped',
54
+ 'test/mongo-qa/count1',
55
+ 'test/mongo-qa/dbs',
56
+ 'test/mongo-qa/find',
57
+ 'test/mongo-qa/find1',
58
+ 'test/mongo-qa/gridfs_in',
59
+ 'test/mongo-qa/gridfs_out',
60
+ 'test/mongo-qa/indices',
61
+ 'test/mongo-qa/remove',
62
+ 'test/mongo-qa/stress1',
63
+ 'test/mongo-qa/test1',
64
+ 'test/mongo-qa/update',
65
+ 'test/test_admin.rb',
66
+ 'test/test_bson.rb',
67
+ 'test/test_byte_buffer.rb',
68
+ 'test/test_chunk.rb',
69
+ 'test/test_collection.rb',
70
+ 'test/test_connection.rb',
71
+ 'test/test_cursor.rb',
72
+ 'test/test_db.rb',
73
+ 'test/test_db_api.rb',
74
+ 'test/test_db_connection.rb',
75
+ 'test/test_grid_store.rb',
76
+ 'test/test_message.rb',
77
+ 'test/test_objectid.rb',
78
+ 'test/test_ordered_hash.rb',
79
+ 'test/test_threading.rb',
80
+ 'test/test_round_trip.rb']
81
+
82
+ Gem::Specification.new do |s|
83
+ s.name = 'mongo-abd'
84
+
85
+ # be sure to change this constant in lib/mongo.rb as well
86
+ s.version = '0.14.1'
87
+
88
+ s.platform = Gem::Platform::RUBY
89
+ s.summary = 'Ruby driver for the 10gen Mongo DB'
90
+ s.description = 'A Ruby driver for the 10gen Mongo DB. For more information about Mongo, see http://www.mongodb.org.'
91
+
92
+ s.require_paths = ['lib']
93
+ s.files = PACKAGE_FILES
94
+ s.test_files = TEST_FILES
95
+
96
+ s.has_rdoc = true
97
+ s.rdoc_options = ['--main', 'README.rdoc', '--inline-source']
98
+ s.extra_rdoc_files = ['README.rdoc']
99
+
100
+ s.authors = ['Jim Menard', 'Mike Dirolf']
101
+ s.email = 'mongodb-dev@googlegroups.com'
102
+ s.homepage = 'http://www.mongodb.org'
103
+ end