blob

package
v0.4.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: 5 Imported by: 27

Documentation

Overview

Package blob implements an interface and support code for persistent storage of untyped binary blobs.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrKeyExists is reported by Put when writing a key that already exists in
	// the store.
	ErrKeyExists = errors.New("key already exists")

	// ErrKeyNotFound is reported by Get or Size when given a key that does not
	// exist in the store.
	ErrKeyNotFound = errors.New("key not found")

	// ErrStopListing is used by a List callback to terminate the listing.
	ErrStopListing = errors.New("stop listing keys")
)

Functions

func IsKeyExists

func IsKeyExists(err error) bool

IsKeyExists reports whether err is or wraps ErrKeyExists.

func IsKeyNotFound

func IsKeyNotFound(err error) bool

IsKeyNotFound reports whether err or is or wraps ErrKeyNotFound. It is false if err == nil.

func KeyExists

func KeyExists(key string) error

KeyExists returns an ErrKeyExists error reporting that key exists in the store. The concrete type is *blob.KeyError.

func KeyNotFound

func KeyNotFound(key string) error

KeyNotFound returns an ErrKeyNotFound error reporting that key was not found. The concrete type is *blob.KeyError.

Types

type CAS

type CAS interface {
	Store

	// CASPut writes data to a content-addressed blob in the underlying store,
	// and returns the assigned key. The target key is returned even in case of
	// error.
	CASPut(ctx context.Context, data CASPutOptions) (string, error)

	// CASKey returns the content address of data without modifying the store.
	// This must be the same value that would be returned by a successful call
	// to CASPut on data.
	CASKey(ctx context.Context, opts CASPutOptions) (string, error)
}

CAS is an optional interface that a store may implement to support content addressing using a one-way hash.

type CASPutOptions

type CASPutOptions struct {
	Data           []byte // the data to be stored
	Prefix, Suffix string // a prefix and suffix to add to the computed key
}

CASPutOptions are the arguments to the CASPut and CASKey methods of a CAS implementation.

type HashCAS

type HashCAS struct {
	Store
	// contains filtered or unexported fields
}

A HashCAS is a content-addressable wrapper that adds the CAS methods to a delegated blob.Store.

func NewCAS

func NewCAS(s Store, h func() hash.Hash) HashCAS

NewCAS constructs a HashCAS that delegates to s and uses h to assign keys.

func (HashCAS) CASKey

func (c HashCAS) CASKey(_ context.Context, opts CASPutOptions) (string, error)

CASKey constructs the content address for the specified data. This implementation never reports an error.

func (HashCAS) CASPut

func (c HashCAS) CASPut(ctx context.Context, opts CASPutOptions) (string, error)

CASPut writes data to a content-addressed blob in the underlying store, and returns the assigned key. The target key is returned even in case of error.

type KeyError

type KeyError struct {
	Err error  // the underlying error
	Key string // the key implicated by the error
}

KeyError is the concrete type of errors involving a blob key. The caller may type-assert to *blob.KeyError to recover the key.

func (*KeyError) Error

func (k *KeyError) Error() string

Error implements the error interface for KeyError. The default error string does not include the key, since error values are often logged by default and keys may be sensitive.

func (*KeyError) Unwrap

func (k *KeyError) Unwrap() error

Unwrap returns the underlying error from k, to support error wrapping.

type ListSyncKeyer added in v0.4.0

type ListSyncKeyer struct {
	Store
}

ListSyncKeyer is a wrapper that adds the SyncKeys method to a delegated blob.Store, using its List method.

func (ListSyncKeyer) SyncKeys added in v0.4.0

func (s ListSyncKeyer) SyncKeys(ctx context.Context, keys []string) ([]string, error)

SyncKeys implements the SyncKeyer interface using the List method of the underlying store. Keys are returned in lexicographic order.

type PutOptions

type PutOptions struct {
	Key     string // the key to associate with the data
	Data    []byte // the data to write
	Replace bool   // whether to replace an existing value for this key
}

PutOptions regulate the behaviour of the Put method of a Store implementation.

type Store

type Store interface {
	// Get fetches the contents of a blob from the store. If the key is not
	// found in the store, Get must report an ErrKeyNotFound error.
	Get(ctx context.Context, key string) ([]byte, error)

	// Put writes a blob to the store. If the store already contains the
	// specified key and opts.Replace is true, the existing value is replaced
	// without error; otherwise Put must report an ErrKeyExists error.
	Put(ctx context.Context, opts PutOptions) error

	// Delete atomically removes a blob from the store. If the key is not found
	// in the store, Delete must report an ErrKeyNotFound error.
	Delete(ctx context.Context, key string) error

	// List calls f with each key in the store in lexicographic order, beginning
	// with the first key greater than or equal to start.  If f reports an error
	// listing stops and List returns.  If f reported an ErrStopListing error,
	// List returns nil; otherwise List returns the error reported by f.
	List(ctx context.Context, start string, f func(string) error) error

	// Len reports the number of keys currently in the store.
	Len(ctx context.Context) (int64, error)

	// Close allows the store to release any resources held open while in use.
	// If an implementation has nothing to release, it must return nil.
	Close(context.Context) error
}

A Store represents a mutable blob store in which each blob is identified by a unique, opaque string key. An implementation of Store is permitted (but not required) to report an error from Put when given an empty key. If the implementation cannot store empty keys, it must report ErrKeyNotFound when operating on an empty key.

Implementations of this interface must be safe for concurrent use by multiple goroutines. Moreover, any sequence of operations on a Store that does not overlap with any Delete executions must be linearizable.1

type SyncKeyer added in v0.4.0

type SyncKeyer interface {
	Store

	// SyncKeys reports which of the given keys are not present in the store.
	// If all the keys are present, SyncKeys returns an empty slice.
	// The order of returned keys is unspecified.
	SyncKeys(ctx context.Context, keys []string) ([]string, error)
}

SyncKeyer is an optional interface that a store may implement to support checking for the presence of keys in the store without fetching them.

Directories

Path Synopsis
Package memstore implements the blob.Store interface using a map.
Package memstore implements the blob.Store interface using a map.
Package storetest provides correctness tests for implementations of the blob.Store interface.
Package storetest provides correctness tests for implementations of the blob.Store interface.

Jump to

Keyboard shortcuts

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