1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
16
17 package mongo
18
19 import (
20 "context"
21 "log"
22
23 "gopkg.in/mgo.v2/bson"
24 "perkeep.org/pkg/blob"
25 )
26
27 func (m *mongoStorage) EnumerateBlobs(ctx context.Context, dest chan<- blob.SizedRef, after string, limit int) error {
28 defer close(dest)
29
30 var b blobDoc
31 var qry bson.M
32 if after != "" {
33 qry = bson.M{"key": bson.M{"$gt": after}}
34 }
35 iter := m.c.Find(qry).Limit(limit).Select(bson.M{"key": 1, "size": 1}).Sort("key").Iter()
36
37 for iter.Next(&b) {
38 br, ok := blob.Parse(b.Key)
39 if !ok {
40 continue
41 }
42 select {
43 case dest <- blob.SizedRef{Ref: br, Size: uint32(b.Size)}:
44 case <-ctx.Done():
45
46 if err := iter.Close(); err != nil {
47 log.Printf("Error closing iterator after enumerating: %v", err)
48 }
49 return ctx.Err()
50 }
51 }
52
53 if err := iter.Close(); err != nil {
54 return err
55 }
56
57 return nil
58 }