bson 4.13.0-java → 4.15.0-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ec2d89585c800c0c61b58f6902381b36273854a1c2a7f8064bfdc50b838cf123
4
- data.tar.gz: be36daad51a87d3e7fd2d783c8a1e7b5d83ab2c4ea6fe515fd02012d5ad996ec
3
+ metadata.gz: bc99b74544db3686a580a0f25c2ef2a36e5d4c95dc17924d36285cc1e23b4f70
4
+ data.tar.gz: 387a4a3a3af6a56e980e93648d44d2b8196e76cc7f65ba494b8d6613d5580660
5
5
  SHA512:
6
- metadata.gz: 79b8c0aa172f5fe850d6b8de145b2e0439a5b07544cfa994a22df322c8f6c88da09b6ca9db7352d0c01fbe9db03546bd9c34a7736a8305d06be992cd68508d9d
7
- data.tar.gz: 64d250ddbf130ccc8db0405dd251f23277666bab3900a1b685f574757cf05788c88437d8de02d501e117dec951abec5a0a9d457a64209dbff1e0d8c9ab3519a9
6
+ metadata.gz: e495d9ae63176d4d594d715f983cc1cf01d23761f383f079397ed2b24927c868c2c45a50d1943eb8bfccab4c8bfbd9a839830a8f6473b31a590a6408f531637c
7
+ data.tar.gz: f1750558d5e912736870d1d4e82c0ae5ca48eec13521efc8d504cb7483bdc2c2ec20912f47fc7ae30efd3d7e7111f9992e8b4c28e18280dc21b00848ac314979
checksums.yaml.gz.sig CHANGED
Binary file
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+ # Copyright (C) 2009-2021 MongoDB 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
+ module BSON
17
+
18
+ # Injects behaviour for encoding and decoding BigDecimals
19
+ # to and from raw bytes as specified by the BSON spec.
20
+ #
21
+ # @see http://bsonspec.org/#/specification
22
+ module BigDecimal
23
+
24
+ # BigDecimals are serialized as Decimal128s under the hood. A Decimal128
25
+ # is type 0x13 in the BSON spec.
26
+ BSON_TYPE = ::String.new(19.chr, encoding: BINARY).freeze
27
+
28
+ # Get the BigDecimal as encoded BSON.
29
+ #
30
+ # @example Get the BigDecimal as encoded BSON.
31
+ # BigDecimal("1").to_bson
32
+ #
33
+ # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
34
+ #
35
+ # @see http://bsonspec.org/#/specification
36
+ def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
37
+ BSON::Decimal128.new(to_s).to_bson(buffer, validating_keys)
38
+ end
39
+
40
+ # Get the BSON type for BigDecimal. This is the same BSON type as
41
+ # BSON::Decimal128.
42
+ def bson_type
43
+ BSON_TYPE
44
+ end
45
+
46
+ module ClassMethods
47
+
48
+ # Deserialize the BigDecimal from raw BSON bytes.
49
+ #
50
+ # @example Get the BigDecimal from BSON.
51
+ # BigDecimal.from_bson(bson)
52
+ #
53
+ # @param [ ByteBuffer ] buffer The byte buffer.
54
+ #
55
+ # @option options [ nil | :bson ] :mode Decoding mode to use.
56
+ #
57
+ # @return [ BigDecimal ] The decimal object.
58
+ def from_bson(buffer, **options)
59
+ Decimal128.from_bson(buffer, **options).to_big_decimal
60
+ end
61
+ end
62
+ end
63
+
64
+ # Enrich the core BigDecimal class with this module.
65
+ ::BigDecimal.send(:include, BigDecimal)
66
+ ::BigDecimal.send(:extend, BigDecimal::ClassMethods)
67
+ end
data/lib/bson/dbref.rb CHANGED
@@ -63,11 +63,38 @@ module BSON
63
63
 
64
64
  # Instantiate a new DBRef.
65
65
  #
66
- # @example Create the DBRef.
66
+ # @example Create the DBRef - hash API.
67
67
  # BSON::DBRef.new({'$ref' => 'users', '$id' => id, '$db' => 'database'})
68
68
  #
69
- # @param [ Hash ] hash the DBRef hash. It must contain $ref and $id.
70
- def initialize(hash)
69
+ # @example Create the DBRef - legacy API.
70
+ # BSON::DBRef.new('users', id, 'database')
71
+ #
72
+ # @param [ Hash | String ] hash_or_collection The DBRef hash, when using
73
+ # the hash API. It must contain $ref and $id. When using the legacy API,
74
+ # this parameter must be a String containing the collection name.
75
+ # @param [ Object ] id The object id, when using the legacy API.
76
+ # @param [ String ] database The database name, when using the legacy API.
77
+ def initialize(hash_or_collection, id = nil, database = nil)
78
+ if hash_or_collection.is_a?(Hash)
79
+ hash = hash_or_collection
80
+
81
+ unless id.nil? && database.nil?
82
+ raise ArgumentError, 'When using the hash API, DBRef constructor accepts only one argument'
83
+ end
84
+ else
85
+ warn("BSON::DBRef constructor called with the legacy API - please use the hash API instead")
86
+
87
+ if id.nil?
88
+ raise ArgumentError, 'When using the legacy constructor API, id must be provided'
89
+ end
90
+
91
+ hash = {
92
+ :$ref => hash_or_collection,
93
+ :$id => id,
94
+ :$db => database,
95
+ }
96
+ end
97
+
71
98
  hash = reorder_fields(hash)
72
99
  %w($ref $id).each do |key|
73
100
  unless hash[key]
@@ -85,7 +112,7 @@ module BSON
85
112
  end
86
113
  end
87
114
 
88
- super
115
+ super(hash)
89
116
  end
90
117
 
91
118
  # Converts the DBRef to raw BSON.
@@ -85,9 +85,13 @@ module BSON
85
85
  private
86
86
 
87
87
  def validate_range!(exponent, significand)
88
- unless valid_significand?(significand) && valid_exponent?(exponent)
88
+ unless valid_exponent?(exponent)
89
89
  raise Decimal128::InvalidRange.new
90
90
  end
91
+
92
+ unless valid_significand?(significand)
93
+ raise Decimal128::UnrepresentablePrecision.new
94
+ end
91
95
  end
92
96
 
93
97
  def valid_significand?(significand)
@@ -302,11 +306,11 @@ module BSON
302
306
 
303
307
  def to_special_bits
304
308
  case @big_decimal.sign
305
- when BigDecimal::SIGN_POSITIVE_INFINITE
309
+ when ::BigDecimal::SIGN_POSITIVE_INFINITE
306
310
  high = INFINITY_MASK
307
- when BigDecimal::SIGN_NEGATIVE_INFINITE
311
+ when ::BigDecimal::SIGN_NEGATIVE_INFINITE
308
312
  high = INFINITY_MASK | SIGN_BIT_MASK
309
- when BigDecimal::SIGN_NaN
313
+ when ::BigDecimal::SIGN_NaN
310
314
  high = NAN_MASK
311
315
  end
312
316
  [ 0, high ]
@@ -315,7 +319,7 @@ module BSON
315
319
  def to_bits
316
320
  sign, significand_str, base, exp = @big_decimal.split
317
321
  exponent = @big_decimal.zero? ? 0 : exp - significand_str.length
318
- is_negative = (sign == BigDecimal::SIGN_NEGATIVE_FINITE || sign == BigDecimal::SIGN_NEGATIVE_ZERO)
322
+ is_negative = (sign == ::BigDecimal::SIGN_NEGATIVE_FINITE || sign == ::BigDecimal::SIGN_NEGATIVE_ZERO)
319
323
  Builder.parts_to_bits(significand_str.to_i,
320
324
  exponent,
321
325
  is_negative)
@@ -20,6 +20,7 @@ module BSON
20
20
 
21
21
  class Decimal128
22
22
  include JSON
23
+ include Comparable
23
24
 
24
25
  # A Decimal128 is type 0x13 in the BSON spec.
25
26
  #
@@ -97,7 +98,16 @@ module BSON
97
98
  end
98
99
  alias :eql? :==
99
100
 
100
- # Create a new Decimal128 from a BigDecimal.
101
+ def <=>(other)
102
+ to_big_decimal <=> case other
103
+ when Decimal128
104
+ other.to_big_decimal
105
+ else
106
+ other
107
+ end
108
+ end
109
+
110
+ # Create a new Decimal128 from a string or a BigDecimal instance.
101
111
  #
102
112
  # @example Create a Decimal128 from a BigDecimal.
103
113
  # Decimal128.new(big_decimal)
@@ -105,7 +115,7 @@ module BSON
105
115
  # @param [ String, BigDecimal ] object The BigDecimal or String to use for
106
116
  # instantiating a Decimal128.
107
117
  #
108
- # @raise [ InvalidBigDecimal ] Raise error unless object argument is a BigDecimal.
118
+ # @raise [ InvalidArgument ] When argument is not a String or BigDecimal.
109
119
  #
110
120
  # @since 4.2.0
111
121
  def initialize(object)
@@ -304,9 +314,7 @@ module BSON
304
314
  end
305
315
  end
306
316
 
307
- # Raised when the exponent or significand provided is outside the valid range.
308
- #
309
- # @api private
317
+ # Raised when the exponent is outside the valid range.
310
318
  #
311
319
  # @since 4.2.0
312
320
  class InvalidRange < RuntimeError
@@ -314,6 +322,7 @@ module BSON
314
322
  # The custom error message for this error.
315
323
  #
316
324
  # @since 4.2.0
325
+ # @deprecated
317
326
  MESSAGE = 'Value out of range for Decimal128 representation.'
318
327
 
319
328
  # Get the custom error message for the exception.
@@ -329,6 +338,21 @@ module BSON
329
338
  end
330
339
  end
331
340
 
341
+ # Raised when the significand provided is outside the valid range.
342
+ #
343
+ # @note This class derives from InvalidRange for backwards compatibility,
344
+ # however when RUBY-1806 is implemented it should be changed to derive
345
+ # from the base BSON exception class.
346
+ class UnrepresentablePrecision < InvalidRange
347
+
348
+ # Get the custom error message for the exception.
349
+ #
350
+ # @return [ String ] The error message.
351
+ def message
352
+ 'The value contains too much precision for Decimal128 representation'
353
+ end
354
+ end
355
+
332
356
  Registry.register(BSON_TYPE, self)
333
357
  end
334
358
  end
data/lib/bson/float.rb CHANGED
@@ -16,7 +16,7 @@
16
16
  module BSON
17
17
 
18
18
  # Injects behaviour for encoding and decoding floating point values
19
- # to and from # raw bytes as specified by the BSON spec.
19
+ # to and from raw bytes as specified by the BSON spec.
20
20
  #
21
21
  # @see http://bsonspec.org/#/specification
22
22
  #
data/lib/bson/version.rb CHANGED
@@ -14,5 +14,5 @@
14
14
  # limitations under the License.
15
15
 
16
16
  module BSON
17
- VERSION = "4.13.0"
17
+ VERSION = "4.15.0"
18
18
  end
data/lib/bson-ruby.jar CHANGED
Binary file
data/lib/bson.rb CHANGED
@@ -74,6 +74,7 @@ require "bson/date"
74
74
  require "bson/date_time"
75
75
  require "bson/db_pointer"
76
76
  require "bson/decimal128"
77
+ require "bson/big_decimal"
77
78
  require "bson/document"
78
79
  require "bson/ext_json"
79
80
  require "bson/false_class"
@@ -102,7 +103,7 @@ require "bson/version"
102
103
  begin
103
104
  if BSON::Environment.jruby?
104
105
  require "bson-ruby.jar"
105
- org.bson.NativeService.new.basicLoad(JRuby.runtime)
106
+ JRuby::Util.load_ext("org.bson.NativeService")
106
107
  else
107
108
  require "bson_native"
108
109
  end
@@ -0,0 +1,316 @@
1
+ # Copyright (C) 2016-2021 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 "spec_helper"
16
+
17
+ describe BSON::BigDecimal do
18
+
19
+ describe '#from_bson' do
20
+ shared_examples_for 'a BSON::BigDecimal deserializer' do
21
+
22
+ let(:decimal128) do
23
+ BSON::Decimal128.new(argument)
24
+ end
25
+
26
+ let(:deserialized_big_decimal) do
27
+ BigDecimal.from_bson(decimal128.to_bson)
28
+ end
29
+
30
+ let(:deserialized_decimal128) do
31
+ BSON::Decimal128.from_bson(decimal128.to_bson)
32
+ end
33
+
34
+ it 'deserializes Decimal128 encoded bson correctly' do
35
+ if deserialized_decimal128.to_s == "NaN"
36
+ expect(deserialized_big_decimal.nan?).to be true
37
+ else
38
+ expect(deserialized_big_decimal).to eq(deserialized_decimal128.to_big_decimal)
39
+ end
40
+ end
41
+ end
42
+
43
+ context 'when Infinity is passed' do
44
+
45
+ let(:argument) { "Infinity" }
46
+
47
+ it_behaves_like 'a BSON::BigDecimal deserializer'
48
+ end
49
+
50
+ context 'when -Infinity is passed' do
51
+
52
+ let(:argument) { "-Infinity" }
53
+
54
+ it_behaves_like 'a BSON::BigDecimal deserializer'
55
+ end
56
+
57
+ context 'when NaN is passed' do
58
+
59
+ let(:argument) { "NaN" }
60
+
61
+ it_behaves_like 'a BSON::BigDecimal deserializer'
62
+ end
63
+
64
+ context 'when -NaN is passed' do
65
+ let(:argument) { "-NaN" }
66
+
67
+ it_behaves_like 'a BSON::BigDecimal deserializer'
68
+ end
69
+
70
+ context 'when SNaN is passed' do
71
+ let(:argument) { "SNaN" }
72
+
73
+ it_behaves_like 'a BSON::BigDecimal deserializer'
74
+ end
75
+
76
+ context 'when -SNaN is passed' do
77
+ let(:argument) { "SNaN" }
78
+
79
+ it_behaves_like 'a BSON::BigDecimal deserializer'
80
+ end
81
+
82
+ context 'when -0 is passed' do
83
+ let(:argument) { "-0" }
84
+
85
+ it_behaves_like 'a BSON::BigDecimal deserializer'
86
+ end
87
+
88
+ context 'when a positive integer is passed' do
89
+ let(:argument) { "12" }
90
+
91
+ it_behaves_like 'a BSON::BigDecimal deserializer'
92
+ end
93
+
94
+ context 'when a negative integer is passed' do
95
+ let(:argument) { "-12" }
96
+
97
+ it_behaves_like 'a BSON::BigDecimal deserializer'
98
+ end
99
+
100
+ context 'when a positive float is passed' do
101
+ let(:argument) { "0.12345" }
102
+
103
+ it_behaves_like 'a BSON::BigDecimal deserializer'
104
+ end
105
+
106
+ context 'when a negative float is passed' do
107
+ let(:argument) { "-0.12345" }
108
+
109
+ it_behaves_like 'a BSON::BigDecimal deserializer'
110
+ end
111
+
112
+ context 'when a large positive integer is passed' do
113
+ let(:argument) { "1234567890123456789012345678901234" }
114
+
115
+ it_behaves_like 'a BSON::BigDecimal deserializer'
116
+ end
117
+
118
+ context 'when a large negative integer is passed' do
119
+ let(:argument) { "-1234567890123456789012345678901234" }
120
+
121
+ it_behaves_like 'a BSON::BigDecimal deserializer'
122
+ end
123
+ end
124
+
125
+ describe "#to_bson" do
126
+ shared_examples_for 'a BSON::BigDecimal serializer' do
127
+
128
+ let(:decimal128) do
129
+ BSON::Decimal128.new(BigDecimal(argument).to_s)
130
+ end
131
+
132
+ let(:decimal_128_bson) do
133
+ decimal128.to_bson
134
+ end
135
+
136
+ let(:big_decimal_bson) do
137
+ BigDecimal(argument).to_bson
138
+ end
139
+
140
+ it 'serializes BigDecimals correctly' do
141
+ expect(decimal_128_bson.to_s).to eq(big_decimal_bson.to_s)
142
+ end
143
+ end
144
+
145
+ context 'when Infinity is passed' do
146
+
147
+ let(:argument) { "Infinity" }
148
+
149
+ it_behaves_like 'a BSON::BigDecimal serializer'
150
+ end
151
+
152
+ context 'when -Infinity is passed' do
153
+
154
+ let(:argument) { "-Infinity" }
155
+
156
+ it_behaves_like 'a BSON::BigDecimal serializer'
157
+ end
158
+
159
+ context 'when NaN is passed' do
160
+
161
+ let(:argument) { "NaN" }
162
+
163
+ it_behaves_like 'a BSON::BigDecimal serializer'
164
+ end
165
+
166
+ context 'when -0 is passed' do
167
+ let(:argument) { "-0" }
168
+
169
+ it_behaves_like 'a BSON::BigDecimal serializer'
170
+ end
171
+
172
+ context 'when a positive integer is passed' do
173
+ let(:argument) { "12" }
174
+
175
+ it_behaves_like 'a BSON::BigDecimal serializer'
176
+ end
177
+
178
+ context 'when a negative integer is passed' do
179
+ let(:argument) { "-12" }
180
+
181
+ it_behaves_like 'a BSON::BigDecimal serializer'
182
+ end
183
+
184
+ context 'when a positive float is passed' do
185
+ let(:argument) { "0.12345" }
186
+
187
+ it_behaves_like 'a BSON::BigDecimal serializer'
188
+ end
189
+
190
+ context 'when a negative float is passed' do
191
+ let(:argument) { "-0.12345" }
192
+
193
+ it_behaves_like 'a BSON::BigDecimal serializer'
194
+ end
195
+
196
+ context 'when a large positive integer is passed' do
197
+ let(:argument) { "1234567890123456789012345678901234" }
198
+
199
+ it_behaves_like 'a BSON::BigDecimal serializer'
200
+ end
201
+
202
+ context 'when a large negative integer is passed' do
203
+ let(:argument) { "-1234567890123456789012345678901234" }
204
+
205
+ it_behaves_like 'a BSON::BigDecimal serializer'
206
+ end
207
+
208
+ context "when passing an out of range Decimal128" do
209
+ let(:argument) { "1E1000000" }
210
+
211
+ it "raises an error" do
212
+ expect do
213
+ BigDecimal(argument).to_bson
214
+ end.to raise_error(BSON::Decimal128::InvalidRange)
215
+ end
216
+ end
217
+
218
+ context "when passing a number with too much precision for Decimal128" do
219
+ let(:argument) { "1.000000000000000000000000000000000000000000000000001" }
220
+
221
+ it "raises an error" do
222
+ expect do
223
+ BigDecimal(argument).to_bson
224
+ end.to raise_error(BSON::Decimal128::UnrepresentablePrecision)
225
+ end
226
+ end
227
+ end
228
+
229
+ describe "#from_bson/#to_bson" do
230
+ shared_examples_for 'a BSON::BigDecimal round trip' do
231
+
232
+ let(:big_decimal) do
233
+ BigDecimal(argument)
234
+ end
235
+
236
+ let(:big_decimal_bson) do
237
+ big_decimal.to_bson
238
+ end
239
+
240
+ let(:deserialized_big_decimal) do
241
+ BigDecimal.from_bson(big_decimal_bson)
242
+ end
243
+
244
+ it 'serializes BigDecimals correctly' do
245
+ if big_decimal.nan?
246
+ expect(deserialized_big_decimal.nan?).to be true
247
+ else
248
+ expect(deserialized_big_decimal).to eq(big_decimal)
249
+ end
250
+ end
251
+ end
252
+
253
+ context 'when Infinity is passed' do
254
+
255
+ let(:argument) { "Infinity" }
256
+
257
+ it_behaves_like 'a BSON::BigDecimal round trip'
258
+ end
259
+
260
+ context 'when -Infinity is passed' do
261
+
262
+ let(:argument) { "-Infinity" }
263
+
264
+ it_behaves_like 'a BSON::BigDecimal round trip'
265
+ end
266
+
267
+ context 'when NaN is passed' do
268
+
269
+ let(:argument) { "NaN" }
270
+
271
+ it_behaves_like 'a BSON::BigDecimal round trip'
272
+ end
273
+
274
+ context 'when -0 is passed' do
275
+ let(:argument) { "-0" }
276
+
277
+ it_behaves_like 'a BSON::BigDecimal round trip'
278
+ end
279
+
280
+ context 'when a positive integer is passed' do
281
+ let(:argument) { "12" }
282
+
283
+ it_behaves_like 'a BSON::BigDecimal round trip'
284
+ end
285
+
286
+ context 'when a negative integer is passed' do
287
+ let(:argument) { "-12" }
288
+
289
+ it_behaves_like 'a BSON::BigDecimal round trip'
290
+ end
291
+
292
+ context 'when a positive float is passed' do
293
+ let(:argument) { "0.12345" }
294
+
295
+ it_behaves_like 'a BSON::BigDecimal round trip'
296
+ end
297
+
298
+ context 'when a negative float is passed' do
299
+ let(:argument) { "-0.12345" }
300
+
301
+ it_behaves_like 'a BSON::BigDecimal round trip'
302
+ end
303
+
304
+ context 'when a large positive integer is passed' do
305
+ let(:argument) { "1234567890123456789012345678901234" }
306
+
307
+ it_behaves_like 'a BSON::BigDecimal round trip'
308
+ end
309
+
310
+ context 'when a large negative integer is passed' do
311
+ let(:argument) { "-1234567890123456789012345678901234" }
312
+
313
+ it_behaves_like 'a BSON::BigDecimal round trip'
314
+ end
315
+ end
316
+ end