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

     1  /*
     2  	This file supports the keyspace for the tarsupervoxels data type.
     3  */
     4  
     5  package tarsupervoxels
     6  
     7  import (
     8  	"fmt"
     9  	"strconv"
    10  
    11  	"github.com/janelia-flyem/dvid/datastore"
    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  	// the byte id for a standard key of a tarsupervoxels keyvalue
    23  	keyStandard = 133
    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 == keyStandard {
    30  		return "supervoxel data filename key"
    31  	}
    32  	return "unknown tarsupervoxels key"
    33  }
    34  
    35  // NewTKey returns the type-specific key corresponding to a supervoxel id in
    36  // simple ASCII bytes.
    37  func NewTKey(supervoxel uint64, ext string) (storage.TKey, error) {
    38  	filename := strconv.FormatUint(supervoxel, 10) + "." + ext
    39  	return storage.NewTKey(keyStandard, []byte(filename)), nil
    40  }
    41  
    42  // DecodeTKey returns the supervoxel id corresponding to the type-specific ke.y
    43  func DecodeTKey(tk storage.TKey) (supervoxel uint64, ext string, err error) {
    44  	var fnameBytes []byte
    45  	if fnameBytes, err = tk.ClassBytes(keyStandard); err != nil {
    46  		return
    47  	}
    48  	_, err = fmt.Sscanf(string(fnameBytes), "%s.%d", &supervoxel, &ext)
    49  	return
    50  }