Class: Couchbase::View
- Inherits:
-
Object
- Object
- Couchbase::View
- Includes:
- Constants, Enumerable
- Defined in:
- lib/couchbase/view.rb
Overview
This class implements Couchbase View execution
Defined Under Namespace
Classes: ArrayWithTotalRows, AsyncHelper
Constant Summary
Constants included from Constants
Constants::S_CAS, Constants::S_DOC, Constants::S_FLAGS, Constants::S_ID, Constants::S_IS_LAST, Constants::S_KEY, Constants::S_META, Constants::S_VALUE
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
-
#each(params = {}) ⇒ Object
Yields each document that was fetched by view.
-
#fetch(params = {}) {|document| ... } ⇒ Array
Performs query to Couchbase view.
-
#fetch_all(params = {}, &block) ⇒ Object
Method for fetching asynchronously all rows and passing array to callback.
- #first(params = {}) ⇒ Object
-
#initialize(bucket, endpoint, params = {}) ⇒ View
constructor
Set up view endpoint and optional params.
-
#inspect ⇒ String
Returns a string containing a human-readable representation of the View.
-
#on_error {|from, reason| ... } ⇒ Object
Registers callback function for handling error objects in view results stream.
- #take(n, params = {}) ⇒ Object
Constructor Details
#initialize(bucket, endpoint, params = {}) ⇒ View
Set up view endpoint and optional params
157 158 159 160 161 162 163 164 165 |
# File 'lib/couchbase/view.rb', line 157 def initialize(bucket, endpoint, params = {}) @bucket = bucket @endpoint = endpoint @params = {:connection_timeout => 75_000}.merge(params) @wrapper_class = params.delete(:wrapper_class) || ViewRow unless @wrapper_class.respond_to?(:wrap) raise ArgumentError, "wrapper class should reposond to :wrap, check the options" end end |
Instance Attribute Details
#params ⇒ Object (readonly)
Returns the value of attribute params.
145 146 147 |
# File 'lib/couchbase/view.rb', line 145 def params @params end |
Instance Method Details
#each(params = {}) ⇒ Object
Yields each document that was fetched by view. It doesn’t instantiate all the results because of streaming JSON parser. Returns Enumerator unless block given.
193 194 195 196 |
# File 'lib/couchbase/view.rb', line 193 def each(params = {}) return enum_for(:each, params) unless block_given? fetch(params) {|doc| yield(doc)} end |
#fetch(params = {}) {|document| ... } ⇒ Array
Avoid using $ symbol as prefix for properties in your documents, because server marks with it meta fields like flags and expiration, therefore dollar prefix is some kind of reserved. It won’t hurt your application. Currently the Couchbase::ViewRow class extracts $flags, $cas and $expiration properties from the document and store them in Couchbase::ViewRow#meta hash.
Performs query to Couchbase view. This method will stream results if block given or return complete result set otherwise. In latter case it defines method total_rows
returning corresponding entry from Couchbase result object.
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/couchbase/view.rb', line 355 def fetch(params = {}, &block) params = @params.merge(params) include_docs = params.delete(:include_docs) quiet = params.delete(:quiet){ true } = {:chunked => true, :extended => true, :type => :view} if body = params.delete(:body) body = MultiJson.dump(body) unless body.is_a?(String) .update(:body => body, :method => params.delete(:method) || :post) end path = Utils.build_query(@endpoint, params) request = @bucket.make_http_request(path, ) if @bucket.async? if block fetch_async(request, include_docs, quiet, block) end else fetch_sync(request, include_docs, quiet, block) end end |
#fetch_all(params = {}, &block) ⇒ Object
Method for fetching asynchronously all rows and passing array to callback
Parameters are same as for #fetch method, but callback is called for whole set for rows instead of one by each.
388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/couchbase/view.rb', line 388 def fetch_all(params = {}, &block) return fetch(params) unless @bucket.async? raise ArgumentError, "Block needed for fetch_all in async mode" unless block all = [] fetch(params) do |row| all << row if row.last? @bucket.create_timer(0) { block.call(all) } end end end |
#first(params = {}) ⇒ Object
198 199 200 201 |
# File 'lib/couchbase/view.rb', line 198 def first(params = {}) params = params.merge(:limit => 1) fetch(params).first end |
#inspect ⇒ String
Returns a string containing a human-readable representation of the Couchbase::View
405 406 407 |
# File 'lib/couchbase/view.rb', line 405 def inspect %(#<#{self.class.name}:#{self.object_id} @endpoint=#{@endpoint.inspect} @params=#{@params.inspect}>) end |
#on_error {|from, reason| ... } ⇒ Object
Registers callback function for handling error objects in view results stream.
244 245 246 247 |
# File 'lib/couchbase/view.rb', line 244 def on_error(&callback) @on_error = callback self # enable call chains end |
#take(n, params = {}) ⇒ Object
203 204 205 206 |
# File 'lib/couchbase/view.rb', line 203 def take(n, params = {}) params = params.merge(:limit => n) fetch(params) end |