forked from GoogleCloudPlatform/gcsfuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_cache.go
154 lines (128 loc) · 3.83 KB
/
type_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
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package inode
import (
"time"
"github.com/jacobsa/util/lrucache"
)
// A cache that maps from a name to information about the type of the object
// with that name. Each name N is in one of the following states:
//
// * Nothing is known about N.
// * We have recorded that N is a file.
// * We have recorded that N is a directory.
// * We have recorded that N is both a file and a directory.
//
// Must be created with newTypeCache. May be contained in a larger struct.
// External synchronization is required.
type typeCache struct {
/////////////////////////
// Constant data
/////////////////////////
ttl time.Duration
/////////////////////////
// Mutable state
/////////////////////////
// A cache mapping file names to the time at which the entry should expire.
//
// INVARIANT: files.CheckInvariants() does not panic
// INVARIANT: Each value is of type time.Time
files lrucache.Cache
// A cache mapping directory names to the time at which the entry should
// expire.
//
// INVARIANT: dirs.CheckInvariants() does not panic
// INVARIANT: Each value is of type time.Time
dirs lrucache.Cache
}
// Create a cache whose information expires with the supplied TTL. If the TTL
// is zero, nothing will ever be cached.
func newTypeCache(
perTypeCapacity int,
ttl time.Duration) (tc typeCache) {
tc = typeCache{
ttl: ttl,
files: lrucache.New(perTypeCapacity),
dirs: lrucache.New(perTypeCapacity),
}
return
}
////////////////////////////////////////////////////////////////////////
// Public interface
////////////////////////////////////////////////////////////////////////
// Panic if any internal invariants have been violated. The careful user can
// arrange to call this at crucial moments.
func (tc *typeCache) CheckInvariants() {
// INVARIANT: files.CheckInvariants() does not panic
tc.files.CheckInvariants()
// INVARIANT: dirs.CheckInvariants() does not panic
tc.dirs.CheckInvariants()
}
// Record that the supplied name is a file. It may still also be a directory.
func (tc *typeCache) NoteFile(now time.Time, name string) {
// Are we disabled?
if tc.ttl == 0 {
return
}
tc.files.Insert(name, now.Add(tc.ttl))
}
// Record that the supplied name is a directory. It may still also be a file.
func (tc *typeCache) NoteDir(now time.Time, name string) {
// Are we disabled?
if tc.ttl == 0 {
return
}
tc.dirs.Insert(name, now.Add(tc.ttl))
}
// Erase all information about the supplied name.
func (tc *typeCache) Erase(name string) {
tc.files.Erase(name)
tc.dirs.Erase(name)
}
// Do we currently think the given name is a file?
func (tc *typeCache) IsFile(now time.Time, name string) (res bool) {
// Is there an entry?
val := tc.files.LookUp(name)
if val == nil {
res = false
return
}
expiration := val.(time.Time)
// Has the entry expired?
if expiration.Before(now) {
tc.files.Erase(name)
res = false
return
}
res = true
return
}
// Do we currently think the given name is a directory?
func (tc *typeCache) IsDir(now time.Time, name string) (res bool) {
// Is there an entry?
val := tc.dirs.LookUp(name)
if val == nil {
res = false
return
}
expiration := val.(time.Time)
// Has the entry expired?
if expiration.Before(now) {
tc.dirs.Erase(name)
res = false
return
}
res = true
return
}