-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
66 lines (57 loc) · 1.55 KB
/
path.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2021 The June Authors. All rights reserved.
// Use of this source code is governed by Apache License
// that can be found in the LICENSE file.
package storekv
import (
"bytes"
"encoding/hex"
"fmt"
"regexp"
"strings"
"github.com/reusee/e5"
"github.com/reusee/june/key"
)
const objPrefix = "obj/"
func (s *Store) objPrefix() string {
buf := new(strings.Builder)
buf.WriteString(s.codec.ID())
buf.WriteString(s.prefix)
buf.WriteString(objPrefix)
return buf.String()
}
func (s *Store) nsPrefix(ns key.Namespace) string {
buf := new(strings.Builder)
buf.WriteString(s.objPrefix())
i := bytes.IndexByte(ns[:], '0')
if i == -1 {
buf.WriteString(hex.EncodeToString(ns[:]))
} else {
buf.WriteString(hex.EncodeToString(ns[:i]))
}
buf.WriteString("/")
return buf.String()
}
func (s *Store) keyToPath(key Key) string {
buf := new(strings.Builder)
buf.WriteString(s.nsPrefix(key.Namespace))
buf.WriteString(key.Hash.String())
return buf.String()
}
var pathPattern = regexp.MustCompile(`([^)]+)/([0-9a-f]+)$`)
func (s *Store) pathToKey(path string) (key Key, err error) {
defer he(&err, e5.Info("path %s", path))
path = strings.TrimPrefix(path, s.codec.ID())
path = strings.TrimPrefix(path, s.prefix)
path = strings.TrimPrefix(path, objPrefix)
parts := pathPattern.FindStringSubmatch(path)
if len(parts) != 3 {
ce(fmt.Errorf("bad path"))
}
bs, err := hex.DecodeString(parts[1])
ce(err, e5.Info("bad namespace"))
copy(key.Namespace[:], bs)
bs, err = hex.DecodeString(parts[2])
ce(err, e5.Info("bad key"))
copy(key.Hash[:], bs)
return
}