github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/cmn/kvdb/api.go (about)

     1  // Package kvdb provides a local key/value database server for AIS.
     2  /*
     3   * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package kvdb
     6  
     7  import (
     8  	"strings"
     9  )
    10  
    11  // ## Collection ##
    12  //   For some databases(e.g., 'buntdb' or 'pudge') the collection is a pure
    13  //   virtual stuff: it is just a prefix of a key in database. But it seems
    14  //   useful to introduce this feature: other databases have 'bucket' concept
    15  //   or other ways to separate data at database level similar to tables in
    16  //   relational databases.
    17  // ## List ##
    18  //   If a pattern is empty, List should return the list of all keys in the
    19  //   collection. A pattern may include '*' and '?'. If a pattern does not
    20  //   include any of those characters, the pattern is considered a prefix and
    21  //   trailing '*' is added automatically
    22  // ## Errors ##
    23  //   Different databases use different ways to returns erros. A driver must
    24  //   standardize across.
    25  
    26  const CollectionSepa = "##"
    27  
    28  type (
    29  	Driver interface {
    30  		// A driver should sync data with local drives on close
    31  		Close() error
    32  		// Write an object to database. Object is marshaled as JSON
    33  		Set(collection, key string, object any) error
    34  		// Read an object from database.
    35  		Get(collection, key string, object any) error
    36  		// Write an already marshaled object or simple string
    37  		SetString(collection, key, data string) error
    38  		// Read a string or an object as JSON from database
    39  		GetString(collection, key string) (string, error)
    40  		// Delete a single object
    41  		Delete(collection, key string) error
    42  		// Delete a collection. It iterates over all subkeys of key
    43  		// `collection` and removes them one by one.
    44  		DeleteCollection(collection string) error
    45  		// Return subkeys of a collection(`key` is empty string) or a key.
    46  		// Pattern is an arbitrary string and may contain '*' and '?' wildcards.
    47  		// If a pattern does not include wildcards, the pattern is uses as a prefix.
    48  		List(collection, pattern string) ([]string, error)
    49  		// Return subkeys with their values: map[key]value
    50  		GetAll(collection, pattern string) (map[string]string, error)
    51  	}
    52  )
    53  
    54  // Extract collection and key names from full key path
    55  func ParsePath(path string) (string, string) {
    56  	pos := strings.Index(path, CollectionSepa)
    57  	if pos < 0 {
    58  		return path, ""
    59  	}
    60  	return path[:pos], path[pos+len(CollectionSepa):]
    61  }