-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a concurrently safe map for the underlying router map
- Loading branch information
Showing
4 changed files
with
123 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package syncx | ||
|
||
import "sync" | ||
|
||
type Map[K comparable, V any] struct { | ||
m sync.Map | ||
} | ||
|
||
func (m *Map[K, V]) Delete(key K) { | ||
m.m.Delete(key) | ||
} | ||
|
||
func (m *Map[K, V]) Load(key K) (value V, ok bool) { | ||
v, ok := m.m.Load(key) | ||
if !ok { | ||
return value, ok | ||
} | ||
return v.(V), ok | ||
} | ||
|
||
func (m *Map[K, V]) LoadAndDelete(key K) (value V, loaded bool) { | ||
v, loaded := m.m.LoadAndDelete(key) | ||
if !loaded { | ||
return value, loaded | ||
} | ||
return v.(V), loaded | ||
} | ||
|
||
func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) { | ||
a, loaded := m.m.LoadOrStore(key, value) | ||
return a.(V), loaded | ||
} | ||
|
||
func (m *Map[K, V]) Range(f func(key K, value V) bool) { | ||
m.m.Range(func(key, value any) bool { return f(key.(K), value.(V)) }) | ||
} | ||
|
||
func (m *Map[K, V]) Store(key K, value V) { m.m.Store(key, value) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package syncx_test | ||
|
||
import ( | ||
"github.com/leap-fish/necs/internal/syncx" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestMap(t *testing.T) { | ||
m := &syncx.Map[int, string]{} | ||
// Test LoadAndStore | ||
actual, loaded := m.LoadOrStore(1, "value1") | ||
assert.False(t, loaded, "Expected loaded=false") | ||
assert.Equal(t, "value1", actual, "Expected actual value 'value1'") | ||
|
||
// Test Load | ||
actualValue, ok := m.Load(1) | ||
assert.True(t, ok, "Expected ok=true") | ||
assert.Equal(t, "value1", actualValue, "Expected actual value 'value1'") | ||
|
||
// Test Store | ||
m.Store(2, "value2") | ||
actualValue, ok = m.Load(2) | ||
assert.True(t, ok, "Expected ok=true") | ||
assert.Equal(t, "value2", actualValue, "Expected actual value 'value2'") | ||
|
||
// Test Delete | ||
m.Delete(1) | ||
_, ok = m.Load(1) | ||
assert.False(t, ok, "Expected ok=false for key 1 after deletion") | ||
|
||
// Test LoadAndDelete | ||
actualValue, loaded = m.LoadAndDelete(2) | ||
assert.True(t, loaded, "Expected loaded=true") | ||
assert.Equal(t, "value2", actualValue, "Expected actual value 'value2'") | ||
|
||
// Test Range | ||
m.Store(3, "value3") | ||
m.Store(4, "value4") | ||
var count int | ||
m.Range(func(key int, value string) bool { | ||
count++ | ||
return true | ||
}) | ||
assert.Equal(t, 2, count, "Expected 2 iterations") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters