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

     1  /*
     2  	This file supports the keyspace for the keyvalue data type.
     3  */
     4  
     5  package keyvalue
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/janelia-flyem/dvid/datastore"
    11  	"github.com/janelia-flyem/dvid/storage"
    12  )
    13  
    14  const (
    15  	// keyUnknown should never be used and is a check for corrupt or incorrectly set keys
    16  	keyUnknown storage.TKeyClass = iota
    17  
    18  	// reserved type-specific key for metadata
    19  	keyProperties = datastore.PropertyTKeyClass
    20  
    21  	// the byte id for a standard key of a keyvalue
    22  	keyStandard = 177
    23  )
    24  
    25  var MinTKey = storage.MinTKey(keyStandard)
    26  var MaxTKey = storage.MaxTKey(keyStandard)
    27  
    28  // DescribeTKeyClass returns a string explanation of what a particular TKeyClass
    29  // is used for.  Implements the datastore.TKeyClassDescriber interface.
    30  func (d *Data) DescribeTKeyClass(tkc storage.TKeyClass) string {
    31  	if tkc == keyStandard {
    32  		return "keyvalue generic key"
    33  	}
    34  	return "unknown keyvalue key"
    35  }
    36  
    37  // NewTKey returns the "key" key component.
    38  func NewTKey(key string) (storage.TKey, error) {
    39  	return storage.NewTKey(keyStandard, append([]byte(key), 0)), nil
    40  }
    41  
    42  // DecodeTKey returns the string key used for this keyvalue.
    43  func DecodeTKey(tk storage.TKey) (string, error) {
    44  	ibytes, err := tk.ClassBytes(keyStandard)
    45  	if err != nil {
    46  		return "", err
    47  	}
    48  	sz := len(ibytes) - 1
    49  	if sz <= 0 {
    50  		return "", fmt.Errorf("empty key")
    51  	}
    52  	if ibytes[sz] != 0 {
    53  		return "", fmt.Errorf("expected 0 byte ending key of keyvalue key, got %d", ibytes[sz])
    54  	}
    55  	return string(ibytes[:sz]), nil
    56  }