spark-server/internal/module/device/model/memory_registry.go

49 lines
1.0 KiB
Go

package model
import (
"context"
"sync"
"time"
devicelogic "spark-server/internal/module/device/logic"
)
type MemoryRegistry struct {
mu sync.Mutex
byFP map[string]*devicelogic.RegistryRow
byDID map[uint64]*devicelogic.RegistryRow
}
func NewMemoryRegistry() *MemoryRegistry {
return &MemoryRegistry{
byFP: make(map[string]*devicelogic.RegistryRow),
byDID: make(map[uint64]*devicelogic.RegistryRow),
}
}
func (m *MemoryRegistry) Upsert(_ context.Context, row *devicelogic.RegistryRow) error {
if row == nil {
return nil
}
m.mu.Lock()
defer m.mu.Unlock()
if old, ok := m.byFP[row.FingerprintHash]; ok {
cp := *old
cp.DeviceName = row.DeviceName
cp.IDFV = row.IDFV
cp.AndroidID = row.AndroidID
cp.LastClientIP = row.LastClientIP
cp.UpdatedAt = time.Now()
m.byFP[row.FingerprintHash] = &cp
m.byDID[cp.DeviceID] = &cp
return nil
}
cp := *row
if cp.UpdatedAt.IsZero() {
cp.UpdatedAt = time.Now()
}
m.byFP[row.FingerprintHash] = &cp
m.byDID[row.DeviceID] = &cp
return nil
}