Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update range function as lock free #9

Merged
merged 1 commit into from
Apr 17, 2024
Merged
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
59 changes: 52 additions & 7 deletions concurrent_swiss_map.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package csmap

import (
"context"
"sync"

"github.com/mhmtszr/concurrent-swiss-map/maphash"
Expand Down Expand Up @@ -166,18 +167,62 @@ func (m *CsMap[K, V]) IsEmpty() bool {
return m.Count() == 0
}

type Tuple[K comparable, V any] struct {
Key K
Val V
}

// Range If the callback function returns true iteration will stop.
func (m *CsMap[K, V]) Range(f func(key K, value V) (stop bool)) {
ch := make(chan Tuple[K, V], m.Count())

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

listenCompleted := m.listen(f, ch)
m.produce(ctx, ch)
listenCompleted.Wait()
}

func (m *CsMap[K, V]) produce(ctx context.Context, ch chan Tuple[K, V]) {
var wg sync.WaitGroup
wg.Add(len(m.shards))
for i := range m.shards {
shard := m.shards[i]
shard.RLock()
stop := shard.items.Iter(f)
if stop {
go func(i int) {
defer wg.Done()

shard := m.shards[i]
shard.RLock()
shard.items.Iter(func(k K, v V) (stop bool) {
select {
case <-ctx.Done():
return true
default:
ch <- Tuple[K, V]{Key: k, Val: v}
}
return false
})
shard.RUnlock()
return
}
shard.RUnlock()
}(i)
}
go func() {
wg.Wait()
close(ch)
}()
}

func (m *CsMap[K, V]) listen(f func(key K, value V) (stop bool), ch chan Tuple[K, V]) *sync.WaitGroup {
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for t := range ch {
if stop := f(t.Key, t.Val); stop {
return
}
}
}()
return &wg
}

type HashShardPair[K comparable, V any] struct {
Expand Down
22 changes: 22 additions & 0 deletions concurrent_swiss_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,28 @@ func TestCustomHasherWithRange(t *testing.T) {
}
}

func TestDeleteFromRange(t *testing.T) {
myMap := csmap.Create[string, int](
csmap.WithSize[string, int](1024),
)

myMap.Store("aaa", 10)
myMap.Store("aab", 11)
myMap.Store("aac", 15)
myMap.Store("aad", 124)
myMap.Store("aaf", 987)

myMap.Range(func(key string, value int) (stop bool) {
if value > 20 {
myMap.Delete(key)
}
return false
})
if myMap.Count() != 3 {
t.Fatal("total should be 3, because currently range deletes values that bigger than 20.")
}
}

func TestBasicConcurrentWriteDeleteCount(t *testing.T) {
myMap := csmap.Create[int, string](
csmap.WithShardCount[int, string](32),
Expand Down
Loading