@cryptotaxi247 / kubo / commits / d39d9ed60

gc: address CR comments

License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>

Kevin Atkinson committed Mar 20, 2017 at 14:46 UTC d39d9ed60b78667e60922868c4763f16c9d66555
3 files changed +23 -13
core/commands/repo.go
+1
@@ -40,6 +40,7 @@ var RepoCmd = &cmds.Command{
40 },
41 }
42
43 +// GcResult is the result returned by "repo gc" command.
44 type GcResult struct {
45 Key *cid.Cid
46 Error string `json:",omitempty"`
core/corerepo/gc.go
+5
@@ -91,6 +91,9 @@ func GarbageCollect(n *core.IpfsNode, ctx context.Context) error {
91 return CollectResult(ctx, rmed, nil)
92 }
93
94 +// CollectResult collects the output of a garbage collection run and calls the
95 +// given callback for each object removed. It also collects all errors into a
96 +// MultiError which is returned after the gc is completed.
97 func CollectResult(ctx context.Context, gcOut <-chan gc.Result, cb func(*cid.Cid)) error {
98 var errors []error
99 loop:
@@ -121,10 +124,12 @@ loop:
124 }
125 }
126
127 +// NewMultiError creates a new MultiError object from a given slice of errors.
128 func NewMultiError(errs ...error) *MultiError {
129 return &MultiError{errs[:len(errs)-1], errs[len(errs)-1]}
130 }
131
132 +// MultiError contains the results of multiple errors.
133 type MultiError struct {
134 Errors []error
135 Summary error
pin/gc/gc.go
+17 -13
@@ -16,6 +16,8 @@ import (
16
17 var log = logging.Logger("gc")
18
19 +// Result represents an incremental output from a garbage collection
20 +// run. It contains either an error, or the cid of a removed object.
21 type Result struct {
22 KeyRemoved *cid.Cid
23 Error error
@@ -66,7 +68,7 @@ func GC(ctx context.Context, bs bstore.GCBlockstore, ls dag.LinkService, pn pin.
68 err := bs.DeleteBlock(k)
69 if err != nil {
70 errors = true
69 - output <- Result{Error: &CouldNotDeleteBlockError{k, err}}
71 + output <- Result{Error: &CannotDeleteBlockError{k, err}}
72 //log.Errorf("Error removing key from blockstore: %s", err)
73 // continue as error is non-fatal
74 continue loop
@@ -82,7 +84,7 @@ func GC(ctx context.Context, bs bstore.GCBlockstore, ls dag.LinkService, pn pin.
84 }
85 }
86 if errors {
85 - output <- Result{Error: ErrCouldNotDeleteSomeBlocks}
87 + output <- Result{Error: ErrCannotDeleteSomeBlocks}
88 }
89 }()
90
@@ -103,6 +105,8 @@ func Descendants(ctx context.Context, getLinks dag.GetLinks, set *cid.Set, roots
105 return nil
106 }
107
108 +// ColoredSet computes the set of nodes in the graph that are pinned by the
109 +// pins in the given pinner.
110 func ColoredSet(ctx context.Context, pn pin.Pinner, ls dag.LinkService, bestEffortRoots []*cid.Cid, output chan<- Result) (*cid.Set, error) {
111 // KeySet currently implemented in memory, in the future, may be bloom filter or
112 // disk backed to conserve memory.
@@ -112,7 +116,7 @@ func ColoredSet(ctx context.Context, pn pin.Pinner, ls dag.LinkService, bestEffo
116 links, err := ls.GetLinks(ctx, cid)
117 if err != nil {
118 errors = true
115 - output <- Result{Error: &CouldNotFetchLinksError{cid, err}}
119 + output <- Result{Error: &CannotFetchLinksError{cid, err}}
120 }
121 return links, nil
122 }
@@ -126,7 +130,7 @@ func ColoredSet(ctx context.Context, pn pin.Pinner, ls dag.LinkService, bestEffo
130 links, err := ls.GetLinks(ctx, cid)
131 if err != nil && err != dag.ErrNotFound {
132 errors = true
129 - output <- Result{Error: &CouldNotFetchLinksError{cid, err}}
133 + output <- Result{Error: &CannotFetchLinksError{cid, err}}
134 }
135 return links, nil
136 }
@@ -147,30 +151,30 @@ func ColoredSet(ctx context.Context, pn pin.Pinner, ls dag.LinkService, bestEffo
151 }
152
153 if errors {
150 - return nil, ErrCouldNotFetchAllLinks
151 - } else {
152 - return gcs, nil
154 + return nil, ErrCannotFetchAllLinks
155 }
156 +
157 + return gcs, nil
158 }
159
156 -var ErrCouldNotFetchAllLinks = errors.New("garbage collection aborted: could not retrieve some links")
160 +var ErrCannotFetchAllLinks = errors.New("garbage collection aborted: could not retrieve some links")
161
158 -var ErrCouldNotDeleteSomeBlocks = errors.New("garbage collection incomplete: could not delete some blocks")
162 +var ErrCannotDeleteSomeBlocks = errors.New("garbage collection incomplete: could not delete some blocks")
163
160 -type CouldNotFetchLinksError struct {
164 +type CannotFetchLinksError struct {
165 Key *cid.Cid
166 Err error
167 }
168
165 -func (e *CouldNotFetchLinksError) Error() string {
169 +func (e *CannotFetchLinksError) Error() string {
170 return fmt.Sprintf("could not retrieve links for %s: %s", e.Key, e.Err)
171 }
172
169 -type CouldNotDeleteBlockError struct {
173 +type CannotDeleteBlockError struct {
174 Key *cid.Cid
175 Err error
176 }
177
174 -func (e *CouldNotDeleteBlockError) Error() string {
178 +func (e *CannotDeleteBlockError) Error() string {
179 return fmt.Sprintf("could not remove %s: %s", e.Key, e.Err)
180 }