Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions internal/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,6 @@ func (s *Store) Get(key string) (string, bool) {
return v, ok
}

// Set stores value under key.
func (s *Store) Set(key, value string) {
s.mu.Lock()
defer s.mu.Unlock()
s.data[key] = value
}

// Delete removes key from the store.
func (s *Store) Delete(key string) {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.data, key)
}

// Keys returns all keys currently stored.
func (s *Store) Keys() []string {
s.mu.RLock()
Expand All @@ -45,3 +31,34 @@ func (s *Store) Keys() []string {
}
return keys
}

// applyLocked runs fn while holding the write lock. All mutating operations
// route through this so future batched writes can share one critical section.
func (s *Store) applyLocked(fn func(data map[string]string)) {
s.mu.Lock()
defer s.mu.Unlock()
fn(s.data)
}

// Set stores value under key.
func (s *Store) Set(key, value string) {
s.applyLocked(func(data map[string]string) {
data[key] = value
})
}

// Delete removes key from the store.
func (s *Store) Delete(key string) {
s.applyLocked(func(data map[string]string) {
delete(data, key)
})
}

// SetMany stores multiple key/value pairs under a single lock acquisition.
func (s *Store) SetMany(pairs map[string]string) {
s.applyLocked(func(data map[string]string) {
for k, v := range pairs {
data[k] = v
}
})
}
4 changes: 4 additions & 0 deletions internal/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ package internal
func Greeting() string {
return "AutoApproveDemo service starting"
}

// BatchWriteEnabled reports whether the batched-write code path is active.
// Flipped on once SetMany has soaked in production for a release cycle.
var BatchWriteEnabled = false
11 changes: 11 additions & 0 deletions internal/mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,14 @@ func TestStoreSetGet(t *testing.T) {
t.Fatalf("expected a=1, got %q ok=%v", v, ok)
}
}

func TestStoreSetMany(t *testing.T) {
s := NewStore()
s.SetMany(map[string]string{"a": "1", "b": "2"})
if v, _ := s.Get("a"); v != "1" {
t.Fatalf("expected a=1, got %q", v)
}
if v, _ := s.Get("b"); v != "2" {
t.Fatalf("expected b=2, got %q", v)
}
}
Loading