add synchronous garbage collect function
Brian Tiger Chow committed
Jan 23, 2015 at 23:32 UTC
af20d67ac0da4040ca59922286f421bae6198b44
1 file changed
+18
core/corerepo/gc.go
+18
@@ -14,6 +14,24 @@ type KeyRemoved struct {
14
Key u.Key
15
}
16
17
+func GarbageCollect(n *core.IpfsNode, ctx context.Context) error {
18
+ ctx, cancel := context.WithCancel(context.Background())
19
+ defer cancel() // in case error occurs during operation
20
+ keychan, err := n.Blockstore.AllKeysChan(ctx)
21
+ if err != nil {
22
+ return err
23
+ }
24
+ for k := range keychan { // rely on AllKeysChan to close chan
25
+ if !n.Pinning.IsPinned(k) {
26
+ err := n.Blockstore.DeleteBlock(k)
27
+ if err != nil {
28
+ return err
29
+ }
30
+ }
31
+ }
32
+ return nil
33
+}
34
+
35
func GarbageCollectAsync(n *core.IpfsNode, ctx context.Context) (<-chan *KeyRemoved, error) {
36
37
keychan, err := n.Blockstore.AllKeysChan(ctx)