kv

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2020 License: Apache-2.0, BSD-2-Clause Imports: 11 Imported by: 4

Documentation

Index

Constants

View Source
const EmptyPrefix = Key("")

Variables

This section is empty.

Functions

func DecodeInt64

func DecodeInt64(b []byte) (int64, error)

func NewMutationDel

func NewMutationDel(k Key) *mutationDel

func NewMutationSet

func NewMutationSet(k Key, v []byte) *mutationSet

Types

type Array

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

func (*Array) Erase

func (l *Array) Erase()

TODO implement with DelPrefix

func (*Array) Extend

func (l *Array) Extend(other *Array)

func (*Array) GetAt

func (l *Array) GetAt(idx uint16) ([]byte, error)

func (*Array) Len

func (l *Array) Len() uint16

Len == 0/empty/non-existent are equivalent

func (*Array) Push

func (l *Array) Push(value []byte)

adds to the end of the list

func (*Array) SetAt

func (l *Array) SetAt(idx uint16, value []byte) bool

type BufferedKVStore

type BufferedKVStore interface {
	KVStore

	// the uncommitted mutations
	Mutations() MutationSequence
	ClearMutations()
	Clone() BufferedKVStore

	Codec() Codec

	// only for testing!
	DangerouslyDumpToMap() Map
	// only for testing!
	DangerouslyDumpToString() string
}

BufferedKVStore represents a KVStore backed by a database. Writes are cached in-memory as a MutationSequence; reads are delegated to the backing database when not cached.

func NewBufferedKVStore

func NewBufferedKVStore(db kvstore.KVStore) BufferedKVStore

type Codec

type Codec interface {
	RCodec
	WCodec
	GetArray(Key) (*Array, error)
	GetDictionary(Key) (*Dictionary, error)
	GetTimestampedLog(Key) (*TimestampedLog, error)
}

Codec is an interface that offers easy conversions between []byte (scalar) and other types (including complex) when manipulating a KVStore

func NewCodec

func NewCodec(kv KVStore) Codec

type DBError

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

Any failed access to the DB will return an instance of DBError

func (DBError) Error

func (d DBError) Error() string

type Dictionary

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

func (*Dictionary) DelAt

func (d *Dictionary) DelAt(key []byte) error

func (*Dictionary) Erase

func (d *Dictionary) Erase()

func (*Dictionary) GetAt

func (d *Dictionary) GetAt(key []byte) ([]byte, error)

func (*Dictionary) HasAt

func (d *Dictionary) HasAt(key []byte) (bool, error)

func (*Dictionary) Iterate

func (d *Dictionary) Iterate(f func(elemKey []byte, value []byte) bool) error

func (*Dictionary) Len

func (d *Dictionary) Len() uint32

func (*Dictionary) SetAt

func (d *Dictionary) SetAt(key []byte, value []byte) error

type KVStore

type KVStore interface {
	Set(key Key, value []byte)
	Del(key Key)
	// Get returns the value, or nil if not found
	Get(key Key) ([]byte, error)
	Has(key Key) (bool, error)
	Iterate(prefix Key, f func(key Key, value []byte) bool) error
	IterateKeys(prefix Key, f func(key Key) bool) error
}

KVStore represents a key-value store where both keys and values are arbitrary byte slices.

type Key

type Key string

Since map cannot have []byte as key, to avoid unnecessary conversions between string and []byte, we use string as key data type, but it does not necessarily have to be a valid UTF-8 string.

func ArrayElemKey

func ArrayElemKey(name string, idx uint16) Key

func ArrayRangeKeys

func ArrayRangeKeys(name string, length uint16, from uint16, to uint16) []Key

ArrayRangeKeys returns the KVStore keys for the items between [from, to) (`to` being not inclusive), assuming it has `length` elements.

func ArraySizeKey

func ArraySizeKey(name string) Key

func (Key) HasPrefix

func (k Key) HasPrefix(prefix Key) bool

type Map

type Map interface {
	KVStore

	IsEmpty() bool

	ToGoMap() map[Key][]byte

	ForEach(func(key Key, value []byte) bool)
	ForEachDeterministic(func(key Key, value []byte) bool)
	Clone() Map

	Read(io.Reader) error
	Write(io.Writer) error

	String() string

	Mutations() MutationSequence

	Codec() Codec
	MustCodec() MustCodec
}

Map is a KVStore backed by an in-memory map

func FromGoMap

func FromGoMap(m map[Key][]byte) Map

func NewMap

func NewMap() Map

create/clone

type MustArray

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

func (*MustArray) Erase

func (a *MustArray) Erase()

func (*MustArray) Extend

func (a *MustArray) Extend(other *MustArray)

func (*MustArray) GetAt

func (a *MustArray) GetAt(idx uint16) []byte

func (*MustArray) Len

func (a *MustArray) Len() uint16

func (*MustArray) Push

func (a *MustArray) Push(value []byte)

func (*MustArray) SetAt

func (a *MustArray) SetAt(idx uint16, value []byte) bool

type MustCodec

type MustCodec interface {
	MustRCodec
	WCodec
	GetArray(Key) *MustArray
	GetDictionary(Key) *MustDictionary
	GetTimestampedLog(Key) *MustTimestampedLog
}

MustCodec is like a Codec that automatically panics on error

func NewMustCodec

func NewMustCodec(kv KVStore) MustCodec

type MustDictionary

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

func (*MustDictionary) DelAt

func (d *MustDictionary) DelAt(key []byte)

func (*MustDictionary) GetAt

func (d *MustDictionary) GetAt(key []byte) []byte

func (*MustDictionary) SetAt

func (d *MustDictionary) SetAt(key []byte, value []byte)

type MustRCodec

type MustRCodec interface {
	Has(key Key) bool
	Get(key Key) []byte
	GetString(key Key) (string, bool)
	GetInt64(key Key) (int64, bool)
	GetAddress(key Key) (*address.Address, bool)
	GetHashValue(key Key) (*hashing.HashValue, bool)
}

MustrCodec is like a RCodec that automatically panics on error

type MustTimestampedLog

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

func (*MustTimestampedLog) Append

func (l *MustTimestampedLog) Append(ts int64, data []byte)

func (*MustTimestampedLog) Earliest

func (l *MustTimestampedLog) Earliest() int64

func (*MustTimestampedLog) Latest

func (l *MustTimestampedLog) Latest() int64

func (*MustTimestampedLog) Len

func (l *MustTimestampedLog) Len() uint32

func (*MustTimestampedLog) TakeTimeSlice

func (l *MustTimestampedLog) TakeTimeSlice(fromTs, toTs int64) *TimeSlice

type Mutation

type Mutation interface {
	Read(io.Reader) error
	Write(io.Writer) error

	String() string

	ApplyTo(kv KVStore)

	// Key returns the key that is mutated
	Key() Key
	// Value returns the value after the mutation (nil if deleted)
	Value() []byte
	// contains filtered or unexported methods
}

Mutation represents a single "set" or "del" operation over a KVStore

type MutationSequence

type MutationSequence interface {
	Read(io.Reader) error
	Write(io.Writer) error

	String() string

	Clone() MutationSequence

	Len() int

	// Iterate over all mutations in order, even ones affecting the same key repeatedly
	Iterate(func(mut Mutation) bool)
	// Iterate over the latest mutation recorded for each key
	IterateLatest(func(key Key, mut Mutation) bool)
	// Iterate over the latest value recorded for each non-deleted key
	IterateValues(prefix Key, f func(key Key, value []byte) bool) (map[Key]bool, bool)

	Latest(key Key) Mutation

	Add(mut Mutation)

	ApplyTo(kv KVStore)
}

func NewMutationSequence

func NewMutationSequence() MutationSequence

type RCodec

type RCodec interface {
	Has(key Key) (bool, error)
	Get(key Key) ([]byte, error)
	GetString(key Key) (string, bool, error)
	GetInt64(key Key) (int64, bool, error)
	GetAddress(key Key) (*address.Address, bool, error)
	GetHashValue(key Key) (*hashing.HashValue, bool, error)
}

RCodec is an interface that offers easy conversions between []byte and other types when manipulating a read-only KVStore

type TimeSlice

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

func (*TimeSlice) Earliest

func (sl *TimeSlice) Earliest() int64

Earliest return timestamp of the first index or 0 if empty

func (*TimeSlice) FromToIndices

func (sl *TimeSlice) FromToIndices() (uint32, uint32)

func (*TimeSlice) IsEmpty

func (sl *TimeSlice) IsEmpty() bool

IsEmpty returns true if slice does not contains points

func (*TimeSlice) Latest

func (sl *TimeSlice) Latest() int64

Earliest returns timestamp of the last index or 0 if empty

func (*TimeSlice) NumPoints

func (sl *TimeSlice) NumPoints() uint32

NumPoints return number of indices (records) in the slice

type TimestampedLog

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

TimestampedLog implement limitless append-only array of records where each record is indexed sequentially and consistently timestamped sequence of timestamps is considered consistent if for any indices i<j, Ti<=Tj, i.e. non-decreasing

func (*TimestampedLog) Append

func (l *TimestampedLog) Append(ts int64, data []byte) error

Append appends data with timestamp to the end of the log. Returns error if timestamp is inconsistent, i.e. less than the latest timestamp

func (*TimestampedLog) Earliest

func (l *TimestampedLog) Earliest() int64

Earliest returns timestamp of the first record in the log, if any, or otherwise it is 0

func (*TimestampedLog) Erase

func (l *TimestampedLog) Erase()

func (*TimestampedLog) Latest

func (l *TimestampedLog) Latest() int64

Latest returns latest timestamp in the log

func (*TimestampedLog) Len

func (l *TimestampedLog) Len() uint32

Len == 0/empty/non-existent are equivalent

func (*TimestampedLog) LoadRecordsRaw

func (l *TimestampedLog) LoadRecordsRaw(fromIdx, toIdx uint32, descending bool) ([][]byte, error)

LoadRecords returns all records in the slice

func (*TimestampedLog) TakeTimeSlice

func (l *TimestampedLog) TakeTimeSlice(fromTs, toTs int64) (*TimeSlice, error)

TakeTimeSlice returns a slice structure, which contains existing indices firstIdx and lastIdx. Timestamps of all records between indices (inclusive) satisfy the condition >= T(firstIdx) and <=T(lastIdx) Any other pair of indices i1<fistId and/or i2>lastIdx does not satisfy the condition. In other words, returned slice contains all possible indices with timestamps between the two given Returned slice may be empty

type TimestampedLogRecord

type TimestampedLogRecord struct {
	Timestamp int64
	Data      []byte
}

func ParseRawLogRecord

func ParseRawLogRecord(raw []byte) (*TimestampedLogRecord, error)

type WCodec

type WCodec interface {
	Del(key Key)
	Set(key Key, value []byte)
	SetString(key Key, value string)
	SetInt64(key Key, value int64)
	SetAddress(key Key, value *address.Address)
	SetHashValue(key Key, value *hashing.HashValue)
}

WCodec is an interface that offers easy conversions between []byte and other types when manipulating a write-only KVStore

Jump to

Keyboard shortcuts

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