db

package
v0.26.0-alpha.43 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2019 License: MPL-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// PeersCache is used for the db entries used for peers DB
	PeersCache storagePrefix = iota
	// DeduplicatorCache is used for the db entries used for messages
	// deduplication cache
	DeduplicatorCache
	// MailserversCache is a list of mail servers provided by users.
	MailserversCache
	// TopicHistoryBucket isolated bucket for storing history metadata.
	TopicHistoryBucket
	// HistoryRequestBucket isolated bucket for storing list of pending requests.
	HistoryRequestBucket
)

Variables

View Source
var (
	// ErrEmptyKey returned if key is not expected to be empty.
	ErrEmptyKey = errors.New("TopicHistoryKey is empty")
)

Functions

func Create

func Create(path, dbName string) (*leveldb.DB, error)

Create returns status pointer to leveldb.DB.

func Key

func Key(prefix storagePrefix, data ...[]byte) []byte

Key creates a DB key for a specified service with specified data

func NewMemoryDB

func NewMemoryDB() (*leveldb.DB, error)

NewMemoryDB returns leveldb with memory backend prefixed with a bucket.

func Open

func Open(path string, opts *opt.Options) (db *leveldb.DB, err error)

Open opens an existing leveldb database

Types

type CommitStorage

type CommitStorage interface {
	Storage
	Commit() error
}

CommitStorage allows to write all tx/batched values atomically.

type DB

type DB interface {
	Get([]byte) ([]byte, error)
	Put([]byte, []byte) error
	Delete([]byte) error
	Range([]byte, []byte) *util.Range
	NewIterator(*util.Range) NamespaceIterator
}

DB is a common interface for DB operations.

type HistoryRequest

type HistoryRequest struct {

	// Generated ID
	ID common.Hash
	// List of the topics
	TopicHistoryKeys []TopicHistoryKey
	// contains filtered or unexported fields
}

HistoryRequest is kept in the database while request is in the progress. Stores necessary information to identify topics with associated ranges included in the request.

func (*HistoryRequest) AddHistory

func (req *HistoryRequest) AddHistory(history TopicHistory)

AddHistory adds instance to internal list of instance and add instance key to the list which will be persisted on disk.

func (HistoryRequest) Delete

func (req HistoryRequest) Delete() error

Delete HistoryRequest from store and update every topic.

func (*HistoryRequest) Histories

func (req *HistoryRequest) Histories() []TopicHistory

Histories returns internal lsit of topic histories.

func (*HistoryRequest) Includes

func (req *HistoryRequest) Includes(history TopicHistory) bool

Includes checks if TopicHistory is included into the request.

func (*HistoryRequest) Load

func (req *HistoryRequest) Load() error

Load reads request and topic histories content from disk and unmarshalls them.

func (*HistoryRequest) RawUnmarshall

func (req *HistoryRequest) RawUnmarshall(val []byte) error

RawUnmarshall unmarshall given bytes into the structure. Used in range queries to unmarshall content of the iter.Value directly into request struct.

func (HistoryRequest) Replace

func (req HistoryRequest) Replace(id common.Hash) error

Replace saves request with new ID and all data attached to the old one.

func (HistoryRequest) Save

func (req HistoryRequest) Save() error

Save persists all attached histories and request itself on the disk.

func (HistoryRequest) Value

func (req HistoryRequest) Value() ([]byte, error)

Value returns content of HistoryRequest as bytes.

type HistoryStore

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

HistoryStore provides utility methods for quering history and requests store.

func NewHistoryStore

func NewHistoryStore(storage Storage) HistoryStore

NewHistoryStore returns HistoryStore instance.

func (HistoryStore) GetAllRequests

func (h HistoryStore) GetAllRequests() ([]HistoryRequest, error)

GetAllRequests loads all not-finished history requests from database.

func (HistoryStore) GetHistoriesByTopic

func (h HistoryStore) GetHistoriesByTopic(topic whisper.TopicType) ([]TopicHistory, error)

GetHistoriesByTopic returns all histories with a given topic. This is needed when we will have multiple range per single topic. TODO explain

func (HistoryStore) GetHistory

func (h HistoryStore) GetHistory(topic whisper.TopicType, duration time.Duration) (TopicHistory, error)

GetHistory creates history instance and loads history from database. Returns instance populated with topic and duration if history is not found in database.

func (HistoryStore) GetRequest

func (h HistoryStore) GetRequest(id common.Hash) (HistoryRequest, error)

GetRequest loads HistoryRequest from database.

func (HistoryStore) NewHistory

func (h HistoryStore) NewHistory(topic whisper.TopicType, duration time.Duration) TopicHistory

NewHistory creates TopicHistory object with required values.

func (HistoryStore) NewRequest

func (h HistoryStore) NewRequest() HistoryRequest

NewRequest returns instance of the HistoryRequest.

type LevelDBNamespace

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

LevelDBNamespace database where all operations will be prefixed with a certain bucket.

func NewDBNamespace

func NewDBNamespace(db Storage, prefix storagePrefix) LevelDBNamespace

NewDBNamespace returns instance that ensures isolated operations.

func NewMemoryDBNamespace

func NewMemoryDBNamespace(prefix storagePrefix) (pdb LevelDBNamespace, err error)

NewMemoryDBNamespace wraps in memory leveldb with provided bucket. Mostly used for tests. Including tests in other packages.

func (LevelDBNamespace) Delete

func (db LevelDBNamespace) Delete(key []byte) error

Delete removes key from database.

func (LevelDBNamespace) Get

func (db LevelDBNamespace) Get(key []byte) ([]byte, error)

func (LevelDBNamespace) NewIterator

func (db LevelDBNamespace) NewIterator(slice *util.Range) NamespaceIterator

NewIterator returns iterator for a given slice.

func (LevelDBNamespace) Put

func (db LevelDBNamespace) Put(key, value []byte) error

func (LevelDBNamespace) Range

func (db LevelDBNamespace) Range(prefix, limit []byte) *util.Range

Range returns leveldb util.Range prefixed with a single byte. If prefix is nil range will iterate over all records in a given bucket.

type LevelDBStorage

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

LevelDBStorage wrapper around leveldb.DB.

func NewLevelDBStorage

func NewLevelDBStorage(db *leveldb.DB) LevelDBStorage

NewLevelDBStorage creates new LevelDBStorage instance.

func NewMemoryLevelDBStorage

func NewMemoryLevelDBStorage() (LevelDBStorage, error)

NewMemoryLevelDBStorage returns LevelDBStorage instance with in memory leveldb backend.

func (LevelDBStorage) Delete

func (db LevelDBStorage) Delete(key []byte) error

Delete removes given key from database..

func (LevelDBStorage) Get

func (db LevelDBStorage) Get(key []byte) ([]byte, error)

Get returns value for a given key.

func (LevelDBStorage) NewIterator

func (db LevelDBStorage) NewIterator(slice *util.Range) iterator.Iterator

NewIterator returns new leveldb iterator.Iterator instance for a given range.

func (LevelDBStorage) NewTx

func (db LevelDBStorage) NewTx() CommitStorage

NewTx is a wrapper around leveldb.Batch that allows to write atomically.

func (LevelDBStorage) Put

func (db LevelDBStorage) Put(key, buf []byte) error

Put upserts given key/value pair.

type LevelDBTx

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

LevelDBTx doesn't provide any read isolation. It allows committing all writes atomically (put/delete).

func (LevelDBTx) Commit

func (tx LevelDBTx) Commit() error

Commit writes batch atomically.

func (LevelDBTx) Delete

func (tx LevelDBTx) Delete(key []byte) error

Delete adds delete operation to associated batch.

func (LevelDBTx) Get

func (tx LevelDBTx) Get(key []byte) ([]byte, error)

Get reads from currently committed state.

func (LevelDBTx) NewIterator

func (tx LevelDBTx) NewIterator(slice *util.Range) iterator.Iterator

NewIterator returns iterator.Iterator that will read from currently committed state.

func (LevelDBTx) Put

func (tx LevelDBTx) Put(key, buf []byte) error

Put adds key/value to associated batch.

type NamespaceIterator

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

NamespaceIterator wraps leveldb iterator, works mostly the same way. The only difference is that first byte of the key is dropped.

func (NamespaceIterator) Error

func (iter NamespaceIterator) Error() error

Error returns accumulated error.

func (NamespaceIterator) Key

func (iter NamespaceIterator) Key() []byte

Key returns key of the current item.

func (NamespaceIterator) Next

func (iter NamespaceIterator) Next() bool

Next moves cursor forward.

func (NamespaceIterator) Prev

func (iter NamespaceIterator) Prev() bool

Prev moves cursor backward.

func (NamespaceIterator) Value

func (iter NamespaceIterator) Value() []byte

Value returns actual value of the current item.

type Storage

type Storage interface {
	Put([]byte, []byte) error
	Delete([]byte) error
	Get([]byte) ([]byte, error)
	NewIterator(*util.Range) iterator.Iterator
}

Storage is an interface for common db operations.

type TopicHistory

type TopicHistory struct {

	// whisper topic
	Topic whisper.TopicType

	Duration time.Duration
	// Timestamp that was used for the first request with this topic.
	// Used to identify overlapping ranges.
	First time.Time
	// Timestamp of the last synced envelope.
	Current time.Time
	End     time.Time

	RequestID common.Hash
	// contains filtered or unexported fields
}

TopicHistory stores necessary information.

func LoadTopicHistoryFromKey

func LoadTopicHistoryFromKey(db DB, key TopicHistoryKey) (th TopicHistory, err error)

LoadTopicHistoryFromKey unmarshalls key into topic and duration and loads value of topic history from given database.

func (TopicHistory) Delete

func (t TopicHistory) Delete() error

Delete removes topic history from database.

func (TopicHistory) Key

func (t TopicHistory) Key() TopicHistoryKey

Key returns unique identifier for this TopicHistory.

func (*TopicHistory) Load

func (t *TopicHistory) Load() error

Load TopicHistory from db using key and unmarshalls it.

func (TopicHistory) Pending

func (t TopicHistory) Pending() bool

Pending returns true if this topic was requested from a mail server.

func (TopicHistory) SameRange

func (t TopicHistory) SameRange(other TopicHistory) bool

SameRange returns true if topic has same range, which means: true if Current is zero and Duration is the same and true if Current is the same

func (TopicHistory) Save

func (t TopicHistory) Save() error

Save persists TopicHistory on disk.

func (TopicHistory) Value

func (t TopicHistory) Value() ([]byte, error)

Value marshalls TopicHistory into bytes.

type TopicHistoryKey

type TopicHistoryKey [12]byte

TopicHistoryKey defines bytes that are used as unique key for TopicHistory. first 4 bytes are whisper.TopicType bytes next 8 bytes are time.Duration encoded in big endian notation.

type TransactionalStorage

type TransactionalStorage interface {
	Storage
	NewTx() CommitStorage
}

TransactionalStorage adds transaction features on top of regular storage.

Jump to

Keyboard shortcuts

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