Skip to content

Commit

Permalink
Map: Adds concurrency/map
Browse files Browse the repository at this point in the history
Map is a new concurrency package that provides a thread-safe generic map
implementation.

It is favored over the sync.Map as it is typed. See:
https://cs.opensource.google/go/go/+/refs/tags/go1.23.1:src/sync/map.go;l=15

Signed-off-by: joshvanl <[email protected]>
  • Loading branch information
JoshVanL committed Sep 23, 2024
1 parent 3823663 commit e9f58af
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions concurrency/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
Copyright 2024 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package concurrency

import (
"sync"
)

// Map is a simple _typed_ map which is safe for concurrent use.
// Favoured over sync.Map as it is typed.
type Map[K comparable, T any] interface {
Clear()
Delete(K)

Check failure on line 24 in concurrency/map.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

interface method Delete must have named param for type K (inamedparam)
Load(K) (T, bool)

Check failure on line 25 in concurrency/map.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

interface method Load must have named param for type K (inamedparam)
LoadAndDelete(K) (T, bool)

Check failure on line 26 in concurrency/map.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

interface method LoadAndDelete must have named param for type K (inamedparam)
Range(func(K, T) bool)

Check failure on line 27 in concurrency/map.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

interface method Range must have all named params (inamedparam)
Store(K, T)

Check failure on line 28 in concurrency/map.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

interface method Store must have named param for type K (inamedparam)
}

type mapimpl[K comparable, T any] struct {
lock sync.RWMutex
m map[K]T
}

func NewMap[K comparable, T any]() Map[K, T] {
return &mapimpl[K, T]{m: make(map[K]T)}
}

func (m *mapimpl[K, T]) Clear() {
m.lock.Lock()
defer m.lock.Unlock()
m.m = make(map[K]T)
}

func (m *mapimpl[K, T]) Delete(k K) {
m.lock.Lock()
defer m.lock.Unlock()
delete(m.m, k)
}

func (m *mapimpl[K, T]) Load(k K) (T, bool) {
m.lock.RLock()
defer m.lock.RUnlock()
v, ok := m.m[k]
return v, ok
}

func (m *mapimpl[K, T]) LoadAndDelete(k K) (T, bool) {
m.lock.Lock()
defer m.lock.Unlock()
v, ok := m.m[k]
delete(m.m, k)
return v, ok
}

func (m *mapimpl[K, T]) Range(f func(K, T) bool) {
m.lock.RLock()
defer m.lock.RUnlock()
for k, v := range m.m {
if !f(k, v) {
break
}
}
}

func (m *mapimpl[K, T]) Store(k K, v T) {
m.lock.Lock()
defer m.lock.Unlock()
m.m[k] = v
}

0 comments on commit e9f58af

Please sign in to comment.