sdk: actually no, let's store all relay urls together in the same kvdb key.

This commit is contained in:
fiatjaf
2025-01-16 10:49:45 -03:00
parent 4cf9631c28
commit 46569b6ef4
7 changed files with 180 additions and 98 deletions

View File

@@ -57,17 +57,29 @@ func (s *Store) Close() error {
return nil
}
func (s *Store) Scan(prefix []byte, fn func(key []byte, value []byte) bool) error {
s.RLock()
defer s.RUnlock()
func (s *Store) Update(key []byte, f func([]byte) ([]byte, error)) error {
s.Lock()
defer s.Unlock()
prefixStr := string(prefix)
for k, v := range s.data {
if strings.HasPrefix(k, prefixStr) {
if !fn([]byte(k), v) {
break
}
}
var val []byte
if v, ok := s.data[string(key)]; ok {
// Return a copy to prevent modification of stored data
val = make([]byte, len(v))
copy(val, v)
}
newVal, err := f(val)
if err != nil {
return err
}
if newVal == nil {
delete(s.data, string(key))
} else {
// Store a copy to prevent modification of stored data
cp := make([]byte, len(newVal))
copy(cp, newVal)
s.data[string(key)] = cp
}
return nil
}