Class: MoSQL::Tailer

Inherits:
Mongoriver::AbstractPersistentTailer
  • Object
show all
Defined in:
lib/mosql/tailer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backends, type, table, opts) ⇒ Tailer

Returns a new instance of Tailer.



12
13
14
15
16
# File 'lib/mosql/tailer.rb', line 12

def initialize(backends, type, table, opts)
  super(backends, type, opts)
  @table   = table
  @service = opts[:service] || "mosql"
end

Class Method Details

.create_table(db, tablename) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/mosql/tailer.rb', line 3

def self.create_table(db, tablename)
  db.create_table?(tablename) do
    column :service,   'TEXT'
    column :timestamp, 'INTEGER'
    primary_key [:service]
  end
  db[tablename.to_sym]
end

Instance Method Details

#read_timestampObject



18
19
20
21
22
23
24
25
# File 'lib/mosql/tailer.rb', line 18

def read_timestamp
  row = @table.where(:service => @service).select([:timestamp]).first
  if row
    BSON::Timestamp.new(row[:timestamp], 0)
  else
    BSON::Timestamp.new(0, 0)
  end
end

#write_timestamp(ts) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mosql/tailer.rb', line 27

def write_timestamp(ts)
  unless @did_insert
    begin
      @table.insert({:service => @service, :timestamp => ts.seconds})
    rescue Sequel::DatabaseError => e
      raise unless MoSQL::SQLAdapter.duplicate_key_error?(e)
    end
    @did_insert = true
  end
  @table.where(:service => @service).update(:timestamp => ts.seconds)
end