2010-02-26 01:57:26 +01:00
|
|
|
#!/usr/bin/ruby
|
|
|
|
|
|
|
|
require 'sbdb'
|
|
|
|
require 'uuidtools'
|
2010-03-23 11:06:05 +01:00
|
|
|
require 'logan'
|
2010-02-26 01:57:26 +01:00
|
|
|
|
2010-03-23 11:06:05 +01:00
|
|
|
module LogAn
|
|
|
|
class Loglines
|
2010-04-01 14:42:39 +02:00
|
|
|
attr_reader :env, :rdb, :dbs, :counter
|
2010-02-26 01:57:26 +01:00
|
|
|
|
2010-03-23 11:06:05 +01:00
|
|
|
def self.new *paras
|
|
|
|
ret = obj = super( *paras)
|
|
|
|
begin ret = yield obj
|
|
|
|
ensure
|
|
|
|
SBDB.raise_barrier &obj.method( :close)
|
|
|
|
end if block_given?
|
|
|
|
ret
|
|
|
|
end
|
2010-02-26 01:57:26 +01:00
|
|
|
|
2010-03-23 11:06:05 +01:00
|
|
|
def initialize env = nil
|
|
|
|
env ||= 'logs'
|
|
|
|
@env = if String === env
|
|
|
|
Dir.mkdir env rescue Errno::EEXIST
|
|
|
|
SBDB::Env.new env,
|
|
|
|
log_config: SBDB::Env::LOG_IN_MEMORY | SBDB::Env::LOG_AUTO_REMOVE,
|
|
|
|
flags: SBDB::CREATE | SBDB::Env::INIT_TXN | Bdb::DB_INIT_MPOOL
|
|
|
|
else env
|
|
|
|
end
|
|
|
|
@rdb = @env[ 'rotates.db', :type => SBDB::Btree, :flags => SBDB::CREATE | SBDB::AUTO_COMMIT]
|
2010-04-01 23:32:24 +02:00
|
|
|
@queue = @env[ "newids.queue", :type => SBDB::Queue,
|
|
|
|
:flags => SBDB::CREATE | SBDB::AUTO_COMMIT, :re_len => 16]
|
2010-04-01 14:42:39 +02:00
|
|
|
@dbs, @counter = {}, 0
|
2010-03-29 11:50:59 +02:00
|
|
|
self.hash_func = lambda {|k|
|
2010-03-23 11:06:05 +01:00
|
|
|
[k.timestamp.to_i/60/60].pack 'N' # Hour-based rotation
|
|
|
|
}
|
|
|
|
end
|
2010-02-26 01:57:26 +01:00
|
|
|
|
2010-03-23 11:06:05 +01:00
|
|
|
def close
|
|
|
|
@env.close
|
|
|
|
end
|
2010-02-26 01:57:26 +01:00
|
|
|
|
2010-03-23 11:06:05 +01:00
|
|
|
def hash_func= exe
|
|
|
|
hash_func &exe
|
2010-02-26 01:57:26 +01:00
|
|
|
end
|
|
|
|
|
2010-03-23 11:06:05 +01:00
|
|
|
def hash_func &exe
|
|
|
|
@hash_func = exe if exe
|
|
|
|
@hash_func
|
|
|
|
end
|
2010-02-26 01:57:26 +01:00
|
|
|
|
2010-03-23 11:06:05 +01:00
|
|
|
def hashing key
|
|
|
|
@hash_func.call key
|
|
|
|
end
|
2010-02-26 01:57:26 +01:00
|
|
|
|
2010-03-23 11:06:05 +01:00
|
|
|
def db_name id
|
|
|
|
hash = hashing id
|
|
|
|
name = @rdb[ hash]
|
|
|
|
if name
|
|
|
|
name = UUIDTools::UUID.parse_raw name
|
|
|
|
else
|
|
|
|
name = UUIDTools::UUID.timestamp_create
|
|
|
|
@rdb[ hash] = name.raw
|
|
|
|
end
|
|
|
|
name
|
|
|
|
end
|
2010-02-26 01:57:26 +01:00
|
|
|
|
2010-03-23 11:06:05 +01:00
|
|
|
def db name
|
2010-04-01 23:32:24 +02:00
|
|
|
@dbs[name] ||= @env[ name.to_s, :type => SBDB::Btree,
|
|
|
|
:flags => SBDB::CREATE | SBDB::AUTO_COMMIT]
|
2010-03-23 11:06:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def sync
|
|
|
|
@dbs.each {|name, db| db.sync }
|
|
|
|
@rdb.sync
|
|
|
|
end
|
2010-02-26 01:57:26 +01:00
|
|
|
|
2010-03-23 11:06:05 +01:00
|
|
|
def put val, sid = nil
|
|
|
|
id = UUIDTools::UUID.timestamp_create
|
|
|
|
dat = [sid || 0x10, val].pack 'Na*'
|
|
|
|
name = db_name id
|
|
|
|
db( name)[ id.raw] = dat
|
2010-04-01 14:42:39 +02:00
|
|
|
@counter += 1
|
2010-04-01 14:38:59 +02:00
|
|
|
@queue.push id.raw
|
2010-03-23 11:06:05 +01:00
|
|
|
end
|
|
|
|
alias emit put
|
|
|
|
|
|
|
|
def get key
|
|
|
|
name = db_name id
|
|
|
|
db( name)[ id.raw]
|
|
|
|
end
|
2010-02-26 01:57:26 +01:00
|
|
|
end
|
|
|
|
end
|