forked from ipfs/kubo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pin.go
332 lines (283 loc) · 7.5 KB
/
pin.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// package pin implemnts structures and methods to keep track of
// which objects a user wants to keep stored locally.
package pin
import (
"encoding/json"
"errors"
"fmt"
"sync"
ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
nsds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/namespace"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
key "github.com/ipfs/go-ipfs/blocks/key"
"github.com/ipfs/go-ipfs/blocks/set"
mdag "github.com/ipfs/go-ipfs/merkledag"
logging "github.com/ipfs/go-ipfs/vendor/QmQg1J6vikuXF9oDvm4wpdeAUvvkVEKW1EYDw9HhTMnP2b/go-log"
)
var log = logging.Logger("pin")
var recursePinDatastoreKey = ds.NewKey("/local/pins/recursive/keys")
var directPinDatastoreKey = ds.NewKey("/local/pins/direct/keys")
var indirectPinDatastoreKey = ds.NewKey("/local/pins/indirect/keys")
type PinMode int
const (
Recursive PinMode = iota
Direct
Indirect
NotPinned
)
type Pinner interface {
IsPinned(key.Key) bool
Pin(context.Context, *mdag.Node, bool) error
Unpin(context.Context, key.Key, bool) error
Flush() error
GetManual() ManualPinner
DirectKeys() []key.Key
IndirectKeys() map[key.Key]int
RecursiveKeys() []key.Key
}
// ManualPinner is for manually editing the pin structure
// Use with care! If used improperly, garbage collection
// may not be successful
type ManualPinner interface {
PinWithMode(key.Key, PinMode)
RemovePinWithMode(key.Key, PinMode)
Pinner
}
// pinner implements the Pinner interface
type pinner struct {
lock sync.RWMutex
recursePin set.BlockSet
directPin set.BlockSet
indirPin *indirectPin
dserv mdag.DAGService
dstore ds.ThreadSafeDatastore
}
// NewPinner creates a new pinner using the given datastore as a backend
func NewPinner(dstore ds.ThreadSafeDatastore, serv mdag.DAGService) Pinner {
// Load set from given datastore...
rcds := nsds.Wrap(dstore, recursePinDatastoreKey)
rcset := set.NewDBWrapperSet(rcds, set.NewSimpleBlockSet())
dirds := nsds.Wrap(dstore, directPinDatastoreKey)
dirset := set.NewDBWrapperSet(dirds, set.NewSimpleBlockSet())
nsdstore := nsds.Wrap(dstore, indirectPinDatastoreKey)
return &pinner{
recursePin: rcset,
directPin: dirset,
indirPin: NewIndirectPin(nsdstore),
dserv: serv,
dstore: dstore,
}
}
// Pin the given node, optionally recursive
func (p *pinner) Pin(ctx context.Context, node *mdag.Node, recurse bool) error {
p.lock.Lock()
defer p.lock.Unlock()
k, err := node.Key()
if err != nil {
return err
}
if recurse {
if p.recursePin.HasKey(k) {
return nil
}
if p.directPin.HasKey(k) {
p.directPin.RemoveBlock(k)
}
err := p.pinLinks(ctx, node)
if err != nil {
return err
}
p.recursePin.AddBlock(k)
} else {
if _, err := p.dserv.Get(ctx, k); err != nil {
return err
}
if p.recursePin.HasKey(k) {
return fmt.Errorf("%s already pinned recursively", k.B58String())
}
p.directPin.AddBlock(k)
}
return nil
}
// Unpin a given key
func (p *pinner) Unpin(ctx context.Context, k key.Key, recursive bool) error {
p.lock.Lock()
defer p.lock.Unlock()
if p.recursePin.HasKey(k) {
if recursive {
p.recursePin.RemoveBlock(k)
node, err := p.dserv.Get(ctx, k)
if err != nil {
return err
}
return p.unpinLinks(ctx, node)
} else {
return fmt.Errorf("%s is pinned recursively", k)
}
} else if p.directPin.HasKey(k) {
p.directPin.RemoveBlock(k)
return nil
} else if p.indirPin.HasKey(k) {
return fmt.Errorf("%s is pinned indirectly. indirect pins cannot be removed directly", k)
} else {
return fmt.Errorf("%s is not pinned", k)
}
}
func (p *pinner) unpinLinks(ctx context.Context, node *mdag.Node) error {
for _, l := range node.Links {
node, err := l.GetNode(ctx, p.dserv)
if err != nil {
return err
}
k, err := node.Key()
if err != nil {
return err
}
p.indirPin.Decrement(k)
err = p.unpinLinks(ctx, node)
if err != nil {
return err
}
}
return nil
}
func (p *pinner) pinIndirectRecurse(ctx context.Context, node *mdag.Node) error {
k, err := node.Key()
if err != nil {
return err
}
p.indirPin.Increment(k)
return p.pinLinks(ctx, node)
}
func (p *pinner) pinLinks(ctx context.Context, node *mdag.Node) error {
for _, ng := range p.dserv.GetDAG(ctx, node) {
subnode, err := ng.Get(ctx)
if err != nil {
// TODO: Maybe just log and continue?
return err
}
err = p.pinIndirectRecurse(ctx, subnode)
if err != nil {
return err
}
}
return nil
}
// IsPinned returns whether or not the given key is pinned
func (p *pinner) IsPinned(key key.Key) bool {
p.lock.RLock()
defer p.lock.RUnlock()
return p.recursePin.HasKey(key) ||
p.directPin.HasKey(key) ||
p.indirPin.HasKey(key)
}
func (p *pinner) RemovePinWithMode(key key.Key, mode PinMode) {
p.lock.Lock()
defer p.lock.Unlock()
switch mode {
case Direct:
p.directPin.RemoveBlock(key)
case Indirect:
p.indirPin.Decrement(key)
case Recursive:
p.recursePin.RemoveBlock(key)
default:
// programmer error, panic OK
panic("unrecognized pin type")
}
}
// LoadPinner loads a pinner and its keysets from the given datastore
func LoadPinner(d ds.ThreadSafeDatastore, dserv mdag.DAGService) (Pinner, error) {
p := new(pinner)
{ // load recursive set
var recurseKeys []key.Key
if err := loadSet(d, recursePinDatastoreKey, &recurseKeys); err != nil {
return nil, err
}
p.recursePin = set.SimpleSetFromKeys(recurseKeys)
}
{ // load direct set
var directKeys []key.Key
if err := loadSet(d, directPinDatastoreKey, &directKeys); err != nil {
return nil, err
}
p.directPin = set.SimpleSetFromKeys(directKeys)
}
{ // load indirect set
var err error
p.indirPin, err = loadIndirPin(d, indirectPinDatastoreKey)
if err != nil {
return nil, err
}
}
// assign services
p.dserv = dserv
p.dstore = d
return p, nil
}
// DirectKeys returns a slice containing the directly pinned keys
func (p *pinner) DirectKeys() []key.Key {
return p.directPin.GetKeys()
}
// IndirectKeys returns a slice containing the indirectly pinned keys
func (p *pinner) IndirectKeys() map[key.Key]int {
return p.indirPin.GetRefs()
}
// RecursiveKeys returns a slice containing the recursively pinned keys
func (p *pinner) RecursiveKeys() []key.Key {
return p.recursePin.GetKeys()
}
// Flush encodes and writes pinner keysets to the datastore
func (p *pinner) Flush() error {
p.lock.Lock()
defer p.lock.Unlock()
err := storeSet(p.dstore, directPinDatastoreKey, p.directPin.GetKeys())
if err != nil {
return err
}
err = storeSet(p.dstore, recursePinDatastoreKey, p.recursePin.GetKeys())
if err != nil {
return err
}
err = storeIndirPin(p.dstore, indirectPinDatastoreKey, p.indirPin)
if err != nil {
return err
}
return nil
}
// helpers to marshal / unmarshal a pin set
func storeSet(d ds.Datastore, k ds.Key, val interface{}) error {
buf, err := json.Marshal(val)
if err != nil {
return err
}
return d.Put(k, buf)
}
func loadSet(d ds.Datastore, k ds.Key, val interface{}) error {
buf, err := d.Get(k)
if err != nil {
return err
}
bf, ok := buf.([]byte)
if !ok {
return errors.New("invalid pin set value in datastore")
}
return json.Unmarshal(bf, val)
}
// PinWithMode is a method on ManualPinners, allowing the user to have fine
// grained control over pin counts
func (p *pinner) PinWithMode(k key.Key, mode PinMode) {
p.lock.Lock()
defer p.lock.Unlock()
switch mode {
case Recursive:
p.recursePin.AddBlock(k)
case Direct:
p.directPin.AddBlock(k)
case Indirect:
p.indirPin.Increment(k)
}
}
func (p *pinner) GetManual() ManualPinner {
return p
}