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
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
-
#design_doc ⇒ Object
readonly
Returns the value of attribute design_doc.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#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
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/couchbase/view.rb', line 87 def initialize(bucket, endpoint, params = {}) @bucket = bucket @endpoint = endpoint @design_doc, @name = parse_endpoint(endpoint) @wrapper_class = params.delete(:wrapper_class) || ViewRow @params = { :connection_timeout => 75_000 }.merge(params) unless @wrapper_class.respond_to?(:wrap) raise ArgumentError, "wrapper class should reposond to :wrap, check the options" end end |
Instance Attribute Details
#design_doc ⇒ Object (readonly)
Returns the value of attribute design_doc.
75 76 77 |
# File 'lib/couchbase/view.rb', line 75 def design_doc @design_doc end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
75 76 77 |
# File 'lib/couchbase/view.rb', line 75 def name @name end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
75 76 77 |
# File 'lib/couchbase/view.rb', line 75 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.
124 125 126 127 |
# File 'lib/couchbase/view.rb', line 124 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.
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/couchbase/view.rb', line 286 def fetch(params = {}) params = @params.merge(params) include_docs = params[:include_docs] quiet = params.fetch(:quiet, true) view = @bucket.client.getView(@design_doc, @name) query = Query.new(params) request = @bucket.client.query(view, query.generate) if block_given? block = Proc.new request.each do |data| doc = @wrapper_class.wrap(@bucket, data.getDocument) block.call(doc) end nil else docs = request.to_a.map { |data| @wrapper_class.wrap(@bucket, data.getDocument) } docs = ArrayWithTotalRows.new(docs) docs.total_rows = request.size docs 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.
325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/couchbase/view.rb', line 325 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
129 130 131 132 |
# File 'lib/couchbase/view.rb', line 129 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
342 343 344 |
# File 'lib/couchbase/view.rb', line 342 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.
175 176 177 178 |
# File 'lib/couchbase/view.rb', line 175 def on_error(&callback) @on_error = callback self # enable call chains end |
#take(n, params = {}) ⇒ Object
134 135 136 137 |
# File 'lib/couchbase/view.rb', line 134 def take(n, params = {}) params = params.merge(:limit => n) fetch(params) end |