-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemcache.go
136 lines (117 loc) · 3.53 KB
/
memcache.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
package fuzzer
import (
"path/filepath"
"strings"
"sync"
"github.com/toxyl/errors"
"github.com/toxyl/flo"
)
type memCache struct {
mu *sync.Mutex
content map[string][]string
}
func explode(str string) []string {
str = strings.TrimSpace(str)
str = strings.ReplaceAll(str, "\r\n", "\n")
str = strings.ReplaceAll(str, "\r", "\n")
res := []string{}
// filter out empty lines and commented lines (first non-space char is #)
for _, s := range strings.Split(str, "\n") {
st := strings.TrimSpace(s)
if st == "" || st[0] == '#' {
continue
}
res = append(res, s)
}
return res
}
func randomLineFromCache(path string) (data string, found bool) {
path = strings.Trim(path, "/")
fnGetLine := func(data []string) string {
l := len(data) - 1
if l < 0 {
return ""
}
return data[randomInt(0, l)]
}
// check if we have the data cached
cache.mu.Lock()
defer cache.mu.Unlock()
if d, ok := cache.content[path]; ok {
return fnGetLine(d), true // return the cached data
}
// nope, data is not cached, good time to do so.
cache.content[path] = []string{}
// we have a few options what the path could refer to:
// a) a directory
if d, ok := importedFiles[path]; ok {
// this is a directory, so we append all file entries to the cache
for _, s := range d {
cache.content[path] = append(cache.content[path], explode(s)...)
}
// there might be subdirectories, let's append their files too
pp := path + "/" // we don't care about the depth, so anything prefixed with the path + slash must be a subdirectory
for p, d := range importedFiles {
if strings.HasPrefix(p, pp) {
// this is a sub directory, append the data
for _, s := range d {
cache.content[path] = append(cache.content[path], explode(s)...)
}
}
}
return fnGetLine(cache.content[path]), true
}
// b) a file
if strings.Count(path, "/") == 0 {
if d, ok := importedFiles[""]; ok {
if s, ok := d[path]; ok {
cache.content[path] = append(cache.content[path], explode(s)...)
return fnGetLine(cache.content[path]), true // we found the file
}
}
return fnGetLine(cache.content[path]), false // we didn't find the file or section
}
// c) a directory + file
if d, ok := importedFiles[filepath.Dir(path)]; ok {
if s, ok := d[filepath.Base(path)]; ok {
cache.content[path] = append(cache.content[path], explode(s)...)
return fnGetLine(cache.content[path]), true // we found the file
}
return fnGetLine(cache.content[path]), false // we didn't find the file
}
return fnGetLine(cache.content[path]), false // we didn't find anything
}
func getRandomLine(path string) string {
data, found := randomLineFromCache(path)
if !found {
return ""
}
return data
}
func importFromDir(dir string) error {
if dir == "" {
return errors.Newf("no import path given")
}
baseDir := flo.Dir(dir)
if !baseDir.Exists() {
return errors.Newf("import path not found")
}
baseDirPath := strings.TrimPrefix(strings.ToLower(baseDir.Path()), "/")
parsePath := func(path, basePath string) (section, data string) {
path = strings.TrimPrefix(path, basePath)
data = filepath.Base(path)
section = filepath.Dir(path)
section = strings.TrimPrefix(section, "/")
section = strings.TrimPrefix(section, ".")
return
}
baseDir.Each(func(f *flo.FileObj) {
section, data := parsePath(f.Path(), baseDir.Path())
section = strings.TrimPrefix(strings.TrimPrefix(section, baseDirPath), "/")
if _, ok := importedFiles[section]; !ok {
importedFiles[section] = map[string]string{}
}
importedFiles[section][data] = f.AsString()
}, nil)
return nil
}