-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
146 lines (114 loc) · 3.04 KB
/
store.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"bytes"
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"io"
"log"
"os"
"strings"
)
const defaultRootFolderName = "ggnetwork"
// StoreOpts is a store option that can modify the PathTransformFunc function
type StoreOpts struct {
// Root is the folder name of the root, containing all the folders/files of the system
Root string
PathTransformFunc PathTransformFunc
}
// Store is an object that takes in StoreOpts
type Store struct {
StoreOpts
}
// PathTransformFunc is a type the take in a string and return a PathKey
type PathTransformFunc func(string) PathKey
// PathKey is a struct that holds the original pathname and transformed pathname
type PathKey struct {
PathName string
FileName string
}
// CASPathTransformFunc
func CASPathTransformFunc(key string) PathKey {
hash := sha1.Sum([]byte(key)) // [20]byte => []byte => [:]
hashStr := hex.EncodeToString(hash[:])
blocksize := 5
sliceLen := len(hashStr) / blocksize
paths := make([]string, sliceLen)
for i := 0; i < sliceLen; i++ {
from, to := i*blocksize, (i*blocksize)+blocksize
paths[i] = hashStr[from:to]
}
return PathKey{
PathName: strings.Join(paths, "/"),
FileName: hashStr,
}
}
func (p PathKey) FullPath() string {
return fmt.Sprintf("%s/%s", p.PathName, p.FileName)
}
var DefaultPathTransformFunc = func(key string) PathKey {
return PathKey{
PathName: key,
FileName: key,
}
}
func NewStore(opts StoreOpts) *Store {
if opts.PathTransformFunc == nil {
opts.PathTransformFunc = DefaultPathTransformFunc
}
if len(opts.Root) == 0 {
opts.Root = defaultRootFolderName
}
return &Store{StoreOpts: opts}
}
func (s *Store) Has(key string) bool {
PathKey := s.PathTransformFunc(key)
fullPathWithRoot := fmt.Sprintf("%s/%s", s.Root, PathKey.FullPath())
_, err := os.Stat(fullPathWithRoot)
if errors.Is(err, os.ErrNotExist) {
return false
}
return true
}
func (s *Store) Delete(key string) error {
pathKey := s.PathTransformFunc(key)
defer func() {
log.Printf("deleted [%s] from disk", pathKey.FileName)
}()
split := strings.Split(pathKey.PathName, "/")
return os.RemoveAll(s.Root + "/" + split[0])
}
func (s *Store) Read(key string) (io.Reader, error) {
f, err := s.readStream(key)
if err != nil {
return nil, err
}
defer f.Close()
buf := new(bytes.Buffer)
_, err = io.Copy(buf, f)
return buf, err
}
func (s *Store) readStream(key string) (io.ReadCloser, error) {
pathKey := s.PathTransformFunc(key)
fullPathWthRoot := fmt.Sprintf("%s/%s", s.Root, pathKey.FullPath())
return os.Open(fullPathWthRoot)
}
func (s *Store) writeStream(key string, r io.Reader) error {
pathKey := s.PathTransformFunc(key)
pathNameWithRoot := fmt.Sprintf("%s/%s", s.Root, pathKey.PathName)
if err := os.MkdirAll(pathNameWithRoot, os.ModePerm); err != nil {
return err
}
fullPathWthRoot := fmt.Sprintf("%s/%s", s.Root, pathKey.FullPath())
f, err := os.Create(fullPathWthRoot)
if err != nil {
return err
}
n, err := io.Copy(f, r)
if err != nil {
return err
}
log.Printf("Written (%d) bytes to disk: %s", n, fullPathWthRoot)
return nil
}