Skip to content

Commit

Permalink
feat(tinykv): add Delete method
Browse files Browse the repository at this point in the history
  • Loading branch information
acouvreur committed Nov 6, 2022
1 parent 8096a4e commit eb83d39
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/tinykv/tinykv.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type entry[T any] struct {

// KV is a registry for values (like/is a concurrent map) with timeout and sliding timeout
type KV[T any] interface {
Delete(k string)
Get(k string) (v T, ok bool)
Keys() (keys []string)
Values() (values []T)
Expand Down Expand Up @@ -104,6 +105,13 @@ func (kv *store[T]) Stop() {
kv.stopOnce.Do(func() { close(kv.stop) })
}

// Delete deletes an entry
func (kv *store[T]) Delete(k string) {
kv.mx.Lock()
defer kv.mx.Unlock()
delete(kv.kv, k)
}

func (kv *store[T]) Get(k string) (T, bool) {
var zero T
kv.mx.Lock()
Expand Down
19 changes: 19 additions & 0 deletions pkg/tinykv/tinykv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,25 @@ func Test03(t *testing.T) {
assert.WithinDuration(putAt, putAt.Add(<-elapsed), time.Millisecond*60)
}

func Test04(t *testing.T) {
assert := assert.New(t)
kv := New(
time.Millisecond*10,
func(k string, v interface{}) {
t.Fatal(k, v)
})

err := kv.Put("1", 1, time.Millisecond*10000)
assert.NoError(err)
<-time.After(time.Millisecond * 50)
kv.Delete("1")
kv.Delete("1")

<-time.After(time.Millisecond * 100)
_, ok := kv.Get("1")
assert.False(ok)
}

func Test05(t *testing.T) {
assert := assert.New(t)
N := 10000
Expand Down

0 comments on commit eb83d39

Please sign in to comment.