@cryptotaxi247 / kubo / commits / 24aa42d85

blockstore.AllKeyChan: avoid channels by using the new NextSync method

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

Kevin Atkinson committed Nov 30, 2016 at 17:45 UTC 24aa42d85d10ca858ba0f8ea0e54ec6151cf3fb4
1 file changed +11 -28
blocks/blockstore/blockstore.go
+11 -28
@@ -181,44 +181,27 @@ func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan *cid.Cid, error)
181 return nil, err
182 }
183
184 - // this function is here to compartmentalize
185 - get := func() (*cid.Cid, bool) {
186 - select {
187 - case <-ctx.Done():
188 - return nil, false
189 - case e, more := <-res.Next():
190 - if !more {
191 - return nil, false
192 - }
193 - if e.Error != nil {
194 - log.Debug("blockstore.AllKeysChan got err:", e.Error)
195 - return nil, false
196 - }
197 -
198 - // need to convert to key.Key using key.KeyFromDsKey.
199 - c, err := dshelp.DsKeyToCid(ds.RawKey(e.Key))
200 - if err != nil {
201 - log.Warningf("error parsing key from DsKey: ", err)
202 - return nil, true
203 - }
204 -
205 - return c, true
206 - }
207 - }
208 -
184 output := make(chan *cid.Cid, dsq.KeysOnlyBufSize)
185 go func() {
186 defer func() {
212 - res.Process().Close() // ensure exit (signals early exit, too)
187 + res.Close() // ensure exit (signals early exit, too)
188 close(output)
189 }()
190
191 for {
217 - k, ok := get()
192 + e, ok := res.NextSync()
193 if !ok {
194 return
195 }
221 - if k == nil {
196 + if e.Error != nil {
197 + log.Debug("blockstore.AllKeysChan got err:", e.Error)
198 + continue
199 + }
200 +
201 + // need to convert to key.Key using key.KeyFromDsKey.
202 + k, err := dshelp.DsKeyToCid(ds.RawKey(e.Key))
203 + if err != nil {
204 + log.Warningf("error parsing key from DsKey: ", err)
205 continue
206 }
207