Method: Baza::QueryBuffer#initialize

Defined in:
lib/baza/query_buffer.rb

#initialize(args) ⇒ QueryBuffer

Constructor. Takes arguments to be used and a block.



9
10
11
12
13
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
# File 'lib/baza/query_buffer.rb', line 9

def initialize(args)
  @args = args
  @db = args.fetch(:db)
  @queries = []
  @inserts = {}
  @queries_count = 0
  @queries_size = 0
  @debug = @args[:debug]
  @lock = Mutex.new

  STDOUT.puts "Query buffer started." if @debug

  return unless block_given?

  if @args[:flush_async]
    @db.clone_conn do |db_flush_async|
      @db_flush_async = db_flush_async

      begin
        yield(self)
      ensure
        flush
        thread_async_join
      end
    end
  else
    begin
      yield(self)
    ensure
      flush
      thread_async_join
    end
  end
end