Skip to content
File

Blob: kv/memory/simple.go

go38 lines
1package memory
2 
3import (
4 "context"
5 
6 "go.miragespace.co/specter/spec/chord"
7)
8 
9var _ chord.SimpleKV = (*MemoryKV)(nil)
10 
11var empty []byte
12 
13func (m *MemoryKV) Put(ctx context.Context, key, value []byte) error {
14 v, _ := m.fetchVal(key)
15 curr := v.simple.Load()
16 if !v.simple.CompareAndSwap(curr, &value) {
17 return chord.ErrKVSimpleConflict
18 }
19 return nil
20}
21 
22func (m *MemoryKV) Get(ctx context.Context, key []byte) ([]byte, error) {
23 v, ok := m.lookupVal(key)
24 if !ok {
25 return nil, nil
26 }
27 return *v.simple.Load(), nil
28}
29 
30func (m *MemoryKV) Delete(ctx context.Context, key []byte) error {
31 v, _ := m.fetchVal(key)
32 curr := v.simple.Load()
33 if !v.simple.CompareAndSwap(curr, &empty) {
34 return chord.ErrKVSimpleConflict
35 }
36 return nil
37}