Class: Couchbase::View
- Inherits:
-
Object
- Object
- Couchbase::View
- Includes:
- Enumerable
- Defined in:
- lib/couchbase/view.rb
Overview
This class implements Couchbase View execution
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.
-
#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.
Constructor Details
#initialize(bucket, endpoint, params = {}) ⇒ View
Set up view endpoint and optional params
74 75 76 77 78 79 80 81 82 |
# File 'lib/couchbase/view.rb', line 74 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.
62 63 64 |
# File 'lib/couchbase/view.rb', line 62 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.
110 111 112 113 |
# File 'lib/couchbase/view.rb', line 110 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.
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/couchbase/view.rb', line 262 def fetch(params = {}) params = @params.merge(params) = {: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 include_docs = params.delete(:include_docs) quiet = true if params.has_key?(:quiet) quiet = params.delete(:quiet) end path = Utils.build_query(@endpoint, params) request = @bucket.make_http_request(path, ) res = [] request.on_body do |chunk| res << chunk request.pause if chunk.value.nil? || chunk.error end filter = ["/rows/", "/errors/"] filter << "/total_rows" unless block_given? parser = YAJI::Parser.new(:filter => filter, :with_path => true) docs = [] parser.on_object do |path, obj| case path when "/total_rows" # if total_rows key present, save it and take next object docs.instance_eval("def total_rows; #{obj}; end") when "/errors/" from, reason = obj["from"], obj["reason"] if @on_error @on_error.call(from, reason) else raise Error::View.new(from, reason) end else if include_docs val, flags, cas = @bucket.get(obj['id'], :extended => true, :quiet => quiet) obj['doc'] = { 'value' => val, 'meta' => { 'id' => obj['id'], 'flags' => flags, 'cas' => cas } } end if block_given? yield @wrapper_class.wrap(@bucket, obj) else docs << @wrapper_class.wrap(@bucket, obj) end end end # run event loop until the terminating chunk will be found # last_res variable keeps latest known chunk of the result last_res = nil while true # feed response received chunks to the parser while r = res.shift if r.error if @on_error @on_error.call("http_error", r.error) break else raise Error::View.new("http_error", r.error, nil) end end last_res = r parser << r.value end if last_res.nil? || !last_res.completed? # shall we run the event loop? request.continue else break end end # return nil for call with block block_given? ? nil : docs end |
#inspect ⇒ String
Returns a string containing a human-readable representation of the Couchbase::View
347 348 349 |
# File 'lib/couchbase/view.rb', line 347 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.
151 152 153 154 |
# File 'lib/couchbase/view.rb', line 151 def on_error(&callback) @on_error = callback self # enable call chains end |