bson 4.1.1-java → 4.2.0-java

Sign up to get free protection for your applications and to get access to all the features.
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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 937c39adeba926daec9cda4e1a3f33ed95d409df
4
- data.tar.gz: 16b8d33f71e2a25762fb10489115dad09ac16b02
3
+ metadata.gz: 2727f24e105392aa4228203a0a7cc793f3ce2a0a
4
+ data.tar.gz: 56c958876a31969a663c72084ab0873c0e83eb6a
5
5
  SHA512:
6
- metadata.gz: 6b4734870f5a0111a4129295f8e851cd65578d0b2b54aae09e15965a8b0408ec6e61f583c7b47798b10c2f3997c11b8db48fa8861a53f4db8499cbb121d047f2
7
- data.tar.gz: 9f6f660b38963ce96184acaa73f6ac4906fb8b9df2318aa093004e56ed48132801c8c650996c232299ae16e78442a8a4ea6c890170bf429f317eb977e4eb6027
6
+ metadata.gz: b7596f9b4b167a80f286b4d90350a117e044dc265ec463d9d9cedf0624eaffd3a2eba5dddfa66727f20bd0f9251ff064cd26fd69e07a542a4a61c5c6a4a05691
7
+ data.tar.gz: b3b6527d51fd6f14eff888aecf7aed9e7e46202f293448eea3a22c0823901c7922b7c85e099a6038306b0c8bf6d723745c767b2dca5d7467eb0ed61f5ed74845
Binary file
data.tar.gz.sig CHANGED
Binary file
data/Rakefile CHANGED
@@ -35,7 +35,7 @@ if jruby?
35
35
  else
36
36
  require "rake/extensiontask"
37
37
  Rake::ExtensionTask.new do |ext|
38
- ext.name = "native"
38
+ ext.name = "bson_native"
39
39
  ext.ext_dir = "ext/bson"
40
40
  ext.lib_dir = "lib"
41
41
  end
@@ -64,8 +64,8 @@ end
64
64
  task :clean_all => :clean do
65
65
  begin
66
66
  Dir.chdir(Pathname(__FILE__).dirname + "lib") do
67
- `rm native.#{extension}`
68
- `rm native.o`
67
+ `rm bson_native.#{extension}`
68
+ `rm bson_native.o`
69
69
  `rm bson-ruby.jar`
70
70
  end
71
71
  rescue Exception => e
@@ -108,6 +108,21 @@ namespace :benchmark do
108
108
  require "bson"
109
109
  benchmark!
110
110
  end
111
+
112
+ namespace :decimal128 do
113
+
114
+ task :from_string do
115
+ puts "Benchmarking creating Decimal128 objects from a string"
116
+ require 'bson'
117
+ benchmark_decimal128_from_string!
118
+ end
119
+
120
+ task :to_string do
121
+ puts "Benchmarking getting a string representation of a Decimal128"
122
+ require 'bson'
123
+ benchmark_decimal128_to_string!
124
+ end
125
+ end
111
126
  end
112
127
 
113
128
  task :default => [ :clean_all, :spec ]
Binary file
@@ -70,10 +70,12 @@ require "bson/code"
70
70
  require "bson/code_with_scope"
71
71
  require "bson/date"
72
72
  require "bson/date_time"
73
+ require "bson/decimal128"
73
74
  require "bson/document"
74
75
  require "bson/false_class"
75
76
  require "bson/float"
76
77
  require "bson/hash"
78
+ require "bson/open_struct"
77
79
  require "bson/max_key"
78
80
  require "bson/min_key"
79
81
  require "bson/nil_class"
@@ -89,8 +91,7 @@ require "bson/undefined"
89
91
  require "bson/version"
90
92
 
91
93
  # If we are using JRuby, attempt to load the Java extensions, if we are using
92
- # MRI or Rubinius, attempt to load the C extenstions. If either of these fail,
93
- # we revert back to a pure Ruby implementation of the Buffer class.
94
+ # MRI or Rubinius, attempt to load the C extensions.
94
95
  #
95
96
  # @since 2.0.0
96
97
  begin
@@ -98,8 +99,9 @@ begin
98
99
  require "bson-ruby.jar"
99
100
  org.bson.NativeService.new.basicLoad(JRuby.runtime)
100
101
  else
101
- require "native"
102
+ require "bson_native"
102
103
  end
103
104
  rescue LoadError
104
- $stderr.puts("BSON is using the pure Ruby implementation.")
105
+ $stderr.puts("Failed to load the necessary extensions.")
106
+ raise
105
107
  end
@@ -35,7 +35,7 @@ module BSON
35
35
  # @note Arrays are encoded as documents, where the index of the value in
36
36
  # the array is the actual key.
37
37
  #
38
- # @return [ String ] The encoded string.
38
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
39
39
  #
40
40
  # @see http://bsonspec.org/#/specification
41
41
  #
@@ -12,6 +12,8 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+ require 'base64'
16
+
15
17
  module BSON
16
18
 
17
19
  # Represents binary data.
@@ -89,7 +91,7 @@ module BSON
89
91
  #
90
92
  # @since 2.0.0
91
93
  def as_json(*args)
92
- { "$binary" => data, "$type" => type }
94
+ { "$binary" => Base64.encode64(data), "$type" => type }
93
95
  end
94
96
 
95
97
  # Instantiate the new binary object.
@@ -124,7 +126,7 @@ module BSON
124
126
  # @example Encode the binary.
125
127
  # binary.to_bson
126
128
  #
127
- # @return [ String ] The encoded binary.
129
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
128
130
  #
129
131
  # @see http://bsonspec.org/#/specification
130
132
  #
@@ -76,7 +76,7 @@ module BSON
76
76
  # @example Encode the code.
77
77
  # code.to_bson
78
78
  #
79
- # @return [ String ] The encoded string.
79
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
80
80
  #
81
81
  # @see http://bsonspec.org/#/specification
82
82
  #
@@ -82,7 +82,7 @@ module BSON
82
82
  # @example Encode the code with scope.
83
83
  # code_with_scope.to_bson
84
84
  #
85
- # @return [ String ] The encoded string.
85
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
86
86
  #
87
87
  # @see http://bsonspec.org/#/specification
88
88
  #
@@ -45,7 +45,7 @@ module BSON
45
45
  #
46
46
  # @since 4.1.0
47
47
  def validating_keys?
48
- !!@validating_keys
48
+ !!(@validating_keys||=nil)
49
49
  end
50
50
  end
51
51
  end
@@ -29,7 +29,7 @@ module BSON
29
29
  # @example Get the date as encoded BSON.
30
30
  # Date.new(2012, 1, 1).to_bson
31
31
  #
32
- # @return [ String ] The encoded string.
32
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
33
33
  #
34
34
  # @see http://bsonspec.org/#/specification
35
35
  #
@@ -29,7 +29,7 @@ module BSON
29
29
  # @example Get the date time as encoded BSON.
30
30
  # DateTime.new(2012, 1, 1, 0, 0, 0).to_bson
31
31
  #
32
- # @return [ String ] The encoded string.
32
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
33
33
  #
34
34
  # @see http://bsonspec.org/#/specification
35
35
  #
@@ -0,0 +1,318 @@
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
+ require 'bigdecimal'
16
+ require 'bson/decimal128/builder'
17
+
18
+ module BSON
19
+
20
+ class Decimal128
21
+
22
+ # A Decimal128 is type 0x13 in the BSON spec.
23
+ #
24
+ # @since 4.2.0
25
+ BSON_TYPE = 19.chr.force_encoding(BINARY).freeze
26
+
27
+ # Exponent offset.
28
+ #
29
+ # @since 4.2.0
30
+ EXPONENT_OFFSET = 6176.freeze
31
+
32
+ # Minimum exponent.
33
+ #
34
+ # @since 4.2.0
35
+ MIN_EXPONENT = -6176.freeze
36
+
37
+ # Maximum exponent.
38
+ #
39
+ # @since 4.2.0
40
+ MAX_EXPONENT = 6111.freeze
41
+
42
+ # Maximum digits of precision.
43
+ #
44
+ # @since 4.2.0
45
+ MAX_DIGITS_OF_PRECISION = 34.freeze
46
+
47
+ # Key for this type when converted to extended json.
48
+ #
49
+ # @since 4.2.0
50
+ EXTENDED_JSON_KEY = "$numberDecimal".freeze
51
+
52
+ # The native type to which this object can be converted.
53
+ #
54
+ # @since 4.2.0
55
+ NATIVE_TYPE = BigDecimal
56
+
57
+ # Get the Decimal128 as JSON hash data.
58
+ #
59
+ # @example Get the Decimal128 as a JSON hash.
60
+ # decimal.as_json
61
+ #
62
+ # @return [ Hash ] The number as a JSON hash.
63
+ #
64
+ # @since 4.2.0
65
+ def as_json(*args)
66
+ { EXTENDED_JSON_KEY => to_s }
67
+ end
68
+
69
+ # Check equality of the decimal128 object with another object.
70
+ #
71
+ # @example Check if the decimal128 object is equal to the other.
72
+ # decimal == other
73
+ #
74
+ # @param [ Object ] other The object to check against.
75
+ #
76
+ # @return [ true, false ] If the objects are equal.
77
+ #
78
+ # @since 4.2.0
79
+ def ==(other)
80
+ return false unless other.is_a?(Decimal128)
81
+ @high == other.instance_variable_get(:@high) &&
82
+ @low == other.instance_variable_get(:@low)
83
+ end
84
+ alias :eql? :==
85
+
86
+ # Create a new Decimal128 from a BigDecimal.
87
+ #
88
+ # @example Create a Decimal128 from a BigDecimal.
89
+ # Decimal128.new(big_decimal)
90
+ #
91
+ # @param [ String, BigDecimal ] object The BigDecimal or String to use for
92
+ # instantiating a Decimal128.
93
+ #
94
+ # @raise [ InvalidBigDecimal ] Raise error unless object argument is a BigDecimal.
95
+ #
96
+ # @since 4.2.0
97
+ def initialize(object)
98
+ if object.is_a?(String)
99
+ set_bits(*Builder::FromString.new(object).bits)
100
+ elsif object.is_a?(BigDecimal)
101
+ set_bits(*Builder::FromBigDecimal.new(object).bits)
102
+ else
103
+ raise InvalidArgument.new
104
+ end
105
+ end
106
+
107
+ # Get the decimal128 as its raw BSON data.
108
+ #
109
+ # @example Get the raw bson bytes in a buffer.
110
+ # decimal.to_bson
111
+ #
112
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
113
+ #
114
+ # @see http://bsonspec.org/#/specification
115
+ #
116
+ # @since 4.2.0
117
+ def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
118
+ buffer.put_decimal128(@low, @high)
119
+ end
120
+
121
+ # Get the hash value for the decimal128.
122
+ #
123
+ # @example Get the hash value.
124
+ # decimal.hash
125
+ #
126
+ # @return [ Integer ] The hash value.
127
+ #
128
+ # @since 4.2.0
129
+ def hash
130
+ num = @high << 64
131
+ num |= @low
132
+ num.hash
133
+ end
134
+
135
+ # Get a nice string for use with object inspection.
136
+ #
137
+ # @example Inspect the decimal128 object.
138
+ # decimal128.inspect
139
+ #
140
+ # @return [ String ] The decimal as a string.
141
+ #
142
+ # @since 4.2.0
143
+ def inspect
144
+ "BSON::Decimal128('#{to_s}')"
145
+ end
146
+
147
+ # Get the string representation of the decimal128.
148
+ #
149
+ # @example Get the decimal128 as a string.
150
+ # decimal128.to_s
151
+ #
152
+ # @return [ String ] The decimal128 as a string.
153
+ #
154
+ # @since 4.2.0
155
+ def to_s
156
+ @string ||= Builder::ToString.new(self).string
157
+ end
158
+ alias :to_str :to_s
159
+
160
+ # Get a Ruby BigDecimal object corresponding to this Decimal128.
161
+ # Note that, when converting to a Ruby BigDecimal, non-zero significant digits
162
+ # are preserved but trailing zeroes may be lost.
163
+ # See the following example:
164
+ #
165
+ # @example
166
+ # decimal128 = BSON::Decimal128.new("0.200")
167
+ # => BSON::Decimal128('0.200')
168
+ # big_decimal = decimal128.to_big_decimal
169
+ # => #<BigDecimal:7fc619c95388,'0.2E0',9(18)>
170
+ # big_decimal.to_s
171
+ # => "0.2E0"
172
+ #
173
+ # Note that the the BSON::Decimal128 object can represent -NaN, sNaN,
174
+ # and -sNaN while Ruby's BigDecimal cannot.
175
+ #
176
+ # @return [ BigDecimal ] The decimal as a BigDecimal.
177
+ #
178
+ # @since 4.2.0
179
+ def to_big_decimal
180
+ @big_decimal ||= BigDecimal.new(to_s)
181
+ end
182
+
183
+ private
184
+
185
+ def set_bits(low, high)
186
+ @low = low
187
+ @high = high
188
+ end
189
+
190
+ class << self
191
+
192
+ # Deserialize the decimal128 from raw BSON bytes.
193
+ #
194
+ # @example Get the decimal128 from BSON.
195
+ # Decimal128.from_bson(bson)
196
+ #
197
+ # @param [ ByteBuffer ] buffer The byte buffer.
198
+ #
199
+ # @return [ BSON::Decimal128 ] The decimal object.
200
+ #
201
+ # @since 4.2.0
202
+ def from_bson(buffer)
203
+ from_bits(*buffer.get_decimal128_bytes.unpack('Q*'))
204
+ end
205
+
206
+ # Instantiate a Decimal128 from a string.
207
+ #
208
+ # @example Create a Decimal128 from a string.
209
+ # BSON::Decimal128.from_string("1.05E+3")
210
+ #
211
+ # @param [ String ] string The string to parse.
212
+ #
213
+ # @raise [ BSON::Decimal128::InvalidString ] If the provided string is invalid.
214
+ #
215
+ # @return [ BSON::Decimal128 ] The new decimal128.
216
+ #
217
+ # @since 4.2.0
218
+ def from_string(string)
219
+ from_bits(*Builder::FromString.new(string).bits)
220
+ end
221
+
222
+ # Instantiate a Decimal128 from high and low bits.
223
+ #
224
+ # @example Create a Decimal128 from high and low bits.
225
+ # BSON::Decimal128.from_bits(high, low)
226
+ #
227
+ # @param [ Integer ] high The high order bits.
228
+ # @param [ Integer ] low The low order bits.
229
+ #
230
+ # @return [ BSON::Decimal128 ] The new decimal128.
231
+ #
232
+ # @since 4.2.0
233
+ def from_bits(low, high)
234
+ decimal = allocate
235
+ decimal.send(:set_bits, low, high)
236
+ decimal
237
+ end
238
+ end
239
+
240
+ # Raised when trying to create a Decimal128 from an object that is neither a String nor a BigDecimal.
241
+ #
242
+ # @api private
243
+ #
244
+ # @since 4.2.0
245
+ class InvalidArgument < ArgumentError
246
+
247
+ # The custom error message for this error.
248
+ #
249
+ # @since 4.2.0
250
+ MESSAGE = 'A Decimal128 can only be created from a String or BigDecimal.'.freeze
251
+
252
+ # Get the custom error message for the exception.
253
+ #
254
+ # @example Get the message.
255
+ # error.message
256
+ #
257
+ # @return [ String ] The error message.
258
+ #
259
+ # @since 4.2.0
260
+ def message
261
+ MESSAGE
262
+ end
263
+ end
264
+
265
+ # Raised when trying to create a Decimal128 from a string with
266
+ # an invalid format.
267
+ #
268
+ # @api private
269
+ #
270
+ # @since 4.2.0
271
+ class InvalidString < RuntimeError
272
+
273
+ # The custom error message for this error.
274
+ #
275
+ # @since 4.2.0
276
+ MESSAGE = 'Invalid string format for creating a Decimal128 object.'.freeze
277
+
278
+ # Get the custom error message for the exception.
279
+ #
280
+ # @example Get the message.
281
+ # error.message
282
+ #
283
+ # @return [ String ] The error message.
284
+ #
285
+ # @since 4.2.0
286
+ def message
287
+ MESSAGE
288
+ end
289
+ end
290
+
291
+ # Raised when the exponent or significand provided is outside the valid range.
292
+ #
293
+ # @api private
294
+ #
295
+ # @since 4.2.0
296
+ class InvalidRange < RuntimeError
297
+
298
+ # The custom error message for this error.
299
+ #
300
+ # @since 4.2.0
301
+ MESSAGE = 'Value out of range for Decimal128 representation.'.freeze
302
+
303
+ # Get the custom error message for the exception.
304
+ #
305
+ # @example Get the message.
306
+ # error.message
307
+ #
308
+ # @return [ String ] The error message.
309
+ #
310
+ # @since 4.2.0
311
+ def message
312
+ MESSAGE
313
+ end
314
+ end
315
+
316
+ Registry.register(BSON_TYPE, self)
317
+ end
318
+ end