Documentation ¶
Overview ¶
microstorage provides an opinionated key-value abstraction for storage.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var InvalidKeyError = µerror.Error{
Kind: "InvalidKeyError",
}
InvalidKeyError is exported as it is used by the interface implementations in order to fulfil the API.
var NotFoundError = µerror.Error{
Kind: "NotFoundError",
}
NotFoundError is exported as it is used by the interface implementations in order to fulfil the API.
var ( // RootKey may be used to list all values in the Storage. RootKey = K{/* contains filtered or unexported fields */} )
Functions ¶
func IsInvalidConfig ¶
IsInvalidConfig asserts invalidConfigError.
func IsInvalidKey ¶
IsInvalidKey asserts InvalidKeyError. The library user's code should use this public key matcher to verify if some storage error is of type InvalidKeyError.
func IsNotFound ¶
IsNotFound asserts NotFoundError. The library user's code should use this public key matcher to verify if some storage error is of type NotFoundError.
func SanitizeKey ¶
SanitizeKey ensures the key has leading slash and does not have trailing slash. It fails with InvalidKeyError when key is invalid.
A valid key does not contain double slashes, is not empty, and does not contain only slashes.
This function is meant to be used by Storage implementations to simplify key validation logic and potentially implementation logic, because it reduces number of invariants to check greatly.
NOTE: There is a special case of this function: SanitizeListKey. isValidKey checks if this storage key is valid, i.e. does not contain double slashes, is not empty, and does not contain only slashes.
func SanitizeListKey ¶
SanitizeListKey sanitizes key ad makes sure it is valid for the Storage.List operation. List is special because unlike other operations it can take "/" as a key. Otherwise this is behaves like SanitizeKey function.
Types ¶
type K ¶
type K struct {
// contains filtered or unexported fields
}
K is an immutable, valid key.
func MustK ¶
MustK is a helper that wraps a call to a function returning (K, error) and panics if the error is non-nil. It is intended for use in variable initializations such as
var k = microstorage.MustK(microstorage.NewK("key"))
func NewK ¶
NewK creates a new immutable key to query the Storage with.
The key should take a format of path separated by slashes "/". E.g. "/a/b/c". The key is sanitized before querying the storage. I.e. leading slash can be added and trailing slash can be removed. E.g. "/a/b/c", "a/b/c/", "a/b/c", and "/a/b/c/" represent the same key.
NewK may fail if the key is not valid. See SanitizeKey to see what a valid key looks like.
Example ¶
firstKey, _ := NewK("/a/b/c") fmt.Println(firstKey.Key()) secondKey, _ := NewK("a/b/c") fmt.Println(secondKey.Key()) thirdKey, _ := NewK("a/b/c/") fmt.Println(thirdKey.Key())
Output: /a/b/c /a/b/c /a/b/c
func (K) Key ¶
Key returns the actual sanitized key value.
Example ¶
key, _ := NewK("/a/b/c") fmt.Println(key.Key())
Output: /a/b/c
func (K) KeyNoLeadingSlash ¶
KeyNoLeadingSlash returns the actual sanitized key value with leading slash stripped.
Example ¶
key, _ := NewK("/a/b/c") fmt.Println(key.KeyNoLeadingSlash())
Output: a/b/c
type KV ¶
type KV struct {
// contains filtered or unexported fields
}
KV is an immutable key-value pair with a valid key.
func MustKV ¶
MustKV is a helper that wraps a call to a function returning (KV, error) and panics if the error is non-nil. It is intended for use in Storage implementations, where the key is known to be valid because it is retrieved from the storage.
func NewKV ¶
NewKV creates a new immutable key-value pair to update Storage with.
The key should take a format of path separated by slashes "/". E.g. "/a/b/c". The key is sanitized before inserting to the storage. I.e. leading slash can be added and trailing slash can be removed. E.g. "/a/b/c", "a/b/c/", "a/b/c", and "/a/b/c/" represent the same key.
The val is an arbitrary value stored under the key.
NewKV may fail if the key is not valid. See SanitizeKey to see what a valid key looks like.
Example ¶
firstKeyValue, _ := NewKV("/a/b/c", "foo") fmt.Println(firstKeyValue.Key()) fmt.Println(firstKeyValue.Val()) secondKeyValue, _ := NewKV("a/b/c", "bar") fmt.Println(secondKeyValue.Key()) fmt.Println(secondKeyValue.Val()) thirdKeyValue, _ := NewKV("a/b/c/", "baz") fmt.Println(thirdKeyValue.Key()) fmt.Println(thirdKeyValue.Val())
Output: /a/b/c foo /a/b/c bar /a/b/c baz
func (KV) K ¶
K returns the K instance created from the key value associated with this key-value pair.
Example ¶
kv, _ := NewKV("/a/b/c", "foo") fmt.Println(kv.K().Key())
Output: /a/b/c
func (KV) Key ¶
Key returns the sanitized key associated with this key-value pair.
Example ¶
kv, _ := NewKV("/a/b/c", "foo") fmt.Println(kv.Key())
Output: /a/b/c
func (KV) KeyNoLeadingSlash ¶
KeyNoLeadingSlashreturns the sanitized key associated with this key-value pair with leading slash stripped.
Example ¶
kv, _ := NewKV("/a/b/c", "foo") fmt.Println(kv.KeyNoLeadingSlash())
Output: a/b/c
type Storage ¶
type Storage interface { // Put stores the given value under the given key. If the value // under the key already exists Put overrides it. Put(ctx context.Context, kv KV) error // Delete removes the value stored under the given key. Delete(ctx context.Context, key K) error // Exists checks if a value under the given key exists or not. Exists(ctx context.Context, key K) (bool, error) // List does a lookup for all keys stored under the key, and returns the // relative key path, if any. // E.g: listing /foo/, with the key /foo/bar, returns bar. List(ctx context.Context, key K) ([]KV, error) // Search does a lookup for the value stored under key and returns it, if any. Search(ctx context.Context, key K) (KV, error) }
Storage represents the abstraction for underlying storage backends.
Directories ¶
Path | Synopsis |
---|---|
Package memory provides a memory storage implementation.
|
Package memory provides a memory storage implementation. |