lib/impala/cursor.rb in impala-0.1.2 vs lib/impala/cursor.rb in impala-0.1.3

- old
+ new

@@ -1,6 +1,9 @@ module Impala + # Cursors are used to iterate over result sets without loading them all + # into memory at once. This can be useful if you're dealing with lots of + # rows. It implements Enumerable, so you can use each/select/map/etc. class Cursor include Enumerable def initialize(handle, service, buffer_length=1024) @handle = handle @@ -9,21 +12,29 @@ @buffer_length = buffer_length @row_buffer = [] @done = false - @closed = false + @open = true end + def inspect + "#<#{self.class}#{open? ? '' : ' (CLOSED)'}>" + end + def each while row = fetch_row yield row end end + # Returns the next available row as a hash, or nil if there are none left. + # @return [Hash, nil] the next available row, or nil if there are none + # left + # @see #fetch_all def fetch_row - raise CursorError.new("Cursor has expired or been closed") if @closed + raise CursorError.new("Cursor has expired or been closed") unless @open if @row_buffer.empty? if @done return nil else @@ -32,16 +43,31 @@ end @row_buffer.shift end + # Returns all the remaining rows in the result set. + # @return [Array<Hash>] the remaining rows in the result set + # @see #fetch_one def fetch_all self.to_a end + # Close the cursor on the remote server. Once a cursor is closed, you + # can no longer fetch any rows from it. def close - @closed = true + @open = false @service.close(@handle) + end + + # Returns true if the cursor is still open. + def open? + @open + end + + # Returns true if there are any more rows to fetch. + def has_more? + !@done || !@row_buffer.empty? end private def fetch_more \ No newline at end of file