bson 1.12.5 → 2.0.0.alpha
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of bson might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +0 -0
- data/CONTRIBUTING.md +0 -0
- data/LICENSE.md +13 -0
- data/README.md +185 -0
- data/Rakefile +83 -0
- data/ext/bson/extconf.rb +3 -0
- data/lib/bson.rb +105 -94
- data/lib/bson/array.rb +74 -0
- data/lib/bson/binary.rb +125 -0
- data/lib/bson/boolean.rb +35 -0
- data/lib/bson/code.rb +96 -0
- data/lib/bson/code_with_scope.rb +105 -0
- data/lib/bson/document.rb +536 -0
- data/lib/bson/encodable.rb +73 -0
- data/lib/bson/false_class.rb +48 -0
- data/lib/bson/float.rb +69 -0
- data/lib/bson/hash.rb +71 -0
- data/lib/bson/int32.rb +46 -0
- data/lib/bson/int64.rb +46 -0
- data/lib/bson/integer.rb +172 -0
- data/lib/bson/json.rb +26 -0
- data/lib/bson/max_key.rb +57 -0
- data/lib/bson/min_key.rb +57 -0
- data/lib/bson/nil_class.rb +57 -0
- data/lib/bson/object_id.rb +309 -0
- data/lib/bson/regexp.rb +111 -0
- data/lib/bson/registry.rb +57 -0
- data/lib/bson/specialized.rb +61 -0
- data/lib/bson/string.rb +173 -0
- data/lib/bson/symbol.rb +74 -0
- data/lib/bson/time.rb +59 -0
- data/lib/bson/timestamp.rb +100 -0
- data/lib/bson/true_class.rb +48 -0
- data/lib/bson/undefined.rb +61 -0
- data/lib/bson/version.rb +4 -0
- metadata +52 -40
- data/LICENSE +0 -190
- data/VERSION +0 -1
- data/bin/b2json +0 -63
- data/bin/j2bson +0 -64
- data/bson.gemspec +0 -34
- data/lib/bson/bson_c.rb +0 -37
- data/lib/bson/bson_java.rb +0 -49
- data/lib/bson/bson_ruby.rb +0 -645
- data/lib/bson/byte_buffer.rb +0 -241
- data/lib/bson/exceptions.rb +0 -37
- data/lib/bson/grow.rb +0 -173
- data/lib/bson/ordered_hash.rb +0 -197
- data/lib/bson/support/hash_with_indifferent_access.rb +0 -174
- data/lib/bson/types/binary.rb +0 -52
- data/lib/bson/types/code.rb +0 -55
- data/lib/bson/types/dbref.rb +0 -40
- data/lib/bson/types/min_max_keys.rb +0 -56
- data/lib/bson/types/object_id.rb +0 -226
- data/lib/bson/types/regex.rb +0 -116
- data/lib/bson/types/timestamp.rb +0 -72
data/lib/bson/json.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "json"
|
3
|
+
|
4
|
+
module BSON
|
5
|
+
|
6
|
+
# Provides common behaviour for JSON serialization of objects.
|
7
|
+
#
|
8
|
+
# @since 2.0.0
|
9
|
+
module JSON
|
10
|
+
|
11
|
+
# Converting an object to JSON simply gets it's hash representation via
|
12
|
+
# as_json, then converts it to a string.
|
13
|
+
#
|
14
|
+
# @example Convert the object to JSON
|
15
|
+
# object.to_json
|
16
|
+
#
|
17
|
+
# @note All types must implement as_json.
|
18
|
+
#
|
19
|
+
# @return [ String ] The object as JSON.
|
20
|
+
#
|
21
|
+
# @since 2.0.0
|
22
|
+
def to_json(*args)
|
23
|
+
as_json.to_json(*args)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/bson/max_key.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module BSON
|
3
|
+
|
4
|
+
# Represents a $maxKey type, which compares less than any other value in the
|
5
|
+
# specification.
|
6
|
+
#
|
7
|
+
# @see http://bsonspec.org/#/specification
|
8
|
+
#
|
9
|
+
# @since 2.0.0
|
10
|
+
class MaxKey
|
11
|
+
include Comparable
|
12
|
+
include JSON
|
13
|
+
include Specialized
|
14
|
+
|
15
|
+
# A $maxKey is type 0x7F in the BSON spec.
|
16
|
+
#
|
17
|
+
# @since 2.0.0
|
18
|
+
BSON_TYPE = 127.chr.force_encoding(BINARY).freeze
|
19
|
+
|
20
|
+
# Constant for always evaluating greater in a comparison.
|
21
|
+
#
|
22
|
+
# @since 2.0.0
|
23
|
+
GREATER = 1.freeze
|
24
|
+
|
25
|
+
# When comparing a max key with any other object, the max key will always
|
26
|
+
# be greater.
|
27
|
+
#
|
28
|
+
# @example Compare with another object.
|
29
|
+
# max_key <=> 1000
|
30
|
+
#
|
31
|
+
# @param [ Object ] The object to compare against.
|
32
|
+
#
|
33
|
+
# @return [ Integer ] Always 1.
|
34
|
+
#
|
35
|
+
# @since 2.0.0
|
36
|
+
def <=>(other)
|
37
|
+
GREATER
|
38
|
+
end
|
39
|
+
|
40
|
+
# Get the max key as JSON hash data.
|
41
|
+
#
|
42
|
+
# @example Get the max key as a JSON hash.
|
43
|
+
# max_key.as_json
|
44
|
+
#
|
45
|
+
# @return [ Hash ] The max key as a JSON hash.
|
46
|
+
#
|
47
|
+
# @since 2.0.0
|
48
|
+
def as_json(*args)
|
49
|
+
{ "$maxKey" => 1 }
|
50
|
+
end
|
51
|
+
|
52
|
+
# Register this type when the module is loaded.
|
53
|
+
#
|
54
|
+
# @since 2.0.0
|
55
|
+
Registry.register(BSON_TYPE, self)
|
56
|
+
end
|
57
|
+
end
|
data/lib/bson/min_key.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module BSON
|
3
|
+
|
4
|
+
# Represents a $minKey type, which compares less than any other value in the
|
5
|
+
# specification.
|
6
|
+
#
|
7
|
+
# @see http://bsonspec.org/#/specification
|
8
|
+
#
|
9
|
+
# @since 2.0.0
|
10
|
+
class MinKey
|
11
|
+
include Comparable
|
12
|
+
include JSON
|
13
|
+
include Specialized
|
14
|
+
|
15
|
+
# A $minKey is type 0xFF in the BSON spec.
|
16
|
+
#
|
17
|
+
# @since 2.0.0
|
18
|
+
BSON_TYPE = 255.chr.force_encoding(BINARY).freeze
|
19
|
+
|
20
|
+
# Constant for always evaluating lesser in a comparison.
|
21
|
+
#
|
22
|
+
# @since 2.0.0
|
23
|
+
LESSER = -1.freeze
|
24
|
+
|
25
|
+
# When comparing a min key with any other object, the min key will always
|
26
|
+
# be lesser.
|
27
|
+
#
|
28
|
+
# @example Compare with another object.
|
29
|
+
# min_key <=> 1000
|
30
|
+
#
|
31
|
+
# @param [ Object ] The object to compare against.
|
32
|
+
#
|
33
|
+
# @return [ Integer ] Always -1.
|
34
|
+
#
|
35
|
+
# @since 2.0.0
|
36
|
+
def <=>(other)
|
37
|
+
LESSER
|
38
|
+
end
|
39
|
+
|
40
|
+
# Get the min key as JSON hash data.
|
41
|
+
#
|
42
|
+
# @example Get the min key as a JSON hash.
|
43
|
+
# min_key.as_json
|
44
|
+
#
|
45
|
+
# @return [ Hash ] The min key as a JSON hash.
|
46
|
+
#
|
47
|
+
# @since 2.0.0
|
48
|
+
def as_json(*args)
|
49
|
+
{ "$minKey" => 1 }
|
50
|
+
end
|
51
|
+
|
52
|
+
# Register this type when the module is loaded.
|
53
|
+
#
|
54
|
+
# @since 2.0.0
|
55
|
+
Registry.register(BSON_TYPE, self)
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module BSON
|
3
|
+
|
4
|
+
# Injects behaviour for encoding and decoding nil values to and from
|
5
|
+
# raw bytes as specified by the BSON spec.
|
6
|
+
#
|
7
|
+
# @see http://bsonspec.org/#/specification
|
8
|
+
#
|
9
|
+
# @since 2.0.0
|
10
|
+
module NilClass
|
11
|
+
|
12
|
+
# A nil is type 0x0A in the BSON spec.
|
13
|
+
#
|
14
|
+
# @since 2.0.0
|
15
|
+
BSON_TYPE = 10.chr.force_encoding(BINARY).freeze
|
16
|
+
|
17
|
+
# Get the nil as encoded BSON.
|
18
|
+
#
|
19
|
+
# @example Get the nil as encoded BSON.
|
20
|
+
# nil.to_bson
|
21
|
+
#
|
22
|
+
# @return [ String ] An empty string.
|
23
|
+
#
|
24
|
+
# @see http://bsonspec.org/#/specification
|
25
|
+
#
|
26
|
+
# @since 2.0.0
|
27
|
+
def to_bson(encoded = ''.force_encoding(BINARY))
|
28
|
+
encoded
|
29
|
+
end
|
30
|
+
|
31
|
+
module ClassMethods
|
32
|
+
# Deserialize NilClass from BSON.
|
33
|
+
#
|
34
|
+
# @param [ BSON ] bson The encoded Null value.
|
35
|
+
#
|
36
|
+
# @return [ nil ] The decoded nil value.
|
37
|
+
#
|
38
|
+
# @see http://bsonspec.org/#/specification
|
39
|
+
#
|
40
|
+
# @since 2.0.0
|
41
|
+
def from_bson(bson)
|
42
|
+
nil
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Register this type when the module is loaded.
|
47
|
+
#
|
48
|
+
# @since 2.0.0
|
49
|
+
Registry.register(BSON_TYPE, ::NilClass)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Enrich the core NilClass class with this module.
|
53
|
+
#
|
54
|
+
# @since 2.0.0
|
55
|
+
::NilClass.send(:include, NilClass)
|
56
|
+
::NilClass.send(:extend, NilClass::ClassMethods)
|
57
|
+
end
|
@@ -0,0 +1,309 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "digest/md5"
|
3
|
+
require "socket"
|
4
|
+
require "thread"
|
5
|
+
|
6
|
+
module BSON
|
7
|
+
|
8
|
+
# Represents object_id data.
|
9
|
+
#
|
10
|
+
# @see http://bsonspec.org/#/specification
|
11
|
+
#
|
12
|
+
# @since 2.0.0
|
13
|
+
class ObjectId
|
14
|
+
include Comparable
|
15
|
+
include JSON
|
16
|
+
|
17
|
+
# A object_id is type 0x07 in the BSON spec.
|
18
|
+
#
|
19
|
+
# @since 2.0.0
|
20
|
+
BSON_TYPE = 7.chr.force_encoding(BINARY).freeze
|
21
|
+
|
22
|
+
# Check equality of the object id with another object.
|
23
|
+
#
|
24
|
+
# @example Check if the object id is equal to the other.
|
25
|
+
# object_id == other
|
26
|
+
#
|
27
|
+
# @param [ Object ] other The object to check against.
|
28
|
+
#
|
29
|
+
# @return [ true, false ] If the objects are equal.
|
30
|
+
#
|
31
|
+
# @since 2.0.0
|
32
|
+
def ==(other)
|
33
|
+
return false unless other.is_a?(ObjectId)
|
34
|
+
to_bson == other.to_bson
|
35
|
+
end
|
36
|
+
|
37
|
+
# Check case equality on the object id.
|
38
|
+
#
|
39
|
+
# @example Check case equality.
|
40
|
+
# object_id === other
|
41
|
+
#
|
42
|
+
# @param [ Object ] other The object to check against.
|
43
|
+
#
|
44
|
+
# @return [ true, false ] If the objects are equal in a case.
|
45
|
+
#
|
46
|
+
# @since 2.0.0
|
47
|
+
def ===(other)
|
48
|
+
return to_str === other.to_str if other.respond_to?(:to_str)
|
49
|
+
super
|
50
|
+
end
|
51
|
+
|
52
|
+
# Return the object id as a JSON hash representation.
|
53
|
+
#
|
54
|
+
# @example Get the object id as JSON.
|
55
|
+
# object_id.as_json
|
56
|
+
#
|
57
|
+
# @return [ Hash ] The object id as a JSON hash.
|
58
|
+
#
|
59
|
+
# @since 2.0.0
|
60
|
+
def as_json(*args)
|
61
|
+
{ "$oid" => to_s }
|
62
|
+
end
|
63
|
+
|
64
|
+
# Compare this object id with another object for use in sorting.
|
65
|
+
#
|
66
|
+
# @example Compare the object id with the other object.
|
67
|
+
# object <=> other
|
68
|
+
#
|
69
|
+
# @param [ Object ] other The object to compare to.
|
70
|
+
#
|
71
|
+
# @return [ Integer ] The result of the comparison.
|
72
|
+
#
|
73
|
+
# @since 2.0.0
|
74
|
+
def <=>(other)
|
75
|
+
to_bson <=> other.to_bson
|
76
|
+
end
|
77
|
+
|
78
|
+
# Return the UTC time at which this ObjectId was generated. This may
|
79
|
+
# be used instread of a created_at timestamp since this information
|
80
|
+
# is always encoded in the object id.
|
81
|
+
#
|
82
|
+
# @example Get the generation time.
|
83
|
+
# object_id.generation_time
|
84
|
+
#
|
85
|
+
# @return [ Time ] The time the id was generated.
|
86
|
+
#
|
87
|
+
# @since 2.0.0
|
88
|
+
def generation_time
|
89
|
+
::Time.at(to_bson.unpack("N")[0]).utc
|
90
|
+
end
|
91
|
+
|
92
|
+
# Get the object id as it's raw BSON data.
|
93
|
+
#
|
94
|
+
# @example Get the raw bson bytes.
|
95
|
+
# object_id.to_bson
|
96
|
+
#
|
97
|
+
# @note Since Moped's BSON and 10gen BSON before 2.0.0 have different
|
98
|
+
# internal representations, we will attempt to repair the data for cases
|
99
|
+
# where the object was instantiated in a non-standard way. (Like a
|
100
|
+
# Marshal.load)
|
101
|
+
#
|
102
|
+
# @return [ String ] The raw bytes.
|
103
|
+
#
|
104
|
+
# @see http://bsonspec.org/#/specification
|
105
|
+
#
|
106
|
+
# @since 2.0.0
|
107
|
+
def to_bson(encoded = ''.force_encoding(BINARY))
|
108
|
+
repair!(@data) if defined?(@data)
|
109
|
+
@raw_data ||= @@generator.next
|
110
|
+
encoded << @raw_data
|
111
|
+
end
|
112
|
+
|
113
|
+
# Get the string representation of the object id.
|
114
|
+
#
|
115
|
+
# @example Get the object id as a string.
|
116
|
+
# object_id.to_s
|
117
|
+
#
|
118
|
+
# @return [ String ] The object id as a string.
|
119
|
+
#
|
120
|
+
# @since 2.0.0
|
121
|
+
def to_s
|
122
|
+
to_bson.to_hex_string.force_encoding(UTF8)
|
123
|
+
end
|
124
|
+
alias :to_str :to_s
|
125
|
+
|
126
|
+
# Raised when trying to create an object id with invalid data.
|
127
|
+
#
|
128
|
+
# @since 2.0.0
|
129
|
+
class Invalid < RuntimeError; end
|
130
|
+
|
131
|
+
private
|
132
|
+
|
133
|
+
def data=(data)
|
134
|
+
@raw_data = data
|
135
|
+
end
|
136
|
+
|
137
|
+
class << self
|
138
|
+
|
139
|
+
# Deserialize the object id from raw BSON bytes.
|
140
|
+
#
|
141
|
+
# @example Get the object id from BSON.
|
142
|
+
# ObjectId.from_bson(bson)
|
143
|
+
#
|
144
|
+
# @param [ String ] bson The raw BSON bytes.
|
145
|
+
#
|
146
|
+
# @return [ BSON::ObjectId ] The object id.
|
147
|
+
#
|
148
|
+
# @since 2.0.0
|
149
|
+
def from_bson(bson)
|
150
|
+
from_data(bson.read(12))
|
151
|
+
end
|
152
|
+
|
153
|
+
# Create a new object id from raw bytes.
|
154
|
+
#
|
155
|
+
# @example Create an object id from raw bytes.
|
156
|
+
# BSON::ObjectId.from_data(data)
|
157
|
+
#
|
158
|
+
# @param [ String ] data The raw bytes.
|
159
|
+
#
|
160
|
+
# @return [ ObjectId ] The new object id.
|
161
|
+
#
|
162
|
+
# @since 2.0.0
|
163
|
+
def from_data(data)
|
164
|
+
object_id = allocate
|
165
|
+
object_id.send(:data=, data)
|
166
|
+
object_id
|
167
|
+
end
|
168
|
+
|
169
|
+
# Create a new object id from a string.
|
170
|
+
#
|
171
|
+
# @example Create an object id from the string.
|
172
|
+
# BSON::ObjectId.from_string(id)
|
173
|
+
#
|
174
|
+
# @param [ String ] string The string to create the id from.
|
175
|
+
#
|
176
|
+
# @raise [ BSON::ObjectId::Invalid ] If the provided string is invalid.
|
177
|
+
#
|
178
|
+
# @return [ BSON::ObjectId ] The new object id.
|
179
|
+
#
|
180
|
+
# @since 2.0.0
|
181
|
+
def from_string(string)
|
182
|
+
unless legal?(string)
|
183
|
+
raise Invalid.new("'#{string}' is an invalid ObjectId.")
|
184
|
+
end
|
185
|
+
from_data([ string ].pack("H*"))
|
186
|
+
end
|
187
|
+
|
188
|
+
# Create a new object id from a time.
|
189
|
+
#
|
190
|
+
# @example Create an object id from a time.
|
191
|
+
# BSON::ObjectId.from_id(time)
|
192
|
+
#
|
193
|
+
# @example Create an object id from a time, ensuring uniqueness.
|
194
|
+
# BSON::ObjectId.from_id(time, unique: true)
|
195
|
+
#
|
196
|
+
# @param [ Time ] time The time to generate from.
|
197
|
+
# @param [ Hash ] options The options.
|
198
|
+
#
|
199
|
+
# @option options [ true, false ] :unique Whether the id should be
|
200
|
+
# unique.
|
201
|
+
#
|
202
|
+
# @return [ ObjectId ] The new object id.
|
203
|
+
#
|
204
|
+
# @since 2.0.0
|
205
|
+
def from_time(time, options = {})
|
206
|
+
from_data(options[:unique] ? @@generator.next(time.to_i) : [ time.to_i ].pack("Nx8"))
|
207
|
+
end
|
208
|
+
|
209
|
+
# Determine if the provided string is a legal object id.
|
210
|
+
#
|
211
|
+
# @example Is the string a legal object id?
|
212
|
+
# BSON::ObjectId.legal?(string)
|
213
|
+
#
|
214
|
+
# @param [ String ] The string to check.
|
215
|
+
#
|
216
|
+
# @return [ true, false ] If the string is legal.
|
217
|
+
#
|
218
|
+
# @since 2.0.0
|
219
|
+
def legal?(string)
|
220
|
+
string.to_s =~ /^[0-9a-f]{24}$/i ? true : false
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
# Inner class that encapsulates the behaviour of actually generating each
|
225
|
+
# part of the ObjectId.
|
226
|
+
#
|
227
|
+
# @api private
|
228
|
+
#
|
229
|
+
# @since 2.0.0
|
230
|
+
class Generator
|
231
|
+
|
232
|
+
# @!attribute machine_id
|
233
|
+
# @return [ String ] The unique machine id.
|
234
|
+
# @since 2.0.0
|
235
|
+
attr_reader :machine_id
|
236
|
+
|
237
|
+
# Instantiate the new object id generator. Will set the machine id once
|
238
|
+
# on the initial instantiation.
|
239
|
+
#
|
240
|
+
# @example Instantiate the generator.
|
241
|
+
# BSON::ObjectId::Generator.new
|
242
|
+
#
|
243
|
+
# @since 2.0.0
|
244
|
+
def initialize
|
245
|
+
@counter = 0
|
246
|
+
@machine_id = Digest::MD5.digest(Socket.gethostname).unpack("N")[0]
|
247
|
+
@mutex = Mutex.new
|
248
|
+
end
|
249
|
+
|
250
|
+
# Return object id data based on the current time, incrementing the
|
251
|
+
# object id counter. Will use the provided time if not nil.
|
252
|
+
#
|
253
|
+
# @example Get the next object id data.
|
254
|
+
# generator.next
|
255
|
+
#
|
256
|
+
# @param [ Time ] time The optional time to generate with.
|
257
|
+
#
|
258
|
+
# @return [ String ] The raw object id bytes.
|
259
|
+
#
|
260
|
+
# @since 2.0.0
|
261
|
+
def next(time = nil)
|
262
|
+
@mutex.lock
|
263
|
+
begin
|
264
|
+
count = @counter = (@counter + 1) % 0xFFFFFF
|
265
|
+
ensure
|
266
|
+
@mutex.unlock rescue nil
|
267
|
+
end
|
268
|
+
generate(time || ::Time.new.to_i, count)
|
269
|
+
end
|
270
|
+
|
271
|
+
# Generate object id data for a given time using the provided counter.
|
272
|
+
#
|
273
|
+
# @example Generate the object id bytes.
|
274
|
+
# generator.generate(time)
|
275
|
+
#
|
276
|
+
# @param [ Integer ] time The time since epoch in seconds.
|
277
|
+
# @param [ Integer ] counter The optional counter.
|
278
|
+
#
|
279
|
+
# @return [ String ] The raw object id bytes.
|
280
|
+
#
|
281
|
+
# @since 2.0.0
|
282
|
+
def generate(time, counter = 0)
|
283
|
+
[ time, machine_id, process_id, counter << 8 ].pack("N NX lXX NX")
|
284
|
+
end
|
285
|
+
|
286
|
+
private
|
287
|
+
|
288
|
+
if jruby?
|
289
|
+
def process_id
|
290
|
+
"#{Process.pid}#{Thread.current.object_id}".hash % 0xFFFF
|
291
|
+
end
|
292
|
+
else
|
293
|
+
def process_id
|
294
|
+
Process.pid % 0xFFFF
|
295
|
+
end
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
299
|
+
# We keep one global generator for object ids.
|
300
|
+
#
|
301
|
+
# @since 2.0.0
|
302
|
+
@@generator = Generator.new
|
303
|
+
|
304
|
+
# Register this type when the module is loaded.
|
305
|
+
#
|
306
|
+
# @since 2.0.0
|
307
|
+
Registry.register(BSON_TYPE, self)
|
308
|
+
end
|
309
|
+
end
|