/usr/lib/ruby/vendor_ruby/em-synchrony/em-mongo.rb is in ruby-em-synchrony 1.0.5-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | begin
require "em-mongo"
rescue LoadError => error
raise "Missing EM-Synchrony dependency: gem install em-mongo"
end
module EM
module Mongo
class Database
def authenticate(username, password)
auth_result = self.collection(SYSTEM_COMMAND_COLLECTION).first({'getnonce' => 1})
auth = BSON::OrderedHash.new
auth['authenticate'] = 1
auth['user'] = username
auth['nonce'] = auth_result['nonce']
auth['key'] = EM::Mongo::Support.auth_key(username, password, auth_result['nonce'])
auth_result2 = self.collection(SYSTEM_COMMAND_COLLECTION).first(auth)
if EM::Mongo::Support.ok?(auth_result2)
true
else
raise AuthenticationError, auth_result2["errmsg"]
end
end
end
class Connection
def initialize(host = DEFAULT_IP, port = DEFAULT_PORT, timeout = nil, opts = {})
f = Fiber.current
@em_connection = EMConnection.connect(host, port, timeout, opts)
@db = {}
# establish connection before returning
EM.next_tick { f.resume }
Fiber.yield
end
end
class Collection
#
# The upcoming versions of EM-Mongo change Collection#find's interface: it
# now returns a deferrable cursor YAY. This breaks compatibility with past
# versions BOO. We'll just choose based on the presence/absence of
# EM::Mongo::Cursor YAY
#
#
# em-mongo version > 0.3.6
#
if defined?(EM::Mongo::Cursor)
# afind is the old (async) find
# afind_one is rewritten to call afind
# find is sync, using a callback on the cursor
# find_one is sync, by calling find and taking the first element.
# first is sync, an alias for find_one
alias :afind :find
def find(*args)
f = Fiber.current
cursor = afind(*args)
cursor.to_a.callback{ |res| f.resume(res) }
Fiber.yield
end
# need to rewrite afind_one manually, as it calls 'find' (reasonably
# expecting it to be what is now known as 'afind')
def afind_one(spec_or_object_id=nil, opts={})
spec = case spec_or_object_id
when nil
{}
when BSON::ObjectId
{:_id => spec_or_object_id}
when Hash
spec_or_object_id
else
raise TypeError, "spec_or_object_id must be an instance of ObjectId or Hash, or nil"
end
afind(spec, opts.merge(:limit => -1)).next_document
end
alias :afirst :afind_one
def find_one(selector={}, opts={})
opts[:limit] = 1
find(selector, opts).first
end
alias :first :find_one
#
# em-mongo version <= 0.3.6
#
else
alias :afind :find
def find(selector={}, opts={})
f = Fiber.current
cb = proc { |res| f.resume(res) }
skip = opts.delete(:skip) || 0
limit = opts.delete(:limit) || 0
order = opts.delete(:order)
@connection.find(@name, skip, limit, order, selector, nil, &cb)
Fiber.yield
end
# need to rewrite afirst manually, as it calls 'find' (reasonably
# expecting it to be what is now known as 'afind')
def afirst(selector={}, opts={}, &blk)
opts[:limit] = 1
afind(selector, opts) do |res|
yield res.first
end
end
def first(selector={}, opts={})
opts[:limit] = 1
find(selector, opts).first
end
end
end
end
end
|