github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/blobserver/mongo/enumerate.go (about)

     1  /*
     2  Copyright 2014 The Camlistore Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package mongo
    18  
    19  import (
    20  	"log"
    21  
    22  	"camlistore.org/pkg/blob"
    23  	"camlistore.org/pkg/context"
    24  	"camlistore.org/third_party/labix.org/v2/mgo/bson"
    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  			// Close the iterator but ignore the error value since we are already cancelling
    46  			if err := iter.Close(); err != nil {
    47  				log.Printf("Error closing iterator after enumerating: %v", err)
    48  			}
    49  			return context.ErrCanceled
    50  		}
    51  	}
    52  
    53  	if err := iter.Close(); err != nil {
    54  		return err
    55  	}
    56  
    57  	return nil
    58  }