...
1 package main
2
3
11
12 import (
13 "fmt"
14 "regexp"
15 "strings"
16
17 "github.com/peterbourgon/diskv/v3"
18 )
19
20 var hex40 = regexp.MustCompile("[0-9a-fA-F]{40}")
21
22 func hexTransform(s string) *diskv.PathKey {
23 if hex40.MatchString(s) {
24 return &diskv.PathKey{Path: []string{"objects", s[0:2]},
25 FileName: s,
26 }
27 }
28
29 folders := strings.Split(s, "/")
30 lfolders := len(folders)
31 if lfolders > 1 {
32 return &diskv.PathKey{Path: folders[:lfolders-1],
33 FileName: folders[lfolders-1],
34 }
35 }
36
37 return &diskv.PathKey{Path: []string{},
38 FileName: s,
39 }
40 }
41
42 func hexInverseTransform(pathKey *diskv.PathKey) string {
43 if hex40.MatchString(pathKey.FileName) {
44 return pathKey.FileName
45 }
46
47 if len(pathKey.Path) == 0 {
48 return pathKey.FileName
49 }
50
51 return strings.Join(pathKey.Path, "/") + "/" + pathKey.FileName
52 }
53
54 func main() {
55 d := diskv.New(diskv.Options{
56 BasePath: "my-data-dir",
57 AdvancedTransform: hexTransform,
58 InverseTransform: hexInverseTransform,
59 CacheSizeMax: 1024 * 1024,
60 })
61
62
63 key := "1bd88421b055327fcc8660c76c4894c4ea4c95d7"
64 d.WriteString(key, "¡Hola!")
65
66 d.WriteString("refs/heads/master", "some text")
67
68 fmt.Println("Enumerating All keys:")
69 c := d.Keys(nil)
70
71 for key := range c {
72 value := d.ReadString(key)
73 fmt.Printf("Key: %s, Value: %s\n", key, value)
74 }
75 }
76
View as plain text