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.rb
CHANGED
data/lib/bson/array.rb
CHANGED
@@ -21,7 +21,6 @@ module BSON
|
|
21
21
|
#
|
22
22
|
# @since 2.0.0
|
23
23
|
module Array
|
24
|
-
include Encodable
|
25
24
|
|
26
25
|
# An array is type 0x04 in the BSON spec.
|
27
26
|
#
|
@@ -41,14 +40,16 @@ module BSON
|
|
41
40
|
# @see http://bsonspec.org/#/specification
|
42
41
|
#
|
43
42
|
# @since 2.0.0
|
44
|
-
def to_bson(
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
43
|
+
def to_bson(buffer = ByteBuffer.new)
|
44
|
+
position = buffer.length
|
45
|
+
buffer.put_int32(0)
|
46
|
+
each_with_index do |value, index|
|
47
|
+
buffer.put_byte(value.bson_type)
|
48
|
+
buffer.put_cstring(index.to_s)
|
49
|
+
value.to_bson(buffer)
|
51
50
|
end
|
51
|
+
buffer.put_byte(NULL_BYTE)
|
52
|
+
buffer.replace_int32(position, buffer.length - position)
|
52
53
|
end
|
53
54
|
|
54
55
|
# Convert the array to an object id. This will only work for arrays of size
|
@@ -84,19 +85,19 @@ module BSON
|
|
84
85
|
|
85
86
|
# Deserialize the array from BSON.
|
86
87
|
#
|
87
|
-
# @param [
|
88
|
+
# @param [ ByteBuffer ] buffer The byte buffer.
|
88
89
|
#
|
89
90
|
# @return [ Array ] The decoded array.
|
90
91
|
#
|
91
92
|
# @see http://bsonspec.org/#/specification
|
92
93
|
#
|
93
94
|
# @since 2.0.0
|
94
|
-
def from_bson(
|
95
|
+
def from_bson(buffer)
|
95
96
|
array = new
|
96
|
-
|
97
|
-
while (type =
|
98
|
-
|
99
|
-
array << BSON::Registry.get(type).from_bson(
|
97
|
+
buffer.get_int32 # throw away the length
|
98
|
+
while (type = buffer.get_byte) != NULL_BYTE
|
99
|
+
buffer.get_cstring
|
100
|
+
array << BSON::Registry.get(type).from_bson(buffer)
|
100
101
|
end
|
101
102
|
array
|
102
103
|
end
|
data/lib/bson/binary.rb
CHANGED
@@ -21,7 +21,6 @@ module BSON
|
|
21
21
|
# @since 2.0.0
|
22
22
|
class Binary
|
23
23
|
include JSON
|
24
|
-
include Encodable
|
25
24
|
|
26
25
|
# A binary is type 0x05 in the BSON spec.
|
27
26
|
#
|
@@ -130,28 +129,29 @@ module BSON
|
|
130
129
|
# @see http://bsonspec.org/#/specification
|
131
130
|
#
|
132
131
|
# @since 2.0.0
|
133
|
-
def to_bson(
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
132
|
+
def to_bson(buffer = ByteBuffer.new)
|
133
|
+
position = buffer.length
|
134
|
+
buffer.put_int32(0)
|
135
|
+
buffer.put_byte(SUBTYPES.fetch(type))
|
136
|
+
buffer.put_int32(data.bytesize) if type == :old
|
137
|
+
buffer.put_bytes(data.force_encoding(BINARY))
|
138
|
+
buffer.replace_int32(position, buffer.length - position - 5)
|
139
139
|
end
|
140
140
|
|
141
141
|
# Deserialize the binary data from BSON.
|
142
142
|
#
|
143
|
-
# @param [
|
143
|
+
# @param [ ByteBuffer ] buffer The byte buffer.
|
144
144
|
#
|
145
145
|
# @return [ Binary ] The decoded binary data.
|
146
146
|
#
|
147
147
|
# @see http://bsonspec.org/#/specification
|
148
148
|
#
|
149
149
|
# @since 2.0.0
|
150
|
-
def self.from_bson(
|
151
|
-
length =
|
152
|
-
type = TYPES[
|
153
|
-
length =
|
154
|
-
data =
|
150
|
+
def self.from_bson(buffer)
|
151
|
+
length = buffer.get_int32
|
152
|
+
type = TYPES[buffer.get_byte]
|
153
|
+
length = buffer.get_int32 if type == :old
|
154
|
+
data = buffer.get_bytes(length)
|
155
155
|
new(data, type)
|
156
156
|
end
|
157
157
|
|
data/lib/bson/boolean.rb
CHANGED
@@ -29,15 +29,15 @@ module BSON
|
|
29
29
|
|
30
30
|
# Deserialize a boolean from BSON.
|
31
31
|
#
|
32
|
-
# @param [
|
32
|
+
# @param [ ByteBuffer ] buffer The byte buffer.
|
33
33
|
#
|
34
34
|
# @return [ TrueClass, FalseClass ] The decoded boolean.
|
35
35
|
#
|
36
36
|
# @see http://bsonspec.org/#/specification
|
37
37
|
#
|
38
38
|
# @since 2.0.0
|
39
|
-
def self.from_bson(
|
40
|
-
|
39
|
+
def self.from_bson(buffer)
|
40
|
+
buffer.get_byte == TrueClass::TRUE_BYTE
|
41
41
|
end
|
42
42
|
|
43
43
|
# Register this type when the module is loaded.
|
data/lib/bson/code.rb
CHANGED
@@ -21,7 +21,6 @@ module BSON
|
|
21
21
|
# @since 2.0.0
|
22
22
|
class Code
|
23
23
|
include JSON
|
24
|
-
include Encodable
|
25
24
|
|
26
25
|
# A code is type 0x0D in the BSON spec.
|
27
26
|
#
|
@@ -82,23 +81,21 @@ module BSON
|
|
82
81
|
# @see http://bsonspec.org/#/specification
|
83
82
|
#
|
84
83
|
# @since 2.0.0
|
85
|
-
def to_bson(
|
86
|
-
|
87
|
-
javascript.to_bson_string(encoded)
|
88
|
-
end
|
84
|
+
def to_bson(buffer = ByteBuffer.new)
|
85
|
+
buffer.put_string(javascript) # @todo: was formerly to_bson_string
|
89
86
|
end
|
90
87
|
|
91
88
|
# Deserialize code from BSON.
|
92
89
|
#
|
93
|
-
# @param [
|
90
|
+
# @param [ ByteBuffer ] buffer The byte buffer.
|
94
91
|
#
|
95
92
|
# @return [ TrueClass, FalseClass ] The decoded code.
|
96
93
|
#
|
97
94
|
# @see http://bsonspec.org/#/specification
|
98
95
|
#
|
99
96
|
# @since 2.0.0
|
100
|
-
def self.from_bson(
|
101
|
-
new(
|
97
|
+
def self.from_bson(buffer)
|
98
|
+
new(buffer.get_string)
|
102
99
|
end
|
103
100
|
|
104
101
|
# Register this type when the module is loaded.
|
data/lib/bson/code_with_scope.rb
CHANGED
@@ -21,7 +21,6 @@ module BSON
|
|
21
21
|
#
|
22
22
|
# @since 2.0.0
|
23
23
|
class CodeWithScope
|
24
|
-
include Encodable
|
25
24
|
include JSON
|
26
25
|
|
27
26
|
# A code with scope is type 0x0F in the BSON spec.
|
@@ -88,28 +87,26 @@ module BSON
|
|
88
87
|
# @see http://bsonspec.org/#/specification
|
89
88
|
#
|
90
89
|
# @since 2.0.0
|
91
|
-
def to_bson(
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
# an extra null byte has been added; we must remove it
|
98
|
-
out.chop!
|
90
|
+
def to_bson(buffer = ByteBuffer.new)
|
91
|
+
position = buffer.length
|
92
|
+
buffer.put_int32(0)
|
93
|
+
buffer.put_string(javascript)
|
94
|
+
scope.to_bson(buffer)
|
95
|
+
buffer.replace_int32(position, buffer.length - position)
|
99
96
|
end
|
100
97
|
|
101
98
|
# Deserialize a code with scope from BSON.
|
102
99
|
#
|
103
|
-
# @param [
|
100
|
+
# @param [ ByteBuffer ] buffer The byte buffer.
|
104
101
|
#
|
105
102
|
# @return [ TrueClass, FalseClass ] The decoded code with scope.
|
106
103
|
#
|
107
104
|
# @see http://bsonspec.org/#/specification
|
108
105
|
#
|
109
106
|
# @since 2.0.0
|
110
|
-
def self.from_bson(
|
111
|
-
|
112
|
-
new(
|
107
|
+
def self.from_bson(buffer)
|
108
|
+
buffer.get_int32 # Throw away the total length.
|
109
|
+
new(buffer.get_string, ::Hash.from_bson(buffer))
|
113
110
|
end
|
114
111
|
|
115
112
|
# Register this type when the module is loaded.
|
data/lib/bson/date.rb
CHANGED
@@ -34,8 +34,8 @@ module BSON
|
|
34
34
|
# @see http://bsonspec.org/#/specification
|
35
35
|
#
|
36
36
|
# @since 2.1.0
|
37
|
-
def to_bson(
|
38
|
-
::Time.utc(year, month, day).to_bson(
|
37
|
+
def to_bson(buffer = ByteBuffer.new)
|
38
|
+
::Time.utc(year, month, day).to_bson(buffer)
|
39
39
|
end
|
40
40
|
|
41
41
|
# Get the BSON type for the date.
|
data/lib/bson/date_time.rb
CHANGED
data/lib/bson/document.rb
CHANGED
@@ -68,6 +68,39 @@ module BSON
|
|
68
68
|
super(convert_key(key), convert_value(value))
|
69
69
|
end
|
70
70
|
|
71
|
+
# Returns true if the given key is present in the document. Will normalize
|
72
|
+
# symbol keys into strings.
|
73
|
+
#
|
74
|
+
# @example Test if a key exists using a symbol
|
75
|
+
# document.has_key?(:test)
|
76
|
+
#
|
77
|
+
# @return [ true, false]
|
78
|
+
#
|
79
|
+
# @since 3.2.7
|
80
|
+
def has_key?(key)
|
81
|
+
super(convert_key(key))
|
82
|
+
end
|
83
|
+
|
84
|
+
alias :include? :has_key?
|
85
|
+
alias :key? :has_key?
|
86
|
+
alias :member? :has_key?
|
87
|
+
|
88
|
+
|
89
|
+
# Returns true if the given value is present in the document. Will normalize
|
90
|
+
# symbols into strings.
|
91
|
+
#
|
92
|
+
# @example Test if a key exists using a symbol
|
93
|
+
# document.has_value?(:test)
|
94
|
+
#
|
95
|
+
# @return [ true, false]
|
96
|
+
#
|
97
|
+
# @since 3.2.7
|
98
|
+
def has_value?(value)
|
99
|
+
super(convert_value(value))
|
100
|
+
end
|
101
|
+
|
102
|
+
alias :value :has_value?
|
103
|
+
|
71
104
|
# Instantiate a new Document. Valid parameters for instantiation is a hash
|
72
105
|
# only or nothing.
|
73
106
|
#
|
data/lib/bson/false_class.rb
CHANGED
data/lib/bson/float.rb
CHANGED
@@ -42,29 +42,23 @@ module BSON
|
|
42
42
|
# @see http://bsonspec.org/#/specification
|
43
43
|
#
|
44
44
|
# @since 2.0.0
|
45
|
-
def to_bson(
|
46
|
-
|
45
|
+
def to_bson(buffer = ByteBuffer.new)
|
46
|
+
buffer.put_double(self)
|
47
47
|
end
|
48
48
|
|
49
49
|
module ClassMethods
|
50
50
|
|
51
51
|
# Deserialize an instance of a Float from a BSON double.
|
52
52
|
#
|
53
|
-
# @param [
|
53
|
+
# @param [ ByteBuffer ] buffer The byte buffer.
|
54
54
|
#
|
55
55
|
# @return [ Float ] The decoded Float.
|
56
56
|
#
|
57
57
|
# @see http://bsonspec.org/#/specification
|
58
58
|
#
|
59
59
|
# @since 2.0.0
|
60
|
-
def from_bson(
|
61
|
-
|
62
|
-
end
|
63
|
-
|
64
|
-
private
|
65
|
-
|
66
|
-
def from_bson_double(double)
|
67
|
-
double.unpack(PACK).first
|
60
|
+
def from_bson(buffer)
|
61
|
+
buffer.get_double
|
68
62
|
end
|
69
63
|
end
|
70
64
|
|
data/lib/bson/hash.rb
CHANGED
@@ -21,7 +21,6 @@ module BSON
|
|
21
21
|
#
|
22
22
|
# @since 2.0.0
|
23
23
|
module Hash
|
24
|
-
include Encodable
|
25
24
|
|
26
25
|
# An hash (embedded document) is type 0x03 in the BSON spec.
|
27
26
|
#
|
@@ -38,14 +37,16 @@ module BSON
|
|
38
37
|
# @see http://bsonspec.org/#/specification
|
39
38
|
#
|
40
39
|
# @since 2.0.0
|
41
|
-
def to_bson(
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
40
|
+
def to_bson(buffer = ByteBuffer.new)
|
41
|
+
position = buffer.length
|
42
|
+
buffer.put_int32(0)
|
43
|
+
each do |field, value|
|
44
|
+
buffer.put_byte(value.bson_type)
|
45
|
+
buffer.put_cstring(field.to_bson_key)
|
46
|
+
value.to_bson(buffer)
|
48
47
|
end
|
48
|
+
buffer.put_byte(NULL_BYTE)
|
49
|
+
buffer.replace_int32(position, buffer.length - position)
|
49
50
|
end
|
50
51
|
|
51
52
|
# Converts the hash to a normalized value in a BSON document.
|
@@ -64,19 +65,19 @@ module BSON
|
|
64
65
|
|
65
66
|
# Deserialize the hash from BSON.
|
66
67
|
#
|
67
|
-
# @param [
|
68
|
+
# @param [ ByteBuffer ] buffer The byte buffer.
|
68
69
|
#
|
69
70
|
# @return [ Array ] The decoded hash.
|
70
71
|
#
|
71
72
|
# @see http://bsonspec.org/#/specification
|
72
73
|
#
|
73
74
|
# @since 2.0.0
|
74
|
-
def from_bson(
|
75
|
+
def from_bson(buffer)
|
75
76
|
hash = Document.allocate
|
76
|
-
|
77
|
-
while (type =
|
78
|
-
field =
|
79
|
-
hash.store(field, BSON::Registry.get(type).from_bson(
|
77
|
+
buffer.get_int32 # Throw away the size - todo: just move read position?
|
78
|
+
while (type = buffer.get_byte) != NULL_BYTE
|
79
|
+
field = buffer.get_cstring
|
80
|
+
hash.store(field, BSON::Registry.get(type).from_bson(buffer))
|
80
81
|
end
|
81
82
|
hash
|
82
83
|
end
|
data/lib/bson/int32.rb
CHANGED
@@ -27,6 +27,11 @@ module BSON
|
|
27
27
|
# @since 2.0.0
|
28
28
|
BSON_TYPE = 16.chr.force_encoding(BINARY).freeze
|
29
29
|
|
30
|
+
# The number of bytes constant.
|
31
|
+
#
|
32
|
+
# @since 4.0.0
|
33
|
+
BYTES_LENGTH = 4
|
34
|
+
|
30
35
|
# Constant for the int 32 pack directive.
|
31
36
|
#
|
32
37
|
# @since 2.0.0
|
@@ -34,21 +39,15 @@ module BSON
|
|
34
39
|
|
35
40
|
# Deserialize an Integer from BSON.
|
36
41
|
#
|
37
|
-
# @param [
|
42
|
+
# @param [ ByteBuffer ] buffer The byte buffer.
|
38
43
|
#
|
39
44
|
# @return [ Integer ] The decoded Integer.
|
40
45
|
#
|
41
46
|
# @see http://bsonspec.org/#/specification
|
42
47
|
#
|
43
48
|
# @since 2.0.0
|
44
|
-
def self.from_bson(
|
45
|
-
|
46
|
-
end
|
47
|
-
|
48
|
-
private
|
49
|
-
|
50
|
-
def self.from_bson_int32(bytes)
|
51
|
-
bytes.unpack(PACK).first
|
49
|
+
def self.from_bson(buffer)
|
50
|
+
buffer.get_int32
|
52
51
|
end
|
53
52
|
|
54
53
|
# Register this type when the module is loaded.
|
data/lib/bson/int64.rb
CHANGED
@@ -34,21 +34,15 @@ module BSON
|
|
34
34
|
|
35
35
|
# Deserialize an Integer from BSON.
|
36
36
|
#
|
37
|
-
# @param [
|
37
|
+
# @param [ ByteBuffer ] buffer The byte buffer.
|
38
38
|
#
|
39
39
|
# @return [ Integer ] The decoded Integer.
|
40
40
|
#
|
41
41
|
# @see http://bsonspec.org/#/specification
|
42
42
|
#
|
43
43
|
# @since 2.0.0
|
44
|
-
def self.from_bson(
|
45
|
-
|
46
|
-
end
|
47
|
-
|
48
|
-
private
|
49
|
-
|
50
|
-
def self.from_bson_int64(bytes)
|
51
|
-
bytes.unpack(PACK).first
|
44
|
+
def self.from_bson(buffer)
|
45
|
+
buffer.get_int64
|
52
46
|
end
|
53
47
|
|
54
48
|
# Register this type when the module is loaded.
|