Hibiki/utilities/specializedstore/memory.go

94 lines
2.1 KiB
Go

package specializedstore
import (
"git.gammaspectra.live/S.O.N.G/Hibiki/panako"
"sync"
)
type MemoryStore struct {
prints map[uint64][]panako.StoreRecord
mutex sync.RWMutex
}
func NewMemoryStore() *MemoryStore {
return &MemoryStore{
prints: make(map[uint64][]panako.StoreRecord),
}
}
func (s *MemoryStore) GetPrints() map[uint64][]panako.StoreRecord {
return s.prints
}
func (s *MemoryStore) GetPanakoMatch(record panako.StoreRecord, lookupRange int) chan *panako.MatchedRecord {
keyStart := record.Hash - uint64(lookupRange)
keyStop := record.Hash + uint64(lookupRange)
channel := make(chan *panako.MatchedRecord)
go func() {
defer close(channel)
s.mutex.RLock()
defer s.mutex.RUnlock()
for key := keyStart; key <= keyStop; key++ {
v, ok := s.prints[key]
if ok {
for _, r := range v {
channel <- &panako.MatchedRecord{
Query: record,
Match: r,
}
}
}
}
}()
return channel
}
func (s *MemoryStore) GetPanakoMatches(records []panako.StoreRecord, lookupRange int) chan *panako.MatchedRecord {
channel := make(chan *panako.MatchedRecord)
go func() {
defer close(channel)
for _, record := range records {
for match := range s.GetPanakoMatch(record, lookupRange) {
channel <- match
}
}
}()
return channel
}
func (s *MemoryStore) GetPanakoRecords(resourceId panako.ResourceId) []panako.StoreRecord {
return nil
}
func (s *MemoryStore) recordExists(record panako.StoreRecord) bool {
v, ok := s.prints[record.Hash]
if ok {
for _, r := range v {
if record.ResourceId == r.ResourceId && record.Time == r.Time && record.Frequency == r.Frequency {
return true
}
}
}
return false
}
func (s *MemoryStore) StorePanakoPrint(record panako.StoreRecord) {
if !s.recordExists(record) {
_, ok := s.prints[record.Hash]
if ok {
s.prints[record.Hash] = append(s.prints[record.Hash], record)
} else {
s.prints[record.Hash] = []panako.StoreRecord{record}
}
}
}
func (s *MemoryStore) StorePanakoPrints(records []panako.StoreRecord) {
for _, record := range records {
s.StorePanakoPrint(record)
}
}