Skip to content
File

Blob: kv/memory/prefix.go

go51 lines
1package memory
2 
3import (
4 "context"
5 
6 "go.miragespace.co/specter/spec/chord"
7)
8 
9var _ chord.PrefixKV = (*MemoryKV)(nil)
10 
11func (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 
21func (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 
35func (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 
44func (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}