bson 4.2.2 → 4.12.1

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