bson 4.2.2 → 4.12.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/README.md +25 -7
- data/Rakefile +16 -9
- data/ext/bson/{native-endian.h → bson-endian.h} +5 -99
- data/ext/bson/bson-native.h +125 -0
- data/ext/bson/bytebuf.c +133 -0
- data/ext/bson/endian.c +117 -0
- data/ext/bson/init.c +355 -0
- data/ext/bson/libbson-utf8.c +230 -0
- data/ext/bson/read.c +411 -0
- data/ext/bson/util.c +95 -0
- data/ext/bson/write.c +680 -0
- data/lib/bson.rb +6 -3
- data/lib/bson/active_support.rb +17 -0
- data/lib/bson/array.rb +57 -17
- data/lib/bson/binary.rb +185 -13
- data/lib/bson/boolean.rb +12 -3
- data/lib/bson/code.rb +16 -2
- data/lib/bson/code_with_scope.rb +32 -5
- data/lib/bson/config.rb +1 -1
- data/lib/bson/date.rb +12 -2
- data/lib/bson/date_time.rb +2 -2
- data/lib/bson/db_pointer.rb +110 -0
- data/lib/bson/decimal128.rb +17 -3
- data/lib/bson/decimal128/builder.rb +1 -1
- data/lib/bson/document.rb +152 -5
- data/lib/bson/environment.rb +2 -1
- data/lib/bson/error.rb +27 -0
- data/lib/bson/ext_json.rb +383 -0
- data/lib/bson/false_class.rb +1 -1
- data/lib/bson/float.rb +48 -2
- data/lib/bson/hash.rb +68 -17
- data/lib/bson/int32.rb +52 -13
- data/lib/bson/int64.rb +59 -15
- data/lib/bson/integer.rb +36 -2
- data/lib/bson/json.rb +1 -1
- data/lib/bson/max_key.rb +13 -1
- data/lib/bson/min_key.rb +13 -1
- data/lib/bson/nil_class.rb +4 -2
- data/lib/bson/object.rb +28 -1
- data/lib/bson/object_id.rb +16 -2
- data/lib/bson/open_struct.rb +1 -1
- data/lib/bson/regexp.rb +27 -4
- data/lib/bson/registry.rb +3 -3
- data/lib/bson/specialized.rb +4 -2
- data/lib/bson/string.rb +5 -3
- data/lib/bson/symbol.rb +99 -7
- data/lib/bson/time.rb +63 -4
- data/lib/bson/time_with_zone.rb +54 -0
- data/lib/bson/timestamp.rb +44 -6
- data/lib/bson/true_class.rb +1 -1
- data/lib/bson/undefined.rb +12 -1
- data/lib/bson/version.rb +2 -2
- data/spec/bson/array_spec.rb +18 -1
- data/spec/bson/binary_spec.rb +100 -3
- data/spec/bson/binary_uuid_spec.rb +189 -0
- data/spec/bson/boolean_spec.rb +1 -1
- data/spec/bson/byte_buffer_read_spec.rb +197 -0
- data/spec/bson/byte_buffer_spec.rb +121 -381
- data/spec/bson/byte_buffer_write_spec.rb +854 -0
- data/spec/bson/code_spec.rb +1 -1
- data/spec/bson/code_with_scope_spec.rb +1 -1
- data/spec/bson/date_spec.rb +1 -1
- data/spec/bson/date_time_spec.rb +54 -1
- data/spec/bson/decimal128_spec.rb +35 -35
- data/spec/bson/document_as_spec.rb +46 -0
- data/spec/bson/document_spec.rb +197 -30
- data/spec/bson/ext_json_parse_spec.rb +308 -0
- data/spec/bson/false_class_spec.rb +1 -1
- data/spec/bson/float_spec.rb +37 -1
- data/spec/bson/hash_as_spec.rb +57 -0
- data/spec/bson/hash_spec.rb +209 -1
- data/spec/bson/int32_spec.rb +180 -6
- data/spec/bson/int64_spec.rb +199 -6
- data/spec/bson/integer_spec.rb +29 -3
- data/spec/bson/json_spec.rb +1 -1
- data/spec/bson/max_key_spec.rb +1 -1
- data/spec/bson/min_key_spec.rb +1 -1
- data/spec/bson/nil_class_spec.rb +1 -1
- data/spec/bson/object_id_spec.rb +1 -1
- data/spec/bson/object_spec.rb +1 -1
- data/spec/bson/open_struct_spec.rb +1 -1
- data/spec/bson/raw_spec.rb +34 -2
- data/spec/bson/regexp_spec.rb +1 -1
- data/spec/bson/registry_spec.rb +1 -1
- data/spec/bson/string_spec.rb +19 -1
- data/spec/bson/symbol_raw_spec.rb +45 -0
- data/spec/bson/symbol_spec.rb +63 -3
- data/spec/bson/time_spec.rb +205 -2
- data/spec/bson/time_with_zone_spec.rb +68 -0
- data/spec/bson/timestamp_spec.rb +56 -1
- data/spec/bson/true_class_spec.rb +1 -1
- data/spec/bson/undefined_spec.rb +1 -1
- data/spec/bson_spec.rb +1 -1
- data/spec/{support → runners}/common_driver.rb +1 -1
- data/spec/runners/corpus.rb +185 -0
- data/spec/{support/corpus.rb → runners/corpus_legacy.rb} +41 -59
- data/spec/spec_helper.rb +40 -3
- data/spec/{bson/driver_bson_spec.rb → spec_tests/common_driver_spec.rb} +1 -0
- data/spec/{bson/corpus_spec.rb → spec_tests/corpus_legacy_spec.rb} +10 -7
- data/spec/spec_tests/corpus_spec.rb +124 -0
- data/spec/spec_tests/data/corpus/README.md +15 -0
- data/spec/spec_tests/data/corpus/array.json +49 -0
- data/spec/spec_tests/data/corpus/binary.json +113 -0
- data/spec/spec_tests/data/corpus/boolean.json +27 -0
- data/spec/spec_tests/data/corpus/code.json +67 -0
- data/spec/spec_tests/data/corpus/code_w_scope.json +78 -0
- data/spec/spec_tests/data/corpus/datetime.json +42 -0
- data/spec/spec_tests/data/corpus/dbpointer.json +56 -0
- data/spec/spec_tests/data/corpus/dbref.json +31 -0
- data/spec/spec_tests/data/corpus/decimal128-1.json +317 -0
- data/spec/spec_tests/data/corpus/decimal128-2.json +793 -0
- data/spec/spec_tests/data/corpus/decimal128-3.json +1771 -0
- data/spec/spec_tests/data/corpus/decimal128-4.json +117 -0
- data/spec/spec_tests/data/corpus/decimal128-5.json +402 -0
- data/spec/spec_tests/data/corpus/decimal128-6.json +119 -0
- data/spec/spec_tests/data/corpus/decimal128-7.json +323 -0
- data/spec/spec_tests/data/corpus/document.json +36 -0
- data/spec/spec_tests/data/corpus/double.json +87 -0
- data/spec/spec_tests/data/corpus/int32.json +43 -0
- data/spec/spec_tests/data/corpus/int64.json +43 -0
- data/spec/spec_tests/data/corpus/maxkey.json +12 -0
- data/spec/spec_tests/data/corpus/minkey.json +12 -0
- data/spec/spec_tests/data/corpus/multi-type-deprecated.json +15 -0
- data/spec/spec_tests/data/corpus/multi-type.json +11 -0
- data/spec/spec_tests/data/corpus/null.json +12 -0
- data/spec/spec_tests/data/corpus/oid.json +28 -0
- data/spec/spec_tests/data/corpus/regex.json +65 -0
- data/spec/spec_tests/data/corpus/string.json +72 -0
- data/spec/spec_tests/data/corpus/symbol.json +80 -0
- data/spec/spec_tests/data/corpus/timestamp.json +34 -0
- data/spec/spec_tests/data/corpus/top.json +236 -0
- data/spec/spec_tests/data/corpus/undefined.json +15 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/array.json +8 -2
- data/spec/{support/corpus-tests/failures → spec_tests/data/corpus_legacy}/binary.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/boolean.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/code.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/code_w_scope.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/document.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/double.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/datetime.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/dbpointer.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/int64.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/symbol.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/int32.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/maxkey.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/minkey.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/null.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/oid.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/regex.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/string.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/timestamp.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/top.json +0 -0
- data/spec/{support/corpus-tests/failures → spec_tests/data/corpus_legacy}/undefined.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-1.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-2.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-3.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-4.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-5.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-6.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-7.json +0 -0
- data/spec/support/shared_examples.rb +3 -5
- data/spec/support/spec_config.rb +16 -0
- data/spec/support/utils.rb +10 -0
- metadata +227 -124
- metadata.gz.sig +0 -0
- data/ext/bson/bson_native.c +0 -762
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 18f69f1d0f65a2c4883500061a6637f6fb314e7758a15cda3941432a7e6fbd0a
|
4
|
+
data.tar.gz: b26789d1b2da697b6113897040ecd297d88367267fd28411609f23229ec8be2e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f90cd2d60cb0904d80ec6a3d75ba7d88af3872b5b1fdd27f9a283026f7c27ffbe84ad5331e571ebe120c7cd8c94d06190da8f1dc9fc783c349667f0ee963de1
|
7
|
+
data.tar.gz: da4c4edb81e99a804789c3914555376db3510a65437cc7b80829ae1f38f3c0f5a45894c8b4d8b24127367d0a9d1f46c9094a7b4f0f18633dbd4c4a8890675b02
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
data/README.md
CHANGED
@@ -1,4 +1,9 @@
|
|
1
|
-
BSON
|
1
|
+
BSON
|
2
|
+
[![Gem Version][rubygems-img]][rubygems-url]
|
3
|
+
[![Build Status][travis-img]][travis-url]
|
4
|
+
[![Build status Windows][appveyor-img]][appveyor-url]
|
5
|
+
[![Coverage Status][coveralls-img]][coveralls-url]
|
6
|
+
[![Inline docs][inch-img]][inch-url]
|
2
7
|
====
|
3
8
|
|
4
9
|
An implementation of the BSON specification in Ruby.
|
@@ -6,18 +11,19 @@ An implementation of the BSON specification in Ruby.
|
|
6
11
|
Compatibility
|
7
12
|
-------------
|
8
13
|
|
9
|
-
BSON is tested against MRI (
|
14
|
+
BSON is tested against MRI (2.3+) and JRuby (9.2+).
|
10
15
|
|
11
16
|
Documentation
|
12
17
|
-------------
|
13
18
|
|
14
|
-
Current documentation can be found
|
19
|
+
Current documentation can be found
|
20
|
+
[here](http://docs.mongodb.org/ecosystem/tutorial/ruby-bson-tutorial/#ruby-bson-tutorial).
|
15
21
|
|
16
22
|
API Documentation
|
17
23
|
-----------------
|
18
24
|
|
19
|
-
The [API Documentation](
|
20
|
-
located at
|
25
|
+
The [API Documentation](https://api.mongodb.com/bson-ruby/current/) is
|
26
|
+
located at api.mongodb.com.
|
21
27
|
|
22
28
|
BSON Specification
|
23
29
|
------------------
|
@@ -27,12 +33,13 @@ The [BSON specification](http://bsonspec.org) is at bsonspec.org.
|
|
27
33
|
Versioning
|
28
34
|
----------
|
29
35
|
|
30
|
-
As of 2.0.0, this project adheres to the
|
36
|
+
As of 2.0.0, this project adheres to the
|
37
|
+
[Semantic Versioning Specification](http://semver.org/).
|
31
38
|
|
32
39
|
License
|
33
40
|
-------
|
34
41
|
|
35
|
-
Copyright (C) 2009-
|
42
|
+
Copyright (C) 2009-2020 MongoDB Inc.
|
36
43
|
|
37
44
|
Licensed under the Apache License, Version 2.0 (the "License");
|
38
45
|
you may not use this file except in compliance with the License.
|
@@ -45,3 +52,14 @@ distributed under the License is distributed on an "AS IS" BASIS,
|
|
45
52
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
46
53
|
See the License for the specific language governing permissions and
|
47
54
|
limitations under the License.
|
55
|
+
|
56
|
+
[rubygems-img]: https://badge.fury.io/rb/bson.svg
|
57
|
+
[rubygems-url]: http://badge.fury.io/rb/bson
|
58
|
+
[travis-img]: https://secure.travis-ci.org/mongodb/bson-ruby.svg?branch=master
|
59
|
+
[travis-url]: http://travis-ci.org/mongodb/bson-ruby
|
60
|
+
[appveyor-img]: https://ci.appveyor.com/api/projects/status/p5aqko7umsx351nm?svg=true
|
61
|
+
[appveyor-url]: https://ci.appveyor.com/project/p-mongo/bson-ruby/branch/master
|
62
|
+
[coveralls-img]: https://coveralls.io/repos/mongodb/bson-ruby/badge.svg?branch=master
|
63
|
+
[coveralls-url]: https://coveralls.io/r/mongodb/bson-ruby?branch=master
|
64
|
+
[inch-img]: http://inch-ci.org/github/mongodb/bson-ruby.svg?branch=master
|
65
|
+
[inch-url]: http://inch-ci.org/github/mongodb/bson-ruby
|
data/Rakefile
CHANGED
@@ -20,6 +20,7 @@ $LOAD_PATH.unshift(File.expand_path("../lib", __FILE__))
|
|
20
20
|
require "rake"
|
21
21
|
require "rake/extensiontask"
|
22
22
|
require "rspec/core/rake_task"
|
23
|
+
require 'fileutils'
|
23
24
|
|
24
25
|
def jruby?
|
25
26
|
defined?(JRUBY_VERSION)
|
@@ -62,15 +63,9 @@ else
|
|
62
63
|
end
|
63
64
|
|
64
65
|
task :clean_all => :clean do
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
`rm bson_native.o`
|
69
|
-
`rm bson-ruby.jar`
|
70
|
-
end
|
71
|
-
rescue Exception => e
|
72
|
-
puts e.message
|
73
|
-
end
|
66
|
+
FileUtils.rm_f(File.join(File.dirname(__FILE__), 'lib', "bson_native.#{extension}"))
|
67
|
+
FileUtils.rm_f(File.join(File.dirname(__FILE__), 'lib', "bson_native.o"))
|
68
|
+
FileUtils.rm_f(File.join(File.dirname(__FILE__), 'lib', "bson-ruby.jar"))
|
74
69
|
end
|
75
70
|
|
76
71
|
task :spec => :compile do
|
@@ -126,3 +121,15 @@ namespace :benchmark do
|
|
126
121
|
end
|
127
122
|
|
128
123
|
task :default => [ :clean_all, :spec ]
|
124
|
+
|
125
|
+
desc "Generate all documentation"
|
126
|
+
task :docs => 'docs:yard'
|
127
|
+
|
128
|
+
namespace :docs do
|
129
|
+
desc "Generate yard documention"
|
130
|
+
task :yard do
|
131
|
+
out = File.join('yard-docs', BSON::VERSION)
|
132
|
+
FileUtils.rm_rf(out)
|
133
|
+
system "yardoc -o #{out} --title bson-#{BSON::VERSION}"
|
134
|
+
end
|
135
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
/*
|
2
|
-
* Copyright 2015-
|
2
|
+
* Copyright (C) 2015-2020 MongoDB Inc.
|
3
3
|
*
|
4
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
5
5
|
* you may not use this file except in compliance with the License.
|
@@ -106,101 +106,7 @@
|
|
106
106
|
# error "The endianness of target architecture is unknown."
|
107
107
|
#endif
|
108
108
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
*
|
114
|
-
* Fallback endianness conversion for 16-bit integers.
|
115
|
-
*
|
116
|
-
* Returns:
|
117
|
-
* The endian swapped version.
|
118
|
-
*
|
119
|
-
* Side effects:
|
120
|
-
* None.
|
121
|
-
*
|
122
|
-
*--------------------------------------------------------------------------
|
123
|
-
*/
|
124
|
-
static uint16_t __bson_uint16_swap_slow(uint16_t v)
|
125
|
-
{
|
126
|
-
return ((v & 0x00FF) << 8) |
|
127
|
-
((v & 0xFF00) >> 8);
|
128
|
-
}
|
129
|
-
|
130
|
-
/*
|
131
|
-
*--------------------------------------------------------------------------
|
132
|
-
*
|
133
|
-
* __bson_uint32_swap_slow --
|
134
|
-
*
|
135
|
-
* Fallback endianness conversion for 32-bit integers.
|
136
|
-
*
|
137
|
-
* Returns:
|
138
|
-
* The endian swapped version.
|
139
|
-
*
|
140
|
-
* Side effects:
|
141
|
-
* None.
|
142
|
-
*
|
143
|
-
*--------------------------------------------------------------------------
|
144
|
-
*/
|
145
|
-
static uint32_t __bson_uint32_swap_slow(uint32_t v)
|
146
|
-
{
|
147
|
-
return ((v & 0x000000FFU) << 24) |
|
148
|
-
((v & 0x0000FF00U) << 8) |
|
149
|
-
((v & 0x00FF0000U) >> 8) |
|
150
|
-
((v & 0xFF000000U) >> 24);
|
151
|
-
}
|
152
|
-
|
153
|
-
|
154
|
-
/*
|
155
|
-
*--------------------------------------------------------------------------
|
156
|
-
*
|
157
|
-
* __bson_uint64_swap_slow --
|
158
|
-
*
|
159
|
-
* Fallback endianness conversion for 64-bit integers.
|
160
|
-
*
|
161
|
-
* Returns:
|
162
|
-
* The endian swapped version.
|
163
|
-
*
|
164
|
-
* Side effects:
|
165
|
-
* None.
|
166
|
-
*
|
167
|
-
*--------------------------------------------------------------------------
|
168
|
-
*/
|
169
|
-
static uint64_t __bson_uint64_swap_slow(uint64_t v)
|
170
|
-
{
|
171
|
-
return ((v & 0x00000000000000FFULL) << 56) |
|
172
|
-
((v & 0x000000000000FF00ULL) << 40) |
|
173
|
-
((v & 0x0000000000FF0000ULL) << 24) |
|
174
|
-
((v & 0x00000000FF000000ULL) << 8) |
|
175
|
-
((v & 0x000000FF00000000ULL) >> 8) |
|
176
|
-
((v & 0x0000FF0000000000ULL) >> 24) |
|
177
|
-
((v & 0x00FF000000000000ULL) >> 40) |
|
178
|
-
((v & 0xFF00000000000000ULL) >> 56);
|
179
|
-
}
|
180
|
-
|
181
|
-
/*
|
182
|
-
*--------------------------------------------------------------------------
|
183
|
-
*
|
184
|
-
* __bson_double_swap_slow --
|
185
|
-
*
|
186
|
-
* Fallback endianness conversion for double floating point.
|
187
|
-
*
|
188
|
-
* Returns:
|
189
|
-
* The endian swapped version.
|
190
|
-
*
|
191
|
-
* Side effects:
|
192
|
-
* None.
|
193
|
-
*
|
194
|
-
*--------------------------------------------------------------------------
|
195
|
-
*/
|
196
|
-
static double __bson_double_swap_slow(double v)
|
197
|
-
{
|
198
|
-
uint64_t uv;
|
199
|
-
|
200
|
-
memcpy(&uv, &v, sizeof(v));
|
201
|
-
uv = BSON_UINT64_SWAP_LE_BE(uv);
|
202
|
-
memcpy(&v, &uv, sizeof(v));
|
203
|
-
|
204
|
-
return v;
|
205
|
-
}
|
206
|
-
|
109
|
+
uint16_t __bson_uint16_swap_slow(uint16_t v);
|
110
|
+
uint32_t __bson_uint32_swap_slow(uint32_t v);
|
111
|
+
uint64_t __bson_uint64_swap_slow(uint64_t v);
|
112
|
+
double __bson_double_swap_slow(double v);
|
@@ -0,0 +1,125 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (C) 2009-2020 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
|
+
|
17
|
+
#include <ruby.h>
|
18
|
+
#include <stdbool.h>
|
19
|
+
#include <stdint.h>
|
20
|
+
#include <unistd.h>
|
21
|
+
#include <time.h>
|
22
|
+
#include "bson-endian.h"
|
23
|
+
|
24
|
+
void
|
25
|
+
rb_bson_utf8_validate (const char *utf8, /* IN */
|
26
|
+
size_t utf8_len, /* IN */
|
27
|
+
bool allow_null, /* IN */
|
28
|
+
const char *data_type); /* IN */
|
29
|
+
|
30
|
+
#define BSON_BYTE_BUFFER_SIZE 1024
|
31
|
+
|
32
|
+
#ifndef HOST_NAME_HASH_MAX
|
33
|
+
#define HOST_NAME_HASH_MAX 256
|
34
|
+
#endif
|
35
|
+
|
36
|
+
/* See the type list in http://bsonspec.org/spec.html. */
|
37
|
+
#define BSON_TYPE_DOUBLE 1
|
38
|
+
#define BSON_TYPE_STRING 2
|
39
|
+
#define BSON_TYPE_DOCUMENT 3
|
40
|
+
#define BSON_TYPE_ARRAY 4
|
41
|
+
#define BSON_TYPE_BOOLEAN 8
|
42
|
+
#define BSON_TYPE_SYMBOL 0x0E
|
43
|
+
#define BSON_TYPE_INT32 0x10
|
44
|
+
#define BSON_TYPE_INT64 0x12
|
45
|
+
|
46
|
+
typedef struct {
|
47
|
+
size_t size;
|
48
|
+
size_t write_position;
|
49
|
+
size_t read_position;
|
50
|
+
char buffer[BSON_BYTE_BUFFER_SIZE];
|
51
|
+
char *b_ptr;
|
52
|
+
} byte_buffer_t;
|
53
|
+
|
54
|
+
#define READ_PTR(byte_buffer_ptr) \
|
55
|
+
(byte_buffer_ptr->b_ptr + byte_buffer_ptr->read_position)
|
56
|
+
|
57
|
+
#define READ_SIZE(byte_buffer_ptr) \
|
58
|
+
(byte_buffer_ptr->write_position - byte_buffer_ptr->read_position)
|
59
|
+
|
60
|
+
#define WRITE_PTR(byte_buffer_ptr) \
|
61
|
+
(byte_buffer_ptr->b_ptr + byte_buffer_ptr->write_position)
|
62
|
+
|
63
|
+
#define ENSURE_BSON_WRITE(buffer_ptr, length) \
|
64
|
+
{ if (buffer_ptr->write_position + length > buffer_ptr->size) rb_bson_expand_buffer(buffer_ptr, length); }
|
65
|
+
|
66
|
+
#define ENSURE_BSON_READ(buffer_ptr, length) \
|
67
|
+
{ if (buffer_ptr->read_position + length > buffer_ptr->write_position) \
|
68
|
+
rb_raise(rb_eRangeError, "Attempted to read %zu bytes, but only %zu bytes remain", (size_t)length, READ_SIZE(buffer_ptr)); }
|
69
|
+
|
70
|
+
VALUE rb_bson_byte_buffer_allocate(VALUE klass);
|
71
|
+
VALUE rb_bson_byte_buffer_initialize(int argc, VALUE *argv, VALUE self);
|
72
|
+
VALUE rb_bson_byte_buffer_length(VALUE self);
|
73
|
+
VALUE rb_bson_byte_buffer_get_byte(VALUE self);
|
74
|
+
VALUE rb_bson_byte_buffer_get_bytes(VALUE self, VALUE i);
|
75
|
+
VALUE rb_bson_byte_buffer_get_cstring(VALUE self);
|
76
|
+
VALUE rb_bson_byte_buffer_get_decimal128_bytes(VALUE self);
|
77
|
+
VALUE rb_bson_byte_buffer_get_double(VALUE self);
|
78
|
+
VALUE rb_bson_byte_buffer_get_int32(VALUE self);
|
79
|
+
VALUE rb_bson_byte_buffer_get_uint32(VALUE self);
|
80
|
+
VALUE rb_bson_byte_buffer_get_int64(VALUE self);
|
81
|
+
VALUE rb_bson_byte_buffer_get_string(VALUE self);
|
82
|
+
VALUE rb_bson_byte_buffer_get_hash(int argc, VALUE *argv, VALUE self);
|
83
|
+
VALUE rb_bson_byte_buffer_get_array(int argc, VALUE *argv, VALUE self);
|
84
|
+
VALUE rb_bson_byte_buffer_put_byte(VALUE self, VALUE byte);
|
85
|
+
VALUE rb_bson_byte_buffer_put_bytes(VALUE self, VALUE bytes);
|
86
|
+
VALUE rb_bson_byte_buffer_put_cstring(VALUE self, VALUE string);
|
87
|
+
VALUE rb_bson_byte_buffer_put_decimal128(VALUE self, VALUE low, VALUE high);
|
88
|
+
VALUE rb_bson_byte_buffer_put_double(VALUE self, VALUE f);
|
89
|
+
VALUE rb_bson_byte_buffer_put_int32(VALUE self, VALUE i);
|
90
|
+
VALUE rb_bson_byte_buffer_put_uint32(VALUE self, VALUE i);
|
91
|
+
VALUE rb_bson_byte_buffer_put_int64(VALUE self, VALUE i);
|
92
|
+
VALUE rb_bson_byte_buffer_put_string(VALUE self, VALUE string);
|
93
|
+
VALUE rb_bson_byte_buffer_put_symbol(VALUE self, VALUE symbol);
|
94
|
+
VALUE rb_bson_byte_buffer_put_hash(VALUE self, VALUE hash, VALUE validating_keys);
|
95
|
+
VALUE rb_bson_byte_buffer_put_array(VALUE self, VALUE array, VALUE validating_keys);
|
96
|
+
VALUE rb_bson_byte_buffer_read_position(VALUE self);
|
97
|
+
VALUE rb_bson_byte_buffer_replace_int32(VALUE self, VALUE index, VALUE i);
|
98
|
+
VALUE rb_bson_byte_buffer_rewind(VALUE self);
|
99
|
+
VALUE rb_bson_byte_buffer_write_position(VALUE self);
|
100
|
+
VALUE rb_bson_byte_buffer_to_s(VALUE self);
|
101
|
+
VALUE rb_bson_object_id_generator_next(int argc, VALUE* args, VALUE self);
|
102
|
+
|
103
|
+
size_t rb_bson_byte_buffer_memsize(const void *ptr);
|
104
|
+
void rb_bson_byte_buffer_free(void *ptr);
|
105
|
+
void rb_bson_expand_buffer(byte_buffer_t* buffer_ptr, size_t length);
|
106
|
+
void rb_bson_generate_machine_id(VALUE rb_md5_class, char *rb_bson_machine_id);
|
107
|
+
|
108
|
+
VALUE pvt_const_get_2(const char *c1, const char *c2);
|
109
|
+
VALUE pvt_const_get_3(const char *c1, const char *c2, const char *c3);
|
110
|
+
|
111
|
+
#define BSON_MODE_DEFAULT 0
|
112
|
+
#define BSON_MODE_BSON 1
|
113
|
+
|
114
|
+
int pvt_get_mode_option(int argc, VALUE *argv);
|
115
|
+
|
116
|
+
/**
|
117
|
+
* The counter for incrementing object ids.
|
118
|
+
*/
|
119
|
+
extern uint32_t rb_bson_object_id_counter;
|
120
|
+
|
121
|
+
extern VALUE rb_bson_registry;
|
122
|
+
|
123
|
+
extern VALUE rb_bson_illegal_key;
|
124
|
+
|
125
|
+
extern const rb_data_type_t rb_byte_buffer_data_type;
|
data/ext/bson/bytebuf.c
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (C) 2009-2020 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
|
+
|
17
|
+
#include "bson-native.h"
|
18
|
+
|
19
|
+
/**
|
20
|
+
* Allocates a bson byte buffer that wraps a byte_buffer_t.
|
21
|
+
*/
|
22
|
+
VALUE rb_bson_byte_buffer_allocate(VALUE klass)
|
23
|
+
{
|
24
|
+
byte_buffer_t *b;
|
25
|
+
VALUE obj = TypedData_Make_Struct(klass, byte_buffer_t, &rb_byte_buffer_data_type, b);
|
26
|
+
b->b_ptr = b->buffer;
|
27
|
+
b->size = BSON_BYTE_BUFFER_SIZE;
|
28
|
+
return obj;
|
29
|
+
}
|
30
|
+
|
31
|
+
/**
|
32
|
+
* Initialize a byte buffer.
|
33
|
+
*/
|
34
|
+
VALUE rb_bson_byte_buffer_initialize(int argc, VALUE *argv, VALUE self)
|
35
|
+
{
|
36
|
+
VALUE bytes;
|
37
|
+
rb_scan_args(argc, argv, "01", &bytes);
|
38
|
+
|
39
|
+
if (!NIL_P(bytes)) {
|
40
|
+
rb_bson_byte_buffer_put_bytes(self, bytes);
|
41
|
+
}
|
42
|
+
|
43
|
+
return self;
|
44
|
+
}
|
45
|
+
|
46
|
+
/**
|
47
|
+
* Expand the byte buffer linearly.
|
48
|
+
*/
|
49
|
+
void rb_bson_expand_buffer(byte_buffer_t* buffer_ptr, size_t length)
|
50
|
+
{
|
51
|
+
const size_t required_size = buffer_ptr->write_position - buffer_ptr->read_position + length;
|
52
|
+
if (required_size <= buffer_ptr->size) {
|
53
|
+
memmove(buffer_ptr->b_ptr, READ_PTR(buffer_ptr), READ_SIZE(buffer_ptr));
|
54
|
+
buffer_ptr->write_position -= buffer_ptr->read_position;
|
55
|
+
buffer_ptr->read_position = 0;
|
56
|
+
} else {
|
57
|
+
char *new_b_ptr;
|
58
|
+
const size_t new_size = required_size * 2;
|
59
|
+
new_b_ptr = ALLOC_N(char, new_size);
|
60
|
+
memcpy(new_b_ptr, READ_PTR(buffer_ptr), READ_SIZE(buffer_ptr));
|
61
|
+
if (buffer_ptr->b_ptr != buffer_ptr->buffer) {
|
62
|
+
xfree(buffer_ptr->b_ptr);
|
63
|
+
}
|
64
|
+
buffer_ptr->b_ptr = new_b_ptr;
|
65
|
+
buffer_ptr->size = new_size;
|
66
|
+
buffer_ptr->write_position -= buffer_ptr->read_position;
|
67
|
+
buffer_ptr->read_position = 0;
|
68
|
+
}
|
69
|
+
}
|
70
|
+
|
71
|
+
/**
|
72
|
+
* Free the memory for the byte buffer.
|
73
|
+
*/
|
74
|
+
void rb_bson_byte_buffer_free(void *ptr)
|
75
|
+
{
|
76
|
+
byte_buffer_t *b = ptr;
|
77
|
+
if (b->b_ptr != b->buffer) {
|
78
|
+
xfree(b->b_ptr);
|
79
|
+
}
|
80
|
+
xfree(b);
|
81
|
+
}
|
82
|
+
|
83
|
+
/**
|
84
|
+
* Get the length of the buffer.
|
85
|
+
*/
|
86
|
+
VALUE rb_bson_byte_buffer_length(VALUE self)
|
87
|
+
{
|
88
|
+
byte_buffer_t *b;
|
89
|
+
TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
|
90
|
+
return UINT2NUM(READ_SIZE(b));
|
91
|
+
}
|
92
|
+
|
93
|
+
/* The docstring is in init.c. */
|
94
|
+
VALUE rb_bson_byte_buffer_read_position(VALUE self)
|
95
|
+
{
|
96
|
+
byte_buffer_t *b;
|
97
|
+
TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
|
98
|
+
return INT2NUM(b->read_position);
|
99
|
+
}
|
100
|
+
|
101
|
+
/* The docstring is in init.c. */
|
102
|
+
VALUE rb_bson_byte_buffer_rewind(VALUE self)
|
103
|
+
{
|
104
|
+
byte_buffer_t *b;
|
105
|
+
TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
|
106
|
+
b->read_position = 0;
|
107
|
+
|
108
|
+
return self;
|
109
|
+
}
|
110
|
+
|
111
|
+
/* The docstring is in init.c. */
|
112
|
+
VALUE rb_bson_byte_buffer_write_position(VALUE self)
|
113
|
+
{
|
114
|
+
byte_buffer_t *b;
|
115
|
+
TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
|
116
|
+
return INT2NUM(b->write_position);
|
117
|
+
}
|
118
|
+
|
119
|
+
/* The docstring is in init.c. */
|
120
|
+
VALUE rb_bson_byte_buffer_to_s(VALUE self)
|
121
|
+
{
|
122
|
+
byte_buffer_t *b;
|
123
|
+
TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
|
124
|
+
return rb_str_new(READ_PTR(b), READ_SIZE(b));
|
125
|
+
}
|
126
|
+
|
127
|
+
/**
|
128
|
+
* Get the size of the byte_buffer_t in memory.
|
129
|
+
*/
|
130
|
+
size_t rb_bson_byte_buffer_memsize(const void *ptr)
|
131
|
+
{
|
132
|
+
return ptr ? sizeof(byte_buffer_t) : 0;
|
133
|
+
}
|