/
openfile.go
207 lines (191 loc) · 3.75 KB
/
openfile.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
198
199
200
201
202
203
204
205
206
207
package meta
import (
"sync"
"time"
)
const (
invalidateAllChunks = 0xFFFFFFFF
invalidateAttrOnly = 0xFFFFFFFE
)
type openFile struct {
sync.RWMutex
attr Attr
refs int
lastCheck time.Time
chunks map[uint32][]Slice
}
type openfiles struct {
sync.Mutex
expire time.Duration
limit uint64
files map[Ino]*openFile
}
func newOpenFiles(expire time.Duration, limit uint64) *openfiles {
of := &openfiles{
expire: expire,
limit: limit,
files: make(map[Ino]*openFile),
}
go of.cleanup()
return of
}
func (o *openfiles) cleanup() {
for {
var (
cnt, deleted, todel int
candidateIno Ino
candidateOf *openFile
)
o.Lock()
if o.limit > 0 && len(o.files) > int(o.limit) {
todel = len(o.files) - int(o.limit)
}
for ino, of := range o.files {
cnt++
if cnt > 1e3 || todel > 0 && deleted >= todel {
break
}
if of.refs <= 0 {
if time.Since(of.lastCheck) > time.Hour*12 {
delete(o.files, ino)
deleted++
continue
}
if todel == 0 {
continue
}
if candidateIno == 0 {
candidateIno = ino
candidateOf = of
continue
}
if of.lastCheck.Before(candidateOf.lastCheck) {
candidateIno = ino
}
delete(o.files, candidateIno)
deleted++
candidateIno = 0
}
}
o.Unlock()
time.Sleep(time.Millisecond * time.Duration(1000*(cnt+1-deleted*2)/(cnt+1)))
}
}
func (o *openfiles) OpenCheck(ino Ino, attr *Attr) bool {
o.Lock()
defer o.Unlock()
of, ok := o.files[ino]
if ok && time.Since(of.lastCheck) < o.expire {
if attr != nil {
*attr = of.attr
}
of.refs++
return true
}
return false
}
func (o *openfiles) Open(ino Ino, attr *Attr) {
o.Lock()
defer o.Unlock()
of, ok := o.files[ino]
if !ok {
of = &openFile{}
of.chunks = make(map[uint32][]Slice)
o.files[ino] = of
} else if attr != nil && attr.Mtime == of.attr.Mtime && attr.Mtimensec == of.attr.Mtimensec {
attr.KeepCache = of.attr.KeepCache
} else {
of.chunks = make(map[uint32][]Slice)
}
if attr != nil {
of.attr = *attr
}
// next open can keep cache if not modified
of.attr.KeepCache = true
of.refs++
of.lastCheck = time.Now()
}
func (o *openfiles) Close(ino Ino) bool {
o.Lock()
defer o.Unlock()
of, ok := o.files[ino]
if ok {
of.refs--
return of.refs <= 0
}
return true
}
func (o *openfiles) Check(ino Ino, attr *Attr) bool {
if attr == nil {
panic("attr is nil")
}
o.Lock()
defer o.Unlock()
of, ok := o.files[ino]
if ok && time.Since(of.lastCheck) < o.expire {
*attr = of.attr
return true
}
return false
}
func (o *openfiles) Update(ino Ino, attr *Attr) bool {
if attr == nil {
panic("attr is nil")
}
o.Lock()
defer o.Unlock()
of, ok := o.files[ino]
if ok {
if attr.Mtime != of.attr.Mtime || attr.Mtimensec != of.attr.Mtimensec {
of.chunks = make(map[uint32][]Slice)
} else {
attr.KeepCache = of.attr.KeepCache
}
of.attr = *attr
of.lastCheck = time.Now()
return true
}
return false
}
func (o *openfiles) IsOpen(ino Ino) bool {
o.Lock()
defer o.Unlock()
of, ok := o.files[ino]
return ok && of.refs > 0
}
func (o *openfiles) ReadChunk(ino Ino, indx uint32) ([]Slice, bool) {
o.Lock()
defer o.Unlock()
of, ok := o.files[ino]
if !ok {
return nil, false
}
cs, ok := of.chunks[indx]
return cs, ok
}
func (o *openfiles) CacheChunk(ino Ino, indx uint32, cs []Slice) {
o.Lock()
defer o.Unlock()
of, ok := o.files[ino]
if ok {
of.chunks[indx] = cs
}
}
func (o *openfiles) InvalidateChunk(ino Ino, indx uint32) {
o.Lock()
defer o.Unlock()
of, ok := o.files[ino]
if ok {
if indx == invalidateAllChunks {
of.chunks = make(map[uint32][]Slice)
} else {
delete(of.chunks, indx)
}
of.lastCheck = time.Unix(0, 0)
}
}
func (o *openfiles) find(ino Ino) *openFile {
o.Lock()
defer o.Unlock()
return o.files[ino]
}