-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcache.go
197 lines (162 loc) · 3.3 KB
/
cache.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"net/mail"
"sort"
"strings"
)
type cacheString map[string]mailFiles
type caches struct {
data map[string]cacheString
indexer *mailIndexer
mailCh chan cacheMail
listCh chan *cacheListRequest
requestCh chan *cacheRequest
addCh chan cacheEntry
removeCh chan mailFiles
}
type cacheRequest struct {
header string
value string
index int
limit int
oldest bool
match keyType
data chan mailFiles
}
type cacheListRequest struct {
header string
data chan []string
}
type cacheEntry struct {
name string
key string
value mailFile
}
type cacheMail struct {
id mailFile
headers mail.Header
}
func newCacheRequest() *cacheRequest {
return &cacheRequest{
data: make(chan mailFiles),
}
}
func newCacheListRequest() *cacheListRequest {
return &cacheListRequest{
data: make(chan []string),
}
}
func newCaches(indexer *mailIndexer, root string) *caches {
c := &caches{
indexer: indexer,
data: make(map[string]cacheString),
listCh: make(chan *cacheListRequest),
requestCh: make(chan *cacheRequest),
addCh: make(chan cacheEntry),
removeCh: make(chan mailFiles),
}
for i := range indexer.keys {
c.initCachesString(i)
}
return c
}
func (c *caches) initCachesString(name string) {
c.data[name] = make(map[string]mailFiles)
}
func (c *caches) add(entry cacheEntry) {
name, key, value := entry.name, entry.key, entry.value
if _, found := c.data[name][key]; !found {
c.data[name][key] = newMailFiles()
}
c.data[name][key] = append(c.data[name][key], value)
}
func (c *caches) remove(files mailFiles) {
for name := range c.data {
for k := range c.data[name] {
sort.Sort(c.data[name][k])
c.data[name][k] = c.data[name][k].diff(files)
}
}
}
func (c *caches) match(header, value string, match keyType) mailFiles {
cache, found := c.data[header]
if !found {
return nil
}
if match == keyTypeAddr {
value = strings.ToLower(value)
}
// Full match from cache
if match != keyTypePart {
return cache[value]
}
// Handle partial matches
results := newMailFiles()
for val, files := range cache {
if strings.Contains(val, value) {
for f := range files {
results = append(results, files[f])
}
}
}
return results
}
func (c *caches) request(r *cacheRequest) {
c.requestCh <- r
}
func (c *caches) respond(r *cacheRequest) {
defer close(r.data)
files := c.match(r.header, r.value, r.match)
lfiles := len(files)
if lfiles == 0 {
return
}
if r.limit == 0 {
r.limit = 1
}
if r.limit > lfiles {
r.limit = lfiles
}
if r.index >= lfiles {
return
}
if !r.oldest {
sort.Sort(sort.Reverse(files))
} else {
sort.Sort(files)
}
// Copy only elements between r.index and r.index + r.limit
result := mailFiles(make([]mailFile, r.limit))
for i, j := 0, r.index; i < r.limit; i, j = i+1, j+1 {
result[i] = files[j]
}
r.data <- result
}
func (c *caches) list(r *cacheListRequest) {
defer close(r.data)
values, found := c.data[r.header]
if !found {
return
}
keys := make([]string, len(values))
i := 0
for k := range values {
keys[i] = k
i = i + 1
}
r.data <- keys
}
func (c *caches) run() {
for {
select {
case r := <-c.listCh:
c.list(r)
case r := <-c.requestCh:
c.respond(r)
case entry := <-c.addCh:
c.add(entry)
case files := <-c.removeCh:
c.remove(files)
}
}
}