Class: Baza::QueryBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/baza/query_buffer.rb

Overview

This class buffers a lot of queries and flushes them out via transactions.

Constant Summary collapse

INITIALIZE_ARGS_ALLOWED =
[:db, :debug, :flush_async]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ QueryBuffer

Constructor. Takes arguments to be used and a block.



7
8
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
# File 'lib/baza/query_buffer.rb', line 7

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

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

  if block_given?
    if @args[:flush_async]
      @args[: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
end

Instance Attribute Details

#thread_asyncObject (readonly)

Returns the value of attribute thread_async.



3
4
5
# File 'lib/baza/query_buffer.rb', line 3

def thread_async
  @thread_async
end

Instance Method Details

#delete(table, where) ⇒ Object

Delete as on a normal Baza::Db.

Example

buffer.delete(:users, => 5)



55
56
57
58
59
# File 'lib/baza/query_buffer.rb', line 55

def delete(table, where)
  STDOUT.puts "Delete called on table #{table} with arguments: '#{where}'." if @debug
  query(@args[:db].delete(table, where, :return_sql => true))
  return nil
end

#flushObject

Flushes all queries out in a transaction. This will automatically be called for every 1000 queries.



87
88
89
90
91
92
93
# File 'lib/baza/query_buffer.rb', line 87

def flush
  if @args[:flush_async]
    flush_async
  else
    flush_real
  end
end

#insert(table, data) ⇒ Object

Plans to inset a hash into a table. It will only be inserted when flush is called.

Examples

buffer.insert(:users, => “John Doe”)



81
82
83
84
# File 'lib/baza/query_buffer.rb', line 81

def insert(table, data)
  query(@args[:db].insert(table, data, :return_sql => true))
  return nil
end

#query(str) ⇒ Object

Adds a query to the buffer.



41
42
43
44
45
46
47
48
49
50
# File 'lib/baza/query_buffer.rb', line 41

def query(str)
  @lock.synchronize do
    STDOUT.print "Adding to buffer: #{str}\n" if @debug
    @queries << str
    @queries_count += 1
  end

  flush if @queries_count >= 1000
  return nil
end

#update(table, update, terms) ⇒ Object

Update as on a normal Baza::Db.

Example

buffer.update(:users, => “Kasper”, => 5)



64
65
66
67
68
# File 'lib/baza/query_buffer.rb', line 64

def update(table, update, terms)
  STDOUT.puts "Update called on table #{table}." if @debug
  query(@args[:db].update(table, update, terms, :return_sql => true))
  return nil
end

#upsert(table, data, terms) ⇒ Object

Shortcut to doing upsert through the buffer instead of through the db-object with the buffer as an argument.

Example

buffer.upsert(:users, => 5, => “Kasper”)



73
74
75
76
# File 'lib/baza/query_buffer.rb', line 73

def upsert(table, data, terms)
  @args[:db].upsert(table, data, terms, :buffer => self)
  return nil
end