bson 4.1.1-java → 4.2.0-java

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 (82) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/Rakefile +18 -3
  5. data/lib/bson-ruby.jar +0 -0
  6. data/lib/bson.rb +6 -4
  7. data/lib/bson/array.rb +1 -1
  8. data/lib/bson/binary.rb +4 -2
  9. data/lib/bson/code.rb +1 -1
  10. data/lib/bson/code_with_scope.rb +1 -1
  11. data/lib/bson/config.rb +1 -1
  12. data/lib/bson/date.rb +1 -1
  13. data/lib/bson/date_time.rb +1 -1
  14. data/lib/bson/decimal128.rb +318 -0
  15. data/lib/bson/decimal128/builder.rb +448 -0
  16. data/lib/bson/document.rb +2 -2
  17. data/lib/bson/environment.rb +13 -1
  18. data/lib/bson/false_class.rb +1 -1
  19. data/lib/bson/float.rb +1 -1
  20. data/lib/bson/hash.rb +1 -1
  21. data/lib/bson/int32.rb +46 -0
  22. data/lib/bson/int64.rb +46 -0
  23. data/lib/bson/integer.rb +1 -1
  24. data/lib/bson/max_key.rb +1 -1
  25. data/lib/bson/min_key.rb +1 -1
  26. data/lib/bson/object_id.rb +3 -2
  27. data/lib/bson/open_struct.rb +57 -0
  28. data/lib/bson/regexp.rb +87 -19
  29. data/lib/bson/registry.rb +1 -1
  30. data/lib/bson/specialized.rb +1 -1
  31. data/lib/bson/string.rb +1 -1
  32. data/lib/bson/symbol.rb +1 -1
  33. data/lib/bson/time.rb +1 -1
  34. data/lib/bson/timestamp.rb +2 -2
  35. data/lib/bson/true_class.rb +1 -1
  36. data/lib/bson/version.rb +2 -2
  37. data/spec/bson/array_spec.rb +1 -1
  38. data/spec/bson/binary_spec.rb +2 -1
  39. data/spec/bson/corpus_spec.rb +68 -0
  40. data/spec/bson/decimal128_spec.rb +1583 -0
  41. data/spec/bson/document_spec.rb +1 -1
  42. data/spec/bson/driver_bson_spec.rb +77 -0
  43. data/spec/bson/int32_spec.rb +58 -0
  44. data/spec/bson/int64_spec.rb +58 -0
  45. data/spec/bson/open_struct_spec.rb +144 -0
  46. data/spec/bson/raw_spec.rb +540 -0
  47. data/spec/bson/regexp_spec.rb +7 -7
  48. data/spec/bson/timestamp_spec.rb +1 -1
  49. data/spec/spec_helper.rb +5 -0
  50. data/spec/support/common_driver.rb +347 -0
  51. data/spec/support/corpus-tests/array.json +43 -0
  52. data/spec/support/corpus-tests/boolean.json +27 -0
  53. data/spec/support/corpus-tests/code.json +67 -0
  54. data/spec/support/corpus-tests/code_w_scope.json +78 -0
  55. data/spec/support/corpus-tests/document.json +36 -0
  56. data/spec/support/corpus-tests/double.json +69 -0
  57. data/spec/support/corpus-tests/failures/binary.json +69 -0
  58. data/spec/support/corpus-tests/failures/datetime.json +31 -0
  59. data/spec/support/corpus-tests/failures/dbpointer.json +42 -0
  60. data/spec/support/corpus-tests/failures/int64.json +38 -0
  61. data/spec/support/corpus-tests/failures/symbol.json +62 -0
  62. data/spec/support/corpus-tests/failures/undefined.json +13 -0
  63. data/spec/support/corpus-tests/int32.json +38 -0
  64. data/spec/support/corpus-tests/maxkey.json +12 -0
  65. data/spec/support/corpus-tests/minkey.json +12 -0
  66. data/spec/support/corpus-tests/null.json +12 -0
  67. data/spec/support/corpus-tests/oid.json +28 -0
  68. data/spec/support/corpus-tests/regex.json +37 -0
  69. data/spec/support/corpus-tests/string.json +67 -0
  70. data/spec/support/corpus-tests/timestamp.json +18 -0
  71. data/spec/support/corpus-tests/top.json +62 -0
  72. data/spec/support/corpus.rb +265 -0
  73. data/spec/support/driver-spec-tests/decimal128/decimal128-1.json +363 -0
  74. data/spec/support/driver-spec-tests/decimal128/decimal128-2.json +793 -0
  75. data/spec/support/driver-spec-tests/decimal128/decimal128-3.json +1771 -0
  76. data/spec/support/driver-spec-tests/decimal128/decimal128-4.json +165 -0
  77. data/spec/support/driver-spec-tests/decimal128/decimal128-5.json +402 -0
  78. data/spec/support/driver-spec-tests/decimal128/decimal128-6.json +131 -0
  79. data/spec/support/driver-spec-tests/decimal128/decimal128-7.json +327 -0
  80. data/spec/support/shared_examples.rb +1 -1
  81. metadata +77 -4
  82. metadata.gz.sig +0 -0
@@ -30,7 +30,19 @@ module BSON
30
30
  #
31
31
  # @since 2.0.0
32
32
  def jruby?
33
- defined?(JRUBY_VERSION)
33
+ @jruby ||= defined?(JRUBY_VERSION)
34
+ end
35
+
36
+ # Determine if we are using Ruby version 1.9.
37
+ #
38
+ # @example Are we running with Ruby version 1.9?
39
+ # Environment.ruby_1_9?
40
+ #
41
+ # @return [ true, false ] If the Ruby version is 1.9.
42
+ #
43
+ # @since 4.2.0
44
+ def ruby_1_9?
45
+ @ruby_1_9 ||= RUBY_VERSION < '2.0.0'
34
46
  end
35
47
  end
36
48
  end
@@ -44,7 +44,7 @@ module BSON
44
44
  # @example Get the false boolean as encoded BSON.
45
45
  # false.to_bson
46
46
  #
47
- # @return [ String ] The encoded string.
47
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
48
48
  #
49
49
  # @see http://bsonspec.org/#/specification
50
50
  #
@@ -37,7 +37,7 @@ module BSON
37
37
  # @example Get the floating point as encoded BSON.
38
38
  # 1.221311.to_bson
39
39
  #
40
- # @return [ String ] The encoded string.
40
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
41
41
  #
42
42
  # @see http://bsonspec.org/#/specification
43
43
  #
@@ -32,7 +32,7 @@ module BSON
32
32
  # @example Get the hash as encoded BSON.
33
33
  # { "field" => "value" }.to_bson
34
34
  #
35
- # @return [ String ] The encoded string.
35
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
36
36
  #
37
37
  # @see http://bsonspec.org/#/specification
38
38
  #
@@ -50,6 +50,52 @@ module BSON
50
50
  buffer.get_int32
51
51
  end
52
52
 
53
+ # Instantiate a BSON Int32.
54
+ #
55
+ # @param [ Integer ] integer The 32-bit integer.
56
+ #
57
+ # @see http://bsonspec.org/#/specification
58
+ #
59
+ # @since 4.2.0
60
+ def initialize(integer)
61
+ out_of_range! unless integer.bson_int32?
62
+ @integer = integer.freeze
63
+ end
64
+
65
+ # Append the integer as encoded BSON to a ByteBuffer.
66
+ #
67
+ # @example Encoded the integer and append to a ByteBuffer.
68
+ # int32.to_bson
69
+ #
70
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded integer.
71
+ #
72
+ # @see http://bsonspec.org/#/specification
73
+ #
74
+ # @since 4.2.0
75
+ def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
76
+ buffer.put_int32(@integer)
77
+ end
78
+
79
+ # Convert the integer to a BSON string key.
80
+ #
81
+ # @example Convert the integer to a BSON key string.
82
+ # int.to_bson_key
83
+ #
84
+ # @param [ true, false ] validating_keys If BSON should validate the key.
85
+ #
86
+ # @return [ String ] The string key.
87
+ #
88
+ # @since 4.2.0
89
+ def to_bson_key(validating_keys = Config.validating_keys?)
90
+ @integer.to_bson_key(validating_keys)
91
+ end
92
+
93
+ private
94
+
95
+ def out_of_range!
96
+ raise RangeError.new("#{self} is not a valid 4 byte integer value.")
97
+ end
98
+
53
99
  # Register this type when the module is loaded.
54
100
  #
55
101
  # @since 2.0.0
@@ -45,6 +45,52 @@ module BSON
45
45
  buffer.get_int64
46
46
  end
47
47
 
48
+ # Instantiate a BSON Int64.
49
+ #
50
+ # @param [ Integer ] integer The 64-bit integer.
51
+ #
52
+ # @see http://bsonspec.org/#/specification
53
+ #
54
+ # @since 4.2.0
55
+ def initialize(integer)
56
+ out_of_range! unless integer.bson_int64?
57
+ @integer = integer.freeze
58
+ end
59
+
60
+ # Append the integer as encoded BSON to a ByteBuffer.
61
+ #
62
+ # @example Encoded the integer and append to a ByteBuffer.
63
+ # int64.to_bson
64
+ #
65
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded integer.
66
+ #
67
+ # @see http://bsonspec.org/#/specification
68
+ #
69
+ # @since 4.2.0
70
+ def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
71
+ buffer.put_int64(@integer)
72
+ end
73
+
74
+ # Convert the integer to a BSON string key.
75
+ #
76
+ # @example Convert the integer to a BSON key string.
77
+ # int.to_bson_key
78
+ #
79
+ # @param [ true, false ] validating_keys If BSON should validate the key.
80
+ #
81
+ # @return [ String ] The string key.
82
+ #
83
+ # @since 4.2.0
84
+ def to_bson_key(validating_keys = Config.validating_keys?)
85
+ @integer.to_bson_key(validating_keys)
86
+ end
87
+
88
+ private
89
+
90
+ def out_of_range!
91
+ raise RangeError.new("#{self} is not a valid 8 byte integer value.")
92
+ end
93
+
48
94
  # Register this type when the module is loaded.
49
95
  #
50
96
  # @since 2.0.0
@@ -98,7 +98,7 @@ module BSON
98
98
  # @example Get the integer as encoded BSON.
99
99
  # 1024.to_bson
100
100
  #
101
- # @return [ String ] The encoded string.
101
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
102
102
  #
103
103
  # @see http://bsonspec.org/#/specification
104
104
  #
@@ -41,7 +41,7 @@ module BSON
41
41
  # @example Compare with another object.
42
42
  # max_key <=> 1000
43
43
  #
44
- # @param [ Object ] The object to compare against.
44
+ # @param [ Object ] other The object to compare against.
45
45
  #
46
46
  # @return [ Integer ] Always 1.
47
47
  #
@@ -41,7 +41,7 @@ module BSON
41
41
  # @example Compare with another object.
42
42
  # min_key <=> 1000
43
43
  #
44
- # @param [ Object ] The object to compare against.
44
+ # @param [ Object ] other The object to compare against.
45
45
  #
46
46
  # @return [ Integer ] Always -1.
47
47
  #
@@ -102,6 +102,7 @@ module BSON
102
102
  def generation_time
103
103
  ::Time.at(generate_data.unpack("N")[0]).utc
104
104
  end
105
+ alias :to_time :generation_time
105
106
 
106
107
  # Get the hash value for the object id.
107
108
  #
@@ -163,7 +164,7 @@ module BSON
163
164
  # where the object was instantiated in a non-standard way. (Like a
164
165
  # Marshal.load)
165
166
  #
166
- # @return [ String ] The raw bytes.
167
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
167
168
  #
168
169
  # @see http://bsonspec.org/#/specification
169
170
  #
@@ -279,7 +280,7 @@ module BSON
279
280
  # @example Is the string a legal object id?
280
281
  # BSON::ObjectId.legal?(string)
281
282
  #
282
- # @param [ String ] The string to check.
283
+ # @param [ String ] string The string to check.
283
284
  #
284
285
  # @return [ true, false ] If the string is legal.
285
286
  #
@@ -0,0 +1,57 @@
1
+ # Copyright (C) 2016 MongoDB Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module BSON
16
+
17
+ # Injects behaviour for encoding OpenStruct objects using hashes
18
+ # to raw bytes as specified by the BSON spec.
19
+ #
20
+ # @see http://bsonspec.org/#/specification
21
+ #
22
+ # @since 4.2.0
23
+ module OpenStruct
24
+
25
+ # Get the OpenStruct as encoded BSON.
26
+ #
27
+ # @example Get the OpenStruct object as encoded BSON.
28
+ # OpenStruct.new({ "field" => "value" }).to_bson
29
+ #
30
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
31
+ #
32
+ # @see http://bsonspec.org/#/specification
33
+ #
34
+ # @since 4.2.0
35
+ def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
36
+ if Environment.ruby_1_9?
37
+ marshal_dump.dup
38
+ else
39
+ to_h
40
+ end.to_bson(buffer, validating_keys)
41
+ end
42
+
43
+ # The BSON type for OpenStruct objects is the Hash type of 0x03.
44
+ #
45
+ # @example Get the bson type.
46
+ # struct.bson_type
47
+ #
48
+ # @return [ String ] The character 0x03.
49
+ #
50
+ # @since 4.2.0
51
+ def bson_type
52
+ ::Hash::BSON_TYPE
53
+ end
54
+ end
55
+
56
+ ::OpenStruct.send(:include, OpenStruct) if defined?(::OpenStruct)
57
+ end
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2015 MongoDB Inc.
1
+ # Copyright (C) 2009-2016 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -51,6 +51,8 @@ module BSON
51
51
  # Ruby multiline constant.
52
52
  #
53
53
  # @since 3.2.6
54
+ #
55
+ # @deprecated Will be removed in 5.0
54
56
  RUBY_MULTILINE_VALUE = 'ms'.freeze
55
57
 
56
58
  # Get the regexp as JSON hash data.
@@ -79,7 +81,10 @@ module BSON
79
81
  # 's' for dotall mode ('.' matches everything),
80
82
  # and 'u' to make \w, \W, etc. match unicode.
81
83
  #
82
- # @return [ String ] The encoded string.
84
+ # @param [ BSON::ByteBuffer ] buffer The byte buffer to append to.
85
+ # @param [ true, false ] validating_keys
86
+ #
87
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
83
88
  #
84
89
  # @see http://bsonspec.org/#/specification
85
90
  #
@@ -92,7 +97,8 @@ module BSON
92
97
  private
93
98
 
94
99
  def bson_options
95
- bson_ignorecase + bson_multiline + bson_extended
100
+ # Ruby's Regexp always has BSON's equivalent of 'm' on, so always add it
101
+ bson_ignorecase + MULTILINE_VALUE + bson_dotall + bson_extended
96
102
  end
97
103
 
98
104
  def bson_extended
@@ -103,8 +109,9 @@ module BSON
103
109
  (options & ::Regexp::IGNORECASE != 0) ? IGNORECASE_VALUE : NO_VALUE
104
110
  end
105
111
 
106
- def bson_multiline
107
- (options & ::Regexp::MULTILINE != 0) ? RUBY_MULTILINE_VALUE : NO_VALUE
112
+ def bson_dotall
113
+ # Ruby Regexp's MULTILINE is equivalent to BSON's dotall value
114
+ (options & ::Regexp::MULTILINE != 0) ? NEWLINE_VALUE : NO_VALUE
108
115
  end
109
116
 
110
117
  # Represents the raw values for the regular expression.
@@ -113,6 +120,7 @@ module BSON
113
120
  #
114
121
  # @since 3.0.0
115
122
  class Raw
123
+ include JSON
116
124
 
117
125
  # @return [ String ] pattern The regex pattern.
118
126
  attr_reader :pattern
@@ -129,7 +137,7 @@ module BSON
129
137
  #
130
138
  # @since 3.0.0
131
139
  def compile
132
- @compiled ||= ::Regexp.new(pattern, options)
140
+ @compiled ||= ::Regexp.new(pattern, options_to_int)
133
141
  end
134
142
 
135
143
  # Initialize the new raw regular expression.
@@ -138,7 +146,11 @@ module BSON
138
146
  # Raw.new(pattern, options)
139
147
  #
140
148
  # @param [ String ] pattern The regular expression pattern.
141
- # @param [ Integer ] options The options.
149
+ # @param [ String, Integer ] options The options.
150
+ #
151
+ # @note The ability to specify options as an Integer is deprecated.
152
+ # Please specify options as a String. The ability to pass options as
153
+ # as Integer will be removed in version 5.0.0.
142
154
  #
143
155
  # @since 3.0.0
144
156
  def initialize(pattern, options)
@@ -153,15 +165,81 @@ module BSON
153
165
  #
154
166
  # @since 3.1.0
155
167
  def respond_to?(method, include_private = false)
156
- compile.respond_to?(method, include_private = false) || super
168
+ compile.respond_to?(method, include_private) || super
157
169
  end
158
170
 
171
+ # Encode the Raw Regexp object to BSON.
172
+ #
173
+ # @example Get the raw regular expression as encoded BSON.
174
+ # raw_regexp.to_bson
175
+ #
176
+ # @note From the BSON spec: The first cstring is the regex pattern,
177
+ # the second is the regex options string. Options are identified
178
+ # by characters, which must be stored in alphabetical order.
179
+ # Valid options are 'i' for case insensitive matching,
180
+ # 'm' for multiline matching, 'x' for verbose mode,
181
+ # 'l' to make \w, \W, etc. locale dependent,
182
+ # 's' for dotall mode ('.' matches everything),
183
+ # and 'u' to make \w, \W, etc. match unicode.
184
+ #
185
+ # @param [ BSON::ByteBuffer ] buffer The byte buffer to append to.
186
+ # @param [ true, false ] validating_keys
187
+ #
188
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
189
+ #
190
+ # @see http://bsonspec.org/#/specification
191
+ #
192
+ # @since 4.2.0
193
+ def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
194
+ return compile.to_bson(buffer, validating_keys) if options.is_a?(Integer)
195
+ buffer.put_cstring(source)
196
+ buffer.put_cstring(options.chars.sort.join)
197
+ end
198
+
199
+ # Get the raw BSON regexp as JSON hash data.
200
+ #
201
+ # @example Get the raw regexp as a JSON hash.
202
+ # raw_regexp.as_json
203
+ #
204
+ # @return [ Hash ] The raw regexp as a JSON hash.
205
+ #
206
+ # @since 4.2.0
207
+ def as_json(*args)
208
+ { "$regex" => source, "$options" => options }
209
+ end
210
+
211
+ # Check equality of the raw bson regexp against another.
212
+ #
213
+ # @example Check if the raw bson regexp is equal to the other.
214
+ # raw_regexp == other
215
+ #
216
+ # @param [ Object ] other The object to check against.
217
+ #
218
+ # @return [ true, false ] If the objects are equal.
219
+ #
220
+ # @since 4.2.0
221
+ def ==(other)
222
+ return false unless other.is_a?(::Regexp::Raw)
223
+ pattern == other.pattern &&
224
+ options == other.options
225
+ end
226
+ alias :eql? :==
227
+
159
228
  private
160
229
 
161
230
  def method_missing(method, *arguments)
162
231
  return super unless respond_to?(method)
163
232
  compile.send(method, *arguments)
164
233
  end
234
+
235
+ def options_to_int
236
+ return options if options.is_a?(Integer)
237
+ opts = 0
238
+ opts |= ::Regexp::IGNORECASE if options.include?(IGNORECASE_VALUE)
239
+ opts |= ::Regexp::MULTILINE if options.include?(NEWLINE_VALUE)
240
+ opts |= ::Regexp::EXTENDED if options.include?(EXTENDED_VALUE)
241
+ opts
242
+ end
165
243
  end
166
244
 
167
245
  module ClassMethods
@@ -177,17 +255,7 @@ module BSON
177
255
  # @since 2.0.0
178
256
  def from_bson(buffer)
179
257
  pattern = buffer.get_cstring
180
- options = 0
181
- while (option = buffer.get_byte) != NULL_BYTE
182
- case option
183
- when IGNORECASE_VALUE
184
- options |= ::Regexp::IGNORECASE
185
- when MULTILINE_VALUE, NEWLINE_VALUE
186
- options |= ::Regexp::MULTILINE
187
- when EXTENDED_VALUE
188
- options |= ::Regexp::EXTENDED
189
- end
190
- end
258
+ options = buffer.get_cstring
191
259
  Raw.new(pattern, options)
192
260
  end
193
261
  end
@@ -53,7 +53,7 @@ module BSON
53
53
  # BSON::Registry.register("\x01", Float)
54
54
  #
55
55
  # @param [ String ] byte The single byte.
56
- # @param [ Class ] The class the byte maps to.
56
+ # @param [ Class ] type The class the byte maps to.
57
57
  #
58
58
  # @return [ Class ] The class.
59
59
  #