moped 0.0.0.beta → 1.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 moped might be problematic. Click here for more details.

Files changed (50) hide show
  1. data/MIT_LICENSE +19 -0
  2. data/README.md +323 -0
  3. data/lib/moped.rb +19 -0
  4. data/lib/moped/bson.rb +25 -0
  5. data/lib/moped/bson/binary.rb +68 -0
  6. data/lib/moped/bson/code.rb +61 -0
  7. data/lib/moped/bson/document.rb +16 -0
  8. data/lib/moped/bson/extensions.rb +81 -0
  9. data/lib/moped/bson/extensions/array.rb +44 -0
  10. data/lib/moped/bson/extensions/boolean.rb +14 -0
  11. data/lib/moped/bson/extensions/false_class.rb +15 -0
  12. data/lib/moped/bson/extensions/float.rb +23 -0
  13. data/lib/moped/bson/extensions/hash.rb +49 -0
  14. data/lib/moped/bson/extensions/integer.rb +37 -0
  15. data/lib/moped/bson/extensions/nil_class.rb +20 -0
  16. data/lib/moped/bson/extensions/regexp.rb +40 -0
  17. data/lib/moped/bson/extensions/string.rb +35 -0
  18. data/lib/moped/bson/extensions/symbol.rb +25 -0
  19. data/lib/moped/bson/extensions/time.rb +21 -0
  20. data/lib/moped/bson/extensions/true_class.rb +15 -0
  21. data/lib/moped/bson/max_key.rb +21 -0
  22. data/lib/moped/bson/min_key.rb +21 -0
  23. data/lib/moped/bson/object_id.rb +123 -0
  24. data/lib/moped/bson/timestamp.rb +15 -0
  25. data/lib/moped/bson/types.rb +67 -0
  26. data/lib/moped/cluster.rb +193 -0
  27. data/lib/moped/collection.rb +67 -0
  28. data/lib/moped/cursor.rb +60 -0
  29. data/lib/moped/database.rb +76 -0
  30. data/lib/moped/errors.rb +61 -0
  31. data/lib/moped/indexes.rb +93 -0
  32. data/lib/moped/logging.rb +25 -0
  33. data/lib/moped/protocol.rb +20 -0
  34. data/lib/moped/protocol/command.rb +27 -0
  35. data/lib/moped/protocol/commands.rb +11 -0
  36. data/lib/moped/protocol/commands/authenticate.rb +54 -0
  37. data/lib/moped/protocol/delete.rb +92 -0
  38. data/lib/moped/protocol/get_more.rb +79 -0
  39. data/lib/moped/protocol/insert.rb +92 -0
  40. data/lib/moped/protocol/kill_cursors.rb +61 -0
  41. data/lib/moped/protocol/message.rb +320 -0
  42. data/lib/moped/protocol/query.rb +131 -0
  43. data/lib/moped/protocol/reply.rb +90 -0
  44. data/lib/moped/protocol/update.rb +107 -0
  45. data/lib/moped/query.rb +230 -0
  46. data/lib/moped/server.rb +73 -0
  47. data/lib/moped/session.rb +253 -0
  48. data/lib/moped/socket.rb +201 -0
  49. data/lib/moped/version.rb +4 -0
  50. metadata +108 -46
@@ -0,0 +1,61 @@
1
+ module Moped
2
+ module BSON
3
+ class Code
4
+
5
+ attr_reader :code, :scope
6
+
7
+ def initialize(code, scope=nil)
8
+ @code = code
9
+ @scope = scope
10
+ end
11
+
12
+ def scoped?
13
+ !!scope
14
+ end
15
+
16
+ def ==(other)
17
+ BSON::Code === other && code == other.code && scope == other.scope
18
+ end
19
+ alias eql? ==
20
+
21
+ def hash
22
+ [code, scope].hash
23
+ end
24
+
25
+ class << self
26
+ def __bson_load__(io)
27
+ code = io.read(*io.read(4).unpack(INT32_PACK)).chop!.force_encoding('utf-8')
28
+ new code
29
+ end
30
+ end
31
+
32
+ def __bson_dump__(io, key)
33
+ if scoped?
34
+ io << Types::CODE_WITH_SCOPE
35
+ io << key
36
+ io << NULL_BYTE
37
+
38
+ code_start = io.length
39
+
40
+ io << START_LENGTH
41
+ io << [code.bytesize+1].pack(INT32_PACK)
42
+ io << code.encode('utf-8').force_encoding('binary')
43
+ io << NULL_BYTE
44
+
45
+ scope.__bson_dump__(io)
46
+
47
+ io[code_start, 4] = [io.bytesize - code_start].pack(INT32_PACK)
48
+
49
+ else
50
+ io << Types::CODE
51
+ io << key
52
+ io << NULL_BYTE
53
+ io << [code.bytesize+1].pack(INT32_PACK)
54
+ io << code.encode('utf-8').force_encoding('binary')
55
+ io << NULL_BYTE
56
+ end
57
+ end
58
+
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,16 @@
1
+ module Moped
2
+ module BSON
3
+ class Document < Hash
4
+ class << self
5
+
6
+ def deserialize(io, document = new)
7
+ __bson_load__(io, document)
8
+ end
9
+
10
+ def serialize(document, io = "")
11
+ document.__bson_dump__(io)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,81 @@
1
+ require "moped/bson/extensions/array"
2
+ require "moped/bson/extensions/boolean"
3
+ require "moped/bson/extensions/false_class"
4
+ require "moped/bson/extensions/float"
5
+ require "moped/bson/extensions/hash"
6
+ require "moped/bson/extensions/integer"
7
+ require "moped/bson/extensions/nil_class"
8
+ require "moped/bson/extensions/regexp"
9
+ require "moped/bson/extensions/string"
10
+ require "moped/bson/extensions/symbol"
11
+ require "moped/bson/extensions/time"
12
+ require "moped/bson/extensions/true_class"
13
+
14
+ module Moped
15
+
16
+ # @private
17
+ class ::Array
18
+ extend BSON::Extensions::Array::ClassMethods
19
+ include BSON::Extensions::Array
20
+ end
21
+
22
+ # @private
23
+ class ::FalseClass
24
+ extend BSON::Extensions::Boolean::ClassMethods
25
+ include BSON::Extensions::FalseClass
26
+ end
27
+
28
+ # @private
29
+ class ::Float
30
+ extend BSON::Extensions::Float::ClassMethods
31
+ include BSON::Extensions::Float
32
+ end
33
+
34
+ # @private
35
+ class ::Hash
36
+ extend BSON::Extensions::Hash::ClassMethods
37
+ include BSON::Extensions::Hash
38
+ end
39
+
40
+ # @private
41
+ class ::Integer
42
+ extend BSON::Extensions::Integer::ClassMethods
43
+ include BSON::Extensions::Integer
44
+ end
45
+
46
+ # @private
47
+ class ::NilClass
48
+ extend BSON::Extensions::NilClass::ClassMethods
49
+ include BSON::Extensions::NilClass
50
+ end
51
+
52
+ # @private
53
+ class ::Regexp
54
+ extend BSON::Extensions::Regexp::ClassMethods
55
+ include BSON::Extensions::Regexp
56
+ end
57
+
58
+ # @private
59
+ class ::String
60
+ extend BSON::Extensions::String::ClassMethods
61
+ include BSON::Extensions::String
62
+ end
63
+
64
+ # @private
65
+ class ::Symbol
66
+ extend BSON::Extensions::Symbol::ClassMethods
67
+ include BSON::Extensions::Symbol
68
+ end
69
+
70
+ # @private
71
+ class ::Time
72
+ extend BSON::Extensions::Time::ClassMethods
73
+ include BSON::Extensions::Time
74
+ end
75
+
76
+ # @private
77
+ class ::TrueClass
78
+ extend BSON::Extensions::Boolean::ClassMethods
79
+ include BSON::Extensions::TrueClass
80
+ end
81
+ end
@@ -0,0 +1,44 @@
1
+ module Moped
2
+ module BSON
3
+ # @private
4
+ module Extensions
5
+ module Array
6
+
7
+ module ClassMethods
8
+ def __bson_load__(io, array = new)
9
+ # Swallow the first four (length) bytes
10
+ io.read 4
11
+
12
+ while (buf = io.readbyte) != 0
13
+ io.gets(NULL_BYTE)
14
+ array << Types::MAP[buf].__bson_load__(io)
15
+ end
16
+
17
+ array
18
+ end
19
+ end
20
+
21
+ def __bson_dump__(io, key)
22
+ io << Types::ARRAY
23
+ io << key
24
+ io << NULL_BYTE
25
+
26
+ start = io.length
27
+
28
+ # write dummy length
29
+ io << START_LENGTH
30
+
31
+ each_with_index do |value, index|
32
+ value.__bson_dump__(io, index.to_s)
33
+ end
34
+ io << EOD
35
+
36
+ stop = io.length
37
+ io[start, 4] = [stop - start].pack(INT32_PACK)
38
+
39
+ io
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,14 @@
1
+ module Moped
2
+ module BSON
3
+ # @private
4
+ module Extensions
5
+ module Boolean
6
+ module ClassMethods
7
+ def __bson_load__(io)
8
+ io.getbyte == 1
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ module Moped
2
+ module BSON
3
+ # @private
4
+ module Extensions
5
+ module FalseClass
6
+ def __bson_dump__(io, key)
7
+ io << Types::BOOLEAN
8
+ io << key
9
+ io << NULL_BYTE
10
+ io << NULL_BYTE
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ module Moped
2
+ module BSON
3
+ # @private
4
+ module Extensions
5
+ module Float
6
+
7
+ module ClassMethods
8
+ def __bson_load__(io)
9
+ io.read(8).unpack(FLOAT_PACK)[0]
10
+ end
11
+ end
12
+
13
+ def __bson_dump__(io, key)
14
+ io << Types::FLOAT
15
+ io << key
16
+ io << NULL_BYTE
17
+ io << [self].pack(FLOAT_PACK)
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,49 @@
1
+ module Moped
2
+ module BSON
3
+ # @private
4
+ module Extensions
5
+ module Hash
6
+
7
+ module ClassMethods
8
+ def __bson_load__(io, doc = new)
9
+ # Swallow the first four (length) bytes
10
+ io.read 4
11
+
12
+ while (buf = io.readbyte) != 0
13
+ key = io.gets(NULL_BYTE).chop!.force_encoding('utf-8')
14
+
15
+ if native_class = Types::MAP[buf]
16
+ doc[key] = native_class.__bson_load__(io)
17
+ end
18
+ end
19
+
20
+ doc
21
+ end
22
+ end
23
+
24
+ def __bson_dump__(io = "", key = nil)
25
+ if key
26
+ io << Types::HASH
27
+ io << key
28
+ io << NULL_BYTE
29
+ end
30
+
31
+ start = io.length
32
+
33
+ # write dummy length
34
+ io << START_LENGTH
35
+
36
+ each do |k, v|
37
+ v.__bson_dump__(io, k.to_s.encode('utf-8').force_encoding('binary'))
38
+ end
39
+ io << EOD
40
+
41
+ stop = io.length
42
+ io[start, 4] = [stop - start].pack INT32_PACK
43
+
44
+ io
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,37 @@
1
+ module Moped
2
+ module BSON
3
+ # @private
4
+ module Extensions
5
+ module Integer
6
+ INT32_MIN = (-(1 << 31)+1)
7
+ INT32_MAX = ((1<<31)-1)
8
+
9
+ INT64_MIN = (-2**64 / 2)
10
+ INT64_MAX = (2**64 / 2 - 1)
11
+
12
+ module ClassMethods
13
+ def __bson_load__(io, bignum=false)
14
+ io.read(4).unpack(INT32_PACK)[0]
15
+ end
16
+ end
17
+
18
+ def __bson_dump__(io, key)
19
+ if self >= INT32_MIN && self <= INT32_MAX
20
+ io << Types::INT32
21
+ io << key
22
+ io << NULL_BYTE
23
+ io << [self].pack(INT32_PACK)
24
+ elsif self >= INT64_MIN && self <= INT64_MAX
25
+ io << Types::INT64
26
+ io << key
27
+ io << NULL_BYTE
28
+ io << [self].pack(INT64_PACK)
29
+ else
30
+ raise RangeError.new("MongoDB can only handle 8-byte ints")
31
+ end
32
+ end
33
+
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,20 @@
1
+ module Moped
2
+ module BSON
3
+ # @private
4
+ module Extensions
5
+ module NilClass
6
+ module ClassMethods
7
+ def __bson_load__(io)
8
+ nil
9
+ end
10
+ end
11
+
12
+ def __bson_dump__(io, key)
13
+ io << Types::NULL
14
+ io << key
15
+ io << NULL_BYTE
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,40 @@
1
+ module Moped
2
+ module BSON
3
+ # @private
4
+ module Extensions
5
+ module Regexp
6
+ module ClassMethods
7
+ def __bson_load__(io)
8
+ source = io.gets(NULL_BYTE).chop!.force_encoding('utf-8')
9
+ options = 0
10
+ while (option = io.getbyte) != 0
11
+ case option
12
+ when 105 # 'i'
13
+ options |= ::Regexp::IGNORECASE
14
+ when 109, 115 # 'm', 's'
15
+ options |= ::Regexp::MULTILINE
16
+ when 120 # 'x'
17
+ options |= ::Regexp::EXTENDED
18
+ end
19
+ end
20
+
21
+ new(source, options)
22
+ end
23
+ end
24
+
25
+ def __bson_dump__(io, key)
26
+ io << Types::REGEX
27
+ io << key
28
+ io << NULL_BYTE
29
+ io << source.force_encoding('binary')
30
+ io << NULL_BYTE
31
+
32
+ io << 'i' if (options & ::Regexp::IGNORECASE) != 0
33
+ io << 'ms' if (options & ::Regexp::MULTILINE) != 0
34
+ io << 'x' if (options & ::Regexp::EXTENDED) != 0
35
+ io << NULL_BYTE
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,35 @@
1
+ module Moped
2
+ module BSON
3
+ # @private
4
+ module Extensions
5
+ module String
6
+ module ClassMethods
7
+ def __bson_load__(io)
8
+ io.read(*io.read(4).unpack(INT32_PACK)).chop!.force_encoding('utf-8')
9
+ end
10
+ end
11
+
12
+ def __bson_dump__(io, key)
13
+ io << Types::STRING
14
+ io << key
15
+ io << NULL_BYTE
16
+
17
+ begin
18
+ data = encode('utf-8')
19
+ rescue EncodingError
20
+ data = dup
21
+ data.force_encoding('utf-8')
22
+
23
+ raise unless data.valid_encoding?
24
+ end
25
+
26
+ data.force_encoding('binary')
27
+
28
+ io << [data.bytesize+1].pack(INT32_PACK)
29
+ io << data
30
+ io << NULL_BYTE
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end