Cache added, Main created
This commit is contained in:
parent
9f9b6b63a2
commit
2eb887d9cc
79
lib/logan/cache.rb
Normal file
79
lib/logan/cache.rb
Normal file
|
@ -0,0 +1,79 @@
|
|||
|
||||
class Cache
|
||||
READ = 1
|
||||
WRITE = 2
|
||||
attr_reader :source, :data
|
||||
|
||||
def initialize source, type = nil, data = nil
|
||||
type ||= READ | WRITE
|
||||
@source, @data, self.type = source, data || {}, type
|
||||
end
|
||||
|
||||
def flush!
|
||||
@data.each {|k,v| @obj[k] = v }
|
||||
@data = {}
|
||||
end
|
||||
|
||||
def dget k
|
||||
@data[k] ||= @obj[k]
|
||||
end
|
||||
|
||||
def oget k
|
||||
@data[k] || @obj[k]
|
||||
end
|
||||
|
||||
def dset k, v
|
||||
@data[k] ||= v
|
||||
end
|
||||
|
||||
def oset k, v
|
||||
@obj[k] = v
|
||||
end
|
||||
|
||||
def type= type
|
||||
self.read, self.write = type & 1 > 0, type & 2 > 0
|
||||
type
|
||||
end
|
||||
|
||||
def read_cache= type
|
||||
@type &= ~ (type ? 0 : 1)
|
||||
define_singleton_method :[], type ? :oget, :dget
|
||||
end
|
||||
|
||||
def write_cache= type
|
||||
@type &= ~ (type ? 0 : 2)
|
||||
define_singleton_method :[], type ? :oset, :dset
|
||||
end
|
||||
|
||||
#include Enumerable
|
||||
#def each &e
|
||||
#return Enumerator.new self, :each unless e
|
||||
#flush!
|
||||
#@obj.each &e
|
||||
#self
|
||||
#end
|
||||
end
|
||||
|
||||
class AutoValueConvertHash
|
||||
include Enumerable
|
||||
|
||||
def initialize obj, encode = nil, each = nil, &decode
|
||||
@object, @encoder = obj, decode.nil? ? encode || Marshal.method( :dump) : nil,
|
||||
@each = each || obj.method( :each) rescue NameError
|
||||
@decode = decode || Marshal.method( :restore)
|
||||
end
|
||||
|
||||
def [] k
|
||||
decode.call @object[k]
|
||||
end
|
||||
|
||||
def []= k, v
|
||||
@object[k] = encode.call v
|
||||
end
|
||||
|
||||
def each *paras
|
||||
@each.call *paras do |k, v|
|
||||
yield k, decode( v)
|
||||
end
|
||||
end
|
||||
end
|
47
lib/logan/inc/main.rb
Normal file
47
lib/logan/inc/main.rb
Normal file
|
@ -0,0 +1,47 @@
|
|||
|
||||
require 'sbdb'
|
||||
require 'safebox'
|
||||
require 'robustserver'
|
||||
|
||||
module LogAn::Inc
|
||||
class Main < RobustServer
|
||||
def config db, type = nil
|
||||
type ||= 1+4
|
||||
ret = @etc[ 'inc.cnf', db, SBDB::RDONLY]
|
||||
ret = AutoValueConvertHash.new ret if type&4 > 0
|
||||
ret = Cache.new ret, type&3 if type&3 > 0
|
||||
ret
|
||||
end
|
||||
|
||||
def initialize conf
|
||||
super
|
||||
@conf = conf
|
||||
@logs = LogAn::Loglines.new 'logs'
|
||||
etc = @conf[:etc] || 'etc'
|
||||
Dir.mkdir etc rescue Errno::EEXIST
|
||||
@etc = SBDB::Env.new( etc,
|
||||
log_config: SBDB::Env::LOG_IN_MEMORY | SBDB::Env::LOG_AUTO_REMOVE,
|
||||
flags: SBDB::CREATE | SBDB::Env::INIT_TXN | Bdb::DB_INIT_MPOOL)
|
||||
@hosts = config 'hosts'
|
||||
@files = config 'files'
|
||||
@fileparser = config 'fileparser'
|
||||
@serv = LogAn::Inc.new :sock => TCPServer.new( *@conf[:server])
|
||||
@sigs[:INT] = @sigs[:TERM] = method(:shutdown)
|
||||
end
|
||||
|
||||
def at_exit
|
||||
@logs and @logs.close
|
||||
@etc and @etc.close
|
||||
end
|
||||
|
||||
def shutdown s = nil
|
||||
$stderr.puts [:signal, s, Signal[s]].inspect if s
|
||||
@serv.close
|
||||
exit 0
|
||||
end
|
||||
|
||||
def run
|
||||
@serv.run
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue