Class: Moped::Protocol::Query
- Inherits:
-
Object
- Object
- Moped::Protocol::Query
- Includes:
- Message
- Defined in:
- lib/moped/protocol/query.rb
Overview
The Protocol class for querying a collection.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#batch_size ⇒ Integer
The batch size of the results.
-
#collection ⇒ String
The collection to query.
- #database ⇒ Object
-
#fields ⇒ Hash?
The fields to include in the reply.
-
#flags ⇒ Array
The flags for the query.
-
#full_collection_name ⇒ String
The namespaced collection name.
-
#length ⇒ Integer
The length of the message.
-
#limit ⇒ Integer
The number of documents to return.
-
#op_code ⇒ Integer
Get the code for a query operation.
-
#request_id ⇒ Integer
The request id of the message.
-
#selector ⇒ Hash
The selector for this query.
-
#skip ⇒ Integer
The number of documents to skip.
Instance Method Summary collapse
-
#basic_selector ⇒ Hash
Get the basic selector.
-
#failure?(reply) ⇒ true, false
Determine if the provided reply message is a failure with respect to a query.
-
#failure_exception(reply) ⇒ Moped::Errors::QueryFailure
Get the exception specific to a failure of this particular operation.
-
#initialize(database, collection, selector, options = {}) ⇒ Query
constructor
Instantiate a new query operation.
-
#log_inspect ⇒ String
Provide the value that will be logged when the query runs.
-
#no_timeout=(enable) ⇒ Object
Set the option on the query to not timeout the cursor.
-
#receive_replies(connection) ⇒ Protocol::Reply
Receive replies to the message.
-
#results(reply) ⇒ Moped::Protocol::Reply
Take the provided reply and return the expected results to api consumers.
Methods included from Message
included, #inspect, #serialize
Constructor Details
#initialize(database, collection, selector, options = {}) ⇒ Query
Instantiate a new query operation.
151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/moped/protocol/query.rb', line 151 def initialize(database, collection, selector, = {}) @database = database @collection = collection @full_collection_name = "#{database}.#{collection}" @selector = selector @request_id = [:request_id] @flags = [:flags] || [] @limit = [:limit] @skip = [:skip] @fields = [:fields] @batch_size = [:batch_size] end |
Instance Attribute Details
#batch_size ⇒ Integer
Returns The batch size of the results.
66 67 68 |
# File 'lib/moped/protocol/query.rb', line 66 def batch_size @batch_size end |
#collection ⇒ String
Returns The collection to query.
62 63 64 |
# File 'lib/moped/protocol/query.rb', line 62 def collection @collection end |
#database ⇒ Object
62 |
# File 'lib/moped/protocol/query.rb', line 62 attr_reader :collection, :database |
#fields ⇒ Hash?
Returns the fields to include in the reply.
54 |
# File 'lib/moped/protocol/query.rb', line 54 document :fields, :optional => true |
#flags ⇒ Array
The flags for the query. Supported flags are: :tailable
, :slave_ok
, :no_cursor_timeout
, :await_data
, :exhaust
.
30 31 32 33 34 |
# File 'lib/moped/protocol/query.rb', line 30 flags :flags, tailable: 2 ** 1, slave_ok: 2 ** 2, no_cursor_timeout: 2 ** 4, await_data: 2 ** 5, exhaust: 2 ** 6 |
#full_collection_name ⇒ String
Returns the namespaced collection name.
38 |
# File 'lib/moped/protocol/query.rb', line 38 cstring :full_collection_name |
#length ⇒ Integer
Returns the length of the message.
12 |
# File 'lib/moped/protocol/query.rb', line 12 int32 :length |
#limit ⇒ Integer
Returns the number of documents to return.
46 |
# File 'lib/moped/protocol/query.rb', line 46 int32 :limit |
#op_code ⇒ Integer
Get the code for a query operation.
22 |
# File 'lib/moped/protocol/query.rb', line 22 int32 :op_code |
#request_id ⇒ Integer
Returns the request id of the message.
16 |
# File 'lib/moped/protocol/query.rb', line 16 int32 :request_id |
#selector ⇒ Hash
Returns the selector for this query.
50 |
# File 'lib/moped/protocol/query.rb', line 50 document :selector |
#skip ⇒ Integer
Returns the number of documents to skip.
42 |
# File 'lib/moped/protocol/query.rb', line 42 int32 :skip |
Instance Method Details
#basic_selector ⇒ Hash
Sometimes, like in cases of deletion we need this since MongoDB does not understand $query in operations like DELETE.
Get the basic selector.
79 80 81 |
# File 'lib/moped/protocol/query.rb', line 79 def basic_selector selector["$query"] || selector end |
#failure?(reply) ⇒ true, false
Determine if the provided reply message is a failure with respect to a query.
108 109 110 |
# File 'lib/moped/protocol/query.rb', line 108 def failure?(reply) reply.query_failure? end |
#failure_exception(reply) ⇒ Moped::Errors::QueryFailure
Get the exception specific to a failure of this particular operation.
93 94 95 |
# File 'lib/moped/protocol/query.rb', line 93 def failure_exception(reply) Errors::QueryFailure.new(self, reply.documents.first) end |
#log_inspect ⇒ String
Provide the value that will be logged when the query runs.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/moped/protocol/query.rb', line 172 def log_inspect type = "QUERY" fields = [] fields << ["%-12s", type] fields << ["database=%s", database] fields << ["collection=%s", collection] fields << ["selector=%s", selector.inspect] fields << ["flags=%s", flags.inspect] fields << ["limit=%s", limit.inspect] fields << ["skip=%s", skip.inspect] fields << ["batch_size=%s", batch_size.inspect] fields << ["fields=%s", self.fields.inspect] f, v = fields.transpose f.join(" ") % v end |
#no_timeout=(enable) ⇒ Object
Set the option on the query to not timeout the cursor.
120 121 122 |
# File 'lib/moped/protocol/query.rb', line 120 def no_timeout=(enable) @flags |= [:no_cursor_timeout] if enable end |
#receive_replies(connection) ⇒ Protocol::Reply
Receive replies to the message.
212 213 214 |
# File 'lib/moped/protocol/query.rb', line 212 def receive_replies(connection) connection.read end |
#results(reply) ⇒ Moped::Protocol::Reply
Take the provided reply and return the expected results to api consumers.
227 228 229 |
# File 'lib/moped/protocol/query.rb', line 227 def results(reply) reply end |