Class: CouchProxy::RowFilter
- Inherits:
-
JSON::Stream::Builder
- Object
- JSON::Stream::Builder
- CouchProxy::RowFilter
- Defined in:
- lib/couchproxy/row_filter.rb
Overview
A JSON::Stream::Parser listener that listens for the ‘rows’ key in a CouchDB map/reduce result stream. As row objects are parsed they are sent to callbacks for processing. Typically the callback will perform some kind of reduce on the rows before sending them to the client.
Example usage: parser = JSON::Stream::Parser.new filter = CouchProxy::RowFilter.new(parser) do
total_rows {|total| puts total }
rows do |rows, complete|
# process rows, complete tells us if this is the last row
end
end
Constant Summary collapse
- TOTAL_ROWS =
'total_rows'.freeze
- MAX_ROWS =
100
Instance Method Summary collapse
- #end_document ⇒ Object
- #end_object ⇒ Object
-
#initialize(parser, &block) ⇒ RowFilter
constructor
A new instance of RowFilter.
- #key(key) ⇒ Object
- #value(value) ⇒ Object
Constructor Details
#initialize(parser, &block) ⇒ RowFilter
Returns a new instance of RowFilter.
23 24 25 26 27 28 |
# File 'lib/couchproxy/row_filter.rb', line 23 def initialize(parser, &block) @listeners = Hash.new {|h, k| h[k] = [] } @total_rows_key = false super(parser) instance_eval(&block) if block_given? end |
Instance Method Details
#end_document ⇒ Object
56 57 58 |
# File 'lib/couchproxy/row_filter.rb', line 56 def end_document notify_rows(@stack.pop.obj['rows'], true) end |
#end_object ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/couchproxy/row_filter.rb', line 60 def end_object super # row object complete if @stack.size == 2 && @stack[-1].obj.size >= MAX_ROWS notify_rows(@stack.pop.obj, false) @stack.push(JSON::Stream::ArrayNode.new) end end |
#key(key) ⇒ Object
43 44 45 46 |
# File 'lib/couchproxy/row_filter.rb', line 43 def key(key) super @total_rows_key = (@stack.size == 1 && key == TOTAL_ROWS) end |
#value(value) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/couchproxy/row_filter.rb', line 48 def value(value) super if @total_rows_key @total_rows_key = false notify_total_rows(value) end end |