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

     1  /*
     2  	This file supports keyspaces for label block data types.
     3  */
     4  
     5  package labelblk
     6  
     7  import (
     8  	"fmt"
     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 = 0
    18  
    19  	// reserved type-specific key for metadata
    20  	keyProperties = datastore.PropertyTKeyClass
    21  
    22  	// since we are reusing imageblk read code, need to use the same key.
    23  	keyLabelBlock = 23
    24  )
    25  
    26  // DescribeTKeyClass returns a string explanation of what a particular TKeyClass
    27  // is used for.  Implements the datastore.TKeyClassDescriber interface.
    28  func (d *Data) DescribeTKeyClass(tkc storage.TKeyClass) string {
    29  	if tkc == keyLabelBlock {
    30  		return "labelblk block key"
    31  	}
    32  	return "unknown labelblk key"
    33  }
    34  
    35  // NewTKeyByCoord returns a TKey for a block coord in string format.
    36  func NewTKeyByCoord(izyx dvid.IZYXString) storage.TKey {
    37  	return storage.NewTKey(keyLabelBlock, []byte(izyx))
    38  }
    39  
    40  // NewTKey returns a TKey for a label block, which is a slice suitable for
    41  // lexicographical ordering on zyx coordinates.
    42  func NewTKey(idx dvid.Index) storage.TKey {
    43  	izyx := idx.(*dvid.IndexZYX)
    44  	return NewTKeyByCoord(izyx.ToIZYXString())
    45  }
    46  
    47  // DecodeKey returns a spatial index from a label block key.
    48  // TODO: Extend this when necessary to allow any form of spatial indexing like CZYX.
    49  func DecodeTKey(tk storage.TKey) (*dvid.IndexZYX, error) {
    50  	ibytes, err := tk.ClassBytes(keyLabelBlock)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	var zyx dvid.IndexZYX
    55  	if err = zyx.IndexFromBytes(ibytes); err != nil {
    56  		return nil, fmt.Errorf("Cannot recover ZYX index from image block key %v: %v\n", tk, err)
    57  	}
    58  	return &zyx, nil
    59  }