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

     1  /*
     2  	This file supports keyspaces for labelvol data type.
     3  */
     4  
     5  package labelvol
     6  
     7  import (
     8  	"encoding/binary"
     9  
    10  	"github.com/janelia-flyem/dvid/datastore"
    11  	"github.com/janelia-flyem/dvid/dvid"
    12  	"github.com/janelia-flyem/dvid/storage"
    13  )
    14  
    15  const (
    16  	// keyUnknown should never be used and is a check for corrupt or incorrectly set keys
    17  	keyUnknown storage.TKeyClass = iota
    18  
    19  	// reserved type-specific key for metadata
    20  	keyProperties = datastore.PropertyTKeyClass
    21  
    22  	// keyLabelBlockRLE have keys ordered by label + block coord, and have a sparse volume
    23  	// encoding for its value. They are also useful for returning all blocks
    24  	// intersected by a label.
    25  	keyLabelBlockRLE = 227
    26  
    27  	keyLabelMax = 228
    28  
    29  	keyRepoLabelMax = 229
    30  )
    31  
    32  // DescribeTKeyClass returns a string explanation of what a particular TKeyClass
    33  // is used for.  Implements the datastore.TKeyClassDescriber interface.
    34  func (d *Data) DescribeTKeyClass(tkc storage.TKeyClass) string {
    35  	switch tkc {
    36  	case keyLabelBlockRLE:
    37  		return "labelvol label + block coord key"
    38  	case keyLabelMax:
    39  		return "labelvol label max key"
    40  	case keyRepoLabelMax:
    41  		return "labelvol repo label max key"
    42  	default:
    43  	}
    44  	return "unknown labelvol key"
    45  }
    46  
    47  // NewTKey returns a TKey for storing a "label + spatial index", where
    48  // the spatial index references a block that contains a voxel with the given label.
    49  func NewTKey(label uint64, block dvid.IZYXString) storage.TKey {
    50  	sz := len(block)
    51  	ibytes := make([]byte, 8+sz)
    52  	binary.BigEndian.PutUint64(ibytes[0:8], label)
    53  	copy(ibytes[8:], []byte(block))
    54  	return storage.NewTKey(keyLabelBlockRLE, ibytes)
    55  }
    56  
    57  // DecodeTKey returns a label and block index bytes from a label block RLE key.
    58  // The block index bytes are returned because different block indices may be used (e.g., CZYX),
    59  // and its up to caller to determine which one is used for this particular key.
    60  func DecodeTKey(tk storage.TKey) (label uint64, block dvid.IZYXString, err error) {
    61  	ibytes, err := tk.ClassBytes(keyLabelBlockRLE)
    62  	if err != nil {
    63  		return
    64  	}
    65  	label = binary.BigEndian.Uint64(ibytes[0:8])
    66  	block = dvid.IZYXString(ibytes[8:])
    67  	return
    68  }
    69  
    70  var (
    71  	maxLabelTKey     = storage.NewTKey(keyLabelMax, nil)
    72  	maxRepoLabelTKey = storage.NewTKey(keyRepoLabelMax, nil)
    73  )