mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-07-21 00:42:25 +02:00
kvstore: get rid of useless copy()s
This commit is contained in:
@ -24,10 +24,7 @@ func (s *Store) Get(key []byte) ([]byte, error) {
|
|||||||
defer s.RUnlock()
|
defer s.RUnlock()
|
||||||
|
|
||||||
if val, ok := s.data[string(key)]; ok {
|
if val, ok := s.data[string(key)]; ok {
|
||||||
// return a copy to prevent modification of stored data
|
return val, nil
|
||||||
cp := make([]byte, len(val))
|
|
||||||
copy(cp, val)
|
|
||||||
return cp, nil
|
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
@ -36,10 +33,7 @@ func (s *Store) Set(key []byte, value []byte) error {
|
|||||||
s.Lock()
|
s.Lock()
|
||||||
defer s.Unlock()
|
defer s.Unlock()
|
||||||
|
|
||||||
// store a copy to prevent modification of stored data
|
s.data[string(key)] = value
|
||||||
cp := make([]byte, len(value))
|
|
||||||
copy(cp, value)
|
|
||||||
s.data[string(key)] = cp
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,13 +55,7 @@ func (s *Store) Update(key []byte, f func([]byte) ([]byte, error)) error {
|
|||||||
s.Lock()
|
s.Lock()
|
||||||
defer s.Unlock()
|
defer s.Unlock()
|
||||||
|
|
||||||
var val []byte
|
val, _ := s.data[string(key)]
|
||||||
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)
|
newVal, err := f(val)
|
||||||
if err == kvstore.NoOp {
|
if err == kvstore.NoOp {
|
||||||
return nil
|
return nil
|
||||||
@ -78,10 +66,7 @@ func (s *Store) Update(key []byte, f func([]byte) ([]byte, error)) error {
|
|||||||
if newVal == nil {
|
if newVal == nil {
|
||||||
delete(s.data, string(key))
|
delete(s.data, string(key))
|
||||||
} else {
|
} else {
|
||||||
// Store a copy to prevent modification of stored data
|
s.data[string(key)] = newVal
|
||||||
cp := make([]byte, len(newVal))
|
|
||||||
copy(cp, newVal)
|
|
||||||
s.data[string(key)] = cp
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user