forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdcache.go
213 lines (193 loc) · 5.86 KB
/
mdcache.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
208
209
210
211
212
213
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libkbfs
import (
"sync"
lru "github.com/hashicorp/golang-lru"
"github.com/keybase/client/go/kbfs/kbfsmd"
"github.com/keybase/client/go/kbfs/tlf"
"github.com/keybase/client/go/kbfs/tlfhandle"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/pkg/errors"
)
// MDCacheStandard implements a simple LRU cache for per-folder
// metadata objects.
type MDCacheStandard struct {
// lock protects `lru` from atomic operations that need atomicity
// across multiple `lru` calls.
lock sync.RWMutex
lru *lru.Cache
idLRU *lru.Cache
nextMDLRU *lru.Cache
}
type mdCacheKey struct {
tlf tlf.ID
rev kbfsmd.Revision
bid kbfsmd.BranchID
}
const (
defaultMDCacheCapacity = 5000
defaultNextMDCacheCapacity = 100
)
// NewMDCacheStandard constructs a new MDCacheStandard using the given
// cache capacity.
func NewMDCacheStandard(capacity int) *MDCacheStandard {
mdLRU, err := lru.New(capacity)
if err != nil {
return nil
}
idLRU, err := lru.New(capacity)
if err != nil {
return nil
}
// Hard-code the nextMD cache size to something small, since only
// one entry is used for each revoked device we need to verify.
nextMDLRU, err := lru.New(defaultNextMDCacheCapacity)
if err != nil {
return nil
}
return &MDCacheStandard{lru: mdLRU, idLRU: idLRU, nextMDLRU: nextMDLRU}
}
// Get implements the MDCache interface for MDCacheStandard.
func (md *MDCacheStandard) Get(tlf tlf.ID, rev kbfsmd.Revision, bid kbfsmd.BranchID) (
ImmutableRootMetadata, error) {
md.lock.RLock()
defer md.lock.RUnlock()
key := mdCacheKey{tlf, rev, bid}
if tmp, ok := md.lru.Get(key); ok {
if rmd, ok := tmp.(ImmutableRootMetadata); ok {
return rmd, nil
}
return ImmutableRootMetadata{}, BadMDError{tlf}
}
return ImmutableRootMetadata{}, NoSuchMDError{tlf, rev, bid}
}
// Put implements the MDCache interface for MDCacheStandard.
func (md *MDCacheStandard) Put(rmd ImmutableRootMetadata) error {
md.lock.Lock()
defer md.lock.Unlock()
key := mdCacheKey{rmd.TlfID(), rmd.Revision(), rmd.BID()}
if _, ok := md.lru.Get(key); ok {
// Don't overwrite the cache. In the case that `rmd` is
// different from what's in the cache, we require that upper
// layers manage the cache explicitly by deleting or replacing
// it explicitly.
return nil
}
md.lru.Add(key, rmd)
return nil
}
// Delete implements the MDCache interface for MDCacheStandard.
func (md *MDCacheStandard) Delete(tlf tlf.ID, rev kbfsmd.Revision,
bid kbfsmd.BranchID) {
md.lock.Lock()
defer md.lock.Unlock()
key := mdCacheKey{tlf, rev, bid}
md.lru.Remove(key)
}
// Replace implements the MDCache interface for MDCacheStandard.
func (md *MDCacheStandard) Replace(newRmd ImmutableRootMetadata,
oldBID kbfsmd.BranchID) error {
md.lock.Lock()
defer md.lock.Unlock()
oldKey := mdCacheKey{newRmd.TlfID(), newRmd.Revision(), oldBID}
newKey := mdCacheKey{newRmd.TlfID(), newRmd.Revision(), newRmd.BID()}
// TODO: implement our own LRU where we can replace the old data
// without affecting the LRU status.
md.lru.Remove(oldKey)
md.lru.Add(newKey, newRmd)
return nil
}
// MarkPutToServer implements the MDCache interface for
// MDCacheStandard.
func (md *MDCacheStandard) MarkPutToServer(
tlf tlf.ID, rev kbfsmd.Revision, bid kbfsmd.BranchID) {
md.lock.Lock()
defer md.lock.Unlock()
key := mdCacheKey{tlf, rev, bid}
tmp, ok := md.lru.Get(key)
if !ok {
return
}
rmd, ok := tmp.(ImmutableRootMetadata)
if !ok {
return
}
rmd.putToServer = true
md.lru.Add(key, rmd)
}
// GetIDForHandle implements the MDCache interface for
// MDCacheStandard.
func (md *MDCacheStandard) GetIDForHandle(handle *tlfhandle.Handle) (tlf.ID, error) {
md.lock.RLock()
defer md.lock.RUnlock()
key := handle.GetCanonicalPath()
tmp, ok := md.idLRU.Get(key)
if !ok {
return tlf.NullID, NoSuchTlfIDError{handle}
}
id, ok := tmp.(tlf.ID)
if !ok {
return tlf.NullID, errors.Errorf("Bad ID for handle %s", key)
}
return id, nil
}
// PutIDForHandle implements the MDCache interface for
// MDCacheStandard.
func (md *MDCacheStandard) PutIDForHandle(handle *tlfhandle.Handle, id tlf.ID) error {
md.lock.RLock()
defer md.lock.RUnlock()
key := handle.GetCanonicalPath()
md.idLRU.Add(key, id)
return nil
}
// ChangeHandleForID implements the MDCache interface for
// MDCacheStandard.
func (md *MDCacheStandard) ChangeHandleForID(
oldHandle *tlfhandle.Handle, newHandle *tlfhandle.Handle) {
md.lock.RLock()
defer md.lock.RUnlock()
oldKey := oldHandle.GetCanonicalPath()
tmp, ok := md.idLRU.Get(oldKey)
if !ok {
return
}
md.idLRU.Remove(oldKey)
newKey := newHandle.GetCanonicalPath()
md.idLRU.Add(newKey, tmp)
}
type mdcacheNextMDKey struct {
tlfID tlf.ID
rootSeqno keybase1.Seqno
}
type mdcacheNextMDVal struct {
nextKbfsRoot *kbfsmd.MerkleRoot
nextMerkleNodes [][]byte
nextRootSeqno keybase1.Seqno
}
// GetNextMD implements the MDCache interface for MDCacheStandard.
func (md *MDCacheStandard) GetNextMD(tlfID tlf.ID, rootSeqno keybase1.Seqno) (
nextKbfsRoot *kbfsmd.MerkleRoot, nextMerkleNodes [][]byte,
nextRootSeqno keybase1.Seqno, err error) {
key := mdcacheNextMDKey{tlfID, rootSeqno}
tmp, ok := md.nextMDLRU.Get(key)
if !ok {
return nil, nil, 0, NextMDNotCachedError{tlfID, rootSeqno}
}
val, ok := tmp.(mdcacheNextMDVal)
if !ok {
return nil, nil, 0, errors.Errorf(
"Bad next MD for %s, seqno=%d", tlfID, rootSeqno)
}
return val.nextKbfsRoot, val.nextMerkleNodes, val.nextRootSeqno, nil
}
// PutNextMD implements the MDCache interface for MDCacheStandard.
func (md *MDCacheStandard) PutNextMD(
tlfID tlf.ID, rootSeqno keybase1.Seqno, nextKbfsRoot *kbfsmd.MerkleRoot,
nextMerkleNodes [][]byte, nextRootSeqno keybase1.Seqno) error {
key := mdcacheNextMDKey{tlfID, rootSeqno}
val := mdcacheNextMDVal{nextKbfsRoot, nextMerkleNodes, nextRootSeqno}
md.nextMDLRU.Add(key, val)
return nil
}