storage

package
v0.1.0-beta6 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2024 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package storage provides a key-value storage abstraction where values are JSON-serializable structs. It is used by git-spice to store metadata in a git repository. It requires all write operations to have a message.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotExist = errors.New("does not exist in store")

ErrNotExist indicates that a key that was expected to exist does not exist.

Functions

This section is empty.

Types

type Backend

type Backend interface {
	// Get retrieves a value from the store
	// and decodes it into dst.
	//
	// If the key does not exist, Get returns ErrNotExist.
	Get(ctx context.Context, key string, dst any) error

	Update(ctx context.Context, req UpdateRequest) error
	Clear(ctx context.Context, msg string) error

	// Keys lists the keys in the store in the given directory,
	// with the directory prefix removed.
	//
	// The directory is defined as '/'-separated components in the key.
	// If dir is empty, all keys are listed.
	Keys(ctx context.Context, dir string) ([]string, error)
}

Backend defines the primitive operations for the key-value store.

type DB

type DB struct{ Backend }

DB is a high-level wrapper around a Backend. It provides a more convenient API for interacting with the store.

func NewDB

func NewDB(b Backend) *DB

NewDB creates a new DB using the given Backend.

func (*DB) Delete

func (db *DB) Delete(ctx context.Context, key string, msg string) error

Delete removes a key from the store.

func (*DB) Set

func (db *DB) Set(ctx context.Context, key string, value any, msg string) error

Set adds or updates a single key in the store.

type GitBackend

type GitBackend struct {
	// contains filtered or unexported fields
}

GitBackend implements a storage backend using a Git repository reference as the storage medium.

func NewGitBackend

func NewGitBackend(cfg GitConfig) *GitBackend

NewGitBackend creates a new GitBackend that stores data in the given Git repository.

func (*GitBackend) Clear

func (g *GitBackend) Clear(ctx context.Context, msg string) error

Clear removes all keys from the store.

func (*GitBackend) Get

func (g *GitBackend) Get(ctx context.Context, key string, v interface{}) error

Get retrieves a value from the store and decodes it into v.

func (*GitBackend) Keys

func (g *GitBackend) Keys(ctx context.Context, dir string) ([]string, error)

Keys lists the keys in the store in the given directory.

func (*GitBackend) Update

func (g *GitBackend) Update(ctx context.Context, req UpdateRequest) error

Update applies a batch of changes to the store.

type GitConfig

type GitConfig struct {
	Repo                    GitRepository
	Ref                     string
	AuthorName, AuthorEmail string
	Log                     *log.Logger
}

GitConfig is used to configure a GitBackend.

type GitRepository

type GitRepository interface {
	PeelToCommit(ctx context.Context, ref string) (git.Hash, error)
	PeelToTree(ctx context.Context, ref string) (git.Hash, error)
	HashAt(ctx context.Context, commitish, path string) (git.Hash, error)

	ReadObject(ctx context.Context, typ git.Type, hash git.Hash, dst io.Writer) error
	WriteObject(ctx context.Context, typ git.Type, src io.Reader) (git.Hash, error)

	ListTree(ctx context.Context, tree git.Hash, opts git.ListTreeOptions) ([]git.TreeEntry, error)
	CommitTree(ctx context.Context, req git.CommitTreeRequest) (git.Hash, error)
	UpdateTree(ctx context.Context, req git.UpdateTreeRequest) (git.Hash, error)
	MakeTree(ctx context.Context, ents []git.TreeEntry) (git.Hash, error)

	SetRef(ctx context.Context, req git.SetRefRequest) error
}

GitRepository is the subset of the git.Repository API used by the state package.

type MemBackend

type MemBackend struct {
	// contains filtered or unexported fields
}

MemBackend is an in-memory storage backend.

func NewMemBackend

func NewMemBackend() *MemBackend

NewMemBackend creates a new MemBackend.

func (*MemBackend) Clear

func (m *MemBackend) Clear(context.Context, string) error

Clear clears all keys in the store.

func (*MemBackend) Get

func (m *MemBackend) Get(ctx context.Context, key string, dst any) error

Get retrieves a value from the store

func (*MemBackend) Keys

func (m *MemBackend) Keys(ctx context.Context, dir string) ([]string, error)

Keys returns a list of keys in the store.

func (*MemBackend) Update

func (m *MemBackend) Update(ctx context.Context, req UpdateRequest) error

Update applies a batch of changes to the store.

type SetRequest

type SetRequest struct {
	// Key to write to.
	Key string

	// Value to serialize to JSON.
	Value any
}

SetRequest is a single operation to add or update a key.

type UpdateRequest

type UpdateRequest struct {
	Sets []SetRequest

	// Deletes lists the keys to delete.
	Deletes []string

	// Message to attach to the batch oepration.
	Message string
}

UpdateRequest performs a batch of write operations in one transaction.

Jump to

Keyboard shortcuts

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