microstorage

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2024 License: Apache-2.0 Imports: 4 Imported by: 9

README

CircleCI Go Report Card

microstorage

Old key-value storage abstraction library that is used in Giant Swarm microservices. Main use-case for this is to provide abstraction between crdstorage and memorystorage.

Documentation

Overview

microstorage provides an opinionated key-value abstraction for storage.

Index

Examples

Constants

This section is empty.

Variables

View Source
var InvalidKeyError = &microerror.Error{
	Kind: "InvalidKeyError",
}

InvalidKeyError is exported as it is used by the interface implementations in order to fulfil the API.

View Source
var NotFoundError = &microerror.Error{
	Kind: "NotFoundError",
}

NotFoundError is exported as it is used by the interface implementations in order to fulfil the API.

View Source
var (
	// RootKey may be used to list all values in the Storage.
	RootKey = K{/* contains filtered or unexported fields */}
)

Functions

func IsInvalidConfig

func IsInvalidConfig(err error) bool

IsInvalidConfig asserts invalidConfigError.

func IsInvalidKey

func IsInvalidKey(err error) bool

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

func IsNotFound(err error) bool

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

func SanitizeKey(key string) (string, error)

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

func SanitizeListKey(key string) (string, error)

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

func MustK(k K, err error) K

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

func NewK(key string) (K, error)

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

func (k K) Key() string

Key returns the actual sanitized key value.

Example
key, _ := NewK("/a/b/c")
fmt.Println(key.Key())
Output:

/a/b/c

func (K) KeyNoLeadingSlash

func (k K) KeyNoLeadingSlash() string

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

func MustKV(kv KV, err error) KV

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

func NewKV(key, val string) (KV, error)

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

func (k KV) K() 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

func (k KV) Key() string

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

func (k KV) KeyNoLeadingSlash() string

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

func (KV) Val

func (k KV) Val() string

Val returns the value associated with this key-value pair.

Example
kv, _ := NewKV("/a/b/c", "foo")
fmt.Println(kv.Val())
Output:

foo

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL