AutoKeyConvertHash added
This commit is contained in:
parent
a819b9b0e8
commit
b76953f57d
|
@ -31,7 +31,9 @@ class LogAn::Analyse
|
||||||
def dbs min, max = nil, &exe
|
def dbs min, max = nil, &exe
|
||||||
return Enumerator.new( self, :dbs, min, max) unless exe
|
return Enumerator.new( self, :dbs, min, max) unless exe
|
||||||
range = timerange min, max
|
range = timerange min, max
|
||||||
@lines.rdb.each &exe
|
@lines.rdb.each do |time, db|
|
||||||
|
exe.call db
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def search min, max = nil, &exe
|
def search min, max = nil, &exe
|
||||||
|
|
|
@ -9,6 +9,10 @@ class LogAn::Cache
|
||||||
@source, @data, self.type = source, data || {}, type
|
@source, @data, self.type = source, data || {}, type
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def close
|
||||||
|
@source.close
|
||||||
|
end
|
||||||
|
|
||||||
def flush!
|
def flush!
|
||||||
@data.each {|k,v| @source[k] = v }
|
@data.each {|k,v| @source[k] = v }
|
||||||
@data = {}
|
@data = {}
|
||||||
|
@ -69,6 +73,10 @@ class LogAn::AutoValueConvertHash
|
||||||
LogAn::Logging.debug encode: @encode, decode: @decode, each: @each
|
LogAn::Logging.debug encode: @encode, decode: @decode, each: @each
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def close
|
||||||
|
@source.close
|
||||||
|
end
|
||||||
|
|
||||||
def [] k
|
def [] k
|
||||||
decode @source[k]
|
decode @source[k]
|
||||||
end
|
end
|
||||||
|
@ -91,3 +99,36 @@ class LogAn::AutoValueConvertHash
|
||||||
end
|
end
|
||||||
end
|
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
|
flags: SBDB::CREATE | SBDB::Env::INIT_TXN | Bdb::DB_INIT_MPOOL
|
||||||
else env
|
else env
|
||||||
end
|
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,
|
@queue = @env[ "newids.queue", :type => SBDB::Queue,
|
||||||
:flags => SBDB::CREATE | SBDB::AUTO_COMMIT, :re_len => 16]
|
:flags => SBDB::CREATE | SBDB::AUTO_COMMIT, :re_len => 16]
|
||||||
@dbs, @counter = {}, 0
|
@dbs, @counter = Cache.new, 0
|
||||||
self.hash_func = lambda {|k|
|
self.hash_func = lambda {|k|
|
||||||
n = k.timestamp.to_i
|
n = k.timestamp.to_i
|
||||||
[n - (n % 3600)].pack 'N' # Hour-based rotation
|
n -= n % 3600
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue