Docs: golint-ify pin package
License: MIT Signed-off-by: Hector Sanjuan <hector@protocol.ai>
Hector Sanjuan committed
Feb 13, 2018 at 12:53 UTC
ee7a8f3482db87c824e01a68c388799b3651bce4
2 files changed
+36
-9
pin/gc/gc.go
+18
-1
@@ -1,3 +1,4 @@
1
+// Package gc provides garbage collection for go-ipfs.
2
package gc
3
4
import (
@@ -35,7 +36,6 @@ type Result struct {
36
//
37
// The routine then iterates over every block in the blockstore and
38
// deletes any block that is not found in the marked set.
38
-//
39
func GC(ctx context.Context, bs bstore.GCBlockstore, dstor dstore.Datastore, pn pin.Pinner, bestEffortRoots []*cid.Cid) <-chan Result {
40
41
elock := log.EventBegin(ctx, "GC.lockWait")
@@ -125,6 +125,9 @@ func GC(ctx context.Context, bs bstore.GCBlockstore, dstor dstore.Datastore, pn
125
return output
126
}
127
128
+// Descendants recursively finds all the descendants of the given roots and
129
+// adds them to the given cid.Set, using the provided dag.GetLinks function
130
+// to walk the tree.
131
func Descendants(ctx context.Context, getLinks dag.GetLinks, set *cid.Set, roots []*cid.Cid) error {
132
for _, c := range roots {
133
set.Add(c)
@@ -191,24 +194,38 @@ func ColoredSet(ctx context.Context, pn pin.Pinner, ng ipld.NodeGetter, bestEffo
194
return gcs, nil
195
}
196
197
+// ErrCannotFetchAllLinks is returned as the last Result in the GC output
198
+// channel when there was a error creating the marked set because of a
199
+// problem when finding descendants.
200
var ErrCannotFetchAllLinks = errors.New("garbage collection aborted: could not retrieve some links")
201
202
+// ErrCannotDeleteSomeBlocks is returned when removing blocks marked for
203
+// deletion fails as the last Result in GC output channel.
204
var ErrCannotDeleteSomeBlocks = errors.New("garbage collection incomplete: could not delete some blocks")
205
206
+// CannotFetchLinksError provides detailed information about which links
207
+// could not be fetched and can appear as a Result in the GC output channel.
208
type CannotFetchLinksError struct {
209
Key *cid.Cid
210
Err error
211
}
212
213
+// Error implements the error interface for this type with a useful
214
+// message.
215
func (e *CannotFetchLinksError) Error() string {
216
return fmt.Sprintf("could not retrieve links for %s: %s", e.Key, e.Err)
217
}
218
219
+// CannotDeleteBlockError provides detailed information about which
220
+// blocks could not be deleted and can appear as a Result in the GC output
221
+// channel.
222
type CannotDeleteBlockError struct {
223
Key *cid.Cid
224
Err error
225
}
226
227
+// Error implements the error interface for this type with a
228
+// useful message.
229
func (e *CannotDeleteBlockError) Error() string {
230
return fmt.Sprintf("could not remove %s: %s", e.Key, e.Err)
231
}
pin/pin.go
+18
-8
@@ -1,4 +1,4 @@
1
-// package pin implements structures and methods to keep track of
1
+// Package pin implements structures and methods to keep track of
2
// which objects a user wants to keep stored locally.
3
package pin
4
@@ -43,8 +43,11 @@ const (
43
linkAll = "all"
44
)
45
46
+// PinMode allows to specify different types of pin (recursive, direct etc.).
47
+// See the Pin Modes constants for a full list.
48
type PinMode int
49
50
+// Pin Modes
51
const (
52
// Recursive pins pin the target cids along with any reachable children.
53
Recursive PinMode = iota
@@ -65,6 +68,7 @@ const (
68
Any
69
)
70
71
+// PinModeToString returns a human-readable name for the PinMode.
72
func PinModeToString(mode PinMode) (string, bool) {
73
m := map[PinMode]string{
74
Recursive: linkRecursive,
@@ -78,6 +82,8 @@ func PinModeToString(mode PinMode) (string, bool) {
82
return s, ok
83
}
84
85
+// StringToPinMode parses the result of PinModeToString() back to a PinMode.
86
+// It returns a boolean which is set to false if the mode is unknown.
87
func StringToPinMode(s string) (PinMode, bool) {
88
m := map[string]PinMode{
89
linkRecursive: Recursive,
@@ -92,6 +98,10 @@ func StringToPinMode(s string) (PinMode, bool) {
98
return mode, ok
99
}
100
101
+// A Pinner provides the necessary methods to keep track of Nodes which are
102
+// to be kept locally, according to a pin mode. In practice, a Pinner is in
103
+// in charge of keeping the list of items from the local storage that should
104
+// not be garbaged-collected.
105
type Pinner interface {
106
// IsPinned returns whether or not the given cid is pinned
107
// and an explanation of why its pinned
@@ -141,6 +151,10 @@ type Pinner interface {
151
InternalPins() []*cid.Cid
152
}
153
154
+// Pinned represents CID which has been pinned with a pinning strategy.
155
+// The Via field allows to identify the pinning parent of this CID, in the
156
+// case that the item is not pinned directly (but rather pinned recursively
157
+// by some ascendant).
158
type Pinned struct {
159
Key *cid.Cid
160
Mode PinMode
@@ -149,11 +163,7 @@ type Pinned struct {
163
164
// Pinned returns whether or not the given cid is pinned
165
func (p Pinned) Pinned() bool {
152
- if p.Mode == NotPinned {
153
- return false
154
- } else {
155
- return true
156
- }
166
+ return p.Mode != NotPinned
167
}
168
169
// String Returns pin status as string
@@ -240,6 +250,7 @@ func (p *pinner) Pin(ctx context.Context, node ipld.Node, recurse bool) error {
250
return nil
251
}
252
253
+// ErrNotPinned is returned when trying to unpin items which are not pinned.
254
var ErrNotPinned = fmt.Errorf("not pinned")
255
256
// Unpin a given key
@@ -258,9 +269,8 @@ func (p *pinner) Unpin(ctx context.Context, c *cid.Cid, recursive bool) error {
269
if recursive {
270
p.recursePin.Remove(c)
271
return nil
261
- } else {
262
- return fmt.Errorf("%s is pinned recursively", c)
272
}
273
+ return fmt.Errorf("%s is pinned recursively", c)
274
case "direct":
275
p.directPin.Remove(c)
276
return nil