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

     1  /*
     2  	This file supports keyspaces for image block data types.
     3  */
     4  
     5  package imageblk
     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 = iota
    18  
    19  	// reserved type-specific key for metadata
    20  	keyProperties = datastore.PropertyTKeyClass
    21  
    22  	// use different id from other label-type keys to improve odds that any bad use of
    23  	// arbitrary key decoding will result in error.
    24  	keyImageBlock = 23
    25  
    26  	// legacy key class where extents property is stored
    27  	metaKeyClass = 24
    28  )
    29  
    30  // DescribeTKeyClass returns a string explanation of what a particular TKeyClass
    31  // is used for.  Implements the datastore.TKeyClassDescriber interface.
    32  func (d *Data) DescribeTKeyClass(tkc storage.TKeyClass) string {
    33  	switch tkc {
    34  	case keyProperties:
    35  		return "imageblk properties key"
    36  	case keyImageBlock:
    37  		return "imageblk block coord key"
    38  	default:
    39  		return "unknown imageblk key"
    40  	}
    41  }
    42  
    43  // NewTKeyByCoord returns a TKey for a block coord in string format.
    44  func NewTKeyByCoord(izyx dvid.IZYXString) storage.TKey {
    45  	return storage.NewTKey(keyImageBlock, []byte(izyx))
    46  }
    47  
    48  // NewTKey returns a type-specific key component for an image block.
    49  // TKey = s
    50  func NewTKey(idx dvid.Index) storage.TKey {
    51  	izyx := idx.(*dvid.IndexZYX)
    52  	return NewTKeyByCoord(izyx.ToIZYXString())
    53  }
    54  
    55  // MetaTKey provides a TKey for metadata (extents)
    56  func MetaTKey() storage.TKey {
    57  	return storage.NewTKey(metaKeyClass, nil)
    58  }
    59  
    60  // DecodeTKey returns a spatial index from a image block key.
    61  // TODO: Extend this when necessary to allow any form of spatial indexing like CZYX.
    62  func DecodeTKey(tk storage.TKey) (*dvid.IndexZYX, error) {
    63  	ibytes, err := tk.ClassBytes(keyImageBlock)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  	var zyx dvid.IndexZYX
    68  	if err = zyx.IndexFromBytes(ibytes); err != nil {
    69  		return nil, fmt.Errorf("Cannot recover ZYX index from image block key %v: %v\n", tk, err)
    70  	}
    71  	return &zyx, nil
    72  }