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)
-
- (Object) params
readonly
Returns the value of attribute params.
Instance Method Summary (collapse)
-
- (Object) each(params = {})
Yields each document that was fetched by view.
-
- (Array) fetch(params = {}) {|document| ... }
Performs query to Couchbase view.
-
- (View) initialize(bucket, endpoint, params = {})
constructor
Set up view endpoint and optional params.
-
- (String) inspect
Returns a string containing a human-readable representation of the View.
-
- (Object) on_error {|from, reason| ... }
Registers callback function for handling error objects in view results stream.
Constructor Details
- (View) initialize(bucket, endpoint, params = {})
Set up view endpoint and optional params
50 51 52 53 54 55 56 57 58 |
# File 'lib/couchbase/view.rb', line 50 def initialize(bucket, endpoint, params = {}) @bucket = bucket @endpoint = endpoint @params = 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
- (Object) params (readonly)
Returns the value of attribute params
38 39 40 |
# File 'lib/couchbase/view.rb', line 38 def params @params end |
Instance Method Details
- (Object) each(params = {})
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.
86 87 88 89 |
# File 'lib/couchbase/view.rb', line 86 def each(params = {}) return enum_for(:each, params) unless block_given? fetch(params) {|doc| yield(doc)} end |
- (Array) fetch(params = {}) {|document| ... }
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.
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 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 |
# File 'lib/couchbase/view.rb', line 232 def fetch(params = {}) params = @params.merge(params) = {:chunked => true, :extended => true} 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_couch_request(path, ) res = [] request.on_body do |chunk| res << chunk request.pause if chunk.value.nil? 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 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 loop do # feed response received chunks to the parser while r = res.shift 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 |
- (String) inspect
Returns a string containing a human-readable representation of the Couchbase::View
293 294 295 |
# File 'lib/couchbase/view.rb', line 293 def inspect %(#<#{self.class.name}:#{self.object_id} @endpoint=#{@endpoint.inspect} @params=#{@params.inspect}>) end |
- (Object) on_error {|from, reason| ... }
Registers callback function for handling error objects in view results stream.
127 128 129 130 |
# File 'lib/couchbase/view.rb', line 127 def on_error(&callback) @on_error = callback self # enable call chains end |