bson 3.2.7 → 4.0.0.beta
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/Rakefile +2 -10
- data/ext/bson/native-endian.h +120 -0
- data/ext/bson/native.c +547 -581
- data/lib/bson.rb +0 -1
- data/lib/bson/array.rb +15 -14
- data/lib/bson/binary.rb +13 -13
- data/lib/bson/boolean.rb +3 -3
- data/lib/bson/code.rb +5 -8
- data/lib/bson/code_with_scope.rb +10 -13
- data/lib/bson/date.rb +2 -2
- data/lib/bson/date_time.rb +2 -2
- data/lib/bson/document.rb +33 -0
- data/lib/bson/false_class.rb +2 -2
- data/lib/bson/float.rb +5 -11
- data/lib/bson/hash.rb +15 -14
- data/lib/bson/int32.rb +8 -9
- data/lib/bson/int64.rb +3 -9
- data/lib/bson/integer.rb +6 -20
- data/lib/bson/nil_class.rb +4 -16
- data/lib/bson/object.rb +1 -1
- data/lib/bson/object_id.rb +14 -16
- data/lib/bson/regexp.rb +7 -7
- data/lib/bson/specialized.rb +6 -6
- data/lib/bson/string.rb +7 -91
- data/lib/bson/symbol.rb +8 -7
- data/lib/bson/time.rb +5 -5
- data/lib/bson/timestamp.rb +8 -6
- data/lib/bson/true_class.rb +2 -2
- data/lib/bson/undefined.rb +1 -26
- data/lib/bson/version.rb +1 -1
- data/spec/bson/array_spec.rb +1 -1
- data/spec/bson/byte_buffer_spec.rb +445 -0
- data/spec/bson/code_with_scope_spec.rb +3 -7
- data/spec/bson/document_spec.rb +66 -10
- data/spec/bson/hash_spec.rb +5 -5
- data/spec/bson/int32_spec.rb +7 -5
- data/spec/bson/integer_spec.rb +1 -6
- data/spec/bson/object_id_spec.rb +2 -39
- data/spec/bson/regexp_spec.rb +1 -1
- data/spec/bson/string_spec.rb +2 -204
- data/spec/bson/symbol_spec.rb +2 -17
- data/spec/support/shared_examples.rb +3 -26
- metadata +13 -11
- metadata.gz.sig +0 -0
- data/lib/bson/encodable.rb +0 -86
data/lib/bson/integer.rb
CHANGED
@@ -22,16 +22,6 @@ module BSON
|
|
22
22
|
# @since 2.0.0
|
23
23
|
module Integer
|
24
24
|
|
25
|
-
# A 32bit integer is type 0x10 in the BSON spec.
|
26
|
-
#
|
27
|
-
# @since 2.0.0
|
28
|
-
INT32_TYPE = 16.chr.force_encoding(BINARY).freeze
|
29
|
-
|
30
|
-
# A 64bit integer is type 0x12 in the BSON spec.
|
31
|
-
#
|
32
|
-
# @since 2.0.0
|
33
|
-
INT64_TYPE = 18.chr.force_encoding(BINARY).freeze
|
34
|
-
|
35
25
|
# The maximum 32 bit integer value.
|
36
26
|
#
|
37
27
|
# @since 2.0.0
|
@@ -100,7 +90,7 @@ module BSON
|
|
100
90
|
#
|
101
91
|
# @since 2.0.0
|
102
92
|
def bson_type
|
103
|
-
bson_int32? ?
|
93
|
+
bson_int32? ? Int32::BSON_TYPE : (bson_int64? ? Int64::BSON_TYPE : out_of_range!)
|
104
94
|
end
|
105
95
|
|
106
96
|
# Get the integer as encoded BSON.
|
@@ -113,11 +103,11 @@ module BSON
|
|
113
103
|
# @see http://bsonspec.org/#/specification
|
114
104
|
#
|
115
105
|
# @since 2.0.0
|
116
|
-
def to_bson(
|
106
|
+
def to_bson(buffer = ByteBuffer.new)
|
117
107
|
if bson_int32?
|
118
|
-
|
108
|
+
buffer.put_int32(self)
|
119
109
|
elsif bson_int64?
|
120
|
-
|
110
|
+
buffer.put_int64(self)
|
121
111
|
else
|
122
112
|
out_of_range!
|
123
113
|
end
|
@@ -155,12 +145,8 @@ module BSON
|
|
155
145
|
encoded << ((self >> 56) & 255)
|
156
146
|
end
|
157
147
|
|
158
|
-
def to_bson_key(
|
159
|
-
|
160
|
-
encoded << BSON_ARRAY_INDEXES[self]
|
161
|
-
else
|
162
|
-
self.to_s.to_bson_cstring(encoded)
|
163
|
-
end
|
148
|
+
def to_bson_key(buffer = ByteBuffer.new)
|
149
|
+
buffer.put_cstring(to_s)
|
164
150
|
end
|
165
151
|
|
166
152
|
private
|
data/lib/bson/nil_class.rb
CHANGED
@@ -21,37 +21,25 @@ module BSON
|
|
21
21
|
#
|
22
22
|
# @since 2.0.0
|
23
23
|
module NilClass
|
24
|
+
include Specialized
|
24
25
|
|
25
26
|
# A nil is type 0x0A in the BSON spec.
|
26
27
|
#
|
27
28
|
# @since 2.0.0
|
28
29
|
BSON_TYPE = 10.chr.force_encoding(BINARY).freeze
|
29
30
|
|
30
|
-
# Get the nil as encoded BSON.
|
31
|
-
#
|
32
|
-
# @example Get the nil as encoded BSON.
|
33
|
-
# nil.to_bson
|
34
|
-
#
|
35
|
-
# @return [ String ] An empty string.
|
36
|
-
#
|
37
|
-
# @see http://bsonspec.org/#/specification
|
38
|
-
#
|
39
|
-
# @since 2.0.0
|
40
|
-
def to_bson(encoded = ''.force_encoding(BINARY))
|
41
|
-
encoded
|
42
|
-
end
|
43
|
-
|
44
31
|
module ClassMethods
|
32
|
+
|
45
33
|
# Deserialize NilClass from BSON.
|
46
34
|
#
|
47
|
-
# @param [
|
35
|
+
# @param [ ByteBuffer ] buffer The byte buffer.
|
48
36
|
#
|
49
37
|
# @return [ nil ] The decoded nil value.
|
50
38
|
#
|
51
39
|
# @see http://bsonspec.org/#/specification
|
52
40
|
#
|
53
41
|
# @since 2.0.0
|
54
|
-
def from_bson(
|
42
|
+
def from_bson(buffer)
|
55
43
|
nil
|
56
44
|
end
|
57
45
|
end
|
data/lib/bson/object.rb
CHANGED
data/lib/bson/object_id.rb
CHANGED
@@ -44,7 +44,7 @@ module BSON
|
|
44
44
|
# @since 2.0.0
|
45
45
|
def ==(other)
|
46
46
|
return false unless other.is_a?(ObjectId)
|
47
|
-
|
47
|
+
generate_data == other.send(:generate_data)
|
48
48
|
end
|
49
49
|
alias :eql? :==
|
50
50
|
|
@@ -86,7 +86,7 @@ module BSON
|
|
86
86
|
#
|
87
87
|
# @since 2.0.0
|
88
88
|
def <=>(other)
|
89
|
-
|
89
|
+
generate_data <=> other.to_bson.to_s
|
90
90
|
end
|
91
91
|
|
92
92
|
# Return the UTC time at which this ObjectId was generated. This may
|
@@ -100,7 +100,7 @@ module BSON
|
|
100
100
|
#
|
101
101
|
# @since 2.0.0
|
102
102
|
def generation_time
|
103
|
-
::Time.at(
|
103
|
+
::Time.at(generate_data.unpack("N")[0]).utc
|
104
104
|
end
|
105
105
|
|
106
106
|
# Get the hash value for the object id.
|
@@ -112,7 +112,7 @@ module BSON
|
|
112
112
|
#
|
113
113
|
# @since 2.0.0
|
114
114
|
def hash
|
115
|
-
|
115
|
+
generate_data.hash
|
116
116
|
end
|
117
117
|
|
118
118
|
# Get a nice string for use with object inspection.
|
@@ -136,7 +136,7 @@ module BSON
|
|
136
136
|
#
|
137
137
|
# @since 2.0.0
|
138
138
|
def marshal_dump
|
139
|
-
|
139
|
+
generate_data
|
140
140
|
end
|
141
141
|
|
142
142
|
# Unmarshal the data into an object id.
|
@@ -168,10 +168,8 @@ module BSON
|
|
168
168
|
# @see http://bsonspec.org/#/specification
|
169
169
|
#
|
170
170
|
# @since 2.0.0
|
171
|
-
def to_bson(
|
172
|
-
|
173
|
-
@raw_data ||= @@generator.next_object_id
|
174
|
-
encoded << @raw_data
|
171
|
+
def to_bson(buffer = ByteBuffer.new)
|
172
|
+
buffer.put_bytes(generate_data)
|
175
173
|
end
|
176
174
|
|
177
175
|
# Get the string representation of the object id.
|
@@ -183,7 +181,7 @@ module BSON
|
|
183
181
|
#
|
184
182
|
# @since 2.0.0
|
185
183
|
def to_s
|
186
|
-
|
184
|
+
generate_data.to_hex_string.force_encoding(UTF8)
|
187
185
|
end
|
188
186
|
alias :to_str :to_s
|
189
187
|
|
@@ -194,9 +192,9 @@ module BSON
|
|
194
192
|
|
195
193
|
private
|
196
194
|
|
197
|
-
def
|
198
|
-
|
199
|
-
|
195
|
+
def generate_data
|
196
|
+
repair if defined?(@data)
|
197
|
+
@raw_data ||= @@generator.next_object_id
|
200
198
|
end
|
201
199
|
|
202
200
|
def repair
|
@@ -211,13 +209,13 @@ module BSON
|
|
211
209
|
# @example Get the object id from BSON.
|
212
210
|
# ObjectId.from_bson(bson)
|
213
211
|
#
|
214
|
-
# @param [
|
212
|
+
# @param [ ByteBuffer ] buffer The byte buffer.
|
215
213
|
#
|
216
214
|
# @return [ BSON::ObjectId ] The object id.
|
217
215
|
#
|
218
216
|
# @since 2.0.0
|
219
|
-
def from_bson(
|
220
|
-
from_data(
|
217
|
+
def from_bson(buffer)
|
218
|
+
from_data(buffer.get_bytes(12))
|
221
219
|
end
|
222
220
|
|
223
221
|
# Create a new object id from raw bytes.
|
data/lib/bson/regexp.rb
CHANGED
@@ -84,9 +84,9 @@ module BSON
|
|
84
84
|
# @see http://bsonspec.org/#/specification
|
85
85
|
#
|
86
86
|
# @since 2.0.0
|
87
|
-
def to_bson(
|
88
|
-
|
89
|
-
|
87
|
+
def to_bson(buffer = ByteBuffer.new)
|
88
|
+
buffer.put_cstring(source)
|
89
|
+
buffer.put_cstring(bson_options)
|
90
90
|
end
|
91
91
|
|
92
92
|
private
|
@@ -168,17 +168,17 @@ module BSON
|
|
168
168
|
|
169
169
|
# Deserialize the regular expression from BSON.
|
170
170
|
#
|
171
|
-
# @param [
|
171
|
+
# @param [ ByteBuffer ] buffer The byte buffer.
|
172
172
|
#
|
173
173
|
# @return [ Regexp ] The decoded regular expression.
|
174
174
|
#
|
175
175
|
# @see http://bsonspec.org/#/specification
|
176
176
|
#
|
177
177
|
# @since 2.0.0
|
178
|
-
def from_bson(
|
179
|
-
pattern =
|
178
|
+
def from_bson(buffer)
|
179
|
+
pattern = buffer.get_cstring
|
180
180
|
options = 0
|
181
|
-
while (option =
|
181
|
+
while (option = buffer.get_byte) != NULL_BYTE
|
182
182
|
case option
|
183
183
|
when IGNORECASE_VALUE
|
184
184
|
options |= ::Regexp::IGNORECASE
|
data/lib/bson/specialized.rb
CHANGED
@@ -45,8 +45,8 @@ module BSON
|
|
45
45
|
# @return [ String ] An empty string.
|
46
46
|
#
|
47
47
|
# @since 2.0.0
|
48
|
-
def to_bson(
|
49
|
-
|
48
|
+
def to_bson(buffer = ByteBuffer.new)
|
49
|
+
buffer
|
50
50
|
end
|
51
51
|
|
52
52
|
private
|
@@ -57,16 +57,16 @@ module BSON
|
|
57
57
|
|
58
58
|
module ClassMethods
|
59
59
|
|
60
|
-
# Deserialize
|
60
|
+
# Deserialize from BSON.
|
61
61
|
#
|
62
|
-
# @param [
|
62
|
+
# @param [ ByteBuffer ] buffer The byte buffer.
|
63
63
|
#
|
64
|
-
# @return [
|
64
|
+
# @return [ Specialized ] The decoded specialized class.
|
65
65
|
#
|
66
66
|
# @see http://bsonspec.org/#/specification
|
67
67
|
#
|
68
68
|
# @since 2.0.0
|
69
|
-
def from_bson(
|
69
|
+
def from_bson(buffer)
|
70
70
|
new
|
71
71
|
end
|
72
72
|
end
|
data/lib/bson/string.rb
CHANGED
@@ -22,7 +22,6 @@ module BSON
|
|
22
22
|
#
|
23
23
|
# @since 2.0.0
|
24
24
|
module String
|
25
|
-
include Encodable
|
26
25
|
|
27
26
|
# A string is type 0x02 in the BSON spec.
|
28
27
|
#
|
@@ -41,10 +40,8 @@ module BSON
|
|
41
40
|
# @see http://bsonspec.org/#/specification
|
42
41
|
#
|
43
42
|
# @since 2.0.0
|
44
|
-
def to_bson(
|
45
|
-
|
46
|
-
to_bson_string(encoded)
|
47
|
-
end
|
43
|
+
def to_bson(buffer = ByteBuffer.new)
|
44
|
+
buffer.put_string(self)
|
48
45
|
end
|
49
46
|
|
50
47
|
# Get the string as a BSON key name encoded C string with checking for special characters.
|
@@ -59,25 +56,8 @@ module BSON
|
|
59
56
|
# @see http://bsonspec.org/#/specification
|
60
57
|
#
|
61
58
|
# @since 2.0.0
|
62
|
-
def to_bson_key
|
63
|
-
|
64
|
-
end
|
65
|
-
|
66
|
-
# Get the string as an encoded C string.
|
67
|
-
#
|
68
|
-
# @example Get the string as an encoded C string.
|
69
|
-
# "test".to_bson_cstring
|
70
|
-
#
|
71
|
-
# @raise [ EncodingError ] If the string is not UTF-8.
|
72
|
-
#
|
73
|
-
# @return [ String ] The encoded string.
|
74
|
-
#
|
75
|
-
# @see http://bsonspec.org/#/specification
|
76
|
-
#
|
77
|
-
# @since 2.0.0
|
78
|
-
def to_bson_cstring(encoded = ''.force_encoding(BINARY))
|
79
|
-
check_for_illegal_characters!
|
80
|
-
to_bson_string(encoded) << NULL_BYTE
|
59
|
+
def to_bson_key
|
60
|
+
self
|
81
61
|
end
|
82
62
|
|
83
63
|
# Convert the string to an object id. This will only work for strings of size
|
@@ -97,27 +77,6 @@ module BSON
|
|
97
77
|
ObjectId.repair(self)
|
98
78
|
end
|
99
79
|
|
100
|
-
# Convert the string to a UTF-8 string then force to binary. This is so
|
101
|
-
# we get errors for strings that are not UTF-8 encoded.
|
102
|
-
#
|
103
|
-
# @example Convert to valid BSON string.
|
104
|
-
# "Straße".to_bson_string
|
105
|
-
#
|
106
|
-
# @raise [ EncodingError ] If the string is not UTF-8.
|
107
|
-
#
|
108
|
-
# @return [ String ] The binary string.
|
109
|
-
#
|
110
|
-
# @since 2.0.0
|
111
|
-
def to_bson_string(encoded = ''.force_encoding(BINARY))
|
112
|
-
begin
|
113
|
-
to_utf8_binary(encoded)
|
114
|
-
rescue EncodingError
|
115
|
-
data = dup.force_encoding(UTF8)
|
116
|
-
raise unless data.valid_encoding?
|
117
|
-
encoded << data.force_encoding(BINARY)
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
80
|
# Convert the string to a hexidecimal representation.
|
122
81
|
#
|
123
82
|
# @example Convert the string to hex.
|
@@ -130,62 +89,19 @@ module BSON
|
|
130
89
|
unpack("H*")[0]
|
131
90
|
end
|
132
91
|
|
133
|
-
# Take the binary string and return a UTF-8 encoded string.
|
134
|
-
#
|
135
|
-
# @example Convert from a BSON string.
|
136
|
-
# "\x00".from_bson_string
|
137
|
-
#
|
138
|
-
# @raise [ EncodingError ] If the string is not UTF-8.
|
139
|
-
#
|
140
|
-
# @return [ String ] The UTF-8 string.
|
141
|
-
#
|
142
|
-
# @since 2.0.0
|
143
|
-
def from_bson_string
|
144
|
-
force_encoding(UTF8)
|
145
|
-
end
|
146
|
-
|
147
|
-
# Set four bytes for int32 in a binary string and return it.
|
148
|
-
#
|
149
|
-
# @example Set int32 in a BSON string.
|
150
|
-
# "".set_int32(pos, int32)
|
151
|
-
#
|
152
|
-
# @param [ Fixnum ] The position to set.
|
153
|
-
# @param [ Fixnum ] The int32 value.
|
154
|
-
#
|
155
|
-
# @return [ String ] The binary string.
|
156
|
-
#
|
157
|
-
# @since 2.0.0
|
158
|
-
def set_int32(pos, int32)
|
159
|
-
self[pos, 4] = [ int32 ].pack(Int32::PACK)
|
160
|
-
end
|
161
|
-
|
162
|
-
private
|
163
|
-
|
164
|
-
def to_utf8_binary(encoded)
|
165
|
-
encoded << encode(UTF8).force_encoding(BINARY)
|
166
|
-
end
|
167
|
-
|
168
92
|
module ClassMethods
|
169
93
|
|
170
94
|
# Deserialize a string from BSON.
|
171
95
|
#
|
172
|
-
# @param [
|
96
|
+
# @param [ ByteBuffer ] buffer The byte buffer.
|
173
97
|
#
|
174
98
|
# @return [ Regexp ] The decoded string.
|
175
99
|
#
|
176
100
|
# @see http://bsonspec.org/#/specification
|
177
101
|
#
|
178
102
|
# @since 2.0.0
|
179
|
-
def from_bson(
|
180
|
-
|
181
|
-
end
|
182
|
-
end
|
183
|
-
|
184
|
-
private
|
185
|
-
|
186
|
-
def check_for_illegal_characters!
|
187
|
-
if include?(NULL_BYTE)
|
188
|
-
raise(ArgumentError, "Illegal C-String '#{self}' contains a null byte.")
|
103
|
+
def from_bson(buffer)
|
104
|
+
buffer.get_string
|
189
105
|
end
|
190
106
|
end
|
191
107
|
|
data/lib/bson/symbol.rb
CHANGED
@@ -40,8 +40,8 @@ module BSON
|
|
40
40
|
# @see http://bsonspec.org/#/specification
|
41
41
|
#
|
42
42
|
# @since 2.0.0
|
43
|
-
def to_bson(
|
44
|
-
to_s.to_bson(
|
43
|
+
def to_bson(buffer = ByteBuffer.new)
|
44
|
+
to_s.to_bson(buffer)
|
45
45
|
end
|
46
46
|
|
47
47
|
# Get the symbol as a BSON key name encoded C symbol.
|
@@ -54,8 +54,8 @@ module BSON
|
|
54
54
|
# @see http://bsonspec.org/#/specification
|
55
55
|
#
|
56
56
|
# @since 2.0.0
|
57
|
-
def to_bson_key
|
58
|
-
to_s.to_bson_key
|
57
|
+
def to_bson_key
|
58
|
+
to_s.to_bson_key
|
59
59
|
end
|
60
60
|
|
61
61
|
# Converts the symbol to a normalized key in a BSON document.
|
@@ -71,17 +71,18 @@ module BSON
|
|
71
71
|
end
|
72
72
|
|
73
73
|
module ClassMethods
|
74
|
+
|
74
75
|
# Deserialize a symbol from BSON.
|
75
76
|
#
|
76
|
-
# @param [
|
77
|
+
# @param [ ByteBuffer ] buffer The byte buffer.
|
77
78
|
#
|
78
79
|
# @return [ Regexp ] The decoded symbol.
|
79
80
|
#
|
80
81
|
# @see http://bsonspec.org/#/specification
|
81
82
|
#
|
82
83
|
# @since 2.0.0
|
83
|
-
def from_bson(
|
84
|
-
|
84
|
+
def from_bson(buffer)
|
85
|
+
buffer.get_string.intern
|
85
86
|
end
|
86
87
|
end
|
87
88
|
|