File
Blob: kv/memory/simple.go
| 1 | package memory |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | |
| 6 | "go.miragespace.co/specter/spec/chord" |
| 7 | ) |
| 8 | |
| 9 | var _ chord.SimpleKV = (*MemoryKV)(nil) |
| 10 | |
| 11 | var empty []byte |
| 12 | |
| 13 | func (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 | |
| 22 | func (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 | |
| 30 | func (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 | } |