From 1715b0a0ec6332f71c25ccc58b7397e6b47637d4 Mon Sep 17 00:00:00 2001 From: Elena Kolevska Date: Tue, 14 May 2024 19:45:00 +0100 Subject: [PATCH] small reorg Signed-off-by: Elena Kolevska --- .gitignore | 4 ++++ concurrency/atomicmap.go | 44 ++++++++++++++--------------------- concurrency/atomicmap_test.go | 3 ++- concurrency/mutexmap.go | 29 +++++++++++++++-------- concurrency/mutexmap_test.go | 2 +- 5 files changed, 43 insertions(+), 39 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b94b0fe --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +**/.DS_Store +.idea +.vscode +.vs \ No newline at end of file diff --git a/concurrency/atomicmap.go b/concurrency/atomicmap.go index 758bb20..097f949 100644 --- a/concurrency/atomicmap.go +++ b/concurrency/atomicmap.go @@ -43,36 +43,26 @@ func (a *AtomicValue[T]) Add(v T) T { return a.value } -type AtomicMap[K comparable, T constraints.Integer] struct { - lock sync.RWMutex - items map[K]*AtomicValue[T] -} - -func NewAtomicMapStringInt64() *AtomicMap[string, int64] { - return &AtomicMap[string, int64]{ - items: make(map[string]*AtomicValue[int64]), - } +type AtomicMap[K comparable, T constraints.Integer] interface { + Get(key K) (*AtomicValue[T], bool) + GetOrCreate(key K, createT T) *AtomicValue[T] + Delete(key K) + ForEach(fn func(key K, value *AtomicValue[T])) + Clear() } -func NewAtomicMapStringInt32() *AtomicMap[string, int32] { - return &AtomicMap[string, int32]{ - items: make(map[string]*AtomicValue[int32]), - } -} - -func NewAtomicMapStringUint64() *AtomicMap[string, uint64] { - return &AtomicMap[string, uint64]{ - items: make(map[string]*AtomicValue[uint64]), - } +type atomicMap[K comparable, T constraints.Integer] struct { + lock sync.RWMutex + items map[K]*AtomicValue[T] } -func NewAtomicMapStringUint32() *AtomicMap[string, uint32] { - return &AtomicMap[string, uint32]{ - items: make(map[string]*AtomicValue[uint32]), +func NewAtomicMap[K comparable, T constraints.Integer]() AtomicMap[K, T] { + return &atomicMap[K, T]{ + items: make(map[K]*AtomicValue[T]), } } -func (a *AtomicMap[K, T]) Get(key K) (*AtomicValue[T], bool) { +func (a *atomicMap[K, T]) Get(key K) (*AtomicValue[T], bool) { a.lock.RLock() defer a.lock.RUnlock() @@ -83,7 +73,7 @@ func (a *AtomicMap[K, T]) Get(key K) (*AtomicValue[T], bool) { return item, true } -func (a *AtomicMap[K, T]) GetOrCreate(key K, createT T) *AtomicValue[T] { +func (a *atomicMap[K, T]) GetOrCreate(key K, createT T) *AtomicValue[T] { a.lock.RLock() item, ok := a.items[key] a.lock.RUnlock() @@ -100,13 +90,13 @@ func (a *AtomicMap[K, T]) GetOrCreate(key K, createT T) *AtomicValue[T] { return item } -func (a *AtomicMap[K, T]) Delete(key K) { +func (a *atomicMap[K, T]) Delete(key K) { a.lock.Lock() delete(a.items, key) a.lock.Unlock() } -func (a *AtomicMap[K, T]) ForEach(fn func(key K, value *AtomicValue[T])) { +func (a *atomicMap[K, T]) ForEach(fn func(key K, value *AtomicValue[T])) { a.lock.RLock() defer a.lock.RUnlock() for k, v := range a.items { @@ -114,7 +104,7 @@ func (a *AtomicMap[K, T]) ForEach(fn func(key K, value *AtomicValue[T])) { } } -func (a *AtomicMap[K, T]) Clear() { +func (a *atomicMap[K, T]) Clear() { a.lock.Lock() defer a.lock.Unlock() a.items = make(map[K]*AtomicValue[T]) diff --git a/concurrency/atomicmap_test.go b/concurrency/atomicmap_test.go index 78bfb0b..d39ac43 100644 --- a/concurrency/atomicmap_test.go +++ b/concurrency/atomicmap_test.go @@ -22,7 +22,8 @@ import ( ) func TestAtomicMapInt32_New_Get_Delete(t *testing.T) { - m := NewAtomicMapStringInt32() + m := NewAtomicMap[string, int32]().(*atomicMap[string, int32]) + require.NotNil(t, m) require.NotNil(t, m.items) require.Empty(t, m.items) diff --git a/concurrency/mutexmap.go b/concurrency/mutexmap.go index 3ca6207..e6b271a 100644 --- a/concurrency/mutexmap.go +++ b/concurrency/mutexmap.go @@ -17,18 +17,27 @@ import ( "sync" ) -type MutexMap[T comparable] struct { +type MutexMap[T comparable] interface { + Lock(key T) + Unlock(key T) + RLock(key T) + RUnlock(key T) + Delete(key T) + Clear() +} + +type mutexMap[T comparable] struct { lock sync.RWMutex items map[T]*sync.RWMutex } -func NewMutexMapString() *MutexMap[string] { - return &MutexMap[string]{ - items: make(map[string]*sync.RWMutex), +func NewMutexMap[T comparable]() MutexMap[T] { + return &mutexMap[T]{ + items: make(map[T]*sync.RWMutex), } } -func (a *MutexMap[T]) Lock(key T) { +func (a *mutexMap[T]) Lock(key T) { a.lock.RLock() mutex, ok := a.items[key] a.lock.RUnlock() @@ -44,7 +53,7 @@ func (a *MutexMap[T]) Lock(key T) { mutex.Lock() } -func (a *MutexMap[T]) Unlock(key T) { +func (a *mutexMap[T]) Unlock(key T) { a.lock.RLock() mutex, ok := a.items[key] a.lock.RUnlock() @@ -53,7 +62,7 @@ func (a *MutexMap[T]) Unlock(key T) { } } -func (a *MutexMap[T]) RLock(key T) { +func (a *mutexMap[T]) RLock(key T) { a.lock.RLock() mutex, ok := a.items[key] a.lock.RUnlock() @@ -69,7 +78,7 @@ func (a *MutexMap[T]) RLock(key T) { mutex.Lock() } -func (a *MutexMap[T]) RUnlock(key T) { +func (a *mutexMap[T]) RUnlock(key T) { a.lock.RLock() mutex, ok := a.items[key] a.lock.RUnlock() @@ -78,13 +87,13 @@ func (a *MutexMap[T]) RUnlock(key T) { } } -func (a *MutexMap[T]) Delete(key T) { +func (a *mutexMap[T]) Delete(key T) { a.lock.Lock() delete(a.items, key) a.lock.Unlock() } -func (a *MutexMap[T]) Clear() { +func (a *mutexMap[T]) Clear() { a.lock.Lock() a.items = make(map[T]*sync.RWMutex) a.lock.Unlock() diff --git a/concurrency/mutexmap_test.go b/concurrency/mutexmap_test.go index 04d7134..65e1690 100644 --- a/concurrency/mutexmap_test.go +++ b/concurrency/mutexmap_test.go @@ -21,7 +21,7 @@ import ( ) func TestNewMutexMap_Add_Delete(t *testing.T) { - mm := NewMutexMapString() + mm := NewMutexMap[string]().(*mutexMap[string]) t.Run("New mutex map", func(t *testing.T) { require.NotNil(t, mm)