moped 1.0.0.rc → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of moped might be problematic. Click here for more details.
- data/CHANGELOG.md +11 -0
- data/README.md +32 -467
- data/lib/moped.rb +0 -1
- data/lib/moped/bson.rb +3 -0
- data/lib/moped/bson/binary.rb +4 -4
- data/lib/moped/bson/code.rb +12 -10
- data/lib/moped/bson/extensions.rb +64 -54
- data/lib/moped/bson/extensions/array.rb +9 -6
- data/lib/moped/bson/extensions/boolean.rb +1 -1
- data/lib/moped/bson/extensions/false_class.rb +5 -2
- data/lib/moped/bson/extensions/float.rb +1 -2
- data/lib/moped/bson/extensions/hash.rb +4 -5
- data/lib/moped/bson/extensions/integer.rb +2 -4
- data/lib/moped/bson/extensions/nil_class.rb +1 -2
- data/lib/moped/bson/extensions/object.rb +13 -0
- data/lib/moped/bson/extensions/regexp.rb +5 -6
- data/lib/moped/bson/extensions/string.rb +24 -13
- data/lib/moped/bson/extensions/symbol.rb +12 -14
- data/lib/moped/bson/extensions/time.rb +4 -4
- data/lib/moped/bson/extensions/true_class.rb +5 -2
- data/lib/moped/bson/max_key.rb +1 -2
- data/lib/moped/bson/min_key.rb +1 -2
- data/lib/moped/bson/object_id.rb +44 -4
- data/lib/moped/bson/types.rb +1 -1
- data/lib/moped/cluster.rb +13 -5
- data/lib/moped/collection.rb +5 -1
- data/lib/moped/connection.rb +4 -4
- data/lib/moped/database.rb +58 -29
- data/lib/moped/logging.rb +3 -3
- data/lib/moped/node.rb +35 -6
- data/lib/moped/protocol/command.rb +1 -4
- data/lib/moped/protocol/delete.rb +2 -0
- data/lib/moped/protocol/get_more.rb +16 -1
- data/lib/moped/protocol/insert.rb +2 -0
- data/lib/moped/protocol/kill_cursors.rb +2 -0
- data/lib/moped/protocol/message.rb +31 -12
- data/lib/moped/protocol/query.rb +15 -3
- data/lib/moped/protocol/reply.rb +6 -4
- data/lib/moped/protocol/update.rb +2 -0
- data/lib/moped/query.rb +60 -5
- data/lib/moped/session.rb +53 -17
- data/lib/moped/session/context.rb +17 -7
- data/lib/moped/threaded.rb +2 -1
- data/lib/moped/version.rb +1 -1
- metadata +11 -6
data/lib/moped.rb
CHANGED
data/lib/moped/bson.rb
CHANGED
data/lib/moped/bson/binary.rb
CHANGED
@@ -48,19 +48,19 @@ module Moped
|
|
48
48
|
io << NULL_BYTE
|
49
49
|
|
50
50
|
if type == :old
|
51
|
-
io << [data.
|
51
|
+
io << [data.bytesize + 4].pack(INT32_PACK)
|
52
52
|
io << SUBTYPE_MAP[type]
|
53
|
-
io << [data.
|
53
|
+
io << [data.bytesize].pack(INT32_PACK)
|
54
54
|
io << data
|
55
55
|
else
|
56
|
-
io << [data.
|
56
|
+
io << [data.bytesize].pack(INT32_PACK)
|
57
57
|
io << SUBTYPE_MAP[type]
|
58
58
|
io << data
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
62
|
def inspect
|
63
|
-
"#<#{self.class.name} type=#{type.inspect} length=#{data.
|
63
|
+
"#<#{self.class.name} type=#{type.inspect} length=#{data.bytesize}>"
|
64
64
|
end
|
65
65
|
|
66
66
|
end
|
data/lib/moped/bson/code.rb
CHANGED
@@ -24,7 +24,7 @@ module Moped
|
|
24
24
|
|
25
25
|
class << self
|
26
26
|
def __bson_load__(io)
|
27
|
-
code = io.read(*io.read(4).unpack(INT32_PACK)).chop
|
27
|
+
code = io.read(*io.read(4).unpack(INT32_PACK)).from_utf8_binary.chop!
|
28
28
|
new code
|
29
29
|
end
|
30
30
|
end
|
@@ -32,14 +32,15 @@ module Moped
|
|
32
32
|
def __bson_dump__(io, key)
|
33
33
|
if scoped?
|
34
34
|
io << Types::CODE_WITH_SCOPE
|
35
|
-
io << key
|
36
|
-
io << NULL_BYTE
|
35
|
+
io << key.to_bson_cstring
|
37
36
|
|
38
|
-
code_start = io.
|
37
|
+
code_start = io.bytesize
|
39
38
|
|
40
39
|
io << START_LENGTH
|
41
|
-
|
42
|
-
|
40
|
+
|
41
|
+
data = code.to_utf8_binary
|
42
|
+
io << [data.bytesize+1].pack(INT32_PACK)
|
43
|
+
io << data
|
43
44
|
io << NULL_BYTE
|
44
45
|
|
45
46
|
scope.__bson_dump__(io)
|
@@ -48,10 +49,11 @@ module Moped
|
|
48
49
|
|
49
50
|
else
|
50
51
|
io << Types::CODE
|
51
|
-
io << key
|
52
|
-
|
53
|
-
|
54
|
-
io <<
|
52
|
+
io << key.to_bson_cstring
|
53
|
+
|
54
|
+
data = code.to_utf8_binary
|
55
|
+
io << [data.bytesize+1].pack(INT32_PACK)
|
56
|
+
io << data
|
55
57
|
io << NULL_BYTE
|
56
58
|
end
|
57
59
|
end
|
@@ -5,6 +5,7 @@ require "moped/bson/extensions/float"
|
|
5
5
|
require "moped/bson/extensions/hash"
|
6
6
|
require "moped/bson/extensions/integer"
|
7
7
|
require "moped/bson/extensions/nil_class"
|
8
|
+
require "moped/bson/extensions/object"
|
8
9
|
require "moped/bson/extensions/regexp"
|
9
10
|
require "moped/bson/extensions/string"
|
10
11
|
require "moped/bson/extensions/symbol"
|
@@ -12,70 +13,79 @@ require "moped/bson/extensions/time"
|
|
12
13
|
require "moped/bson/extensions/true_class"
|
13
14
|
|
14
15
|
module Moped
|
16
|
+
module BSON
|
17
|
+
module Extensions
|
15
18
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
19
|
+
# @private
|
20
|
+
class ::Array
|
21
|
+
extend Moped::BSON::Extensions::Array::ClassMethods
|
22
|
+
include Moped::BSON::Extensions::Array
|
23
|
+
end
|
21
24
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
25
|
+
# @private
|
26
|
+
class ::FalseClass
|
27
|
+
extend Moped::BSON::Extensions::Boolean::ClassMethods
|
28
|
+
include Moped::BSON::Extensions::FalseClass
|
29
|
+
end
|
27
30
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
31
|
+
# @private
|
32
|
+
class ::Float
|
33
|
+
extend Moped::BSON::Extensions::Float::ClassMethods
|
34
|
+
include Moped::BSON::Extensions::Float
|
35
|
+
end
|
33
36
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
37
|
+
# @private
|
38
|
+
class ::Hash
|
39
|
+
extend Moped::BSON::Extensions::Hash::ClassMethods
|
40
|
+
include Moped::BSON::Extensions::Hash
|
41
|
+
end
|
39
42
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
43
|
+
# @private
|
44
|
+
class ::Integer
|
45
|
+
extend Moped::BSON::Extensions::Integer::ClassMethods
|
46
|
+
include Moped::BSON::Extensions::Integer
|
47
|
+
end
|
45
48
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
49
|
+
# @private
|
50
|
+
class ::NilClass
|
51
|
+
extend Moped::BSON::Extensions::NilClass::ClassMethods
|
52
|
+
include Moped::BSON::Extensions::NilClass
|
53
|
+
end
|
51
54
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
end
|
55
|
+
# @private
|
56
|
+
class ::Object
|
57
|
+
include Moped::BSON::Extensions::Object
|
58
|
+
end
|
57
59
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
60
|
+
# @private
|
61
|
+
class ::Regexp
|
62
|
+
extend Moped::BSON::Extensions::Regexp::ClassMethods
|
63
|
+
include Moped::BSON::Extensions::Regexp
|
64
|
+
end
|
63
65
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
66
|
+
# @private
|
67
|
+
class ::String
|
68
|
+
extend Moped::BSON::Extensions::String::ClassMethods
|
69
|
+
include Moped::BSON::Extensions::String
|
70
|
+
end
|
69
71
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
72
|
+
# @private
|
73
|
+
class ::Symbol
|
74
|
+
extend Moped::BSON::Extensions::Symbol::ClassMethods
|
75
|
+
include Moped::BSON::Extensions::Symbol
|
76
|
+
end
|
77
|
+
|
78
|
+
# @private
|
79
|
+
class ::Time
|
80
|
+
extend Moped::BSON::Extensions::Time::ClassMethods
|
81
|
+
include Moped::BSON::Extensions::Time
|
82
|
+
end
|
75
83
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
84
|
+
# @private
|
85
|
+
class ::TrueClass
|
86
|
+
extend Moped::BSON::Extensions::Boolean::ClassMethods
|
87
|
+
include Moped::BSON::Extensions::TrueClass
|
88
|
+
end
|
89
|
+
end
|
80
90
|
end
|
81
91
|
end
|
@@ -20,20 +20,23 @@ module Moped
|
|
20
20
|
|
21
21
|
def __bson_dump__(io, key)
|
22
22
|
io << Types::ARRAY
|
23
|
-
io << key
|
24
|
-
io << NULL_BYTE
|
23
|
+
io << key.to_bson_cstring
|
25
24
|
|
26
|
-
start = io.
|
25
|
+
start = io.bytesize
|
27
26
|
|
28
27
|
# write dummy length
|
29
28
|
io << START_LENGTH
|
30
29
|
|
31
|
-
|
32
|
-
|
30
|
+
index, length = 0, self.length
|
31
|
+
|
32
|
+
while index < length
|
33
|
+
slice(index).__bson_dump__(io, index.to_s)
|
34
|
+
index += 1
|
33
35
|
end
|
36
|
+
|
34
37
|
io << EOD
|
35
38
|
|
36
|
-
stop = io.
|
39
|
+
stop = io.bytesize
|
37
40
|
io[start, 4] = [stop - start].pack(INT32_PACK)
|
38
41
|
|
39
42
|
io
|
@@ -10,7 +10,7 @@ module Moped
|
|
10
10
|
io.read 4
|
11
11
|
|
12
12
|
while (buf = io.readbyte) != 0
|
13
|
-
key = io.gets(NULL_BYTE).chop!
|
13
|
+
key = io.gets(NULL_BYTE).from_utf8_binary.chop!
|
14
14
|
|
15
15
|
if native_class = Types::MAP[buf]
|
16
16
|
doc[key] = native_class.__bson_load__(io)
|
@@ -24,11 +24,10 @@ module Moped
|
|
24
24
|
def __bson_dump__(io = "", key = nil)
|
25
25
|
if key
|
26
26
|
io << Types::HASH
|
27
|
-
io << key
|
28
|
-
io << NULL_BYTE
|
27
|
+
io << key.to_bson_cstring
|
29
28
|
end
|
30
29
|
|
31
|
-
start = io.
|
30
|
+
start = io.bytesize
|
32
31
|
|
33
32
|
# write dummy length
|
34
33
|
io << START_LENGTH
|
@@ -38,7 +37,7 @@ module Moped
|
|
38
37
|
end
|
39
38
|
io << EOD
|
40
39
|
|
41
|
-
stop = io.
|
40
|
+
stop = io.bytesize
|
42
41
|
io[start, 4] = [stop - start].pack INT32_PACK
|
43
42
|
|
44
43
|
io
|
@@ -18,13 +18,11 @@ module Moped
|
|
18
18
|
def __bson_dump__(io, key)
|
19
19
|
if self >= INT32_MIN && self <= INT32_MAX
|
20
20
|
io << Types::INT32
|
21
|
-
io << key
|
22
|
-
io << NULL_BYTE
|
21
|
+
io << key.to_bson_cstring
|
23
22
|
io << [self].pack(INT32_PACK)
|
24
23
|
elsif self >= INT64_MIN && self <= INT64_MAX
|
25
24
|
io << Types::INT64
|
26
|
-
io << key
|
27
|
-
io << NULL_BYTE
|
25
|
+
io << key.to_bson_cstring
|
28
26
|
io << [self].pack(INT64_PACK)
|
29
27
|
else
|
30
28
|
raise RangeError.new("MongoDB can only handle 8-byte ints")
|
@@ -5,9 +5,9 @@ module Moped
|
|
5
5
|
module Regexp
|
6
6
|
module ClassMethods
|
7
7
|
def __bson_load__(io)
|
8
|
-
source = io.gets(NULL_BYTE).chop!
|
8
|
+
source = io.gets(NULL_BYTE).from_utf8_binary.chop!
|
9
9
|
options = 0
|
10
|
-
while (option = io.
|
10
|
+
while (option = io.readbyte) != 0
|
11
11
|
case option
|
12
12
|
when 105 # 'i'
|
13
13
|
options |= ::Regexp::IGNORECASE
|
@@ -24,10 +24,9 @@ module Moped
|
|
24
24
|
|
25
25
|
def __bson_dump__(io, key)
|
26
26
|
io << Types::REGEX
|
27
|
-
io << key
|
28
|
-
|
29
|
-
io << source
|
30
|
-
io << NULL_BYTE
|
27
|
+
io << key.to_bson_cstring
|
28
|
+
|
29
|
+
io << source.to_bson_cstring
|
31
30
|
|
32
31
|
io << 'i' if (options & ::Regexp::IGNORECASE) != 0
|
33
32
|
io << 'ms' if (options & ::Regexp::MULTILINE) != 0
|
@@ -5,29 +5,40 @@ module Moped
|
|
5
5
|
module String
|
6
6
|
module ClassMethods
|
7
7
|
def __bson_load__(io)
|
8
|
-
io.read(*io.read(4).unpack(INT32_PACK)).chop
|
8
|
+
io.read(*io.read(4).unpack(INT32_PACK)).from_utf8_binary.chop!
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
12
|
def __bson_dump__(io, key)
|
13
13
|
io << Types::STRING
|
14
|
-
io << key
|
15
|
-
|
14
|
+
io << key.to_bson_cstring
|
15
|
+
|
16
|
+
data = to_utf8_binary
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
data.force_encoding('utf-8')
|
18
|
+
io << [ data.bytesize + 1 ].pack(INT32_PACK)
|
19
|
+
io << data
|
20
|
+
io << NULL_BYTE
|
21
|
+
end
|
22
22
|
|
23
|
-
|
23
|
+
def to_bson_cstring
|
24
|
+
if include? NULL_BYTE
|
25
|
+
raise EncodingError, "#{inspect} cannot be converted to a BSON " \
|
26
|
+
"cstring because it contains a null byte"
|
24
27
|
end
|
25
28
|
|
26
|
-
|
29
|
+
to_utf8_binary << NULL_BYTE
|
30
|
+
end
|
27
31
|
|
28
|
-
|
29
|
-
|
30
|
-
|
32
|
+
def to_utf8_binary
|
33
|
+
encode(UTF8_ENCODING).force_encoding(BINARY_ENCODING)
|
34
|
+
rescue EncodingError
|
35
|
+
data = dup.force_encoding(UTF8_ENCODING)
|
36
|
+
raise unless data.valid_encoding?
|
37
|
+
data.force_encoding(BINARY_ENCODING)
|
38
|
+
end
|
39
|
+
|
40
|
+
def from_utf8_binary
|
41
|
+
force_encoding(UTF8_ENCODING).encode!
|
31
42
|
end
|
32
43
|
end
|
33
44
|
end
|