moped 1.5.3 → 2.0.0.beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of moped might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +42 -5
- data/README.md +1 -1
- data/lib/moped.rb +10 -13
- data/lib/moped/address.rb +56 -0
- data/lib/moped/authenticatable.rb +89 -0
- data/lib/moped/cluster.rb +169 -136
- data/lib/moped/collection.rb +53 -19
- data/lib/moped/connection.rb +69 -10
- data/lib/moped/connection/manager.rb +49 -0
- data/lib/moped/connection/pool.rb +198 -0
- data/lib/moped/connection/queue.rb +93 -0
- data/lib/moped/connection/reaper.rb +52 -0
- data/lib/moped/connection/socket.rb +4 -0
- data/lib/moped/connection/socket/connectable.rb +169 -0
- data/lib/moped/connection/socket/ssl.rb +52 -0
- data/lib/moped/connection/socket/tcp.rb +25 -0
- data/lib/moped/connection/sockets.rb +4 -0
- data/lib/moped/cursor.rb +3 -5
- data/lib/moped/database.rb +18 -24
- data/lib/moped/errors.rb +35 -6
- data/lib/moped/executable.rb +96 -0
- data/lib/moped/failover.rb +41 -0
- data/lib/moped/failover/disconnect.rb +31 -0
- data/lib/moped/failover/ignore.rb +29 -0
- data/lib/moped/failover/reconfigure.rb +34 -0
- data/lib/moped/failover/retry.rb +37 -0
- data/lib/moped/indexes.rb +4 -1
- data/lib/moped/instrumentable.rb +39 -0
- data/lib/moped/instrumentable/log.rb +43 -0
- data/lib/moped/instrumentable/noop.rb +31 -0
- data/lib/moped/loggable.rb +110 -0
- data/lib/moped/node.rb +316 -297
- data/lib/moped/operation.rb +3 -0
- data/lib/moped/operation/read.rb +62 -0
- data/lib/moped/operation/write.rb +57 -0
- data/lib/moped/protocol/command.rb +65 -4
- data/lib/moped/protocol/commands/authenticate.rb +1 -2
- data/lib/moped/protocol/delete.rb +16 -0
- data/lib/moped/protocol/get_more.rb +102 -31
- data/lib/moped/protocol/insert.rb +17 -0
- data/lib/moped/protocol/message.rb +44 -46
- data/lib/moped/protocol/query.rb +175 -92
- data/lib/moped/protocol/reply.rb +19 -8
- data/lib/moped/protocol/update.rb +18 -0
- data/lib/moped/query.rb +43 -17
- data/lib/moped/read_preference.rb +49 -0
- data/lib/moped/read_preference/nearest.rb +55 -0
- data/lib/moped/read_preference/primary.rb +60 -0
- data/lib/moped/read_preference/primary_preferred.rb +55 -0
- data/lib/moped/read_preference/secondary.rb +50 -0
- data/lib/moped/read_preference/secondary_preferred.rb +53 -0
- data/lib/moped/read_preference/selectable.rb +79 -0
- data/lib/moped/readable.rb +55 -0
- data/lib/moped/session.rb +122 -70
- data/lib/moped/{mongo_uri.rb → uri.rb} +75 -31
- data/lib/moped/version.rb +1 -1
- data/lib/moped/write_concern.rb +33 -0
- data/lib/moped/write_concern/propagate.rb +38 -0
- data/lib/moped/write_concern/unverified.rb +28 -0
- metadata +79 -44
- data/lib/moped/bson.rb +0 -45
- data/lib/moped/bson/binary.rb +0 -137
- data/lib/moped/bson/code.rb +0 -112
- data/lib/moped/bson/document.rb +0 -41
- data/lib/moped/bson/extensions.rb +0 -91
- data/lib/moped/bson/extensions/array.rb +0 -37
- data/lib/moped/bson/extensions/boolean.rb +0 -16
- data/lib/moped/bson/extensions/false_class.rb +0 -19
- data/lib/moped/bson/extensions/float.rb +0 -22
- data/lib/moped/bson/extensions/hash.rb +0 -39
- data/lib/moped/bson/extensions/integer.rb +0 -36
- data/lib/moped/bson/extensions/nil_class.rb +0 -19
- data/lib/moped/bson/extensions/object.rb +0 -11
- data/lib/moped/bson/extensions/regexp.rb +0 -38
- data/lib/moped/bson/extensions/string.rb +0 -45
- data/lib/moped/bson/extensions/symbol.rb +0 -33
- data/lib/moped/bson/extensions/time.rb +0 -23
- data/lib/moped/bson/extensions/true_class.rb +0 -19
- data/lib/moped/bson/max_key.rb +0 -51
- data/lib/moped/bson/min_key.rb +0 -51
- data/lib/moped/bson/object_id.rb +0 -301
- data/lib/moped/bson/timestamp.rb +0 -38
- data/lib/moped/bson/types.rb +0 -67
- data/lib/moped/logging.rb +0 -58
- data/lib/moped/session/context.rb +0 -115
- data/lib/moped/sockets/connectable.rb +0 -167
- data/lib/moped/sockets/ssl.rb +0 -50
- data/lib/moped/sockets/tcp.rb +0 -23
- data/lib/moped/threaded.rb +0 -69
@@ -1,22 +0,0 @@
|
|
1
|
-
module Moped
|
2
|
-
module BSON
|
3
|
-
module Extensions
|
4
|
-
|
5
|
-
module Float
|
6
|
-
|
7
|
-
def __bson_dump__(io, key)
|
8
|
-
io << Types::FLOAT
|
9
|
-
io << key.to_bson_cstring
|
10
|
-
io << [self].pack(FLOAT_PACK)
|
11
|
-
end
|
12
|
-
|
13
|
-
module ClassMethods
|
14
|
-
|
15
|
-
def __bson_load__(io)
|
16
|
-
io.read(8).unpack(FLOAT_PACK)[0]
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
@@ -1,39 +0,0 @@
|
|
1
|
-
module Moped
|
2
|
-
module BSON
|
3
|
-
module Extensions
|
4
|
-
|
5
|
-
module Hash
|
6
|
-
|
7
|
-
def __bson_dump__(io = "", key = nil)
|
8
|
-
if key
|
9
|
-
io << Types::HASH
|
10
|
-
io << key.to_bson_cstring
|
11
|
-
end
|
12
|
-
start = io.bytesize
|
13
|
-
io << START_LENGTH # write dummy length
|
14
|
-
each do |k, v|
|
15
|
-
v.__bson_dump__(io, k.to_s)
|
16
|
-
end
|
17
|
-
io << EOD
|
18
|
-
stop = io.bytesize
|
19
|
-
io[start, 4] = [stop - start].pack INT32_PACK
|
20
|
-
io
|
21
|
-
end
|
22
|
-
|
23
|
-
module ClassMethods
|
24
|
-
|
25
|
-
def __bson_load__(io, doc = new)
|
26
|
-
io.read(4) # Swallow the first four (length) bytes
|
27
|
-
while (buf = io.readbyte) != 0
|
28
|
-
key = io.gets(NULL_BYTE).from_utf8_binary.chop!
|
29
|
-
if native_class = Types::MAP[buf]
|
30
|
-
doc[key] = native_class.__bson_load__(io)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
doc
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
@@ -1,36 +0,0 @@
|
|
1
|
-
module Moped
|
2
|
-
module BSON
|
3
|
-
module Extensions
|
4
|
-
|
5
|
-
module Integer
|
6
|
-
|
7
|
-
INT32_MIN = (-(1 << 31)+1)
|
8
|
-
INT32_MAX = ((1<<31)-1)
|
9
|
-
|
10
|
-
INT64_MIN = (-2**64 / 2)
|
11
|
-
INT64_MAX = (2**64 / 2 - 1)
|
12
|
-
|
13
|
-
def __bson_dump__(io, key)
|
14
|
-
if self >= INT32_MIN && self <= INT32_MAX
|
15
|
-
io << Types::INT32
|
16
|
-
io << key.to_bson_cstring
|
17
|
-
io << [self].pack(INT32_PACK)
|
18
|
-
elsif self >= INT64_MIN && self <= INT64_MAX
|
19
|
-
io << Types::INT64
|
20
|
-
io << key.to_bson_cstring
|
21
|
-
io << [self].pack(INT64_PACK)
|
22
|
-
else
|
23
|
-
raise RangeError.new("MongoDB can only handle 8-byte ints")
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
module ClassMethods
|
28
|
-
|
29
|
-
def __bson_load__(io, bignum=false)
|
30
|
-
io.read(4).unpack(INT32_PACK)[0]
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
module Moped
|
2
|
-
module BSON
|
3
|
-
module Extensions
|
4
|
-
|
5
|
-
module NilClass
|
6
|
-
|
7
|
-
def __bson_dump__(io, key)
|
8
|
-
io << Types::NULL
|
9
|
-
io << key.to_bson_cstring
|
10
|
-
end
|
11
|
-
|
12
|
-
module ClassMethods
|
13
|
-
|
14
|
-
def __bson_load__(io); nil; end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
@@ -1,38 +0,0 @@
|
|
1
|
-
module Moped
|
2
|
-
module BSON
|
3
|
-
module Extensions
|
4
|
-
|
5
|
-
module Regexp
|
6
|
-
|
7
|
-
def __bson_dump__(io, key)
|
8
|
-
io << Types::REGEX
|
9
|
-
io << key.to_bson_cstring
|
10
|
-
io << source.to_bson_cstring
|
11
|
-
io << 'i' if (options & ::Regexp::IGNORECASE) != 0
|
12
|
-
io << 'ms' if (options & ::Regexp::MULTILINE) != 0
|
13
|
-
io << 'x' if (options & ::Regexp::EXTENDED) != 0
|
14
|
-
io << NULL_BYTE
|
15
|
-
end
|
16
|
-
|
17
|
-
module ClassMethods
|
18
|
-
|
19
|
-
def __bson_load__(io)
|
20
|
-
source = io.gets(NULL_BYTE).from_utf8_binary.chop!
|
21
|
-
options = 0
|
22
|
-
while (option = io.readbyte) != 0
|
23
|
-
case option
|
24
|
-
when 105 # 'i'
|
25
|
-
options |= ::Regexp::IGNORECASE
|
26
|
-
when 109, 115 # 'm', 's'
|
27
|
-
options |= ::Regexp::MULTILINE
|
28
|
-
when 120 # 'x'
|
29
|
-
options |= ::Regexp::EXTENDED
|
30
|
-
end
|
31
|
-
end
|
32
|
-
new(source, options)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
@@ -1,45 +0,0 @@
|
|
1
|
-
module Moped
|
2
|
-
module BSON
|
3
|
-
module Extensions
|
4
|
-
|
5
|
-
module String
|
6
|
-
|
7
|
-
def __bson_dump__(io, key)
|
8
|
-
io << Types::STRING
|
9
|
-
io << key.to_bson_cstring
|
10
|
-
data = to_utf8_binary
|
11
|
-
io << [ data.bytesize + 1 ].pack(INT32_PACK)
|
12
|
-
io << data
|
13
|
-
io << NULL_BYTE
|
14
|
-
end
|
15
|
-
|
16
|
-
def to_bson_cstring
|
17
|
-
if include? NULL_BYTE
|
18
|
-
raise EncodingError, "#{inspect} cannot be converted to a BSON " \
|
19
|
-
"cstring because it contains a null byte"
|
20
|
-
end
|
21
|
-
to_utf8_binary << NULL_BYTE
|
22
|
-
end
|
23
|
-
|
24
|
-
def to_utf8_binary
|
25
|
-
encode(Moped::BSON::UTF8_ENCODING).force_encoding(Moped::BSON::BINARY_ENCODING)
|
26
|
-
rescue EncodingError
|
27
|
-
data = dup.force_encoding(Moped::BSON::UTF8_ENCODING)
|
28
|
-
raise unless data.valid_encoding?
|
29
|
-
data.force_encoding(Moped::BSON::BINARY_ENCODING)
|
30
|
-
end
|
31
|
-
|
32
|
-
def from_utf8_binary
|
33
|
-
force_encoding(Moped::BSON::UTF8_ENCODING).encode!
|
34
|
-
end
|
35
|
-
|
36
|
-
module ClassMethods
|
37
|
-
|
38
|
-
def __bson_load__(io)
|
39
|
-
io.read(*io.read(4).unpack(INT32_PACK)).from_utf8_binary.chop!
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
module Moped
|
2
|
-
module BSON
|
3
|
-
module Extensions
|
4
|
-
|
5
|
-
module Symbol
|
6
|
-
|
7
|
-
def __bson_dump__(io, key)
|
8
|
-
io << Types::SYMBOL
|
9
|
-
io << key.to_bson_cstring
|
10
|
-
data = to_utf8_binary
|
11
|
-
io << [ data.bytesize + 1 ].pack(INT32_PACK)
|
12
|
-
io << data
|
13
|
-
io << NULL_BYTE
|
14
|
-
end
|
15
|
-
|
16
|
-
def to_bson_cstring
|
17
|
-
to_s.to_bson_cstring
|
18
|
-
end
|
19
|
-
|
20
|
-
def to_utf8_binary
|
21
|
-
to_s.to_utf8_binary
|
22
|
-
end
|
23
|
-
|
24
|
-
module ClassMethods
|
25
|
-
|
26
|
-
def __bson_load__(io)
|
27
|
-
io.read(*io.read(4).unpack(INT32_PACK)).from_utf8_binary.chop!.intern
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
module Moped
|
2
|
-
module BSON
|
3
|
-
module Extensions
|
4
|
-
|
5
|
-
module Time
|
6
|
-
|
7
|
-
def __bson_dump__(io, key)
|
8
|
-
io << Types::TIME
|
9
|
-
io << key.to_bson_cstring
|
10
|
-
io << [(to_i * 1000) + (usec / 1000)].pack(INT64_PACK)
|
11
|
-
end
|
12
|
-
|
13
|
-
module ClassMethods
|
14
|
-
|
15
|
-
def __bson_load__(io)
|
16
|
-
seconds, fragment = io.read(8).unpack(INT64_PACK)[0].divmod 1000
|
17
|
-
at(seconds, fragment * 1000).utc
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
module Moped
|
2
|
-
module BSON
|
3
|
-
module Extensions
|
4
|
-
|
5
|
-
module TrueClass
|
6
|
-
|
7
|
-
def __bson_dump__(io, key)
|
8
|
-
io << Types::BOOLEAN
|
9
|
-
io << key.to_bson_cstring
|
10
|
-
io << Types::TRUE
|
11
|
-
end
|
12
|
-
|
13
|
-
def __safe_options__
|
14
|
-
{ safe: true }
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
data/lib/moped/bson/max_key.rb
DELETED
@@ -1,51 +0,0 @@
|
|
1
|
-
module Moped
|
2
|
-
module BSON
|
3
|
-
|
4
|
-
# Represents the maximum key value in the database.
|
5
|
-
class MaxKey
|
6
|
-
|
7
|
-
class << self
|
8
|
-
|
9
|
-
# Check equality on the object.
|
10
|
-
#
|
11
|
-
# @example Check equality.
|
12
|
-
# object == other
|
13
|
-
#
|
14
|
-
# @param [ Object ] other The object to check against.
|
15
|
-
#
|
16
|
-
# @return [ true, false ] If the objects are equal.
|
17
|
-
#
|
18
|
-
# @since 1.0.0
|
19
|
-
def ===(other)
|
20
|
-
other == self
|
21
|
-
end
|
22
|
-
|
23
|
-
# Load the max key from the raw data.
|
24
|
-
#
|
25
|
-
# @example Load the max key.
|
26
|
-
# Moped::BSON::MaxKey.__bson_load("")
|
27
|
-
#
|
28
|
-
# @param [ String ] io The raw bytes.
|
29
|
-
#
|
30
|
-
# @return [ Class ] The Moped::BSON::MaxKey class.
|
31
|
-
#
|
32
|
-
# @since 1.0.0
|
33
|
-
def __bson_load__(io); self; end
|
34
|
-
|
35
|
-
# Dump the max key to the raw bytes.
|
36
|
-
#
|
37
|
-
# @example Dump the max key.
|
38
|
-
# Moped::BSON::MaxKey.__bson_dump__("", "max")
|
39
|
-
#
|
40
|
-
# @param [ String ] io The raw bytes to write to.
|
41
|
-
# @param [ String ] key The field name.
|
42
|
-
#
|
43
|
-
# @since 1.0.0
|
44
|
-
def __bson_dump__(io, key)
|
45
|
-
io << Types::MAX_KEY
|
46
|
-
io << key.to_bson_cstring
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
data/lib/moped/bson/min_key.rb
DELETED
@@ -1,51 +0,0 @@
|
|
1
|
-
module Moped
|
2
|
-
module BSON
|
3
|
-
|
4
|
-
# Represents the minimum key value in the database.
|
5
|
-
class MinKey
|
6
|
-
|
7
|
-
class << self
|
8
|
-
|
9
|
-
# Check equality on the object.
|
10
|
-
#
|
11
|
-
# @example Check equality.
|
12
|
-
# object == other
|
13
|
-
#
|
14
|
-
# @param [ Object ] other The object to check against.
|
15
|
-
#
|
16
|
-
# @return [ true, false ] If the objects are equal.
|
17
|
-
#
|
18
|
-
# @since 1.0.0
|
19
|
-
def ===(other)
|
20
|
-
other == self
|
21
|
-
end
|
22
|
-
|
23
|
-
# Load the min key from the raw data.
|
24
|
-
#
|
25
|
-
# @example Load the min key.
|
26
|
-
# Moped::BSON::MinKey.__bson_load("")
|
27
|
-
#
|
28
|
-
# @param [ String ] io The raw bytes.
|
29
|
-
#
|
30
|
-
# @return [ Class ] The Moped::BSON::MinKey class.
|
31
|
-
#
|
32
|
-
# @since 1.0.0
|
33
|
-
def __bson_load__(io); self; end
|
34
|
-
|
35
|
-
# Dump the min key to the raw bytes.
|
36
|
-
#
|
37
|
-
# @example Dump the min key.
|
38
|
-
# Moped::BSON::MinKey.__bson_dump__("", "min")
|
39
|
-
#
|
40
|
-
# @param [ String ] io The raw bytes to write to.
|
41
|
-
# @param [ String ] key The field name.
|
42
|
-
#
|
43
|
-
# @since 1.0.0
|
44
|
-
def __bson_dump__(io, key)
|
45
|
-
io << Types::MIN_KEY
|
46
|
-
io << key.to_bson_cstring
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
data/lib/moped/bson/object_id.rb
DELETED
@@ -1,301 +0,0 @@
|
|
1
|
-
require "digest/md5"
|
2
|
-
require "socket"
|
3
|
-
|
4
|
-
module Moped
|
5
|
-
module BSON
|
6
|
-
class ObjectId
|
7
|
-
include Comparable
|
8
|
-
|
9
|
-
# Serialize the object id to its raw bytes.
|
10
|
-
#
|
11
|
-
# @example Serialize the object id.
|
12
|
-
# object_id.__bson_dump__("", "_id")
|
13
|
-
#
|
14
|
-
# @param [ String ] io The raw bytes to write to.
|
15
|
-
# @param [ String ] key The field name.
|
16
|
-
#
|
17
|
-
# @since 1.0.0
|
18
|
-
def __bson_dump__(io, key)
|
19
|
-
io << Types::OBJECT_ID
|
20
|
-
io << key.to_bson_cstring
|
21
|
-
io << data
|
22
|
-
end
|
23
|
-
|
24
|
-
# Check equality on the object.
|
25
|
-
#
|
26
|
-
# @example Check equality.
|
27
|
-
# object === other
|
28
|
-
#
|
29
|
-
# @param [ Object ] other The object to check against.
|
30
|
-
#
|
31
|
-
# @return [ true, false ] If the objects are equal.
|
32
|
-
#
|
33
|
-
# @since 1.0.0
|
34
|
-
def ===(other)
|
35
|
-
return to_str === other.to_str if other.respond_to?(:to_str)
|
36
|
-
super
|
37
|
-
end
|
38
|
-
|
39
|
-
# Check equality on the object.
|
40
|
-
#
|
41
|
-
# @example Check equality.
|
42
|
-
# object == other
|
43
|
-
#
|
44
|
-
# @param [ Object ] other The object to check against.
|
45
|
-
#
|
46
|
-
# @return [ true, false ] If the objects are equal.
|
47
|
-
#
|
48
|
-
# @since 1.0.0
|
49
|
-
def ==(other)
|
50
|
-
BSON::ObjectId === other && data == other.data
|
51
|
-
end
|
52
|
-
alias :eql? :==
|
53
|
-
|
54
|
-
# Compare this object with another object, used in sorting.
|
55
|
-
#
|
56
|
-
# @example Compare the two objects.
|
57
|
-
# object <=> other
|
58
|
-
#
|
59
|
-
# @param [ Object ] other The object to compare to.
|
60
|
-
#
|
61
|
-
# @return [ Integer ] The result of the comparison.
|
62
|
-
#
|
63
|
-
# @since 1.0.0
|
64
|
-
def <=>(other)
|
65
|
-
data <=> other.data
|
66
|
-
end
|
67
|
-
|
68
|
-
# Get the raw data (bytes) for the object id.
|
69
|
-
#
|
70
|
-
# @example Get the raw data.
|
71
|
-
# object_id.data
|
72
|
-
#
|
73
|
-
# @return [ String ] The raw bytes.
|
74
|
-
#
|
75
|
-
# @since 1.0.0
|
76
|
-
def data
|
77
|
-
# If @data is defined, then we know we've been loaded in some
|
78
|
-
# non-standard way, so we attempt to repair the data.
|
79
|
-
repair! @data if defined? @data
|
80
|
-
@raw_data ||= @@generator.next
|
81
|
-
end
|
82
|
-
|
83
|
-
# Return the UTC time at which this ObjectId was generated. This may
|
84
|
-
# be used instread of a created_at timestamp since this information
|
85
|
-
# is always encoded in the object id.
|
86
|
-
#
|
87
|
-
# @example Get the generation time.
|
88
|
-
# object_id.generation_time
|
89
|
-
#
|
90
|
-
# @return [ Time ] The time the id was generated.
|
91
|
-
#
|
92
|
-
# @since 1.0.0
|
93
|
-
def generation_time
|
94
|
-
Time.at(data.unpack("N")[0]).utc
|
95
|
-
end
|
96
|
-
|
97
|
-
# Gets the hash code for the object.
|
98
|
-
#
|
99
|
-
# @example Get the hash code.
|
100
|
-
# object.hash
|
101
|
-
#
|
102
|
-
# @return [ Fixnum ] The hash code.
|
103
|
-
#
|
104
|
-
# @since 1.0.0
|
105
|
-
def hash
|
106
|
-
data.hash
|
107
|
-
end
|
108
|
-
|
109
|
-
# Gets the string inspection for the object.
|
110
|
-
#
|
111
|
-
# @example Get the string inspection.
|
112
|
-
# object.inspect
|
113
|
-
#
|
114
|
-
# @return [ String ] The inspection.
|
115
|
-
#
|
116
|
-
# @since 1.0.0
|
117
|
-
def inspect
|
118
|
-
to_s.inspect
|
119
|
-
end
|
120
|
-
|
121
|
-
# Dump the object for use in a marshal dump.
|
122
|
-
#
|
123
|
-
# @example Dump the object.
|
124
|
-
# object.marshal_dump
|
125
|
-
#
|
126
|
-
# @return [ String ] The dumped object.
|
127
|
-
#
|
128
|
-
# @since 1.0.0
|
129
|
-
def marshal_dump
|
130
|
-
data
|
131
|
-
end
|
132
|
-
|
133
|
-
# Load the object from the marshal dump.
|
134
|
-
#
|
135
|
-
# @example Load the object.
|
136
|
-
# object.marshal_load("")
|
137
|
-
#
|
138
|
-
# @param [ String ] data The raw data.
|
139
|
-
#
|
140
|
-
# @since 1.0.0
|
141
|
-
def marshal_load(data)
|
142
|
-
self.data = data
|
143
|
-
end
|
144
|
-
|
145
|
-
# Convert the object to a JSON string.
|
146
|
-
#
|
147
|
-
# @example Convert to a JSON string.
|
148
|
-
# obejct.to_json
|
149
|
-
#
|
150
|
-
# @return [ String ] The object as JSON.
|
151
|
-
#
|
152
|
-
# @since 1.0.0
|
153
|
-
def to_json(*args)
|
154
|
-
"{\"$oid\": \"#{to_s}\"}"
|
155
|
-
end
|
156
|
-
|
157
|
-
# Get the string representation of the object.
|
158
|
-
#
|
159
|
-
# @example Get the string representation.
|
160
|
-
# object.to_s
|
161
|
-
#
|
162
|
-
# @return [ String ] The string representation.
|
163
|
-
#
|
164
|
-
# @since 1.0.0
|
165
|
-
def to_s
|
166
|
-
data.unpack("H*")[0].force_encoding(Moped::BSON::UTF8_ENCODING)
|
167
|
-
end
|
168
|
-
alias :to_str :to_s
|
169
|
-
|
170
|
-
private
|
171
|
-
|
172
|
-
# Private interface for setting the internal data for an object id.
|
173
|
-
def data=(data)
|
174
|
-
@raw_data = data
|
175
|
-
end
|
176
|
-
|
177
|
-
# Attempts to repair ObjectId data marshalled in previous formats.
|
178
|
-
#
|
179
|
-
# The first check covers an ObjectId generated by the mongo-ruby-driver.
|
180
|
-
#
|
181
|
-
# The second check covers an ObjectId generated by moped before a custom
|
182
|
-
# marshal strategy was added.
|
183
|
-
def repair!(data)
|
184
|
-
if data.is_a?(Array) && data.size == 12
|
185
|
-
self.data = data.pack("C*")
|
186
|
-
elsif data.is_a?(String) && data.size == 12
|
187
|
-
self.data = data
|
188
|
-
else
|
189
|
-
raise TypeError, "Could not convert #{data.inspect} into an ObjectId"
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
|
-
class << self
|
194
|
-
|
195
|
-
def __bson_load__(io)
|
196
|
-
from_data(io.read(12))
|
197
|
-
end
|
198
|
-
|
199
|
-
# Create a new object id from a string.
|
200
|
-
#
|
201
|
-
# @example Create an object id from the string.
|
202
|
-
# Moped::BSON::ObjectId.from_string(id)
|
203
|
-
#
|
204
|
-
# @param [ String ] string The string to create from.
|
205
|
-
#
|
206
|
-
# @return [ ObjectId ] The new object id.
|
207
|
-
#
|
208
|
-
# @since 1.0.0
|
209
|
-
def from_string(string)
|
210
|
-
raise Errors::InvalidObjectId.new(string) unless legal?(string)
|
211
|
-
from_data [string].pack("H*")
|
212
|
-
end
|
213
|
-
|
214
|
-
# Create a new object id from a time.
|
215
|
-
#
|
216
|
-
# @example Create an object id from a time.
|
217
|
-
# Moped::BSON::ObjectId.from_id(time)
|
218
|
-
#
|
219
|
-
# @example Create an object id from a time, ensuring uniqueness.
|
220
|
-
# Moped::BSON::ObjectId.from_id(time, unique: true)
|
221
|
-
#
|
222
|
-
# @param [ Time ] time The time to generate from.
|
223
|
-
# @param [ Hash ] options The options.
|
224
|
-
#
|
225
|
-
# @option options [ true, false ] :unique Whether the id should be
|
226
|
-
# unique.
|
227
|
-
#
|
228
|
-
# @return [ ObjectId ] The new object id.
|
229
|
-
#
|
230
|
-
# @since 1.0.0
|
231
|
-
def from_time(time, options = nil)
|
232
|
-
unique = (options || {})[:unique]
|
233
|
-
from_data(unique ? @@generator.next(time.to_i) : [ time.to_i ].pack("Nx8"))
|
234
|
-
end
|
235
|
-
|
236
|
-
# Determine if the string is a legal object id.
|
237
|
-
#
|
238
|
-
# @example Is the string a legal object id?
|
239
|
-
# Moped::BSON::ObjectId.legal?(string)
|
240
|
-
#
|
241
|
-
# @param [ String ] The string to test.
|
242
|
-
#
|
243
|
-
# @return [ true, false ] If the string is legal.
|
244
|
-
#
|
245
|
-
# @since 1.0.0
|
246
|
-
def legal?(string)
|
247
|
-
string.to_s =~ /\A\h{24}\z/ ? true : false
|
248
|
-
end
|
249
|
-
|
250
|
-
# Create a new object id from some raw data.
|
251
|
-
#
|
252
|
-
# @example Create an object id from raw data.
|
253
|
-
# Moped::BSON::ObjectId.from_data(data)
|
254
|
-
#
|
255
|
-
# @param [ String ] data The raw bytes.
|
256
|
-
#
|
257
|
-
# @return [ ObjectId ] The new object id.
|
258
|
-
#
|
259
|
-
# @since 1.0.0
|
260
|
-
def from_data(data)
|
261
|
-
id = allocate
|
262
|
-
id.send(:data=, data)
|
263
|
-
id
|
264
|
-
end
|
265
|
-
end
|
266
|
-
|
267
|
-
# @api private
|
268
|
-
class Generator
|
269
|
-
def initialize
|
270
|
-
# Generate and cache 3 bytes of identifying information from the current
|
271
|
-
# machine.
|
272
|
-
@machine_id = Digest::MD5.digest(Socket.gethostname).unpack("N")[0]
|
273
|
-
|
274
|
-
@mutex = Mutex.new
|
275
|
-
@counter = 0
|
276
|
-
end
|
277
|
-
|
278
|
-
# Return object id data based on the current time, incrementing the
|
279
|
-
# object id counter.
|
280
|
-
def next(time = nil)
|
281
|
-
@mutex.lock
|
282
|
-
begin
|
283
|
-
counter = @counter = (@counter + 1) % 0xFFFFFF
|
284
|
-
ensure
|
285
|
-
@mutex.unlock rescue nil
|
286
|
-
end
|
287
|
-
|
288
|
-
generate(time || Time.new.to_i, counter)
|
289
|
-
end
|
290
|
-
|
291
|
-
# Generate object id data for a given time using the provided +counter+.
|
292
|
-
def generate(time, counter = 0)
|
293
|
-
process_thread_id = "#{Process.pid}#{Thread.current.object_id}".hash % 0xFFFF
|
294
|
-
[time, @machine_id, process_thread_id, counter << 8].pack("N NX lXX NX")
|
295
|
-
end
|
296
|
-
end
|
297
|
-
|
298
|
-
@@generator = Generator.new
|
299
|
-
end
|
300
|
-
end
|
301
|
-
end
|