Home Download Docs Code Community
import "perkeep/pkg/sorted"
Overview
Index
Subdirectories

Overview ▾

Package sorted provides a KeyValue interface and constructor registry.

Index

Constants
Variables
func CheckSizes(key, value string) error
func Foreach(kv KeyValue, fn func(key, value string) error) error
func ForeachInRange(kv KeyValue, start, end string, fn func(key, value string) error) error
func RegisterKeyValue(typ string, fn func(jsonconfig.Obj) (KeyValue, error))
type BatchMutation
    func NewBatchMutation() BatchMutation
type Iterator
type KeyValue
    func NewKeyValue(cfg jsonconfig.Obj) (KeyValue, error)
    func NewKeyValueMaybeWipe(cfg jsonconfig.Obj) (KeyValue, error)
    func NewMemoryKeyValue() KeyValue
type Mutation
type NeedWipeError
    func (e NeedWipeError) Error() string
type ReadTransaction
type TransactionalReader
type Wiper

Package files

kv.go mem.go

Constants

const (
    MaxKeySize   = 767   // Maximum size, in bytes, for a key in any store implementing KeyValue.
    MaxValueSize = 63000 // Maximum size, in bytes, for a value in any store implementing KeyValue. MaxKeySize and MaxValueSize values originate from InnoDB and MySQL limitations.
)
const DefaultKVFileType = "leveldb"

Variables

var (
    ErrNotFound      = errors.New("sorted: key not found")
    ErrKeyTooLarge   = fmt.Errorf("sorted: key size is over %v", MaxKeySize)
    ErrValueTooLarge = fmt.Errorf("sorted: value size is over %v", MaxValueSize)
)

func CheckSizes

func CheckSizes(key, value string) error

CheckSizes returns ErrKeyTooLarge if key does not respect KeyMaxSize or ErrValueTooLarge if value does not respect ValueMaxSize

func Foreach

func Foreach(kv KeyValue, fn func(key, value string) error) error

Foreach runs fn for each key/value pair in kv. If fn returns an error, that same error is returned from Foreach and iteration stops.

func ForeachInRange

func ForeachInRange(kv KeyValue, start, end string, fn func(key, value string) error) error

ForeachInRange runs fn for each key/value pair in kv in the range of start and end, which behave the same as kv.Find. If fn returns an error, that same error is returned from Foreach and iteration stops.

func RegisterKeyValue

func RegisterKeyValue(typ string, fn func(jsonconfig.Obj) (KeyValue, error))

type BatchMutation

type BatchMutation interface {
    Set(key, value string)
    Delete(key string)
}

func NewBatchMutation

func NewBatchMutation() BatchMutation

type Iterator

type Iterator interface {
    // Next moves the iterator to the next key/value pair.
    // It returns false when the iterator is exhausted.
    Next() bool

    // Key returns the key of the current key/value pair.
    // Only valid after a call to Next returns true.
    Key() string

    // KeyBytes returns the key as bytes. The returned bytes
    // should not be written and are invalid after the next call
    // to Next or Close.
    // TODO(bradfitz): rename this and change it to return a
    // mem.RO instead?
    KeyBytes() []byte

    // Value returns the value of the current key/value pair.
    // Only valid after a call to Next returns true.
    Value() string

    // ValueBytes returns the value as bytes. The returned bytes
    // should not be written and are invalid after the next call
    // to Next or Close.
    // TODO(bradfitz): rename this and change it to return a
    // mem.RO instead?
    ValueBytes() []byte

    // Close closes the iterator and returns any accumulated error. Exhausting
    // all the key/value pairs in a table is not considered to be an error.
    // It is valid to call Close multiple times. Other methods should not be
    // called after the iterator has been closed.
    Close() error
}

Iterator iterates over an index KeyValue's key/value pairs in key order.

An iterator must be closed after use, but it is not necessary to read an iterator until exhaustion.

An iterator is not necessarily goroutine-safe, but it is safe to use multiple iterators concurrently, with each in a dedicated goroutine.

type KeyValue

type KeyValue interface {
    // Get gets the value for the given key. It returns ErrNotFound if the DB
    // does not contain the key.
    Get(key string) (string, error)

    Set(key, value string) error

    // Delete deletes keys. Deleting a non-existent key does not return an error.
    Delete(key string) error

    BeginBatch() BatchMutation
    CommitBatch(b BatchMutation) error

    // Find returns an iterator positioned before the first key/value pair
    // whose key is 'greater than or equal to' the given key. There may be no
    // such pair, in which case the iterator will return false on Next.
    //
    // The optional end value specifies the exclusive upper
    // bound. If the empty string, the iterator returns keys
    // where "key >= start".
    // If non-empty, the iterator returns keys where
    // "key >= start && key < endHint".
    //
    // Any error encountered will be implicitly returned via the iterator. An
    // error-iterator will yield no key/value pairs and closing that iterator
    // will return that error.
    Find(start, end string) Iterator

    // Close is a polite way for the server to shut down the storage.
    // Implementations should never lose data after a Set, Delete,
    // or CommmitBatch, though.
    Close() error
}

KeyValue is a sorted, enumerable key-value interface supporting batch mutations.

func NewKeyValue

func NewKeyValue(cfg jsonconfig.Obj) (KeyValue, error)

NewKeyValue returns a KeyValue as defined by cfg. It returns an error of type NeedWipeError when the returned KeyValue should be fixed with Wipe.

func NewKeyValueMaybeWipe

func NewKeyValueMaybeWipe(cfg jsonconfig.Obj) (KeyValue, error)

NewKeyValueMaybeWipe calls NewKeyValue and wipes the KeyValue if, and only if, NewKeyValue has returned a NeedWipeError.

func NewMemoryKeyValue

func NewMemoryKeyValue() KeyValue

NewMemoryKeyValue returns a KeyValue implementation that's backed only by memory. It's mostly useful for tests and development.

type Mutation

type Mutation interface {
    Key() string
    Value() string
    IsDelete() bool
}

type NeedWipeError

type NeedWipeError struct {
    Msg string
}

NeedWipeError is returned by NewKeyValue when the returned KeyValue is not usable until Wipe has been called on it.

func (NeedWipeError) Error

func (e NeedWipeError) Error() string

type ReadTransaction

type ReadTransaction interface {
    Get(key string) (string, error)
    Find(start, end string) Iterator

    // End the transaction.
    Close() error
}

ReadTransaction is a read-only transaction on a KeyValue. It admits the same read operations as the KeyValue itself, but writes that occur after the transaction is created are not observed.

Users should close the transaction as soon as it as no longer needed, as failing to do so can tie up resources.

type TransactionalReader

type TransactionalReader interface {
    KeyValue

    // BeginReadTx begins a read-only transaction.
    BeginReadTx() ReadTransaction
}

TransactionalReader is an optional interface that may be implemented by storage implementations. It may be implemented when a storage backend supports multiple atomic reads.

type Wiper

type Wiper interface {
    KeyValue

    // Wipe removes all key/value pairs, and resets the storage to a blank state.
    // For a given storage, when NewKeyValue returns a NeedWipeError, Wipe
    // should be implemented so that it fixes the returned KeyValue.
    Wipe() error
}

Wiper is an optional interface that may be implemented by storage implementations.

Subdirectories

Name      Synopsis
..
buffer      Package buffer provides a sorted.KeyValue implementation that buffers one KeyValue implementation in front of an another.
kvfile      Package kvfile provides an implementation of sorted.KeyValue on top of a single mutable database file on disk using modernc.org/kv.
kvtest      Package kvtest tests sorted.KeyValue implementations.
leveldb      Package leveldb provides an implementation of sorted.KeyValue on top of a single mutable database file on disk using github.com/syndtr/goleveldb.
mongo      Package mongo provides an implementation of sorted.KeyValue using MongoDB.
mysql      Package mysql provides an implementation of sorted.KeyValue on top of MySQL.
postgres      Package postgres provides an implementation of sorted.KeyValue on top of PostgreSQL.
sqlite      Package sqlite provides an implementation of sorted.KeyValue using an SQLite database file.
sqlkv      Package sqlkv implements the sorted.KeyValue interface using an *sql.DB.
Website layout inspired by memcached.
Content by the authors.