File
Blob: kv/memory/prefix.go
| 1 | package memory |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | |
| 6 | "go.miragespace.co/specter/spec/chord" |
| 7 | ) |
| 8 | |
| 9 | var _ chord.PrefixKV = (*MemoryKV)(nil) |
| 10 | |
| 11 | func (m *MemoryKV) PrefixAppend(ctx context.Context, prefix []byte, child []byte) error { |
| 12 | v, _ := m.fetchVal(prefix) |
| 13 | |
| 14 | if !v.children.Add(string(child)) { |
| 15 | return chord.ErrKVPrefixConflict |
| 16 | } |
| 17 | |
| 18 | return nil |
| 19 | } |
| 20 | |
| 21 | func (m *MemoryKV) PrefixList(ctx context.Context, prefix []byte) ([][]byte, error) { |
| 22 | children := make([][]byte, 0) |
| 23 | v, ok := m.lookupVal(prefix) |
| 24 | if !ok { |
| 25 | return children, nil |
| 26 | } |
| 27 | v.children.Range(func(value string) bool { |
| 28 | children = append(children, []byte(value)) |
| 29 | return true |
| 30 | }) |
| 31 | |
| 32 | return children, nil |
| 33 | } |
| 34 | |
| 35 | func (m *MemoryKV) PrefixContains(ctx context.Context, prefix []byte, child []byte) (bool, error) { |
| 36 | v, ok := m.lookupVal(prefix) |
| 37 | if !ok { |
| 38 | return false, nil |
| 39 | } |
| 40 | |
| 41 | return v.children.Contains(string(child)), nil |
| 42 | } |
| 43 | |
| 44 | func (m *MemoryKV) PrefixRemove(ctx context.Context, prefix []byte, needle []byte) error { |
| 45 | v, _ := m.fetchVal(prefix) |
| 46 | |
| 47 | v.children.Remove(string(needle)) |
| 48 | |
| 49 | return nil |
| 50 | } |