-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.go
48 lines (37 loc) · 793 Bytes
/
memory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package hubdb
import (
"fmt"
"strings"
"github.com/ysmood/dehub/lib/xsync"
)
type Memory struct {
list xsync.Map[string, string]
}
func NewMemory() *Memory {
return &Memory{
list: xsync.Map[string, string]{},
}
}
func (db *Memory) StoreLocation(id string, addr string) error {
db.list.Store(id, addr)
return nil
}
func (db *Memory) LoadLocation(idPrefix string) (string, string, error) {
var addr, locID string
db.list.Range(func(id, value string) bool {
if strings.HasPrefix(id, idPrefix) {
locID = id
addr = value
return false
}
return true
})
if addr == "" {
return "", "", fmt.Errorf("%w via id prefix: %s", ErrNotFound, idPrefix)
}
return addr, locID, nil
}
func (db *Memory) DeleteLocation(id string) error {
db.list.Delete(id)
return nil
}