bson 4.1.1 → 5.2.0
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.
- checksums.yaml +5 -5
- data/README.md +106 -7
- data/Rakefile +86 -43
- data/ext/bson/{native-endian.h → bson-endian.h} +14 -107
- data/ext/bson/bson-native.h +135 -0
- data/ext/bson/bytebuf.c +133 -0
- data/ext/bson/endian.c +117 -0
- data/ext/bson/extconf.rb +8 -3
- data/ext/bson/init.c +364 -0
- data/ext/bson/libbson-utf8.c +230 -0
- data/ext/bson/read.c +470 -0
- data/ext/bson/util.c +250 -0
- data/ext/bson/write.c +675 -0
- data/lib/bson/active_support.rb +19 -0
- data/lib/bson/array.rb +97 -30
- data/lib/bson/big_decimal.rb +77 -0
- data/lib/bson/binary.rb +510 -70
- data/lib/bson/boolean.rb +15 -4
- data/lib/bson/code.rb +25 -12
- data/lib/bson/code_with_scope.rb +41 -15
- data/lib/bson/config.rb +3 -28
- data/lib/bson/date.rb +16 -4
- data/lib/bson/date_time.rb +6 -4
- data/lib/bson/db_pointer.rb +110 -0
- data/lib/bson/dbref.rb +154 -0
- data/lib/bson/decimal128/builder.rb +456 -0
- data/lib/bson/decimal128.rb +272 -0
- data/lib/bson/document.rb +177 -7
- data/lib/bson/environment.rb +17 -2
- data/lib/bson/error/bson_decode_error.rb +11 -0
- data/lib/bson/error/ext_json_parse_error.rb +11 -0
- data/lib/bson/error/illegal_key.rb +23 -0
- data/lib/bson/error/invalid_binary_type.rb +37 -0
- data/lib/bson/error/invalid_dbref_argument.rb +12 -0
- data/lib/bson/error/invalid_decimal128_argument.rb +25 -0
- data/lib/bson/error/invalid_decimal128_range.rb +27 -0
- data/lib/bson/error/invalid_decimal128_string.rb +26 -0
- data/lib/bson/error/invalid_key.rb +24 -0
- data/lib/bson/error/invalid_object_id.rb +11 -0
- data/lib/bson/error/invalid_regexp_pattern.rb +13 -0
- data/lib/bson/error/unrepresentable_precision.rb +19 -0
- data/lib/bson/error/unserializable_class.rb +13 -0
- data/lib/bson/error/unsupported_binary_subtype.rb +12 -0
- data/lib/bson/error/unsupported_type.rb +11 -0
- data/lib/bson/error.rb +22 -0
- data/lib/bson/ext_json.rb +389 -0
- data/lib/bson/false_class.rb +6 -4
- data/lib/bson/float.rb +43 -7
- data/lib/bson/hash.rb +152 -37
- data/lib/bson/int32.rb +104 -6
- data/lib/bson/int64.rb +111 -8
- data/lib/bson/integer.rb +43 -9
- data/lib/bson/json.rb +3 -1
- data/lib/bson/max_key.rb +21 -10
- data/lib/bson/min_key.rb +21 -10
- data/lib/bson/nil_class.rb +7 -3
- data/lib/bson/object.rb +25 -17
- data/lib/bson/object_id.rb +98 -113
- data/lib/bson/open_struct.rb +59 -0
- data/lib/bson/regexp.rb +129 -56
- data/lib/bson/registry.rb +7 -10
- data/lib/bson/specialized.rb +8 -4
- data/lib/bson/string.rb +12 -32
- data/lib/bson/symbol.rb +107 -11
- data/lib/bson/time.rb +68 -7
- data/lib/bson/time_with_zone.rb +67 -0
- data/lib/bson/timestamp.rb +50 -10
- data/lib/bson/true_class.rb +6 -4
- data/lib/bson/undefined.rb +28 -2
- data/lib/bson/vector.rb +44 -0
- data/lib/bson/version.rb +6 -14
- data/lib/bson.rb +22 -12
- data/spec/README.md +14 -0
- data/spec/bson/array_spec.rb +38 -62
- data/spec/bson/big_decimal_spec.rb +328 -0
- data/spec/bson/binary_spec.rb +199 -53
- data/spec/bson/binary_uuid_spec.rb +190 -0
- data/spec/bson/boolean_spec.rb +2 -1
- data/spec/bson/byte_buffer_read_spec.rb +198 -0
- data/spec/bson/byte_buffer_spec.rb +122 -381
- data/spec/bson/byte_buffer_write_spec.rb +855 -0
- data/spec/bson/code_spec.rb +6 -4
- data/spec/bson/code_with_scope_spec.rb +6 -4
- data/spec/bson/config_spec.rb +1 -35
- data/spec/bson/date_spec.rb +2 -1
- data/spec/bson/date_time_spec.rb +55 -1
- data/spec/bson/dbref_legacy_spec.rb +186 -0
- data/spec/bson/dbref_spec.rb +487 -0
- data/spec/bson/decimal128_spec.rb +1840 -0
- data/spec/bson/document_as_spec.rb +61 -0
- data/spec/bson/document_spec.rb +205 -32
- data/spec/bson/ext_json_parse_spec.rb +346 -0
- data/spec/bson/false_class_spec.rb +9 -1
- data/spec/bson/float_spec.rb +42 -1
- data/spec/bson/hash_as_spec.rb +58 -0
- data/spec/bson/hash_spec.rb +318 -66
- data/spec/bson/int32_spec.rb +248 -1
- data/spec/bson/int64_spec.rb +308 -1
- data/spec/bson/integer_spec.rb +61 -3
- data/spec/bson/json_spec.rb +2 -1
- data/spec/bson/max_key_spec.rb +6 -4
- data/spec/bson/min_key_spec.rb +6 -4
- data/spec/bson/nil_class_spec.rb +2 -1
- data/spec/bson/object_id_spec.rb +95 -5
- data/spec/bson/object_spec.rb +3 -2
- data/spec/bson/open_struct_spec.rb +87 -0
- data/spec/bson/raw_spec.rb +594 -0
- data/spec/bson/regexp_spec.rb +61 -8
- data/spec/bson/registry_spec.rb +3 -2
- data/spec/bson/string_spec.rb +26 -33
- data/spec/bson/symbol_raw_spec.rb +70 -0
- data/spec/bson/symbol_spec.rb +77 -20
- data/spec/bson/time_spec.rb +206 -2
- data/spec/bson/time_with_zone_spec.rb +69 -0
- data/spec/bson/timestamp_spec.rb +58 -2
- data/spec/bson/true_class_spec.rb +9 -1
- data/spec/bson/undefined_spec.rb +28 -1
- data/spec/bson/vector_spec.rb +33 -0
- data/spec/bson_spec.rb +2 -1
- data/spec/runners/binary_vector.rb +78 -0
- data/spec/runners/common_driver.rb +348 -0
- data/spec/runners/corpus.rb +191 -0
- data/spec/runners/corpus_legacy.rb +248 -0
- data/spec/shared/LICENSE +20 -0
- data/spec/shared/bin/get-mongodb-download-url +17 -0
- data/spec/shared/bin/s3-copy +45 -0
- data/spec/shared/bin/s3-upload +69 -0
- data/spec/shared/lib/mrss/child_process_helper.rb +80 -0
- data/spec/shared/lib/mrss/cluster_config.rb +231 -0
- data/spec/shared/lib/mrss/constraints.rb +378 -0
- data/spec/shared/lib/mrss/docker_runner.rb +298 -0
- data/spec/shared/lib/mrss/eg_config_utils.rb +51 -0
- data/spec/shared/lib/mrss/event_subscriber.rb +210 -0
- data/spec/shared/lib/mrss/lite_constraints.rb +238 -0
- data/spec/shared/lib/mrss/release/candidate.rb +281 -0
- data/spec/shared/lib/mrss/release/product_data.rb +144 -0
- data/spec/shared/lib/mrss/server_version_registry.rb +113 -0
- data/spec/shared/lib/mrss/session_registry.rb +69 -0
- data/spec/shared/lib/mrss/session_registry_legacy.rb +60 -0
- data/spec/shared/lib/mrss/spec_organizer.rb +179 -0
- data/spec/shared/lib/mrss/utils.rb +37 -0
- data/spec/shared/lib/tasks/candidate.rake +64 -0
- data/spec/shared/share/Dockerfile.erb +251 -0
- data/spec/shared/share/haproxy-1.conf +16 -0
- data/spec/shared/share/haproxy-2.conf +17 -0
- data/spec/shared/shlib/config.sh +27 -0
- data/spec/shared/shlib/distro.sh +84 -0
- data/spec/shared/shlib/server.sh +423 -0
- data/spec/shared/shlib/set_env.sh +110 -0
- data/spec/spec_helper.rb +61 -1
- data/spec/spec_tests/binary_vector_spec.rb +82 -0
- data/spec/spec_tests/common_driver_spec.rb +84 -0
- data/spec/spec_tests/corpus_legacy_spec.rb +72 -0
- data/spec/spec_tests/corpus_spec.rb +134 -0
- data/spec/spec_tests/data/binary_vector/README.md +61 -0
- data/spec/spec_tests/data/binary_vector/float32.json +65 -0
- data/spec/spec_tests/data/binary_vector/int8.json +57 -0
- data/spec/spec_tests/data/binary_vector/packed_bit.json +83 -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 +153 -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 +51 -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 +165 -0
- data/spec/spec_tests/data/corpus/decimal128-5.json +402 -0
- data/spec/spec_tests/data/corpus/decimal128-6.json +131 -0
- data/spec/spec_tests/data/corpus/decimal128-7.json +327 -0
- data/spec/spec_tests/data/corpus/document.json +60 -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 +262 -0
- data/spec/spec_tests/data/corpus/undefined.json +15 -0
- data/spec/spec_tests/data/corpus_legacy/array.json +49 -0
- data/spec/spec_tests/data/corpus_legacy/binary.json +69 -0
- data/spec/spec_tests/data/corpus_legacy/boolean.json +27 -0
- data/spec/spec_tests/data/corpus_legacy/code.json +67 -0
- data/spec/spec_tests/data/corpus_legacy/code_w_scope.json +78 -0
- data/spec/spec_tests/data/corpus_legacy/document.json +36 -0
- data/spec/spec_tests/data/corpus_legacy/double.json +69 -0
- data/spec/spec_tests/data/corpus_legacy/failures/datetime.json +31 -0
- data/spec/spec_tests/data/corpus_legacy/failures/dbpointer.json +42 -0
- data/spec/spec_tests/data/corpus_legacy/failures/int64.json +38 -0
- data/spec/spec_tests/data/corpus_legacy/failures/symbol.json +62 -0
- data/spec/spec_tests/data/corpus_legacy/int32.json +38 -0
- data/spec/spec_tests/data/corpus_legacy/maxkey.json +12 -0
- data/spec/spec_tests/data/corpus_legacy/minkey.json +12 -0
- data/spec/spec_tests/data/corpus_legacy/null.json +12 -0
- data/spec/spec_tests/data/corpus_legacy/oid.json +28 -0
- data/spec/spec_tests/data/corpus_legacy/regex.json +37 -0
- data/spec/spec_tests/data/corpus_legacy/string.json +67 -0
- data/spec/spec_tests/data/corpus_legacy/timestamp.json +18 -0
- data/spec/spec_tests/data/corpus_legacy/top.json +62 -0
- data/spec/spec_tests/data/corpus_legacy/undefined.json +13 -0
- data/spec/spec_tests/data/decimal128/decimal128-1.json +363 -0
- data/spec/spec_tests/data/decimal128/decimal128-2.json +793 -0
- data/spec/spec_tests/data/decimal128/decimal128-3.json +1771 -0
- data/spec/spec_tests/data/decimal128/decimal128-4.json +165 -0
- data/spec/spec_tests/data/decimal128/decimal128-5.json +402 -0
- data/spec/spec_tests/data/decimal128/decimal128-6.json +131 -0
- data/spec/spec_tests/data/decimal128/decimal128-7.json +327 -0
- data/spec/support/shared_examples.rb +32 -11
- data/spec/support/spec_config.rb +17 -0
- data/spec/support/utils.rb +58 -0
- metadata +284 -45
- checksums.yaml.gz.sig +0 -3
- data/ext/bson/native.c +0 -722
- data.tar.gz.sig +0 -0
- metadata.gz.sig +0 -0
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
|
+
}
|
data/ext/bson/endian.c
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2015-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 <string.h>
|
|
18
|
+
#include <stdint.h>
|
|
19
|
+
#include "bson-endian.h"
|
|
20
|
+
|
|
21
|
+
/*
|
|
22
|
+
*--------------------------------------------------------------------------
|
|
23
|
+
*
|
|
24
|
+
* __bson_uint16_swap_slow --
|
|
25
|
+
*
|
|
26
|
+
* Fallback endianness conversion for 16-bit integers.
|
|
27
|
+
*
|
|
28
|
+
* Returns:
|
|
29
|
+
* The endian swapped version.
|
|
30
|
+
*
|
|
31
|
+
* Side effects:
|
|
32
|
+
* None.
|
|
33
|
+
*
|
|
34
|
+
*--------------------------------------------------------------------------
|
|
35
|
+
*/
|
|
36
|
+
uint16_t __bson_uint16_swap_slow(uint16_t v)
|
|
37
|
+
{
|
|
38
|
+
return ((v & 0x00FF) << 8) |
|
|
39
|
+
((v & 0xFF00) >> 8);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/*
|
|
43
|
+
*--------------------------------------------------------------------------
|
|
44
|
+
*
|
|
45
|
+
* __bson_uint32_swap_slow --
|
|
46
|
+
*
|
|
47
|
+
* Fallback endianness conversion for 32-bit integers.
|
|
48
|
+
*
|
|
49
|
+
* Returns:
|
|
50
|
+
* The endian swapped version.
|
|
51
|
+
*
|
|
52
|
+
* Side effects:
|
|
53
|
+
* None.
|
|
54
|
+
*
|
|
55
|
+
*--------------------------------------------------------------------------
|
|
56
|
+
*/
|
|
57
|
+
uint32_t __bson_uint32_swap_slow(uint32_t v)
|
|
58
|
+
{
|
|
59
|
+
return ((v & 0x000000FFU) << 24) |
|
|
60
|
+
((v & 0x0000FF00U) << 8) |
|
|
61
|
+
((v & 0x00FF0000U) >> 8) |
|
|
62
|
+
((v & 0xFF000000U) >> 24);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
/*
|
|
67
|
+
*--------------------------------------------------------------------------
|
|
68
|
+
*
|
|
69
|
+
* __bson_uint64_swap_slow --
|
|
70
|
+
*
|
|
71
|
+
* Fallback endianness conversion for 64-bit integers.
|
|
72
|
+
*
|
|
73
|
+
* Returns:
|
|
74
|
+
* The endian swapped version.
|
|
75
|
+
*
|
|
76
|
+
* Side effects:
|
|
77
|
+
* None.
|
|
78
|
+
*
|
|
79
|
+
*--------------------------------------------------------------------------
|
|
80
|
+
*/
|
|
81
|
+
uint64_t __bson_uint64_swap_slow(uint64_t v)
|
|
82
|
+
{
|
|
83
|
+
return ((v & 0x00000000000000FFULL) << 56) |
|
|
84
|
+
((v & 0x000000000000FF00ULL) << 40) |
|
|
85
|
+
((v & 0x0000000000FF0000ULL) << 24) |
|
|
86
|
+
((v & 0x00000000FF000000ULL) << 8) |
|
|
87
|
+
((v & 0x000000FF00000000ULL) >> 8) |
|
|
88
|
+
((v & 0x0000FF0000000000ULL) >> 24) |
|
|
89
|
+
((v & 0x00FF000000000000ULL) >> 40) |
|
|
90
|
+
((v & 0xFF00000000000000ULL) >> 56);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/*
|
|
94
|
+
*--------------------------------------------------------------------------
|
|
95
|
+
*
|
|
96
|
+
* __bson_double_swap_slow --
|
|
97
|
+
*
|
|
98
|
+
* Fallback endianness conversion for double floating point.
|
|
99
|
+
*
|
|
100
|
+
* Returns:
|
|
101
|
+
* The endian swapped version.
|
|
102
|
+
*
|
|
103
|
+
* Side effects:
|
|
104
|
+
* None.
|
|
105
|
+
*
|
|
106
|
+
*--------------------------------------------------------------------------
|
|
107
|
+
*/
|
|
108
|
+
double __bson_double_swap_slow(double v)
|
|
109
|
+
{
|
|
110
|
+
uint64_t uv;
|
|
111
|
+
|
|
112
|
+
memcpy(&uv, &v, sizeof(v));
|
|
113
|
+
uv = BSON_UINT64_SWAP_LE_BE(uv);
|
|
114
|
+
memcpy(&v, &uv, sizeof(v));
|
|
115
|
+
|
|
116
|
+
return v;
|
|
117
|
+
}
|
data/ext/bson/extconf.rb
CHANGED
data/ext/bson/init.c
ADDED
|
@@ -0,0 +1,364 @@
|
|
|
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
|
+
#include "bson-native.h"
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The counter for incrementing object ids.
|
|
20
|
+
*/
|
|
21
|
+
uint32_t rb_bson_object_id_counter;
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
VALUE rb_bson_registry;
|
|
25
|
+
|
|
26
|
+
const rb_data_type_t rb_byte_buffer_data_type = {
|
|
27
|
+
"bson/byte_buffer",
|
|
28
|
+
{ NULL, rb_bson_byte_buffer_free, rb_bson_byte_buffer_memsize }
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
VALUE _ref_str, _id_str, _db_str;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Initialize the bson_native extension.
|
|
35
|
+
*/
|
|
36
|
+
void Init_bson_native()
|
|
37
|
+
{
|
|
38
|
+
char rb_bson_machine_id[256];
|
|
39
|
+
|
|
40
|
+
_ref_str = rb_str_new_cstr("$ref");
|
|
41
|
+
rb_gc_register_mark_object(_ref_str);
|
|
42
|
+
_id_str = rb_str_new_cstr("$id");
|
|
43
|
+
rb_gc_register_mark_object(_id_str);
|
|
44
|
+
_db_str = rb_str_new_cstr("$db");
|
|
45
|
+
rb_gc_register_mark_object(_db_str);
|
|
46
|
+
|
|
47
|
+
rb_require("digest/md5");
|
|
48
|
+
|
|
49
|
+
VALUE rb_bson_module = rb_define_module("BSON");
|
|
50
|
+
|
|
51
|
+
/* Document-class: BSON::ByteBuffer
|
|
52
|
+
*
|
|
53
|
+
* Stores BSON-serialized data and provides efficient serialization and
|
|
54
|
+
* deserialization of common Ruby classes using native code.
|
|
55
|
+
*/
|
|
56
|
+
VALUE rb_byte_buffer_class = rb_define_class_under(rb_bson_module, "ByteBuffer", rb_cObject);
|
|
57
|
+
|
|
58
|
+
VALUE rb_bson_object_id_class = rb_const_get(rb_bson_module, rb_intern("ObjectId"));
|
|
59
|
+
VALUE rb_bson_object_id_generator_class = rb_const_get(rb_bson_object_id_class, rb_intern("Generator"));
|
|
60
|
+
VALUE rb_digest_class = rb_const_get(rb_cObject, rb_intern("Digest"));
|
|
61
|
+
VALUE rb_md5_class = rb_const_get(rb_digest_class, rb_intern("MD5"));
|
|
62
|
+
|
|
63
|
+
rb_define_alloc_func(rb_byte_buffer_class, rb_bson_byte_buffer_allocate);
|
|
64
|
+
rb_define_method(rb_byte_buffer_class, "initialize", rb_bson_byte_buffer_initialize, -1);
|
|
65
|
+
|
|
66
|
+
/*
|
|
67
|
+
* call-seq:
|
|
68
|
+
* buffer.length -> Fixnum
|
|
69
|
+
*
|
|
70
|
+
* Returns the number of bytes available to be read in the buffer.
|
|
71
|
+
*
|
|
72
|
+
* When a buffer is being written to, each added byte increases its length.
|
|
73
|
+
* When a buffer is being read from, each read byte decreases its length.
|
|
74
|
+
*/
|
|
75
|
+
rb_define_method(rb_byte_buffer_class, "length", rb_bson_byte_buffer_length, 0);
|
|
76
|
+
|
|
77
|
+
/*
|
|
78
|
+
* call-seq:
|
|
79
|
+
* buffer.read_position -> Fixnum
|
|
80
|
+
*
|
|
81
|
+
* Returns the read position in the buffer.
|
|
82
|
+
*/
|
|
83
|
+
rb_define_method(rb_byte_buffer_class, "read_position", rb_bson_byte_buffer_read_position, 0);
|
|
84
|
+
|
|
85
|
+
rb_define_method(rb_byte_buffer_class, "get_byte", rb_bson_byte_buffer_get_byte, 0);
|
|
86
|
+
rb_define_method(rb_byte_buffer_class, "get_bytes", rb_bson_byte_buffer_get_bytes, 1);
|
|
87
|
+
rb_define_method(rb_byte_buffer_class, "get_cstring", rb_bson_byte_buffer_get_cstring, 0);
|
|
88
|
+
rb_define_method(rb_byte_buffer_class, "get_decimal128_bytes", rb_bson_byte_buffer_get_decimal128_bytes, 0);
|
|
89
|
+
rb_define_method(rb_byte_buffer_class, "get_double", rb_bson_byte_buffer_get_double, 0);
|
|
90
|
+
|
|
91
|
+
/*
|
|
92
|
+
* call-seq:
|
|
93
|
+
* buffer.get_hash(**options) -> Hash
|
|
94
|
+
*
|
|
95
|
+
* Reads a document from the byte buffer and returns it as a BSON::Document.
|
|
96
|
+
*
|
|
97
|
+
* @option options [ nil | :bson ] :mode Decoding mode to use.
|
|
98
|
+
*
|
|
99
|
+
* @return [ BSON::Document ] The decoded document.
|
|
100
|
+
*/
|
|
101
|
+
rb_define_method(rb_byte_buffer_class, "get_hash", rb_bson_byte_buffer_get_hash, -1);
|
|
102
|
+
|
|
103
|
+
/*
|
|
104
|
+
* call-seq:
|
|
105
|
+
* buffer.get_array(**options) -> Array
|
|
106
|
+
*
|
|
107
|
+
* Reads an array from the byte buffer.
|
|
108
|
+
*
|
|
109
|
+
* @option options [ nil | :bson ] :mode Decoding mode to use.
|
|
110
|
+
*
|
|
111
|
+
* @return [ Array ] The decoded array.
|
|
112
|
+
*/
|
|
113
|
+
rb_define_method(rb_byte_buffer_class, "get_array", rb_bson_byte_buffer_get_array, -1);
|
|
114
|
+
|
|
115
|
+
rb_define_method(rb_byte_buffer_class, "get_int32", rb_bson_byte_buffer_get_int32, 0);
|
|
116
|
+
|
|
117
|
+
/*
|
|
118
|
+
* call-seq:
|
|
119
|
+
* buffer.get_uint32(buffer) -> Fixnum
|
|
120
|
+
*
|
|
121
|
+
* Reads an unsigned 32 bit number from the byte buffer.
|
|
122
|
+
*
|
|
123
|
+
* @return [ Fixnum ] The unsigned 32 bits integer from the buffer
|
|
124
|
+
*
|
|
125
|
+
* @api private
|
|
126
|
+
*/
|
|
127
|
+
rb_define_method(rb_byte_buffer_class, "get_uint32", rb_bson_byte_buffer_get_uint32, 0);
|
|
128
|
+
rb_define_method(rb_byte_buffer_class, "get_int64", rb_bson_byte_buffer_get_int64, 0);
|
|
129
|
+
rb_define_method(rb_byte_buffer_class, "get_string", rb_bson_byte_buffer_get_string, 0);
|
|
130
|
+
|
|
131
|
+
/*
|
|
132
|
+
* call-seq:
|
|
133
|
+
* buffer.write_position -> Fixnum
|
|
134
|
+
*
|
|
135
|
+
* Returns the write position in the buffer.
|
|
136
|
+
*/
|
|
137
|
+
rb_define_method(rb_byte_buffer_class, "write_position", rb_bson_byte_buffer_write_position, 0);
|
|
138
|
+
|
|
139
|
+
/*
|
|
140
|
+
* call-seq:
|
|
141
|
+
* buffer.put_byte(binary_str) -> ByteBuffer
|
|
142
|
+
*
|
|
143
|
+
* Writes the specified byte string, which must be of length 1,
|
|
144
|
+
* to the byte buffer.
|
|
145
|
+
*
|
|
146
|
+
* Returns the modified +self+.
|
|
147
|
+
*/
|
|
148
|
+
rb_define_method(rb_byte_buffer_class, "put_byte", rb_bson_byte_buffer_put_byte, 1);
|
|
149
|
+
|
|
150
|
+
/*
|
|
151
|
+
* call-seq:
|
|
152
|
+
* buffer.put_bytes(binary_str) -> ByteBuffer
|
|
153
|
+
*
|
|
154
|
+
* Writes the specified byte string to the byte buffer.
|
|
155
|
+
*
|
|
156
|
+
* This method writes exactly the provided byte string - in particular, it
|
|
157
|
+
* does not prepend the length, and does not append a null byte at the end.
|
|
158
|
+
*
|
|
159
|
+
* Returns the modified +self+.
|
|
160
|
+
*/
|
|
161
|
+
rb_define_method(rb_byte_buffer_class, "put_bytes", rb_bson_byte_buffer_put_bytes, 1);
|
|
162
|
+
|
|
163
|
+
/*
|
|
164
|
+
* call-seq:
|
|
165
|
+
* buffer.put_string(str) -> ByteBuffer
|
|
166
|
+
*
|
|
167
|
+
* Writes the specified string to the byte buffer as a BSON string.
|
|
168
|
+
*
|
|
169
|
+
* Unlike #put_bytes, this method writes the provided byte string as
|
|
170
|
+
* a "BSON string" - the string is prefixed with its length and suffixed
|
|
171
|
+
* with a null byte. The byte string may contain null bytes itself thus
|
|
172
|
+
* the null terminator is redundant, but it is required by the BSON
|
|
173
|
+
* specification.
|
|
174
|
+
*
|
|
175
|
+
* +str+ must either already be in UTF-8 encoding or be a string encodable
|
|
176
|
+
* to UTF-8. In particular, a string in BINARY/ASCII-8BIT encoding is
|
|
177
|
+
* generally not suitable for this method. +EncodingError+ will be raised
|
|
178
|
+
* if +str+ cannot be encoded in UTF-8, or if +str+ claims to be encoded in
|
|
179
|
+
* UTF-8 but contains bytes/byte sequences which are not valid in UTF-8.
|
|
180
|
+
* Use #put_bytes to write arbitrary byte strings to the buffer.
|
|
181
|
+
*
|
|
182
|
+
* Returns the modified +self+.
|
|
183
|
+
*/
|
|
184
|
+
rb_define_method(rb_byte_buffer_class, "put_string", rb_bson_byte_buffer_put_string, 1);
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* call-seq:
|
|
188
|
+
* buffer.put_cstring(obj) -> ByteBuffer
|
|
189
|
+
*
|
|
190
|
+
* Converts +obj+ to a string, which must not contain any null bytes, and
|
|
191
|
+
* which must be valid UTF-8, and writes the string to the buffer as a
|
|
192
|
+
* BSON cstring. +obj+ can be an instance of String, Symbol or Fixnum.
|
|
193
|
+
*
|
|
194
|
+
* If the string serialization of +obj+ contains null bytes, this method
|
|
195
|
+
* raises +ArgumentError+. If +obj+ is of an unsupported type, this method
|
|
196
|
+
* raises +TypeError+.
|
|
197
|
+
*
|
|
198
|
+
* BSON cstring serialization contains no length of the string (relying
|
|
199
|
+
* instead on the null terminator), unlike the BSON string serialization.
|
|
200
|
+
*/
|
|
201
|
+
rb_define_method(rb_byte_buffer_class, "put_cstring", rb_bson_byte_buffer_put_cstring, 1);
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* call-seq:
|
|
205
|
+
* buffer.put_symbol(sym) -> ByteBuffer
|
|
206
|
+
*
|
|
207
|
+
* Converts +sym+ to a string and writes the resulting string to the byte
|
|
208
|
+
* buffer.
|
|
209
|
+
*
|
|
210
|
+
* The symbol may contain null bytes.
|
|
211
|
+
*
|
|
212
|
+
* The symbol value is assumed to be encoded in UTF-8. If the symbol value
|
|
213
|
+
* contains bytes or byte sequences that are not valid in UTF-8, this method
|
|
214
|
+
* raises +EncodingError+.
|
|
215
|
+
*
|
|
216
|
+
* Note: due to the string conversion, a symbol written to the buffer becomes
|
|
217
|
+
* indistinguishable from a string with the same value written to the buffer.
|
|
218
|
+
*/
|
|
219
|
+
rb_define_method(rb_byte_buffer_class, "put_symbol", rb_bson_byte_buffer_put_symbol, 1);
|
|
220
|
+
|
|
221
|
+
/*
|
|
222
|
+
* call-seq:
|
|
223
|
+
* buffer.put_int32(fixnum) -> ByteBuffer
|
|
224
|
+
*
|
|
225
|
+
* Writes a 32-bit integer value to the buffer.
|
|
226
|
+
*
|
|
227
|
+
* If the argument cannot be represented in 32 bits, raises RangeError.
|
|
228
|
+
*
|
|
229
|
+
* Returns the modified +self+.
|
|
230
|
+
*/
|
|
231
|
+
rb_define_method(rb_byte_buffer_class, "put_int32", rb_bson_byte_buffer_put_int32, 1);
|
|
232
|
+
|
|
233
|
+
/*
|
|
234
|
+
* call-seq:
|
|
235
|
+
* buffer.put_uint32(fixnum) -> ByteBuffer
|
|
236
|
+
*
|
|
237
|
+
* Writes an unsigned 32-bit integer value to the buffer.
|
|
238
|
+
*
|
|
239
|
+
* If the argument cannot be represented in 32 bits, raises RangeError.
|
|
240
|
+
*
|
|
241
|
+
* Returns the modified +self+.
|
|
242
|
+
*
|
|
243
|
+
* @api private
|
|
244
|
+
*
|
|
245
|
+
*/
|
|
246
|
+
rb_define_method(rb_byte_buffer_class, "put_uint32", rb_bson_byte_buffer_put_uint32, 1);
|
|
247
|
+
|
|
248
|
+
/*
|
|
249
|
+
* call-seq:
|
|
250
|
+
* buffer.put_int64(fixnum) -> ByteBuffer
|
|
251
|
+
*
|
|
252
|
+
* Writes a 64-bit integer value to the buffer.
|
|
253
|
+
*
|
|
254
|
+
* If the argument cannot be represented in 64 bits, raises RangeError.
|
|
255
|
+
*
|
|
256
|
+
* Returns the modified +self+.
|
|
257
|
+
*/
|
|
258
|
+
rb_define_method(rb_byte_buffer_class, "put_int64", rb_bson_byte_buffer_put_int64, 1);
|
|
259
|
+
|
|
260
|
+
/*
|
|
261
|
+
* call-seq:
|
|
262
|
+
* buffer.put_double(double) -> ByteBuffer
|
|
263
|
+
*
|
|
264
|
+
* Writes a 64-bit floating point value to the buffer.
|
|
265
|
+
*
|
|
266
|
+
* Returns the modified +self+.
|
|
267
|
+
*/
|
|
268
|
+
rb_define_method(rb_byte_buffer_class, "put_double", rb_bson_byte_buffer_put_double, 1);
|
|
269
|
+
|
|
270
|
+
/*
|
|
271
|
+
* call-seq:
|
|
272
|
+
* buffer.put_decimal128(low_64bit, high_64bit) -> ByteBuffer
|
|
273
|
+
*
|
|
274
|
+
* Writes a 128-bit Decimal128 value to the buffer.
|
|
275
|
+
*
|
|
276
|
+
* +low_64bit+ and +high_64bit+ are Fixnum objects containing the low and
|
|
277
|
+
* the high parts of the 128-bit Decimal128 value, respectively.
|
|
278
|
+
*
|
|
279
|
+
* Returns the modified +self+.
|
|
280
|
+
*/
|
|
281
|
+
rb_define_method(rb_byte_buffer_class, "put_decimal128", rb_bson_byte_buffer_put_decimal128, 2);
|
|
282
|
+
|
|
283
|
+
/*
|
|
284
|
+
* call-seq:
|
|
285
|
+
* buffer.put_hash(hash) -> ByteBuffer
|
|
286
|
+
*
|
|
287
|
+
* Writes a Hash into the byte buffer.
|
|
288
|
+
*
|
|
289
|
+
* Returns the modified +self+.
|
|
290
|
+
*/
|
|
291
|
+
rb_define_method(rb_byte_buffer_class, "put_hash", rb_bson_byte_buffer_put_hash, 1);
|
|
292
|
+
|
|
293
|
+
/*
|
|
294
|
+
* call-seq:
|
|
295
|
+
* buffer.put_array(array) -> ByteBuffer
|
|
296
|
+
*
|
|
297
|
+
* Writes an Array into the byte buffer.
|
|
298
|
+
*
|
|
299
|
+
* Returns the modified +self+.
|
|
300
|
+
*/
|
|
301
|
+
rb_define_method(rb_byte_buffer_class, "put_array", rb_bson_byte_buffer_put_array, 1);
|
|
302
|
+
|
|
303
|
+
/*
|
|
304
|
+
* call-seq:
|
|
305
|
+
* buffer.replace_int32(position, fixnum) -> ByteBuffer
|
|
306
|
+
*
|
|
307
|
+
* Replaces a 32-bit integer value at the specified position in the buffer.
|
|
308
|
+
*
|
|
309
|
+
* The position must be a non-negative integer, and must be completely
|
|
310
|
+
* contained within the data already written. For example, if the buffer has
|
|
311
|
+
* the write position of 12, the acceptable range of positions for this
|
|
312
|
+
* method is 0..8.
|
|
313
|
+
*
|
|
314
|
+
* If the argument cannot be represented in 32 bits, raises RangeError.
|
|
315
|
+
*
|
|
316
|
+
* Returns the modified +self+.
|
|
317
|
+
*/
|
|
318
|
+
rb_define_method(rb_byte_buffer_class, "replace_int32", rb_bson_byte_buffer_replace_int32, 2);
|
|
319
|
+
|
|
320
|
+
/*
|
|
321
|
+
* call-seq:
|
|
322
|
+
* buffer.rewind! -> ByteBuffer
|
|
323
|
+
*
|
|
324
|
+
* Resets the read position to the beginning of the byte buffer.
|
|
325
|
+
*
|
|
326
|
+
* Note: +rewind!+ does not change the buffer's write position.
|
|
327
|
+
*
|
|
328
|
+
* Returns the modified +self+.
|
|
329
|
+
*/
|
|
330
|
+
rb_define_method(rb_byte_buffer_class, "rewind!", rb_bson_byte_buffer_rewind, 0);
|
|
331
|
+
|
|
332
|
+
/*
|
|
333
|
+
* call-seq:
|
|
334
|
+
* buffer.to_s -> String
|
|
335
|
+
*
|
|
336
|
+
* Returns the contents of the buffer as a binary string.
|
|
337
|
+
*
|
|
338
|
+
* If the buffer is used for reading, the returned contents is the data
|
|
339
|
+
* that was not yet read. If the buffer is used for writing, the returned
|
|
340
|
+
* contents is the complete data that has been written so far.
|
|
341
|
+
*
|
|
342
|
+
* Note: this method copies the buffer's contents into a newly allocated
|
|
343
|
+
* +String+ instance. It does not return a reference to the data stored in
|
|
344
|
+
* the buffer itself.
|
|
345
|
+
*/
|
|
346
|
+
rb_define_method(rb_byte_buffer_class, "to_s", rb_bson_byte_buffer_to_s, 0);
|
|
347
|
+
|
|
348
|
+
rb_define_method(rb_bson_object_id_generator_class, "next_object_id", rb_bson_object_id_generator_next, -1);
|
|
349
|
+
rb_define_method(rb_bson_object_id_generator_class, "reset_counter", rb_bson_object_id_generator_reset_counter, -1);
|
|
350
|
+
|
|
351
|
+
// Get the object id machine id and hash it.
|
|
352
|
+
rb_require("digest/md5");
|
|
353
|
+
gethostname(rb_bson_machine_id, sizeof(rb_bson_machine_id));
|
|
354
|
+
rb_bson_machine_id[255] = '\0';
|
|
355
|
+
rb_bson_generate_machine_id(rb_md5_class, rb_bson_machine_id);
|
|
356
|
+
|
|
357
|
+
pvt_init_rand();
|
|
358
|
+
|
|
359
|
+
// Set the object id counter to a random 3-byte integer
|
|
360
|
+
rb_bson_object_id_counter = pvt_rand() % 0xFFFFFF;
|
|
361
|
+
|
|
362
|
+
rb_bson_registry = rb_const_get(rb_bson_module, rb_intern("Registry"));
|
|
363
|
+
rb_gc_register_mark_object(rb_bson_registry);
|
|
364
|
+
}
|