bson 1.7.0-java
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.
- data/LICENSE.txt +190 -0
- data/bin/b2json +65 -0
- data/bin/j2bson +72 -0
- data/ext/java/jar/jbson.jar +0 -0
- data/ext/java/jar/mongo-2.6.5.jar +0 -0
- data/lib/bson.rb +101 -0
- data/lib/bson/bson_c.rb +41 -0
- data/lib/bson/bson_java.rb +30 -0
- data/lib/bson/bson_ruby.rb +641 -0
- data/lib/bson/byte_buffer.rb +280 -0
- data/lib/bson/exceptions.rb +41 -0
- data/lib/bson/ordered_hash.rb +187 -0
- data/lib/bson/types/binary.rb +56 -0
- data/lib/bson/types/code.rb +59 -0
- data/lib/bson/types/dbref.rb +46 -0
- data/lib/bson/types/min_max_keys.rb +60 -0
- data/lib/bson/types/object_id.rb +230 -0
- data/lib/bson/types/timestamp.rb +76 -0
- data/lib/bson/version.rb +3 -0
- data/test/bson/binary_test.rb +13 -0
- data/test/bson/bson_test.rb +667 -0
- data/test/bson/byte_buffer_test.rb +208 -0
- data/test/bson/hash_with_indifferent_access_test.rb +38 -0
- data/test/bson/json_test.rb +17 -0
- data/test/bson/object_id_test.rb +148 -0
- data/test/bson/ordered_hash_test.rb +247 -0
- data/test/bson/test_helper.rb +30 -0
- data/test/bson/timestamp_test.rb +46 -0
- metadata +91 -0
@@ -0,0 +1,56 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
# --
|
4
|
+
# Copyright (C) 2008-2011 10gen Inc.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
# ++
|
18
|
+
|
19
|
+
require 'bson/byte_buffer'
|
20
|
+
|
21
|
+
module BSON
|
22
|
+
|
23
|
+
# An array of binary bytes with a MongoDB subtype. See the subtype
|
24
|
+
# constants for reference.
|
25
|
+
#
|
26
|
+
# Use this class when storing binary data in documents.
|
27
|
+
class Binary < ByteBuffer
|
28
|
+
|
29
|
+
SUBTYPE_SIMPLE = 0x00
|
30
|
+
SUBTYPE_BYTES = 0x02
|
31
|
+
SUBTYPE_UUID = 0x03
|
32
|
+
SUBTYPE_MD5 = 0x05
|
33
|
+
SUBTYPE_USER_DEFINED = 0x80
|
34
|
+
|
35
|
+
# One of the SUBTYPE_* constants. Default is SUBTYPE_BYTES.
|
36
|
+
attr_accessor :subtype
|
37
|
+
|
38
|
+
# Create a buffer for storing binary data in MongoDB.
|
39
|
+
#
|
40
|
+
# @param [Array, String] data to story as BSON binary. If a string is given, the on
|
41
|
+
# Ruby 1.9 it will be forced to the binary encoding.
|
42
|
+
# @param [Fixnum] one of four values specifying a BSON binary subtype. Possible values are
|
43
|
+
# SUBTYPE_BYTES, SUBTYPE_UUID, SUBTYPE_MD5, and SUBTYPE_USER_DEFINED.
|
44
|
+
#
|
45
|
+
# @see http://www.mongodb.org/display/DOCS/BSON#BSON-noteondatabinary BSON binary subtypes.
|
46
|
+
def initialize(data=[], subtype=SUBTYPE_SIMPLE)
|
47
|
+
super(data)
|
48
|
+
@subtype = subtype
|
49
|
+
end
|
50
|
+
|
51
|
+
def inspect
|
52
|
+
"<BSON::Binary:#{object_id}>"
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
# --
|
4
|
+
# Copyright (C) 2008-2011 10gen Inc.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
# ++
|
18
|
+
|
19
|
+
module BSON
|
20
|
+
|
21
|
+
# JavaScript code to be evaluated by MongoDB.
|
22
|
+
class Code
|
23
|
+
|
24
|
+
# Hash mapping identifiers to their values
|
25
|
+
attr_accessor :scope, :code
|
26
|
+
|
27
|
+
# Wrap code to be evaluated by MongoDB.
|
28
|
+
#
|
29
|
+
# @param [String] code the JavaScript code.
|
30
|
+
# @param [Hash] a document mapping identifiers to values, which
|
31
|
+
# represent the scope in which the code is to be executed.
|
32
|
+
def initialize(code, scope={})
|
33
|
+
@code = code
|
34
|
+
@scope = scope
|
35
|
+
|
36
|
+
unless @code.is_a?(String)
|
37
|
+
raise ArgumentError, "BSON::Code must be in the form of a String; #{@code.class} is not allowed."
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def length
|
42
|
+
@code.length
|
43
|
+
end
|
44
|
+
|
45
|
+
def ==(other)
|
46
|
+
self.class == other.class &&
|
47
|
+
@code == other.code && @scope == other.scope
|
48
|
+
end
|
49
|
+
|
50
|
+
def inspect
|
51
|
+
"<BSON::Code:#{object_id} @data=\"#{@code}\" @scope=\"#{@scope.inspect}\">"
|
52
|
+
end
|
53
|
+
|
54
|
+
def to_bson_code
|
55
|
+
self
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
# --
|
4
|
+
# Copyright (C) 2008-2011 10gen Inc.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
# ++
|
18
|
+
|
19
|
+
module BSON
|
20
|
+
|
21
|
+
# A reference to another object in a MongoDB database.
|
22
|
+
class DBRef
|
23
|
+
|
24
|
+
attr_reader :namespace, :object_id
|
25
|
+
|
26
|
+
# Create a DBRef. Use this class in conjunction with DB#dereference.
|
27
|
+
#
|
28
|
+
# @param [String] a collection name
|
29
|
+
# @param [ObjectId] an object id
|
30
|
+
#
|
31
|
+
# @core dbrefs constructor_details
|
32
|
+
def initialize(namespace, object_id)
|
33
|
+
@namespace = namespace
|
34
|
+
@object_id = object_id
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_s
|
38
|
+
"ns: #{namespace}, id: #{object_id}"
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_hash
|
42
|
+
{"$ns" => @namespace, "$id" => @object_id }
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
# --
|
4
|
+
# Copyright (C) 2008-2011 10gen Inc.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
# ++
|
18
|
+
|
19
|
+
module BSON
|
20
|
+
|
21
|
+
# A class representing the BSON MaxKey type. MaxKey will always compare greater than
|
22
|
+
# all other BSON types and values.
|
23
|
+
#
|
24
|
+
# @example Sorting (assume @numbers is a collection):
|
25
|
+
#
|
26
|
+
# >> @numbers.save({"n" => Mongo::MaxKey.new})
|
27
|
+
# >> @numbers.save({"n" => 0})
|
28
|
+
# >> @numbers.save({"n" => 5_000_000})
|
29
|
+
# >> @numbers.find.sort("n").to_a
|
30
|
+
# => [{"_id"=>4b5a050c238d3bace2000004, "n"=>0},
|
31
|
+
# {"_id"=>4b5a04e6238d3bace2000002, "n"=>5_000_000},
|
32
|
+
# {"_id"=>4b5a04ea238d3bace2000003, "n"=>#<Mongo::MaxKey:0x1014ef410>},
|
33
|
+
# ]
|
34
|
+
class MaxKey
|
35
|
+
|
36
|
+
def ==(obj)
|
37
|
+
obj.class == MaxKey
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# A class representing the BSON MinKey type. MinKey will always compare less than
|
42
|
+
# all other BSON types and values.
|
43
|
+
#
|
44
|
+
# @example Sorting (assume @numbers is a collection):
|
45
|
+
#
|
46
|
+
# >> @numbers.save({"n" => Mongo::MinKey.new})
|
47
|
+
# >> @numbers.save({"n" => -1_000_000})
|
48
|
+
# >> @numbers.save({"n" => 1_000_000})
|
49
|
+
# >> @numbers.find.sort("n").to_a
|
50
|
+
# => [{"_id"=>4b5a050c238d3bace2000004, "n"=>#<Mongo::MinKey:0x1014ef410>},
|
51
|
+
# {"_id"=>4b5a04e6238d3bace2000002, "n"=>-1_000_000},
|
52
|
+
# {"_id"=>4b5a04ea238d3bace2000003, "n"=>1_000_000},
|
53
|
+
# ]
|
54
|
+
class MinKey
|
55
|
+
|
56
|
+
def ==(obj)
|
57
|
+
obj.class == MinKey
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,230 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
# --
|
4
|
+
# Copyright (C) 2008-2011 10gen Inc.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
# ++
|
18
|
+
|
19
|
+
require 'thread'
|
20
|
+
require 'socket'
|
21
|
+
require 'digest/md5'
|
22
|
+
|
23
|
+
module BSON
|
24
|
+
|
25
|
+
def BSON::ObjectId(s)
|
26
|
+
ObjectId.from_string(s)
|
27
|
+
end
|
28
|
+
|
29
|
+
# Generates MongoDB object ids.
|
30
|
+
#
|
31
|
+
# @core objectids
|
32
|
+
class ObjectId
|
33
|
+
attr_accessor :data
|
34
|
+
|
35
|
+
# Create a new object id. If no parameter is given, an id corresponding
|
36
|
+
# to the ObjectId BSON data type will be created. This is a 12-byte value
|
37
|
+
# consisting of a 4-byte timestamp, a 3-byte machine id, a 2-byte process id,
|
38
|
+
# and a 3-byte counter.
|
39
|
+
#
|
40
|
+
# @param [Array] data should be an array of bytes. If you want
|
41
|
+
# to generate a standard MongoDB object id, leave this argument blank.
|
42
|
+
#
|
43
|
+
# @option opts :data (nil) An array of bytes to use as the object id.
|
44
|
+
# @option opts :time (nil) The value of this object ids timestamp. Note that
|
45
|
+
# the remaining bytes will consist of the standard machine id, pid, and counter. If
|
46
|
+
# you need a zeroed timestamp, used ObjectId.from_time.
|
47
|
+
def initialize(data=nil, time=nil)
|
48
|
+
@data = data || generate(time)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Determine if the supplied string is legal. Legal strings will
|
52
|
+
# consist of 24 hexadecimal characters.
|
53
|
+
#
|
54
|
+
# @param [String] str
|
55
|
+
#
|
56
|
+
# @return [Boolean]
|
57
|
+
def self.legal?(str)
|
58
|
+
str =~ /^[0-9a-f]{24}$/i ? true : false
|
59
|
+
end
|
60
|
+
|
61
|
+
# Create an object id from the given time. This is useful for doing range
|
62
|
+
# queries; it works because MongoDB's object ids begin
|
63
|
+
# with a timestamp.
|
64
|
+
#
|
65
|
+
# @param [Time] time a utc time to encode as an object id.
|
66
|
+
#
|
67
|
+
# @option opts [:unique] (false) If false, the object id's bytes
|
68
|
+
# succeeding the timestamp will be zeroed; if true, they'll
|
69
|
+
# consist of the standard machine id, pid, and counter.
|
70
|
+
#
|
71
|
+
# @return [BSON::ObjectId]
|
72
|
+
#
|
73
|
+
# @example Return all document created before Jan 1, 2010.
|
74
|
+
# time = Time.utc(2010, 1, 1)
|
75
|
+
# time_id = ObjectId.from_time(time)
|
76
|
+
# collection.find({'_id' => {'$lt' => time_id}})
|
77
|
+
def self.from_time(time, opts={})
|
78
|
+
unique = opts.fetch(:unique, false)
|
79
|
+
if unique
|
80
|
+
self.new(nil, time)
|
81
|
+
else
|
82
|
+
self.new([time.to_i,0,0].pack("NNN").unpack("C12"))
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# Adds a primary key to the given document if needed.
|
87
|
+
#
|
88
|
+
# @param [Hash] doc a document requiring an _id.
|
89
|
+
#
|
90
|
+
# @return [BSON::ObjectId, Object] returns a newly-created or
|
91
|
+
# current _id for the given document.
|
92
|
+
def self.create_pk(doc)
|
93
|
+
doc.has_key?(:_id) || doc.has_key?('_id') ? doc : doc.merge!(:_id => self.new)
|
94
|
+
end
|
95
|
+
|
96
|
+
# Check equality of this object id with another.
|
97
|
+
#
|
98
|
+
# @param [BSON::ObjectId] object_id
|
99
|
+
def eql?(object_id)
|
100
|
+
object_id.kind_of?(BSON::ObjectId) and self.data == object_id.data
|
101
|
+
end
|
102
|
+
alias_method :==, :eql?
|
103
|
+
|
104
|
+
# Get a unique hashcode for this object.
|
105
|
+
# This is required since we've defined an #eql? method.
|
106
|
+
#
|
107
|
+
# @return [Integer]
|
108
|
+
def hash
|
109
|
+
@data.hash
|
110
|
+
end
|
111
|
+
|
112
|
+
# Get an array representation of the object id.
|
113
|
+
#
|
114
|
+
# @return [Array]
|
115
|
+
def to_a
|
116
|
+
@data.dup
|
117
|
+
end
|
118
|
+
|
119
|
+
# Given a string representation of an ObjectId, return a new ObjectId
|
120
|
+
# with that value.
|
121
|
+
#
|
122
|
+
# @param [String] str
|
123
|
+
#
|
124
|
+
# @return [BSON::ObjectId]
|
125
|
+
def self.from_string(str)
|
126
|
+
raise InvalidObjectId, "illegal ObjectId format: #{str}" unless legal?(str)
|
127
|
+
data = []
|
128
|
+
12.times do |i|
|
129
|
+
data[i] = str[i * 2, 2].to_i(16)
|
130
|
+
end
|
131
|
+
self.new(data)
|
132
|
+
end
|
133
|
+
|
134
|
+
# Get a string representation of this object id.
|
135
|
+
#
|
136
|
+
# @return [String]
|
137
|
+
def to_s
|
138
|
+
@data.map {|e| v=e.to_s(16); v.size == 1 ? "0#{v}" : v }.join
|
139
|
+
end
|
140
|
+
|
141
|
+
def inspect
|
142
|
+
"BSON::ObjectId('#{to_s}')"
|
143
|
+
end
|
144
|
+
|
145
|
+
# Convert to MongoDB extended JSON format. Since JSON includes type information,
|
146
|
+
# but lacks an ObjectId type, this JSON format encodes the type using an $oid key.
|
147
|
+
#
|
148
|
+
# @return [String] the object id represented as MongoDB extended JSON.
|
149
|
+
def to_json(*a)
|
150
|
+
"{\"$oid\": \"#{to_s}\"}"
|
151
|
+
end
|
152
|
+
|
153
|
+
# Create the JSON hash structure convert to MongoDB extended format. Rails 2.3.3
|
154
|
+
# introduced as_json to create the needed hash structure to encode objects into JSON.
|
155
|
+
#
|
156
|
+
# @return [Hash] the hash representation as MongoDB extended JSON
|
157
|
+
def as_json(options ={})
|
158
|
+
{"$oid" => to_s}
|
159
|
+
end
|
160
|
+
|
161
|
+
# Return the UTC time at which this ObjectId was generated. This may
|
162
|
+
# be used in lieu of a created_at timestamp since this information
|
163
|
+
# is always encoded in the object id.
|
164
|
+
#
|
165
|
+
# @return [Time] the time at which this object was created.
|
166
|
+
def generation_time
|
167
|
+
Time.at(@data.pack("C4").unpack("N")[0]).utc
|
168
|
+
end
|
169
|
+
|
170
|
+
def self.machine_id
|
171
|
+
@@machine_id
|
172
|
+
end
|
173
|
+
|
174
|
+
private
|
175
|
+
|
176
|
+
if RUBY_PLATFORM =~ /java/
|
177
|
+
@@generator = Java::OrgBsonTypes::ObjectId
|
178
|
+
@@machine_id = [@@generator.genMachineId].pack("N")[0,3]
|
179
|
+
|
180
|
+
def generate(oid_time=nil)
|
181
|
+
data = (oid_time ? @@generator.new(oid_time) : @@generator.new)
|
182
|
+
|
183
|
+
oid = ''
|
184
|
+
oid += [data.timeSecond].pack("N")
|
185
|
+
oid += [data._machine].pack("N")
|
186
|
+
oid += [data._inc].pack("N")
|
187
|
+
oid.unpack("C12")
|
188
|
+
end
|
189
|
+
|
190
|
+
else
|
191
|
+
@@lock = Mutex.new
|
192
|
+
@@index = 0
|
193
|
+
@@machine_id = Digest::MD5.digest(Socket.gethostname)[0, 3]
|
194
|
+
|
195
|
+
# We need to check whether BSON_CODER is defined because it's required by
|
196
|
+
# the BSON C extensions.
|
197
|
+
if defined?(BSON::BSON_CODER) && BSON::BSON_CODER == BSON::BSON_RUBY
|
198
|
+
# This gets overwritten by the C extension if it loads.
|
199
|
+
def generate(oid_time=nil)
|
200
|
+
oid = ''
|
201
|
+
|
202
|
+
# 4 bytes current time
|
203
|
+
if oid_time
|
204
|
+
t = oid_time.to_i
|
205
|
+
else
|
206
|
+
t = Time.new.to_i
|
207
|
+
end
|
208
|
+
oid += [t].pack("N")
|
209
|
+
|
210
|
+
# 3 bytes machine
|
211
|
+
oid += @@machine_id
|
212
|
+
|
213
|
+
# 2 bytes pid
|
214
|
+
oid += [Process.pid % 0xFFFF].pack("n")
|
215
|
+
|
216
|
+
# 3 bytes inc
|
217
|
+
oid += [get_inc].pack("N")[1, 3]
|
218
|
+
|
219
|
+
oid.unpack("C12")
|
220
|
+
end
|
221
|
+
|
222
|
+
def get_inc
|
223
|
+
@@lock.synchronize do
|
224
|
+
@@index = (@@index + 1) % 0xFFFFFF
|
225
|
+
end
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|