Debian Codegraph @

examples/content-addressable-store/cas.go

raw
1 package main
2
3 import (
4 "crypto/md5"
5 "fmt"
6 "io"
7
8 "github.com/peterbourgon/diskv/v3"
9 )
10
11 const transformBlockSize = 2 // grouping of chars per directory depth
12
13 func blockTransform(s string) []string {
14 var (
15 sliceSize = len(s) / transformBlockSize
16 pathSlice = make([]string, sliceSize)
17 )
18 for i := 0; i < sliceSize; i++ {
19 from, to := i*transformBlockSize, (i*transformBlockSize)+transformBlockSize
20 pathSlice[i] = s[from:to]
21 }
22 return pathSlice
23 }
24
25 func main() {
26 d := diskv.New(diskv.Options{
27 BasePath: "data",
28 Transform: blockTransform,
29 CacheSizeMax: 1024 * 1024, // 1MB
30 })
31
32 for _, valueStr := range []string{
33 "I am the very model of a modern Major-General",
34 "I've information vegetable, animal, and mineral",
35 "I know the kings of England, and I quote the fights historical",
36 "From Marathon to Waterloo, in order categorical",
37 "I'm very well acquainted, too, with matters mathematical",
38 "I understand equations, both the simple and quadratical",
39 "About binomial theorem I'm teeming with a lot o' news",
40 "With many cheerful facts about the square of the hypotenuse",
41 } {
42 d.Write(md5sum(valueStr), []byte(valueStr))
43 }
44
45 var keyCount int
46 for key := range d.Keys(nil) {
47 val, err := d.Read(key)
48 if err != nil {
49 panic(fmt.Sprintf("key %s had no value", key))
50 }
51 fmt.Printf("%s: %s\n", key, val)
52 keyCount++
53 }
54 fmt.Printf("%d total keys\n", keyCount)
55
56 // d.EraseAll() // leave it commented out to see how data is kept on disk
57 }
58
59 func md5sum(s string) string {
60 h := md5.New()
61 io.WriteString(h, s)
62 return fmt.Sprintf("%x", h.Sum(nil))
63 }