github.com/janelia-flyem/dvid@v1.0.0/datatype/annotation/lowlevel.go (about)

     1  package annotation
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	"github.com/janelia-flyem/dvid/datastore"
     9  	"github.com/janelia-flyem/dvid/dvid"
    10  	"github.com/janelia-flyem/dvid/storage"
    11  )
    12  
    13  // scan does a range query on all blocks of annotations and compiles stats.
    14  func (d *Data) scan(ctx *datastore.VersionedCtx, w http.ResponseWriter, byCoord, keysOnly bool) error {
    15  	timedLog := dvid.NewTimeLog()
    16  
    17  	store, err := datastore.GetOrderedKeyValueDB(d)
    18  	if err != nil {
    19  		return err
    20  	}
    21  	var minTKey, maxTKey storage.TKey
    22  	if byCoord {
    23  		minTKey, maxTKey = BlockTKeyRange()
    24  	} else {
    25  		minTKey = storage.MinTKey(keyBlock)
    26  		maxTKey = storage.MaxTKey(keyBlock)
    27  	}
    28  
    29  	var numKV, numEmpty uint64
    30  	if keysOnly {
    31  		keyChan := make(storage.KeyChan)
    32  		go func() {
    33  			store.SendKeysInRange(ctx, minTKey, maxTKey, keyChan)
    34  			close(keyChan)
    35  		}()
    36  		for key := range keyChan {
    37  			if key != nil {
    38  				numKV++
    39  			}
    40  		}
    41  	} else {
    42  		err = store.ProcessRange(ctx, minTKey, maxTKey, nil, func(chunk *storage.Chunk) error {
    43  			numKV++
    44  			if len(chunk.V) == 0 {
    45  				numEmpty++
    46  			}
    47  			return nil
    48  		})
    49  	}
    50  	if err != nil {
    51  		return err
    52  	}
    53  	jsonBytes, err := json.Marshal(struct {
    54  		NumKV    uint64 `json:"num kv pairs"`
    55  		NumEmpty uint64 `json:"num empty blocks"`
    56  	}{numKV, numEmpty})
    57  	if err != nil {
    58  		return err
    59  	}
    60  	fmt.Fprint(w, string(jsonBytes))
    61  	timedLog.Infof("Scanned %d blocks (%d empty) in a /scan request (byCoord = %t, keysOnly = %t)",
    62  		numKV, numEmpty, byCoord, keysOnly)
    63  	return nil
    64  }