Class: QC::DurableArray

Inherits:
Object
  • Object
show all
Defined in:
lib/queue_classic/durable_array.rb

Instance Method Summary collapse

Constructor Details

#initialize(database) ⇒ DurableArray

Returns a new instance of DurableArray.



4
5
6
7
8
# File 'lib/queue_classic/durable_array.rb', line 4

def initialize(database)
  @database = database
  @table_name = @database.table_name
  @top_boundary = @database.top_boundary
end

Instance Method Details

#<<(details) ⇒ Object



10
11
12
13
# File 'lib/queue_classic/durable_array.rb', line 10

def <<(details)
  execute("INSERT INTO #{@table_name} (details) VALUES ($1)", OkJson.encode(details))
  @database.notify if ENV["QC_LISTENING_WORKER"] == "true"
end

#countObject



15
16
17
# File 'lib/queue_classic/durable_array.rb', line 15

def count
  execute("SELECT COUNT(*) FROM #{@table_name}")[0]["count"].to_i
end

#delete(job) ⇒ Object



19
20
21
22
# File 'lib/queue_classic/durable_array.rb', line 19

def delete(job)
  execute("DELETE FROM #{@table_name} WHERE id = $1;", job.id)
  job
end

#eachObject



32
33
34
35
36
# File 'lib/queue_classic/durable_array.rb', line 32

def each
  execute("SELECT * FROM #{@table_name} ORDER BY id ASC;").each do |r|
    yield Job.new(r)
  end
end

#execute(sql, *params) ⇒ Object



46
47
48
# File 'lib/queue_classic/durable_array.rb', line 46

def execute(sql, *params)
  @database.execute(sql, *params)
end

#find_manyObject



42
43
44
# File 'lib/queue_classic/durable_array.rb', line 42

def find_many
  execute(*yield).map { |r| Job.new(r) }
end

#find_one(&blk) ⇒ Object



38
39
40
# File 'lib/queue_classic/durable_array.rb', line 38

def find_one(&blk)
  find_many(&blk).pop
end

#firstObject



28
29
30
# File 'lib/queue_classic/durable_array.rb', line 28

def first
  find_one { ["SELECT * FROM lock_head($1, $2);", @table_name, @top_boundary] }
end

#search_details_column(q) ⇒ Object



24
25
26
# File 'lib/queue_classic/durable_array.rb', line 24

def search_details_column(q)
  find_many { ["SELECT * FROM #{@table_name} WHERE details LIKE $1;", "%#{q}%"] }
end