storage

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: May 12, 2019 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package storage provides the basic data structures, indexing, persistency and low leve API for oracles and vectors.

Index

Constants

View Source
const (
	// DatFileExt holds the default file extension for data files.
	DatFileExt = ".dat"
)

Variables

View Source
var (

	// ErrInvalidID is returned when the system detects a collision of
	// identifiers, usually due to multiple Sum instances running on the
	// same data path.
	ErrInvalidID = errors.New("identifier is not unique")
	// ErrRecordNotFound is the 404 of Sum, it is returned when the storage
	// manager can't find an object mapped to the queried identifier.
	ErrRecordNotFound = errors.New("record not found")
)

Functions

func Flush

func Flush(m proto.Message, fileName string) error

Flush serializes and saves to file a generic protobuf message. It returns an error if unsuccessful.

func ListPath

func ListPath(dataPath string) (string, map[string]string, error)

ListPath enumerates .dat files in a given folder and returns the same folder as an absolute path and a map of files.

func Load

func Load(fileName string, m proto.Message) error

Load reads and deserializes a file into a generic protobuf message.

Types

type Driver

type Driver interface {
	// Make must allocate and return a new protobuf object
	// when the index needs to.
	Make() proto.Message
	// GetID must access the protobuf message and return a
	// unique integer identifier that the index will use to
	// map that type of messages.
	GetID(m proto.Message) uint64
	// SetID must access the protobuf message and set its
	// unique integer identifier. This is generally called
	// by instances of storage.Index when a new object is
	// created and a unique id is associated to it for the
	// first time.
	SetID(m proto.Message, id uint64)
	// Copy must copy the contents of the src message into
	// the dst message. An error can be returned to signal
	// the index that something went wrong during the copy.
	Copy(dst proto.Message, src proto.Message) error
}

Driver is the generic interface for a module handling internal details of a specific protobuf object. It is used by the storage.Index in order to access object identifiers and internal fields.

type Index

type Index struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Index is a generic data structure used to map any types of protobuf encoded messages to unique integer identifiers and persist them on disk transparently.

func WithDriver

func WithDriver(dataPath string, driver Driver) *Index

WithDriver creates a new Index object with the specified storage.Driver used to handle the specifics of the protobuf messages being handled but this instance of the index.

func (*Index) Create

func (i *Index) Create(record proto.Message) error

Create stores the profobuf message in the index, setting its identifier to a new, unique value. Once created the object will be used in memory and persisted on disk.

func (*Index) Delete

func (i *Index) Delete(id uint64) proto.Message

Delete removes a stored object from the index given its identifier, it will return the removed object itself if found, or nil. This operation will also remove the object data file from disk.

func (*Index) Find

func (i *Index) Find(id uint64) proto.Message

Find returns the instance of a stored object given its identifier, or nil if the object can not be found.

func (*Index) ForEach

func (i *Index) ForEach(cb func(record proto.Message) error) error

ForEach executes a callback passing as argument every element of the index, it interrupts the loop if the callback returns an error, the same error will be returned.

func (*Index) Load

func (i *Index) Load() error

Load enumerates files in the data folder while deserializing them and mapping them into the index by their identifiers.

func (*Index) NextID

func (i *Index) NextID(next uint64)

NextID sets the value for the integer identifier to use every future record. NOTE: This method is just for internal use and the only reason why it's exposed is because of unit tests, do not use.

func (*Index) Objects

func (i *Index) Objects() []proto.Message

Objects return a list of proto.Message objects stored in this index.

func (*Index) Size

func (i *Index) Size() int

Size returns the number of elements stored in this index.

func (*Index) Update

func (i *Index) Update(record proto.Message) error

Update changes the contents of a stored object given a protobuf message with its identifier and the new values to use. This operation will flush the record on disk.

type OracleDriver

type OracleDriver struct {
}

OracleDriver is the specialized implementation of a storage.Driver interface, used to access the internal fields of pb.Oracle objects in the index.

func (OracleDriver) Copy

func (d OracleDriver) Copy(mdst proto.Message, msrc proto.Message) error

Copy copies the Name and Code fields from the source object to the destination one.

func (OracleDriver) GetID

func (d OracleDriver) GetID(m proto.Message) uint64

GetID returns the unique identifier of the pb.Oracle object.

func (OracleDriver) Make

func (d OracleDriver) Make() proto.Message

Make returns a new pb.Oracle object.

func (OracleDriver) SetID

func (d OracleDriver) SetID(m proto.Message, id uint64)

SetID sets the unique identifier of the pb.Oracle object.

type Oracles

type Oracles struct {
	*Index
}

Oracles is specialized version of a storage.Index used to map, store and persist pb.Oracle objects.

func LoadOracles

func LoadOracles(dataPath string) (*Oracles, error)

LoadOracles loads raw protobuf oracles from the data files found in a given path.

func (*Oracles) Delete

func (o *Oracles) Delete(id uint64) *pb.Oracle

Delete removes an oracle from the index given its identifier, it returns the deleted raw *pb.Oracle object, or nil if not found.

func (*Oracles) Find

func (o *Oracles) Find(id uint64) *pb.Oracle

Find returns a *pb.Oracle object given its identifier, or nil if not found.

type RecordDriver

type RecordDriver struct {
}

RecordDriver is the specialized implementation of a storage.Driver interface, used to access the internal fields of pb.Record objects in the index.

func (RecordDriver) Copy

func (d RecordDriver) Copy(mdst proto.Message, msrc proto.Message) error

Copy copies the Meta and Data fields, if filled, from the source object to the destination one.

func (RecordDriver) GetID

func (d RecordDriver) GetID(m proto.Message) uint64

GetID returns the unique identifier of the pb.Record object.

func (RecordDriver) Make

func (d RecordDriver) Make() proto.Message

Make returns a new pb.Record object.

func (RecordDriver) SetID

func (d RecordDriver) SetID(m proto.Message, id uint64)

SetID sets the unique identifier of the pb.Record object.

type Records

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

Records is specialized version of a storage.Index used to map, store and persist pb.Record objects.

func LoadRecords

func LoadRecords(dataPath string) (*Records, error)

LoadRecords loads and indexes raw protobuf records from the data files found in a given path.

func (*Records) Create added in v1.2.0

func (r *Records) Create(record *pb.Record) error

func (*Records) Delete

func (r *Records) Delete(id uint64) *pb.Record

Delete removes a stored pb.Record from the index given its identifier, it will return the removed object itself if found, or nil.

func (*Records) Find

func (r *Records) Find(id uint64) *pb.Record

Find returns the instance of a stored pb.Record given its identifier or nil if the object can not be found.

func (*Records) FindBy

func (r *Records) FindBy(meta string, val string) []*pb.Record

FindBy returns the list of pb.Record objects indexed by a specific meta value.

func (*Records) Update added in v1.2.0

func (r *Records) Update(record *pb.Record) error

Jump to

Keyboard shortcuts

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