Skip to content

Commit

Permalink
small reorg
Browse files Browse the repository at this point in the history
Signed-off-by: Elena Kolevska <[email protected]>
  • Loading branch information
elena-kolevska committed May 14, 2024
1 parent d43a33c commit 1715b0a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 39 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**/.DS_Store
.idea
.vscode
.vs
44 changes: 17 additions & 27 deletions concurrency/atomicmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()
Expand All @@ -100,21 +90,21 @@ 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 {
fn(k, v)
}
}

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])
Expand Down
3 changes: 2 additions & 1 deletion concurrency/atomicmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 19 additions & 10 deletions concurrency/mutexmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion concurrency/mutexmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1715b0a

Please sign in to comment.