Class: RubySnowflake::StreamingResult

Inherits:
Result
  • Object
show all
Defined in:
lib/ruby_snowflake/streaming_result.rb

Instance Attribute Summary

Attributes inherited from Result

#data

Instance Method Summary collapse

Methods inherited from Result

#[]=, #columns, #first, #get_all_rows

Constructor Details

#initialize(partition_count, row_type_data, retreive_proc) ⇒ StreamingResult

Returns a new instance of StreamingResult.



9
10
11
12
# File 'lib/ruby_snowflake/streaming_result.rb', line 9

def initialize(partition_count, row_type_data, retreive_proc)
  super(partition_count, row_type_data)
  @retreive_proc = retreive_proc
end

Instance Method Details

#eachObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby_snowflake/streaming_result.rb', line 14

def each
  return to_enum(:each) unless block_given?

  thread_pool = Concurrent::FixedThreadPool.new 1

  data.each_with_index do |_partition, index|
    next_index = [index+1, data.size-1].min
    if data[next_index].nil? # prefetch
      data[next_index] = Concurrent::Future.execute(executor: thread_pool) do
        @retreive_proc.call(next_index)
      end
    end

    if data[index].is_a? Concurrent::Future
      data[index] = data[index].value # wait for it to finish
    end

    data[index].each do |row|
      yield wrap_row(row)
    end

    # After iterating over the current partition, clear the data to release memory
    data[index].clear

    # Reassign to a symbol so:
    # - When looking at the list of partitions in `data` it is easier to detect
    # - Will raise an exception if `data.each` is attempted to be called again
    # - It won't trigger prefetch detection as `next_index`
    data[index] = :finished
  end
end

#lastObject



51
52
53
# File 'lib/ruby_snowflake/streaming_result.rb', line 51

def last
  not_implemented
end

#sizeObject



47
48
49
# File 'lib/ruby_snowflake/streaming_result.rb', line 47

def size
  not_implemented
end