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

     1  /*
     2  	This file supports the keyspace for the keyvalue data type.
     3  */
     4  package neuronjson
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/janelia-flyem/dvid/datastore"
    10  	"github.com/janelia-flyem/dvid/storage"
    11  )
    12  
    13  const (
    14  	// keyUnknown should never be used and is a check for corrupt or incorrectly set keys
    15  	keyUnknown storage.TKeyClass = iota
    16  
    17  	// reserved type-specific key for metadata
    18  	keyProperties = datastore.PropertyTKeyClass
    19  
    20  	// key = bodyid. value = serialized map[string]interface{} annotation
    21  	keyAnnotation = 179
    22  
    23  	// single key-value for neutu/neu3 schema JSON
    24  	keySchema = 180
    25  
    26  	// single key-value for neutu/neu3 schema-batch JSON
    27  	keySchemaBatch = 181
    28  
    29  	// single key-value for JSON validation schema
    30  	keyJSONSchema = 182
    31  )
    32  
    33  var MinAnnotationTKey = storage.MinTKey(keyAnnotation)
    34  var MaxAnnotationTKey = storage.MaxTKey(keyAnnotation)
    35  
    36  // DescribeTKeyClass returns a string explanation of what a particular TKeyClass
    37  // is used for.  Implements the datastore.TKeyClassDescriber interface.
    38  func (d *Data) DescribeTKeyClass(tkc storage.TKeyClass) string {
    39  	switch tkc {
    40  	case keyAnnotation:
    41  		return "neuron annotation"
    42  	case keySchema:
    43  		return "neutu/neu3 schema"
    44  	case keySchemaBatch:
    45  		return "neutu/neu3 schema for batches"
    46  	case keyJSONSchema:
    47  		return "neuron annotation JSON schema for validation"
    48  	default:
    49  	}
    50  	return "unknown neuronjson key"
    51  }
    52  
    53  // NewTKey returns a TKey for the annotation kv pairs.
    54  func NewTKey(key string) (storage.TKey, error) {
    55  	return storage.NewTKey(keyAnnotation, append([]byte(key), 0)), nil
    56  }
    57  
    58  // DecodeTKey returns the string of the bodyid used for this annotation.
    59  func DecodeTKey(tk storage.TKey) (string, error) {
    60  	ibytes, err := tk.ClassBytes(keyAnnotation)
    61  	if err != nil {
    62  		return "", err
    63  	}
    64  	sz := len(ibytes) - 1
    65  	if sz <= 0 {
    66  		return "", fmt.Errorf("empty key")
    67  	}
    68  	if ibytes[sz] != 0 {
    69  		return "", fmt.Errorf("expected 0 byte ending key of neuronjson key, got %d", ibytes[sz])
    70  	}
    71  	return string(ibytes[:sz]), nil
    72  }
    73  
    74  // NewJSONSchemaTKey returns a TKey for JSON validation schema storage.
    75  func NewJSONSchemaTKey() (storage.TKey, error) {
    76  	return storage.NewTKey(keyJSONSchema, nil), nil
    77  }
    78  
    79  // NewSchemaTKey returns a TKey for schema storage.
    80  func NewSchemaTKey() (storage.TKey, error) {
    81  	return storage.NewTKey(keySchema, nil), nil
    82  }
    83  
    84  // NewSchemaBatchTKey returns a TKey for batch schema storage.
    85  func NewSchemaBatchTKey() (storage.TKey, error) {
    86  	return storage.NewTKey(keySchemaBatch, nil), nil
    87  }
    88  
    89  func getMetadataKey(meta Schema) (tkey storage.TKey, err error) {
    90  	switch meta {
    91  	case JSONSchema:
    92  		if tkey, err = NewJSONSchemaTKey(); err != nil {
    93  			return
    94  		}
    95  	case NeuSchema:
    96  		if tkey, err = NewSchemaTKey(); err != nil {
    97  			return
    98  		}
    99  	case NeuSchemaBatch:
   100  		if tkey, err = NewSchemaBatchTKey(); err != nil {
   101  			return
   102  		}
   103  	}
   104  	return tkey, nil
   105  }