From cfee5861074169017e2eca6b05655cbba53f62e3 Mon Sep 17 00:00:00 2001 From: Mateo Presa Castro Date: Sat, 15 Nov 2025 10:23:16 +0100 Subject: [PATCH 1/2] feat: use sync.Map --- internal/kv/kv_test.go | 4 ++-- internal/store/store.go | 41 +++++++++++++++++++---------------------- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/internal/kv/kv_test.go b/internal/kv/kv_test.go index 442bb0f..3b2f6f8 100644 --- a/internal/kv/kv_test.go +++ b/internal/kv/kv_test.go @@ -225,11 +225,11 @@ func TestDistributedKVReplication(t *testing.T) { // Read from node2. Since it left no data should be replicated. value, err = node2.Get("test-key2") if err == nil { - t.Fatalf("we should no have be able to read the data from a node that left the cluster: %v", err) + t.Fatalf("we should not be able to read the data from a node that left the cluster: %v", err) } if string(value) == "test-value2" { - t.Errorf("we read the data from a node not in the cluser") + t.Errorf("we read the data from a node not in the cluster") } } diff --git a/internal/store/store.go b/internal/store/store.go index 48f0c57..da55a06 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -13,51 +13,48 @@ type Storer interface { } type Store struct { - db map[string][]byte - mu sync.RWMutex + m sync.Map } func New() *Store { - return &Store{db: map[string][]byte{}} + return &Store{m: sync.Map{}} } func (s *Store) Get(key string) ([]byte, error) { - s.mu.RLock() - defer s.mu.RUnlock() - value, ok := s.db[key] + value, ok := s.m.Load(key) if !ok { return nil, errors.New("not found") } - return value, nil + + bytes, ok := value.([]byte) + if !ok { + return nil, errors.New("not bytes") + } + return bytes, nil } func (s *Store) Set(key string, value []byte) error { - s.mu.Lock() - defer s.mu.Unlock() - s.db[key] = value + s.m.Store(key, value) return nil } func (s *Store) Delete(key string) error { - s.mu.Lock() - defer s.mu.Unlock() - _, ok := s.db[key] - if !ok { - return errors.New("not found") - } - delete(s.db, key) + s.m.Delete(key) return nil } func (s *Store) List() <-chan []byte { c := make(chan []byte) - s.mu.RLock() go func() { - defer s.mu.RUnlock() defer close(c) - for _, val := range s.db { - c <- val - } + s.m.Range(func(key any, val any) bool { + bytes, ok := val.([]byte) + if !ok { + return false + } + c <- bytes + return true + }) }() return c } From ed25d201ca4ae85ddb8870d1bab757936b89378e Mon Sep 17 00:00:00 2001 From: Mateo Presa Castro Date: Sat, 15 Nov 2025 10:29:35 +0100 Subject: [PATCH 2/2] fix: comments --- internal/store/store.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/internal/store/store.go b/internal/store/store.go index da55a06..f1fdc19 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -17,7 +17,7 @@ type Store struct { } func New() *Store { - return &Store{m: sync.Map{}} + return &Store{} } func (s *Store) Get(key string) ([]byte, error) { @@ -28,7 +28,7 @@ func (s *Store) Get(key string) ([]byte, error) { bytes, ok := value.([]byte) if !ok { - return nil, errors.New("not bytes") + return nil, errors.New("value is not of type []byte") } return bytes, nil } @@ -48,10 +48,7 @@ func (s *Store) List() <-chan []byte { go func() { defer close(c) s.m.Range(func(key any, val any) bool { - bytes, ok := val.([]byte) - if !ok { - return false - } + bytes, _ := val.([]byte) c <- bytes return true })