Browse Source

Remove unused libs/cmap. (#7245)

A follow-up to #7197.
pull/7248/head
M. J. Fromberger 3 years ago
committed by GitHub
parent
commit
6c7d6f761b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 0 additions and 204 deletions
  1. +0
    -91
      libs/cmap/cmap.go
  2. +0
    -113
      libs/cmap/cmap_test.go

+ 0
- 91
libs/cmap/cmap.go View File

@ -1,91 +0,0 @@
package cmap
import (
tmsync "github.com/tendermint/tendermint/internal/libs/sync"
)
// CMap is a goroutine-safe map
type CMap struct {
m map[string]interface{}
l tmsync.Mutex
}
func NewCMap() *CMap {
return &CMap{
m: make(map[string]interface{}),
}
}
func (cm *CMap) Set(key string, value interface{}) {
cm.l.Lock()
cm.m[key] = value
cm.l.Unlock()
}
// GetOrSet returns the existing value if present. Othewise, it stores `newValue` and returns it.
func (cm *CMap) GetOrSet(key string, newValue interface{}) (value interface{}, alreadyExists bool) {
cm.l.Lock()
defer cm.l.Unlock()
if v, ok := cm.m[key]; ok {
return v, true
}
cm.m[key] = newValue
return newValue, false
}
func (cm *CMap) Get(key string) interface{} {
cm.l.Lock()
val := cm.m[key]
cm.l.Unlock()
return val
}
func (cm *CMap) Has(key string) bool {
cm.l.Lock()
_, ok := cm.m[key]
cm.l.Unlock()
return ok
}
func (cm *CMap) Delete(key string) {
cm.l.Lock()
delete(cm.m, key)
cm.l.Unlock()
}
func (cm *CMap) Size() int {
cm.l.Lock()
size := len(cm.m)
cm.l.Unlock()
return size
}
func (cm *CMap) Clear() {
cm.l.Lock()
cm.m = make(map[string]interface{})
cm.l.Unlock()
}
func (cm *CMap) Keys() []string {
cm.l.Lock()
keys := make([]string, 0, len(cm.m))
for k := range cm.m {
keys = append(keys, k)
}
cm.l.Unlock()
return keys
}
func (cm *CMap) Values() []interface{} {
cm.l.Lock()
items := make([]interface{}, 0, len(cm.m))
for _, v := range cm.m {
items = append(items, v)
}
cm.l.Unlock()
return items
}

+ 0
- 113
libs/cmap/cmap_test.go View File

@ -1,113 +0,0 @@
package cmap
import (
"fmt"
"strings"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func TestIterateKeysWithValues(t *testing.T) {
cmap := NewCMap()
for i := 1; i <= 10; i++ {
cmap.Set(fmt.Sprintf("key%d", i), fmt.Sprintf("value%d", i))
}
// Testing size
assert.Equal(t, 10, cmap.Size())
assert.Equal(t, 10, len(cmap.Keys()))
assert.Equal(t, 10, len(cmap.Values()))
// Iterating Keys, checking for matching Value
for _, key := range cmap.Keys() {
val := strings.ReplaceAll(key, "key", "value")
assert.Equal(t, val, cmap.Get(key))
}
// Test if all keys are within []Keys()
keys := cmap.Keys()
for i := 1; i <= 10; i++ {
assert.Contains(t, keys, fmt.Sprintf("key%d", i), "cmap.Keys() should contain key")
}
// Delete 1 Key
cmap.Delete("key1")
assert.NotEqual(
t,
len(keys),
len(cmap.Keys()),
"[]keys and []Keys() should not be equal, they are copies, one item was removed",
)
}
func TestContains(t *testing.T) {
cmap := NewCMap()
cmap.Set("key1", "value1")
// Test for known values
assert.True(t, cmap.Has("key1"))
assert.Equal(t, "value1", cmap.Get("key1"))
// Test for unknown values
assert.False(t, cmap.Has("key2"))
assert.Nil(t, cmap.Get("key2"))
}
func BenchmarkCMapHas(b *testing.B) {
m := NewCMap()
for i := 0; i < 1000; i++ {
m.Set(string(rune(i)), i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Has(string(rune(i)))
}
}
func TestCMap_GetOrSet_Parallel(t *testing.T) {
tests := []struct {
name string
newValue interface{}
parallelism int
}{
{"test1", "a", 4},
{"test2", "a", 40},
{"test3", "a", 1},
}
//nolint:scopelint
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cm := NewCMap()
wg := sync.WaitGroup{}
wg.Add(tt.parallelism)
for i := 0; i < tt.parallelism; i++ {
go func() {
defer wg.Done()
gotValue, _ := cm.GetOrSet(tt.name, tt.newValue)
assert.EqualValues(t, tt.newValue, gotValue)
}()
}
wg.Wait()
})
}
}
func TestCMap_GetOrSet_Exists(t *testing.T) {
cm := NewCMap()
gotValue, exists := cm.GetOrSet("key", 1000)
assert.False(t, exists)
assert.EqualValues(t, 1000, gotValue)
gotValue, exists = cm.GetOrSet("key", 2000)
assert.True(t, exists)
assert.EqualValues(t, 1000, gotValue)
}

Loading…
Cancel
Save