-
Notifications
You must be signed in to change notification settings - Fork 0
/
gc.go
68 lines (60 loc) · 1.4 KB
/
gc.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
package corerepo
import (
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
"github.com/jbenet/go-ipfs/core"
u "github.com/jbenet/go-ipfs/util"
eventlog "github.com/jbenet/go-ipfs/thirdparty/eventlog"
)
var log = eventlog.Logger("corerepo")
type KeyRemoved struct {
Key u.Key
}
func GarbageCollect(n *core.IpfsNode, ctx context.Context) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel() // in case error occurs during operation
keychan, err := n.Blockstore.AllKeysChan(ctx)
if err != nil {
return err
}
for k := range keychan { // rely on AllKeysChan to close chan
if !n.Pinning.IsPinned(k) {
err := n.Blockstore.DeleteBlock(k)
if err != nil {
return err
}
}
}
return nil
}
func GarbageCollectAsync(n *core.IpfsNode, ctx context.Context) (<-chan *KeyRemoved, error) {
keychan, err := n.Blockstore.AllKeysChan(ctx)
if err != nil {
return nil, err
}
output := make(chan *KeyRemoved)
go func() {
defer close(output)
for {
select {
case k, ok := <-keychan:
if !ok {
return
}
if !n.Pinning.IsPinned(k) {
err := n.Blockstore.DeleteBlock(k)
if err != nil {
log.Debugf("Error removing key from blockstore: %s", err)
continue
}
select {
case output <- &KeyRemoved{k}:
case <-ctx.Done():
}
}
case <-ctx.Done():
return
}
}
}()
return output, nil
}