Hibiki/panako/store.go

57 lines
1.7 KiB
Go

package panako
type Store interface {
GetPanakoMatch(record StoreRecord, lookupRange int) chan *MatchedRecord
GetPanakoMatches(records []StoreRecord, lookupRange int) chan *MatchedRecord
//GetPanakoRecords optional to implement
GetPanakoRecords(resourceId ResourceId) []StoreRecord
StorePanakoPrint(record StoreRecord)
StorePanakoPrints(records []StoreRecord)
}
type ResourceId uint64
type MatchedRecord struct {
Query StoreRecord
Match StoreRecord
}
type StoreRecord struct {
ResourceId ResourceId
Hash uint64
Time uint32
Frequency uint32
}
// GetCompactPackedPrint Only use this when the Strategy instance option Instance.HashType is CompactHash, and bounds range is known for Time (limited to 20-bit) and Frequency (limited to 12-bit)
// This lets most of the max range for Frequency, and several hours of uninterrupted timestamps for Time
func (r *StoreRecord) GetCompactPackedPrint() (uint32, uint32) {
return uint32(r.Hash), (r.Time & 0b1111_1111_1111_1111_1111) | ((r.Frequency & 0b1111_1111_1111) << 20)
}
func (r *StoreRecord) GetPackedPrint() (uint64, uint64) {
return r.Hash, (uint64(r.Time) << 32) | uint64(r.Frequency)
}
func (r *MatchedRecord) DeltaTime() int64 {
return int64(r.Match.Time) - int64(r.Query.Time)
}
func NewStoreRecordFromCompactPacked(resourceId ResourceId, hash, packed uint32) StoreRecord {
return StoreRecord{
ResourceId: resourceId,
Hash: uint64(hash),
Time: packed & 0b1111_1111_1111_1111_1111,
Frequency: (packed >> 20) & 0b1111_1111_1111,
}
}
func NewStoreRecordFromPacked(resourceId ResourceId, hash, packed uint64) StoreRecord {
return StoreRecord{
ResourceId: resourceId,
Hash: hash,
Time: uint32(packed >> 32),
Frequency: uint32(packed & 0xFFFFFFFF),
}
}