bson 1.0.9 → 1.1
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/Rakefile +23 -0
- data/bson.gemspec +1 -0
- data/ext/java/jar/bson.jar +0 -0
- data/ext/java/jar/jbson.jar +0 -0
- data/ext/java/jar/jruby.jar +0 -0
- data/ext/java/jar/mongo.jar +0 -0
- data/lib/bson.rb +32 -22
- data/lib/bson/bson_java.rb +21 -0
- data/lib/bson/bson_ruby.rb +7 -8
- data/lib/bson/ordered_hash.rb +2 -2
- data/lib/bson/types/code.rb +16 -3
- data/lib/bson/types/dbref.rb +4 -0
- data/lib/bson/types/object_id.rb +9 -0
- metadata +44 -63
- data/lib/bson/types/objectid.rb +0 -187
- data/test/mongo_bson/basic_test.rb +0 -99
- data/test/mongo_bson/binary_test.rb +0 -15
- data/test/mongo_bson/bson_test.rb +0 -543
- data/test/mongo_bson/byte_buffer_test.rb +0 -190
- data/test/mongo_bson/jruby_bson_test.rb +0 -227
- data/test/mongo_bson/jruby_encode_test.rb +0 -396
- data/test/mongo_bson/json_test.rb +0 -16
- data/test/mongo_bson/object_id_test.rb +0 -132
- data/test/mongo_bson/ordered_hash_test.rb +0 -197
data/Rakefile
CHANGED
@@ -13,6 +13,29 @@ require 'rbconfig'
|
|
13
13
|
include Config
|
14
14
|
ENV['TEST_MODE'] = 'TRUE'
|
15
15
|
|
16
|
+
task :java do
|
17
|
+
Rake::Task['build:java'].invoke
|
18
|
+
Rake::Task['test:ruby'].invoke
|
19
|
+
end
|
20
|
+
|
21
|
+
namespace :build do
|
22
|
+
desc "Build the java extensions."
|
23
|
+
task :java do
|
24
|
+
puts "Building Java extensions..."
|
25
|
+
java_dir = File.join(File.dirname(__FILE__), 'ext', 'java')
|
26
|
+
jar_dir = File.join(java_dir, 'jar')
|
27
|
+
|
28
|
+
jruby_jar = File.join(jar_dir, 'jruby.jar')
|
29
|
+
mongo_jar = File.join(jar_dir, 'mongo.jar')
|
30
|
+
bson_jar = File.join(jar_dir, 'bson.jar')
|
31
|
+
|
32
|
+
src_base = File.join(java_dir, 'src')
|
33
|
+
|
34
|
+
system("javac -Xlint:unchecked -classpath #{jruby_jar}:#{mongo_jar}:#{bson_jar} #{File.join(src_base, 'org', 'jbson', '*.java')}")
|
35
|
+
system("cd #{src_base} && jar cf #{File.join(jar_dir, 'jbson.jar')} #{File.join('.', 'org', 'jbson', '*.class')}")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
16
39
|
desc "Test the MongoDB Ruby driver."
|
17
40
|
task :test do
|
18
41
|
puts "\nThis option has changed."
|
data/bson.gemspec
CHANGED
@@ -14,6 +14,7 @@ Gem::Specification.new do |s|
|
|
14
14
|
s.files = ['Rakefile', 'bson.gemspec', 'LICENSE.txt']
|
15
15
|
s.files += ['lib/bson.rb'] + Dir['lib/bson/**/*.rb']
|
16
16
|
s.files += ['bin/b2json', 'bin/j2bson']
|
17
|
+
s.files += Dir['ext/java/jar/**/*.jar']
|
17
18
|
s.test_files = Dir['test/mongo_bson/*.rb']
|
18
19
|
|
19
20
|
s.executables = ['b2json', 'j2bson']
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
data/lib/bson.rb
CHANGED
@@ -2,10 +2,10 @@
|
|
2
2
|
|
3
3
|
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
4
4
|
|
5
|
-
MINIMUM_BSON_EXT_VERSION = "1.
|
5
|
+
MINIMUM_BSON_EXT_VERSION = "1.1"
|
6
6
|
|
7
7
|
module BSON
|
8
|
-
VERSION = "1.
|
8
|
+
VERSION = "1.1"
|
9
9
|
def self.serialize(obj, check_keys=false, move_id=false)
|
10
10
|
BSON_CODER.serialize(obj, check_keys, move_id)
|
11
11
|
end
|
@@ -32,34 +32,44 @@ module BSON
|
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
require '
|
39
|
-
|
40
|
-
|
41
|
-
puts "Able to load bson_ext version #{CBson::VERSION}, but >= #{MINIMUM_BSON_EXT_VERSION} is required."
|
42
|
-
raise LoadError
|
43
|
-
end
|
44
|
-
require 'bson/bson_c'
|
35
|
+
if RUBY_PLATFORM =~ /java/
|
36
|
+
jar_dir = File.join(File.dirname(__FILE__), '..', 'ext', 'java', 'jar')
|
37
|
+
require File.join(jar_dir, 'mongo.jar')
|
38
|
+
require File.join(jar_dir, 'bson.jar')
|
39
|
+
require File.join(jar_dir, 'jbson.jar')
|
40
|
+
require 'bson/bson_java'
|
45
41
|
module BSON
|
46
|
-
BSON_CODER =
|
42
|
+
BSON_CODER = BSON_JAVA
|
47
43
|
end
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
44
|
+
else
|
45
|
+
begin
|
46
|
+
# Need this for running test with and without c ext in Ruby 1.9.
|
47
|
+
raise LoadError if ENV['TEST_MODE'] && !ENV['C_EXT']
|
48
|
+
require 'bson_ext/cbson'
|
49
|
+
raise LoadError unless defined?(CBson::VERSION)
|
50
|
+
if CBson::VERSION < MINIMUM_BSON_EXT_VERSION
|
51
|
+
puts "Able to load bson_ext version #{CBson::VERSION}, but >= #{MINIMUM_BSON_EXT_VERSION} is required."
|
52
|
+
raise LoadError
|
53
|
+
end
|
54
|
+
require 'bson/bson_c'
|
55
|
+
module BSON
|
56
|
+
BSON_CODER = BSON_C
|
57
|
+
end
|
58
|
+
rescue LoadError
|
59
|
+
require 'bson/bson_ruby'
|
60
|
+
module BSON
|
61
|
+
BSON_CODER = BSON_RUBY
|
62
|
+
end
|
63
|
+
warn "\n**Notice: C extension not loaded. This is required for optimum MongoDB Ruby driver performance."
|
64
|
+
warn " You can install the extension as follows:\n gem install bson_ext\n"
|
65
|
+
warn " If you continue to receive this message after installing, make sure that the"
|
66
|
+
warn " bson_ext gem is in your load path and that the bson_ext and mongo gems are of the same version.\n"
|
52
67
|
end
|
53
|
-
warn "\n**Notice: C extension not loaded. This is required for optimum MongoDB Ruby driver performance."
|
54
|
-
warn " You can install the extension as follows:\n gem install bson_ext\n"
|
55
|
-
warn " If you continue to receive this message after installing, make sure that the"
|
56
|
-
warn " bson_ext gem is in your load path and that the bson_ext and mongo gems are of the same version.\n"
|
57
68
|
end
|
58
69
|
|
59
70
|
require 'bson/types/binary'
|
60
71
|
require 'bson/types/code'
|
61
72
|
require 'bson/types/dbref'
|
62
|
-
require 'bson/types/objectid'
|
63
73
|
require 'bson/types/object_id'
|
64
74
|
require 'bson/types/min_max_keys'
|
65
75
|
|
@@ -0,0 +1,21 @@
|
|
1
|
+
include Java
|
2
|
+
module BSON
|
3
|
+
class BSON_JAVA
|
4
|
+
|
5
|
+
# TODO: Pool or cache instances of RubyBSONEncoder so that
|
6
|
+
# we don't create a new one on each call to #serialize.
|
7
|
+
def self.serialize(obj, check_keys=false, move_id=false)
|
8
|
+
raise InvalidDocument, "BSON_JAVA.serialize takes a Hash" unless obj.is_a?(Hash)
|
9
|
+
enc = Java::OrgJbson::RubyBSONEncoder.new(JRuby.runtime)
|
10
|
+
ByteBuffer.new(enc.encode(obj))
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.deserialize(buf)
|
14
|
+
dec = Java::OrgBson::BSONDecoder.new
|
15
|
+
callback = Java::OrgJbson::RubyBSONCallback.new(JRuby.runtime)
|
16
|
+
dec.decode(buf.to_s.to_java_bytes, callback)
|
17
|
+
callback.get
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
data/lib/bson/bson_ruby.rb
CHANGED
@@ -44,6 +44,7 @@ module BSON
|
|
44
44
|
|
45
45
|
def initialize
|
46
46
|
@buf = ByteBuffer.new
|
47
|
+
@encoder = BSON_RUBY
|
47
48
|
end
|
48
49
|
|
49
50
|
if RUBY_VERSION >= '1.9'
|
@@ -303,7 +304,7 @@ module BSON
|
|
303
304
|
def deserialize_object_data(buf)
|
304
305
|
size = buf.get_int
|
305
306
|
buf.position -= 4
|
306
|
-
object =
|
307
|
+
object = @encoder.new().deserialize(buf.get(size))
|
307
308
|
if object.has_key? "$ref"
|
308
309
|
DBRef.new(object["$ref"], object["$id"])
|
309
310
|
else
|
@@ -358,7 +359,7 @@ module BSON
|
|
358
359
|
|
359
360
|
scope_size = buf.get_int
|
360
361
|
buf.position -= 4
|
361
|
-
scope =
|
362
|
+
scope = @encoder.new().deserialize(buf.get(scope_size))
|
362
363
|
|
363
364
|
Code.new(encoded_str(code), scope)
|
364
365
|
end
|
@@ -452,7 +453,7 @@ module BSON
|
|
452
453
|
def serialize_object_element(buf, key, val, check_keys, opcode=OBJECT)
|
453
454
|
buf.put(opcode)
|
454
455
|
self.class.serialize_key(buf, key)
|
455
|
-
buf.put_array(
|
456
|
+
buf.put_array(@encoder.new.serialize(val, check_keys).to_a)
|
456
457
|
end
|
457
458
|
|
458
459
|
def serialize_array_element(buf, key, val, check_keys)
|
@@ -527,9 +528,9 @@ module BSON
|
|
527
528
|
len_pos = buf.position
|
528
529
|
buf.put_int(0)
|
529
530
|
|
530
|
-
buf.put_int(val.length + 1)
|
531
|
-
self.class.serialize_cstr(buf, val)
|
532
|
-
buf.put_array(
|
531
|
+
buf.put_int(val.code.length + 1)
|
532
|
+
self.class.serialize_cstr(buf, val.code)
|
533
|
+
buf.put_array(@encoder.new.serialize(val.scope).to_a)
|
533
534
|
|
534
535
|
end_pos = buf.position
|
535
536
|
buf.put_int(end_pos - len_pos, len_pos)
|
@@ -564,8 +565,6 @@ module BSON
|
|
564
565
|
ARRAY
|
565
566
|
when Regexp
|
566
567
|
REGEX
|
567
|
-
when ObjectID
|
568
|
-
OID
|
569
568
|
when ObjectId
|
570
569
|
OID
|
571
570
|
when DBRef
|
data/lib/bson/ordered_hash.rb
CHANGED
@@ -30,7 +30,7 @@ module BSON
|
|
30
30
|
when BSON::OrderedHash
|
31
31
|
keys == other.keys && values == other.values
|
32
32
|
else
|
33
|
-
|
33
|
+
super
|
34
34
|
end
|
35
35
|
rescue
|
36
36
|
false
|
@@ -126,7 +126,7 @@ module BSON
|
|
126
126
|
def reject(&block)
|
127
127
|
clone = self.clone
|
128
128
|
return clone unless block_given?
|
129
|
-
clone.delete_if
|
129
|
+
clone.delete_if(&block)
|
130
130
|
end
|
131
131
|
|
132
132
|
def clear
|
data/lib/bson/types/code.rb
CHANGED
@@ -19,10 +19,10 @@
|
|
19
19
|
module BSON
|
20
20
|
|
21
21
|
# JavaScript code to be evaluated by MongoDB.
|
22
|
-
class Code
|
22
|
+
class Code
|
23
23
|
|
24
24
|
# Hash mapping identifiers to their values
|
25
|
-
attr_accessor :scope
|
25
|
+
attr_accessor :scope, :code
|
26
26
|
|
27
27
|
# Wrap code to be evaluated by MongoDB.
|
28
28
|
#
|
@@ -30,9 +30,22 @@ module BSON
|
|
30
30
|
# @param [Hash] a document mapping identifiers to values, which
|
31
31
|
# represent the scope in which the code is to be executed.
|
32
32
|
def initialize(code, scope={})
|
33
|
-
|
33
|
+
@code = code
|
34
34
|
@scope = scope
|
35
35
|
end
|
36
36
|
|
37
|
+
def length
|
38
|
+
@code.length
|
39
|
+
end
|
40
|
+
|
41
|
+
def ==(other)
|
42
|
+
self.class == other.class &&
|
43
|
+
@code == other.code && @scope == other.scope
|
44
|
+
end
|
45
|
+
|
46
|
+
def inspect
|
47
|
+
"<BSON::Code:#{object_id} @data=\"#{@code}\" @scope=\"#{@scope.inspect}\">"
|
48
|
+
end
|
49
|
+
|
37
50
|
end
|
38
51
|
end
|
data/lib/bson/types/dbref.rb
CHANGED
data/lib/bson/types/object_id.rb
CHANGED
@@ -44,6 +44,8 @@ module BSON
|
|
44
44
|
@data = data || generate
|
45
45
|
end
|
46
46
|
|
47
|
+
attr_accessor :data
|
48
|
+
|
47
49
|
# Determine if the supplied string is legal. Legal strings will
|
48
50
|
# consist of 24 hexadecimal characters.
|
49
51
|
#
|
@@ -106,6 +108,13 @@ module BSON
|
|
106
108
|
@data.dup
|
107
109
|
end
|
108
110
|
|
111
|
+
# Get the array representation without cloning.
|
112
|
+
#
|
113
|
+
# @return [Array]
|
114
|
+
def data
|
115
|
+
@data
|
116
|
+
end
|
117
|
+
|
109
118
|
# Given a string representation of an ObjectId, return a new ObjectId
|
110
119
|
# with that value.
|
111
120
|
#
|
metadata
CHANGED
@@ -1,62 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bson
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 5
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
version: 1.0.9
|
6
|
+
- 1
|
7
|
+
- 1
|
8
|
+
version: "1.1"
|
11
9
|
platform: ruby
|
12
10
|
authors:
|
13
|
-
- Jim Menard
|
14
|
-
- Mike Dirolf
|
15
|
-
- Kyle Banker
|
11
|
+
- Jim Menard
|
12
|
+
- Mike Dirolf
|
13
|
+
- Kyle Banker
|
16
14
|
autorequire:
|
17
15
|
bindir: bin
|
18
16
|
cert_chain: []
|
19
17
|
|
20
|
-
date: 2010-
|
18
|
+
date: 2010-10-04 00:00:00 -04:00
|
21
19
|
default_executable:
|
22
20
|
dependencies: []
|
23
21
|
|
24
22
|
description: A Ruby BSON implementation for MongoDB. For more information about Mongo, see http://www.mongodb.org. For more information on BSON, see http://www.bsonspec.org.
|
25
23
|
email: mongodb-dev@googlegroups.com
|
26
24
|
executables:
|
27
|
-
- b2json
|
28
|
-
- j2bson
|
25
|
+
- b2json
|
26
|
+
- j2bson
|
29
27
|
extensions: []
|
30
28
|
|
31
29
|
extra_rdoc_files: []
|
32
30
|
|
33
31
|
files:
|
34
|
-
- Rakefile
|
35
|
-
- bson.gemspec
|
36
|
-
- LICENSE.txt
|
37
|
-
- lib/bson.rb
|
38
|
-
- lib/bson/bson_c.rb
|
39
|
-
- lib/bson/
|
40
|
-
- lib/bson/
|
41
|
-
- lib/bson/
|
42
|
-
- lib/bson/
|
43
|
-
- lib/bson/
|
44
|
-
- lib/bson/types/
|
45
|
-
- lib/bson/types/
|
46
|
-
- lib/bson/types/
|
47
|
-
- lib/bson/types/
|
48
|
-
- lib/bson/types/
|
49
|
-
- bin/b2json
|
50
|
-
- bin/j2bson
|
51
|
-
-
|
52
|
-
-
|
53
|
-
-
|
54
|
-
-
|
55
|
-
- test/mongo_bson/jruby_bson_test.rb
|
56
|
-
- test/mongo_bson/jruby_encode_test.rb
|
57
|
-
- test/mongo_bson/json_test.rb
|
58
|
-
- test/mongo_bson/object_id_test.rb
|
59
|
-
- test/mongo_bson/ordered_hash_test.rb
|
32
|
+
- Rakefile
|
33
|
+
- bson.gemspec
|
34
|
+
- LICENSE.txt
|
35
|
+
- lib/bson.rb
|
36
|
+
- lib/bson/bson_c.rb
|
37
|
+
- lib/bson/bson_java.rb
|
38
|
+
- lib/bson/bson_ruby.rb
|
39
|
+
- lib/bson/byte_buffer.rb
|
40
|
+
- lib/bson/exceptions.rb
|
41
|
+
- lib/bson/ordered_hash.rb
|
42
|
+
- lib/bson/types/binary.rb
|
43
|
+
- lib/bson/types/code.rb
|
44
|
+
- lib/bson/types/dbref.rb
|
45
|
+
- lib/bson/types/min_max_keys.rb
|
46
|
+
- lib/bson/types/object_id.rb
|
47
|
+
- bin/b2json
|
48
|
+
- bin/j2bson
|
49
|
+
- ext/java/jar/bson.jar
|
50
|
+
- ext/java/jar/jbson.jar
|
51
|
+
- ext/java/jar/jruby.jar
|
52
|
+
- ext/java/jar/mongo.jar
|
60
53
|
has_rdoc: true
|
61
54
|
homepage: http://www.mongodb.org
|
62
55
|
licenses: []
|
@@ -65,39 +58,27 @@ post_install_message:
|
|
65
58
|
rdoc_options: []
|
66
59
|
|
67
60
|
require_paths:
|
68
|
-
- lib
|
61
|
+
- lib
|
69
62
|
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
-
none: false
|
71
63
|
requirements:
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
version: "0"
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
78
69
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
-
none: false
|
80
70
|
requirements:
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
version: "0"
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
version: "0"
|
87
76
|
requirements: []
|
88
77
|
|
89
78
|
rubyforge_project:
|
90
|
-
rubygems_version: 1.3.
|
79
|
+
rubygems_version: 1.3.6
|
91
80
|
signing_key:
|
92
81
|
specification_version: 3
|
93
82
|
summary: Ruby implementation of BSON
|
94
|
-
test_files:
|
95
|
-
|
96
|
-
- test/mongo_bson/binary_test.rb
|
97
|
-
- test/mongo_bson/bson_test.rb
|
98
|
-
- test/mongo_bson/byte_buffer_test.rb
|
99
|
-
- test/mongo_bson/jruby_bson_test.rb
|
100
|
-
- test/mongo_bson/jruby_encode_test.rb
|
101
|
-
- test/mongo_bson/json_test.rb
|
102
|
-
- test/mongo_bson/object_id_test.rb
|
103
|
-
- test/mongo_bson/ordered_hash_test.rb
|
83
|
+
test_files: []
|
84
|
+
|
data/lib/bson/types/objectid.rb
DELETED
@@ -1,187 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
|
3
|
-
# --
|
4
|
-
# Copyright (C) 2008-2010 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
|
-
@@lock = Mutex.new
|
34
|
-
@@index = 0
|
35
|
-
|
36
|
-
# @deprecated
|
37
|
-
#
|
38
|
-
# Create a new object id. If no parameter is given, an id corresponding
|
39
|
-
# to the ObjectID BSON data type will be created. This is a 12-byte value
|
40
|
-
# consisting of a 4-byte timestamp, a 3-byte machine id, a 2-byte process id,
|
41
|
-
# and a 3-byte counter.
|
42
|
-
#
|
43
|
-
# @param [Array] data should be an array of bytes. If you want
|
44
|
-
# to generate a standard MongoDB object id, leave this argument blank.
|
45
|
-
def initialize(data=nil)
|
46
|
-
warn "BSON::ObjectID is deprecated. Please use BSON::ObjectId instead."
|
47
|
-
@data = data || generate
|
48
|
-
end
|
49
|
-
|
50
|
-
# Determine if the supplied string is legal. Legal strings will
|
51
|
-
# consist of 24 hexadecimal characters.
|
52
|
-
#
|
53
|
-
# @param [String] str
|
54
|
-
#
|
55
|
-
# @return [Boolean]
|
56
|
-
def self.legal?(str)
|
57
|
-
len = 24
|
58
|
-
str =~ /([0-9a-f]+)/i
|
59
|
-
match = $1
|
60
|
-
str && str.length == len && match == str
|
61
|
-
end
|
62
|
-
|
63
|
-
# Create an object id from the given time. This is useful for doing range
|
64
|
-
# queries; it works because MongoDB's object ids begin
|
65
|
-
# with a timestamp.
|
66
|
-
#
|
67
|
-
# @param [Time] time a utc time to encode as an object id.
|
68
|
-
#
|
69
|
-
# @return [Mongo::ObjectID]
|
70
|
-
#
|
71
|
-
# @example Return all document created before Jan 1, 2010.
|
72
|
-
# time = Time.utc(2010, 1, 1)
|
73
|
-
# time_id = ObjectID.from_time(time)
|
74
|
-
# collection.find({'_id' => {'$lt' => time_id}})
|
75
|
-
def self.from_time(time)
|
76
|
-
self.new([time.to_i,0,0].pack("NNN").unpack("C12"))
|
77
|
-
end
|
78
|
-
|
79
|
-
# Adds a primary key to the given document if needed.
|
80
|
-
#
|
81
|
-
# @param [Hash] doc a document requiring an _id.
|
82
|
-
#
|
83
|
-
# @return [Mongo::ObjectID, Object] returns a newly-created or
|
84
|
-
# current _id for the given document.
|
85
|
-
def self.create_pk(doc)
|
86
|
-
doc.has_key?(:_id) || doc.has_key?('_id') ? doc : doc.merge!(:_id => self.new)
|
87
|
-
end
|
88
|
-
|
89
|
-
# Check equality of this object id with another.
|
90
|
-
#
|
91
|
-
# @param [Mongo::ObjectID] object_id
|
92
|
-
def eql?(object_id)
|
93
|
-
@data == object_id.instance_variable_get("@data")
|
94
|
-
end
|
95
|
-
alias_method :==, :eql?
|
96
|
-
|
97
|
-
# Get a unique hashcode for this object.
|
98
|
-
# This is required since we've defined an #eql? method.
|
99
|
-
#
|
100
|
-
# @return [Integer]
|
101
|
-
def hash
|
102
|
-
@data.hash
|
103
|
-
end
|
104
|
-
|
105
|
-
# Get an array representation of the object id.
|
106
|
-
#
|
107
|
-
# @return [Array]
|
108
|
-
def to_a
|
109
|
-
@data.dup
|
110
|
-
end
|
111
|
-
|
112
|
-
# Given a string representation of an ObjectID, return a new ObjectID
|
113
|
-
# with that value.
|
114
|
-
#
|
115
|
-
# @param [String] str
|
116
|
-
#
|
117
|
-
# @return [Mongo::ObjectID]
|
118
|
-
def self.from_string(str)
|
119
|
-
raise InvalidObjectID, "illegal ObjectID format" unless legal?(str)
|
120
|
-
data = []
|
121
|
-
12.times do |i|
|
122
|
-
data[i] = str[i * 2, 2].to_i(16)
|
123
|
-
end
|
124
|
-
self.new(data)
|
125
|
-
end
|
126
|
-
|
127
|
-
# Get a string representation of this object id.
|
128
|
-
#
|
129
|
-
# @return [String]
|
130
|
-
def to_s
|
131
|
-
str = ' ' * 24
|
132
|
-
12.times do |i|
|
133
|
-
str[i * 2, 2] = '%02x' % @data[i]
|
134
|
-
end
|
135
|
-
str
|
136
|
-
end
|
137
|
-
|
138
|
-
def inspect
|
139
|
-
"BSON::ObjectID('#{to_s}')"
|
140
|
-
end
|
141
|
-
|
142
|
-
# Convert to MongoDB extended JSON format. Since JSON includes type information,
|
143
|
-
# but lacks an ObjectID type, this JSON format encodes the type using an $oid key.
|
144
|
-
#
|
145
|
-
# @return [String] the object id represented as MongoDB extended JSON.
|
146
|
-
def to_json(*a)
|
147
|
-
"{\"$oid\": \"#{to_s}\"}"
|
148
|
-
end
|
149
|
-
|
150
|
-
# Return the UTC time at which this ObjectID was generated. This may
|
151
|
-
# be used in lieu of a created_at timestamp since this information
|
152
|
-
# is always encoded in the object id.
|
153
|
-
#
|
154
|
-
# @return [Time] the time at which this object was created.
|
155
|
-
def generation_time
|
156
|
-
Time.at(@data.pack("C4").unpack("N")[0]).utc
|
157
|
-
end
|
158
|
-
|
159
|
-
private
|
160
|
-
|
161
|
-
# This gets overwritten by the C extension if it loads.
|
162
|
-
def generate
|
163
|
-
oid = ''
|
164
|
-
|
165
|
-
# 4 bytes current time
|
166
|
-
time = Time.new.to_i
|
167
|
-
oid += [time].pack("N")
|
168
|
-
|
169
|
-
# 3 bytes machine
|
170
|
-
oid += Digest::MD5.digest(Socket.gethostname)[0, 3]
|
171
|
-
|
172
|
-
# 2 bytes pid
|
173
|
-
oid += [Process.pid % 0xFFFF].pack("n")
|
174
|
-
|
175
|
-
# 3 bytes inc
|
176
|
-
oid += [get_inc].pack("N")[1, 3]
|
177
|
-
|
178
|
-
oid.unpack("C12")
|
179
|
-
end
|
180
|
-
|
181
|
-
def get_inc
|
182
|
-
@@lock.synchronize do
|
183
|
-
@@index = (@@index + 1) % 0xFFFFFF
|
184
|
-
end
|
185
|
-
end
|
186
|
-
end
|
187
|
-
end
|