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.
Files changed (226) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +106 -7
  3. data/Rakefile +86 -43
  4. data/ext/bson/{native-endian.h → bson-endian.h} +14 -107
  5. data/ext/bson/bson-native.h +135 -0
  6. data/ext/bson/bytebuf.c +133 -0
  7. data/ext/bson/endian.c +117 -0
  8. data/ext/bson/extconf.rb +8 -3
  9. data/ext/bson/init.c +364 -0
  10. data/ext/bson/libbson-utf8.c +230 -0
  11. data/ext/bson/read.c +470 -0
  12. data/ext/bson/util.c +250 -0
  13. data/ext/bson/write.c +675 -0
  14. data/lib/bson/active_support.rb +19 -0
  15. data/lib/bson/array.rb +97 -30
  16. data/lib/bson/big_decimal.rb +77 -0
  17. data/lib/bson/binary.rb +510 -70
  18. data/lib/bson/boolean.rb +15 -4
  19. data/lib/bson/code.rb +25 -12
  20. data/lib/bson/code_with_scope.rb +41 -15
  21. data/lib/bson/config.rb +3 -28
  22. data/lib/bson/date.rb +16 -4
  23. data/lib/bson/date_time.rb +6 -4
  24. data/lib/bson/db_pointer.rb +110 -0
  25. data/lib/bson/dbref.rb +154 -0
  26. data/lib/bson/decimal128/builder.rb +456 -0
  27. data/lib/bson/decimal128.rb +272 -0
  28. data/lib/bson/document.rb +177 -7
  29. data/lib/bson/environment.rb +17 -2
  30. data/lib/bson/error/bson_decode_error.rb +11 -0
  31. data/lib/bson/error/ext_json_parse_error.rb +11 -0
  32. data/lib/bson/error/illegal_key.rb +23 -0
  33. data/lib/bson/error/invalid_binary_type.rb +37 -0
  34. data/lib/bson/error/invalid_dbref_argument.rb +12 -0
  35. data/lib/bson/error/invalid_decimal128_argument.rb +25 -0
  36. data/lib/bson/error/invalid_decimal128_range.rb +27 -0
  37. data/lib/bson/error/invalid_decimal128_string.rb +26 -0
  38. data/lib/bson/error/invalid_key.rb +24 -0
  39. data/lib/bson/error/invalid_object_id.rb +11 -0
  40. data/lib/bson/error/invalid_regexp_pattern.rb +13 -0
  41. data/lib/bson/error/unrepresentable_precision.rb +19 -0
  42. data/lib/bson/error/unserializable_class.rb +13 -0
  43. data/lib/bson/error/unsupported_binary_subtype.rb +12 -0
  44. data/lib/bson/error/unsupported_type.rb +11 -0
  45. data/lib/bson/error.rb +22 -0
  46. data/lib/bson/ext_json.rb +389 -0
  47. data/lib/bson/false_class.rb +6 -4
  48. data/lib/bson/float.rb +43 -7
  49. data/lib/bson/hash.rb +152 -37
  50. data/lib/bson/int32.rb +104 -6
  51. data/lib/bson/int64.rb +111 -8
  52. data/lib/bson/integer.rb +43 -9
  53. data/lib/bson/json.rb +3 -1
  54. data/lib/bson/max_key.rb +21 -10
  55. data/lib/bson/min_key.rb +21 -10
  56. data/lib/bson/nil_class.rb +7 -3
  57. data/lib/bson/object.rb +25 -17
  58. data/lib/bson/object_id.rb +98 -113
  59. data/lib/bson/open_struct.rb +59 -0
  60. data/lib/bson/regexp.rb +129 -56
  61. data/lib/bson/registry.rb +7 -10
  62. data/lib/bson/specialized.rb +8 -4
  63. data/lib/bson/string.rb +12 -32
  64. data/lib/bson/symbol.rb +107 -11
  65. data/lib/bson/time.rb +68 -7
  66. data/lib/bson/time_with_zone.rb +67 -0
  67. data/lib/bson/timestamp.rb +50 -10
  68. data/lib/bson/true_class.rb +6 -4
  69. data/lib/bson/undefined.rb +28 -2
  70. data/lib/bson/vector.rb +44 -0
  71. data/lib/bson/version.rb +6 -14
  72. data/lib/bson.rb +22 -12
  73. data/spec/README.md +14 -0
  74. data/spec/bson/array_spec.rb +38 -62
  75. data/spec/bson/big_decimal_spec.rb +328 -0
  76. data/spec/bson/binary_spec.rb +199 -53
  77. data/spec/bson/binary_uuid_spec.rb +190 -0
  78. data/spec/bson/boolean_spec.rb +2 -1
  79. data/spec/bson/byte_buffer_read_spec.rb +198 -0
  80. data/spec/bson/byte_buffer_spec.rb +122 -381
  81. data/spec/bson/byte_buffer_write_spec.rb +855 -0
  82. data/spec/bson/code_spec.rb +6 -4
  83. data/spec/bson/code_with_scope_spec.rb +6 -4
  84. data/spec/bson/config_spec.rb +1 -35
  85. data/spec/bson/date_spec.rb +2 -1
  86. data/spec/bson/date_time_spec.rb +55 -1
  87. data/spec/bson/dbref_legacy_spec.rb +186 -0
  88. data/spec/bson/dbref_spec.rb +487 -0
  89. data/spec/bson/decimal128_spec.rb +1840 -0
  90. data/spec/bson/document_as_spec.rb +61 -0
  91. data/spec/bson/document_spec.rb +205 -32
  92. data/spec/bson/ext_json_parse_spec.rb +346 -0
  93. data/spec/bson/false_class_spec.rb +9 -1
  94. data/spec/bson/float_spec.rb +42 -1
  95. data/spec/bson/hash_as_spec.rb +58 -0
  96. data/spec/bson/hash_spec.rb +318 -66
  97. data/spec/bson/int32_spec.rb +248 -1
  98. data/spec/bson/int64_spec.rb +308 -1
  99. data/spec/bson/integer_spec.rb +61 -3
  100. data/spec/bson/json_spec.rb +2 -1
  101. data/spec/bson/max_key_spec.rb +6 -4
  102. data/spec/bson/min_key_spec.rb +6 -4
  103. data/spec/bson/nil_class_spec.rb +2 -1
  104. data/spec/bson/object_id_spec.rb +95 -5
  105. data/spec/bson/object_spec.rb +3 -2
  106. data/spec/bson/open_struct_spec.rb +87 -0
  107. data/spec/bson/raw_spec.rb +594 -0
  108. data/spec/bson/regexp_spec.rb +61 -8
  109. data/spec/bson/registry_spec.rb +3 -2
  110. data/spec/bson/string_spec.rb +26 -33
  111. data/spec/bson/symbol_raw_spec.rb +70 -0
  112. data/spec/bson/symbol_spec.rb +77 -20
  113. data/spec/bson/time_spec.rb +206 -2
  114. data/spec/bson/time_with_zone_spec.rb +69 -0
  115. data/spec/bson/timestamp_spec.rb +58 -2
  116. data/spec/bson/true_class_spec.rb +9 -1
  117. data/spec/bson/undefined_spec.rb +28 -1
  118. data/spec/bson/vector_spec.rb +33 -0
  119. data/spec/bson_spec.rb +2 -1
  120. data/spec/runners/binary_vector.rb +78 -0
  121. data/spec/runners/common_driver.rb +348 -0
  122. data/spec/runners/corpus.rb +191 -0
  123. data/spec/runners/corpus_legacy.rb +248 -0
  124. data/spec/shared/LICENSE +20 -0
  125. data/spec/shared/bin/get-mongodb-download-url +17 -0
  126. data/spec/shared/bin/s3-copy +45 -0
  127. data/spec/shared/bin/s3-upload +69 -0
  128. data/spec/shared/lib/mrss/child_process_helper.rb +80 -0
  129. data/spec/shared/lib/mrss/cluster_config.rb +231 -0
  130. data/spec/shared/lib/mrss/constraints.rb +378 -0
  131. data/spec/shared/lib/mrss/docker_runner.rb +298 -0
  132. data/spec/shared/lib/mrss/eg_config_utils.rb +51 -0
  133. data/spec/shared/lib/mrss/event_subscriber.rb +210 -0
  134. data/spec/shared/lib/mrss/lite_constraints.rb +238 -0
  135. data/spec/shared/lib/mrss/release/candidate.rb +281 -0
  136. data/spec/shared/lib/mrss/release/product_data.rb +144 -0
  137. data/spec/shared/lib/mrss/server_version_registry.rb +113 -0
  138. data/spec/shared/lib/mrss/session_registry.rb +69 -0
  139. data/spec/shared/lib/mrss/session_registry_legacy.rb +60 -0
  140. data/spec/shared/lib/mrss/spec_organizer.rb +179 -0
  141. data/spec/shared/lib/mrss/utils.rb +37 -0
  142. data/spec/shared/lib/tasks/candidate.rake +64 -0
  143. data/spec/shared/share/Dockerfile.erb +251 -0
  144. data/spec/shared/share/haproxy-1.conf +16 -0
  145. data/spec/shared/share/haproxy-2.conf +17 -0
  146. data/spec/shared/shlib/config.sh +27 -0
  147. data/spec/shared/shlib/distro.sh +84 -0
  148. data/spec/shared/shlib/server.sh +423 -0
  149. data/spec/shared/shlib/set_env.sh +110 -0
  150. data/spec/spec_helper.rb +61 -1
  151. data/spec/spec_tests/binary_vector_spec.rb +82 -0
  152. data/spec/spec_tests/common_driver_spec.rb +84 -0
  153. data/spec/spec_tests/corpus_legacy_spec.rb +72 -0
  154. data/spec/spec_tests/corpus_spec.rb +134 -0
  155. data/spec/spec_tests/data/binary_vector/README.md +61 -0
  156. data/spec/spec_tests/data/binary_vector/float32.json +65 -0
  157. data/spec/spec_tests/data/binary_vector/int8.json +57 -0
  158. data/spec/spec_tests/data/binary_vector/packed_bit.json +83 -0
  159. data/spec/spec_tests/data/corpus/README.md +15 -0
  160. data/spec/spec_tests/data/corpus/array.json +49 -0
  161. data/spec/spec_tests/data/corpus/binary.json +153 -0
  162. data/spec/spec_tests/data/corpus/boolean.json +27 -0
  163. data/spec/spec_tests/data/corpus/code.json +67 -0
  164. data/spec/spec_tests/data/corpus/code_w_scope.json +78 -0
  165. data/spec/spec_tests/data/corpus/datetime.json +42 -0
  166. data/spec/spec_tests/data/corpus/dbpointer.json +56 -0
  167. data/spec/spec_tests/data/corpus/dbref.json +51 -0
  168. data/spec/spec_tests/data/corpus/decimal128-1.json +317 -0
  169. data/spec/spec_tests/data/corpus/decimal128-2.json +793 -0
  170. data/spec/spec_tests/data/corpus/decimal128-3.json +1771 -0
  171. data/spec/spec_tests/data/corpus/decimal128-4.json +165 -0
  172. data/spec/spec_tests/data/corpus/decimal128-5.json +402 -0
  173. data/spec/spec_tests/data/corpus/decimal128-6.json +131 -0
  174. data/spec/spec_tests/data/corpus/decimal128-7.json +327 -0
  175. data/spec/spec_tests/data/corpus/document.json +60 -0
  176. data/spec/spec_tests/data/corpus/double.json +87 -0
  177. data/spec/spec_tests/data/corpus/int32.json +43 -0
  178. data/spec/spec_tests/data/corpus/int64.json +43 -0
  179. data/spec/spec_tests/data/corpus/maxkey.json +12 -0
  180. data/spec/spec_tests/data/corpus/minkey.json +12 -0
  181. data/spec/spec_tests/data/corpus/multi-type-deprecated.json +15 -0
  182. data/spec/spec_tests/data/corpus/multi-type.json +11 -0
  183. data/spec/spec_tests/data/corpus/null.json +12 -0
  184. data/spec/spec_tests/data/corpus/oid.json +28 -0
  185. data/spec/spec_tests/data/corpus/regex.json +65 -0
  186. data/spec/spec_tests/data/corpus/string.json +72 -0
  187. data/spec/spec_tests/data/corpus/symbol.json +80 -0
  188. data/spec/spec_tests/data/corpus/timestamp.json +34 -0
  189. data/spec/spec_tests/data/corpus/top.json +262 -0
  190. data/spec/spec_tests/data/corpus/undefined.json +15 -0
  191. data/spec/spec_tests/data/corpus_legacy/array.json +49 -0
  192. data/spec/spec_tests/data/corpus_legacy/binary.json +69 -0
  193. data/spec/spec_tests/data/corpus_legacy/boolean.json +27 -0
  194. data/spec/spec_tests/data/corpus_legacy/code.json +67 -0
  195. data/spec/spec_tests/data/corpus_legacy/code_w_scope.json +78 -0
  196. data/spec/spec_tests/data/corpus_legacy/document.json +36 -0
  197. data/spec/spec_tests/data/corpus_legacy/double.json +69 -0
  198. data/spec/spec_tests/data/corpus_legacy/failures/datetime.json +31 -0
  199. data/spec/spec_tests/data/corpus_legacy/failures/dbpointer.json +42 -0
  200. data/spec/spec_tests/data/corpus_legacy/failures/int64.json +38 -0
  201. data/spec/spec_tests/data/corpus_legacy/failures/symbol.json +62 -0
  202. data/spec/spec_tests/data/corpus_legacy/int32.json +38 -0
  203. data/spec/spec_tests/data/corpus_legacy/maxkey.json +12 -0
  204. data/spec/spec_tests/data/corpus_legacy/minkey.json +12 -0
  205. data/spec/spec_tests/data/corpus_legacy/null.json +12 -0
  206. data/spec/spec_tests/data/corpus_legacy/oid.json +28 -0
  207. data/spec/spec_tests/data/corpus_legacy/regex.json +37 -0
  208. data/spec/spec_tests/data/corpus_legacy/string.json +67 -0
  209. data/spec/spec_tests/data/corpus_legacy/timestamp.json +18 -0
  210. data/spec/spec_tests/data/corpus_legacy/top.json +62 -0
  211. data/spec/spec_tests/data/corpus_legacy/undefined.json +13 -0
  212. data/spec/spec_tests/data/decimal128/decimal128-1.json +363 -0
  213. data/spec/spec_tests/data/decimal128/decimal128-2.json +793 -0
  214. data/spec/spec_tests/data/decimal128/decimal128-3.json +1771 -0
  215. data/spec/spec_tests/data/decimal128/decimal128-4.json +165 -0
  216. data/spec/spec_tests/data/decimal128/decimal128-5.json +402 -0
  217. data/spec/spec_tests/data/decimal128/decimal128-6.json +131 -0
  218. data/spec/spec_tests/data/decimal128/decimal128-7.json +327 -0
  219. data/spec/support/shared_examples.rb +32 -11
  220. data/spec/support/spec_config.rb +17 -0
  221. data/spec/support/utils.rb +58 -0
  222. metadata +284 -45
  223. checksums.yaml.gz.sig +0 -3
  224. data/ext/bson/native.c +0 -722
  225. data.tar.gz.sig +0 -0
  226. metadata.gz.sig +0 -0
@@ -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
@@ -1,3 +1,8 @@
1
- require "mkmf"
2
- $CFLAGS << " -Wall -g -std=c99"
3
- create_makefile("native")
1
+ # frozen_string_literal: true
2
+ # rubocop:disable all
3
+
4
+ require 'mkmf'
5
+
6
+ append_cflags(["-Wall", "-g", "-std=c99"])
7
+
8
+ create_makefile('bson_native')
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
+ }