shed

package
v0.0.0-...-cc9b3d1 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2021 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package shed provides a simple abstraction components to compose more complex operations on storage data organized in fields and indexes.

Only type which holds logical information about swarm storage chunks data and metadata is Item. This part is not generalized mostly for performance reasons.

Example (Store)

Example_store constructs a simple storage implementation using shed package.

Output:

true

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

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

DB provides abstractions over LevelDB in order to implement complex structures using fields and ordered indexes. It provides a schema functionality to store fields and indexes information about naming and types.

func NewDB

func NewDB(path string, metricsPrefix string) (db *DB, err error)

NewDB constructs a new DB and validates the schema if it exists in database on the given path. metricsPrefix is used for metrics collection for the given DB.

func (*DB) Close

func (db *DB) Close() (err error)

Close closes LevelDB database.

func (*DB) Delete

func (db *DB) Delete(key []byte) (err error)

Delete wraps LevelDB Delete method to increment metrics counter.

func (*DB) Get

func (db *DB) Get(key []byte) (value []byte, err error)

Get wraps LevelDB Get method to increment metrics counter.

func (*DB) Has

func (db *DB) Has(key []byte) (yes bool, err error)

Has wraps LevelDB Has method to increment metrics counter.

func (*DB) NewIndex

func (db *DB) NewIndex(name string, funcs IndexFuncs) (f Index, err error)

NewIndex returns a new Index instance with defined name and encoding functions. The name must be unique and will be validated on database schema for a key prefix byte.

func (*DB) NewIterator

func (db *DB) NewIterator() iterator.Iterator

NewIterator wraps LevelDB NewIterator method to increment metrics counter.

func (*DB) NewStringField

func (db *DB) NewStringField(name string) (f StringField, err error)

NewStringField retruns a new Instance of StringField. It validates its name and type against the database schema.

func (*DB) NewStructField

func (db *DB) NewStructField(name string) (f StructField, err error)

NewStructField returns a new StructField. It validates its name and type against the database schema.

func (*DB) NewUint64Field

func (db *DB) NewUint64Field(name string) (f Uint64Field, err error)

NewUint64Field returns a new Uint64Field. It validates its name and type against the database schema.

func (*DB) NewUint64Vector

func (db *DB) NewUint64Vector(name string) (f Uint64Vector, err error)

NewUint64Vector returns a new Uint64Vector. It validates its name and type against the database schema.

func (*DB) Put

func (db *DB) Put(key []byte, value []byte) (err error)

Put wraps LevelDB Put method to increment metrics counter.

func (*DB) WriteBatch

func (db *DB) WriteBatch(batch *leveldb.Batch) (err error)

WriteBatch wraps LevelDB Write method to increment metrics counter.

type Index

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

Index represents a set of LevelDB key value pairs that have common prefix. It holds functions for encoding and decoding keys and values to provide transparent actions on saved data which inclide: - getting a particular Item - saving a particular Item - iterating over a sorted LevelDB keys It implements IndexIteratorInterface interface.

func (Index) Count

func (f Index) Count() (count int, err error)

Count returns the number of items in index.

func (Index) CountFrom

func (f Index) CountFrom(start Item) (count int, err error)

CountFrom returns the number of items in index keys starting from the key encoded from the provided Item.

func (Index) Delete

func (f Index) Delete(keyFields Item) (err error)

Delete accepts Item to remove a key/value pair from the database based on its fields.

func (Index) DeleteInBatch

func (f Index) DeleteInBatch(batch *leveldb.Batch, keyFields Item) (err error)

DeleteInBatch is the same as Delete just the operation is performed on the batch instead on the database.

func (Index) First

func (f Index) First(prefix []byte) (i Item, err error)

First returns the first item in the Index which encoded key starts with a prefix. If the prefix is nil, the first element of the whole index is returned. If Index has no elements, a leveldb.ErrNotFound error is returned.

func (Index) Get

func (f Index) Get(keyFields Item) (out Item, err error)

Get accepts key fields represented as Item to retrieve a value from the index and return maximum available information from the index represented as another Item.

func (Index) Has

func (f Index) Has(keyFields Item) (bool, error)

Has accepts key fields represented as Item to check if there this Item's encoded key is stored in the index.

func (Index) Iterate

func (f Index) Iterate(fn IndexIterFunc, options *IterateOptions) (err error)

Iterate function iterates over keys of the Index. If IterateOptions is nil, the iterations is over all keys.

func (Index) Last

func (f Index) Last(prefix []byte) (i Item, err error)

Last returns the last item in the Index which encoded key starts with a prefix. If the prefix is nil, the last element of the whole index is returned. If Index has no elements, a leveldb.ErrNotFound error is returned.

func (Index) Put

func (f Index) Put(i Item) (err error)

Put accepts Item to encode information from it and save it to the database.

func (Index) PutInBatch

func (f Index) PutInBatch(batch *leveldb.Batch, i Item) (err error)

PutInBatch is the same as Put method, but it just saves the key/value pair to the batch instead directly to the database.

type IndexFuncs

type IndexFuncs struct {
	EncodeKey   func(fields Item) (key []byte, err error)
	DecodeKey   func(key []byte) (e Item, err error)
	EncodeValue func(fields Item) (value []byte, err error)
	DecodeValue func(keyFields Item, value []byte) (e Item, err error)
}

IndexFuncs structure defines functions for encoding and decoding LevelDB keys and values for a specific index.

type IndexIterFunc

type IndexIterFunc func(item Item) (stop bool, err error)

IndexIterFunc is a callback on every Item that is decoded by iterating on an Index keys. By returning a true for stop variable, iteration will stop, and by returning the error, that error will be propagated to the called iterator method on Index.

type Item

type Item struct {
	Address         []byte
	Data            []byte
	AccessTimestamp int64
	StoreTimestamp  int64
	// UseMockStore is a pointer to identify
	// an unset state of the field in Join function.
	UseMockStore *bool
}

Item holds fields relevant to Swarm Chunk data and metadata. All information required for swarm storage and operations on that storage must be defined here. This structure is logically connected to swarm storage, the only part of this package that is not generalized, mostly for performance reasons.

Item is a type that is used for retrieving, storing and encoding chunk data and metadata. It is passed as an argument to Index encoding functions, get function and put function. But it is also returned with additional data from get function call and as the argument in iterator function definition.

func (Item) Merge

func (i Item) Merge(i2 Item) (new Item)

Merge is a helper method to construct a new Item by filling up fields with default values of a particular Item with values from another one.

type IterateOptions

type IterateOptions struct {
	// StartFrom is the Item to start the iteration from.
	StartFrom *Item
	// If SkipStartFromItem is true, StartFrom item will not
	// be iterated on.
	SkipStartFromItem bool
	// Iterate over items which keys have a common prefix.
	Prefix []byte
}

IterateOptions defines optional parameters for Iterate function.

type StringField

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

StringField is the most simple field implementation that stores an arbitrary string under a specific LevelDB key.

func (StringField) Get

func (f StringField) Get() (val string, err error)

Get returns a string value from database. If the value is not found, an empty string is returned an no error.

func (StringField) Put

func (f StringField) Put(val string) (err error)

Put stores a string in the database.

func (StringField) PutInBatch

func (f StringField) PutInBatch(batch *leveldb.Batch, val string)

PutInBatch stores a string in a batch that can be saved later in database.

type StructField

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

StructField is a helper to store complex structure by encoding it in RLP format.

func (StructField) Get

func (f StructField) Get(val interface{}) (err error)

Get unmarshals data from the database to a provided val. If the data is not found leveldb.ErrNotFound is returned.

func (StructField) Put

func (f StructField) Put(val interface{}) (err error)

Put marshals provided val and saves it to the database.

func (StructField) PutInBatch

func (f StructField) PutInBatch(batch *leveldb.Batch, val interface{}) (err error)

PutInBatch marshals provided val and puts it into the batch.

type Uint64Field

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

Uint64Field provides a way to have a simple counter in the database. It transparently encodes uint64 type value to bytes.

func (Uint64Field) Dec

func (f Uint64Field) Dec() (val uint64, err error)

Dec decrements a uint64 value in the database. This operation is not goroutine save. The field is protected from overflow to a negative value.

func (Uint64Field) DecInBatch

func (f Uint64Field) DecInBatch(batch *leveldb.Batch) (val uint64, err error)

DecInBatch decrements a uint64 value in the batch by retreiving a value from the database, not the same batch. This operation is not goroutine save. The field is protected from overflow to a negative value.

func (Uint64Field) Get

func (f Uint64Field) Get() (val uint64, err error)

Get retrieves a uint64 value from the database. If the value is not found in the database a 0 value is returned and no error.

func (Uint64Field) Inc

func (f Uint64Field) Inc() (val uint64, err error)

Inc increments a uint64 value in the database. This operation is not goroutine save.

func (Uint64Field) IncInBatch

func (f Uint64Field) IncInBatch(batch *leveldb.Batch) (val uint64, err error)

IncInBatch increments a uint64 value in the batch by retreiving a value from the database, not the same batch. This operation is not goroutine save.

func (Uint64Field) Put

func (f Uint64Field) Put(val uint64) (err error)

Put encodes uin64 value and stores it in the database.

func (Uint64Field) PutInBatch

func (f Uint64Field) PutInBatch(batch *leveldb.Batch, val uint64)

PutInBatch stores a uint64 value in a batch that can be saved later in the database.

type Uint64Vector

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

Uint64Vector provides a way to have multiple counters in the database. It transparently encodes uint64 type value to bytes.

func (Uint64Vector) Dec

func (f Uint64Vector) Dec(i uint64) (val uint64, err error)

Dec decrements a uint64 value at index i in the database. This operation is not goroutine safe. The field is protected from overflow to a negative value.

func (Uint64Vector) DecInBatch

func (f Uint64Vector) DecInBatch(batch *leveldb.Batch, i uint64) (val uint64, err error)

DecInBatch decrements a uint64 value at index i in the batch by retreiving a value from the database, not the same batch. This operation is not goroutine safe. The field is protected from overflow to a negative value.

func (Uint64Vector) Get

func (f Uint64Vector) Get(i uint64) (val uint64, err error)

Get retrieves a uint64 value at index i from the database. If the value is not found in the database a 0 value is returned and no error.

func (Uint64Vector) Inc

func (f Uint64Vector) Inc(i uint64) (val uint64, err error)

Inc increments a uint64 value in the database. This operation is not goroutine safe.

func (Uint64Vector) IncInBatch

func (f Uint64Vector) IncInBatch(batch *leveldb.Batch, i uint64) (val uint64, err error)

IncInBatch increments a uint64 value at index i in the batch by retreiving a value from the database, not the same batch. This operation is not goroutine safe.

func (Uint64Vector) Put

func (f Uint64Vector) Put(i, val uint64) (err error)

Put encodes uin64 value and stores it in the database.

func (Uint64Vector) PutInBatch

func (f Uint64Vector) PutInBatch(batch *leveldb.Batch, i, val uint64)

PutInBatch stores a uint64 value at index i in a batch that can be saved later in the database.

Jump to

Keyboard shortcuts

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