Sha256: 741a7429f5b940264faeab6732e0f5704ace8e4c48b104626a67ffde2fc54987

Contents?: true

Size: 1011 Bytes

Versions: 4

Compression:

Stored size: 1011 Bytes

Contents

module MoSQL
  class Tailer < Mongoriver::AbstractPersistentTailer
    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

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

    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

    def write_timestamp(ts)
      begin
        @table.insert({:service => @service, :timestamp => ts.seconds})
      rescue Sequel::DatabaseError => e
        raise unless e.message =~ /duplicate key value violates unique constraint/
        @table.where(:service => @service).update(:timestamp => ts.seconds)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mosql-0.2.0 lib/mosql/tailer.rb
mosql-0.1.2 lib/mosql/tailer.rb
mosql-0.1.1 lib/mosql/tailer.rb
mosql-0.1.0 lib/mosql/tailer.rb