AutoKeyConvertHash added
This commit is contained in:
parent
a819b9b0e8
commit
b76953f57d
3 changed files with 51 additions and 4 deletions
|
@ -31,7 +31,9 @@ class LogAn::Analyse
|
|||
def dbs min, max = nil, &exe
|
||||
return Enumerator.new( self, :dbs, min, max) unless exe
|
||||
range = timerange min, max
|
||||
@lines.rdb.each &exe
|
||||
@lines.rdb.each do |time, db|
|
||||
exe.call db
|
||||
end
|
||||
end
|
||||
|
||||
def search min, max = nil, &exe
|
||||
|
|
|
@ -9,6 +9,10 @@ class LogAn::Cache
|
|||
@source, @data, self.type = source, data || {}, type
|
||||
end
|
||||
|
||||
def close
|
||||
@source.close
|
||||
end
|
||||
|
||||
def flush!
|
||||
@data.each {|k,v| @source[k] = v }
|
||||
@data = {}
|
||||
|
@ -69,6 +73,10 @@ class LogAn::AutoValueConvertHash
|
|||
LogAn::Logging.debug encode: @encode, decode: @decode, each: @each
|
||||
end
|
||||
|
||||
def close
|
||||
@source.close
|
||||
end
|
||||
|
||||
def [] k
|
||||
decode @source[k]
|
||||
end
|
||||
|
@ -91,3 +99,36 @@ class LogAn::AutoValueConvertHash
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
class LogAn::AutoKeyConvertHash
|
||||
include Enumerable
|
||||
attr_reader :source
|
||||
|
||||
def initialize source, encode = nil, each = nil, &decode
|
||||
@source, @encode = source, encode || ( decode.nil? && Marshal.method( :dump) )
|
||||
@each, @decode = each, decode || Marshal.method( :restore)
|
||||
@each ||= source.method( :each) rescue NameError
|
||||
define_singleton_method :encode, &@encode if @encode
|
||||
define_singleton_method :decode, &@decode if @decode
|
||||
LogAn::Logging.debug encode: @encode, decode: @decode, each: @each
|
||||
end
|
||||
|
||||
def close
|
||||
@source.close
|
||||
end
|
||||
|
||||
def [] k
|
||||
@source[ encode( k)]
|
||||
end
|
||||
|
||||
def []= k, v
|
||||
@source[ encode( k)] = v
|
||||
end
|
||||
|
||||
def each *paras
|
||||
return Enumerator.new self, :each unless block_given?
|
||||
@each.call *paras do |k, v|
|
||||
yield decode( k), v
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -26,13 +26,17 @@ module LogAn
|
|||
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]
|
||||
@rdb = AutoValueConvertHash.new(
|
||||
AutoKeyConvertHash.new(
|
||||
@env[ 'rotates.db', :type => SBDB::Btree, :flags => SBDB::CREATE | SBDB::AUTO_COMMIT],
|
||||
lambda {|key| [key.to_i].pack 'N' }) {|key| Time.at key.unpack( 'N') },
|
||||
lambda {|val| String === val ? val : val.raw } {|val| val && UUIDTools::UUID.parse_raw( val) }
|
||||
@queue = @env[ "newids.queue", :type => SBDB::Queue,
|
||||
:flags => SBDB::CREATE | SBDB::AUTO_COMMIT, :re_len => 16]
|
||||
@dbs, @counter = {}, 0
|
||||
@dbs, @counter = Cache.new, 0
|
||||
self.hash_func = lambda {|k|
|
||||
n = k.timestamp.to_i
|
||||
[n - (n % 3600)].pack 'N' # Hour-based rotation
|
||||
n -= n % 3600
|
||||
}
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue