db

package
v0.108.2 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2022 License: MPL-2.0 Imports: 13 Imported by: 23

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 added in v0.35.0

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 added in v0.35.0

type CommitStorage interface {
	Storage
	Commit() error
}

CommitStorage allows to write all tx/batched values atomically.

type DB added in v0.35.0

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 added in v0.35.0

type HistoryRequest struct {

	// Generated ID
	ID types.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 added in v0.35.0

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 added in v0.35.0

func (req HistoryRequest) Delete() error

Delete HistoryRequest from store and update every topic.

func (*HistoryRequest) Histories added in v0.35.0

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

Histories returns internal lsit of topic histories.

func (*HistoryRequest) Includes added in v0.35.0

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

Includes checks if TopicHistory is included into the request.

func (*HistoryRequest) Load added in v0.35.0

func (req *HistoryRequest) Load() error

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

func (*HistoryRequest) RawUnmarshall added in v0.35.0

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 added in v0.35.0

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

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

func (HistoryRequest) Save added in v0.35.0

func (req HistoryRequest) Save() error

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

func (HistoryRequest) Value added in v0.35.0

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

Value returns content of HistoryRequest as bytes.

type HistoryStore added in v0.35.0

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

HistoryStore provides utility methods for quering history and requests store.

func NewHistoryStore added in v0.35.0

func NewHistoryStore(storage Storage) HistoryStore

NewHistoryStore returns HistoryStore instance.

func (HistoryStore) GetAllRequests added in v0.35.0

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

GetAllRequests loads all not-finished history requests from database.

func (HistoryStore) GetHistoriesByTopic added in v0.35.0

func (h HistoryStore) GetHistoriesByTopic(topic types.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 added in v0.35.0

func (h HistoryStore) GetHistory(topic types.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 added in v0.35.0

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

GetRequest loads HistoryRequest from database.

func (HistoryStore) NewHistory added in v0.35.0

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

NewHistory creates TopicHistory object with required values.

func (HistoryStore) NewRequest added in v0.35.0

func (h HistoryStore) NewRequest() HistoryRequest

NewRequest returns instance of the HistoryRequest.

type LevelDBNamespace added in v0.35.0

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

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

func NewDBNamespace added in v0.35.0

func NewDBNamespace(db Storage, prefix storagePrefix) LevelDBNamespace

NewDBNamespace returns instance that ensures isolated operations.

func NewMemoryDBNamespace added in v0.35.0

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 added in v0.35.0

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

Delete removes key from database.

func (LevelDBNamespace) Get added in v0.35.0

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

func (LevelDBNamespace) NewIterator added in v0.35.0

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

NewIterator returns iterator for a given slice.

func (LevelDBNamespace) Put added in v0.35.0

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

func (LevelDBNamespace) Range added in v0.35.0

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 added in v0.35.0

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

LevelDBStorage wrapper around leveldb.DB.

func NewLevelDBStorage added in v0.35.0

func NewLevelDBStorage(db *leveldb.DB) LevelDBStorage

NewLevelDBStorage creates new LevelDBStorage instance.

func NewMemoryLevelDBStorage added in v0.35.0

func NewMemoryLevelDBStorage() (LevelDBStorage, error)

NewMemoryLevelDBStorage returns LevelDBStorage instance with in memory leveldb backend.

func (LevelDBStorage) Delete added in v0.35.0

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

Delete removes given key from database..

func (LevelDBStorage) Get added in v0.35.0

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

Get returns value for a given key.

func (LevelDBStorage) NewIterator added in v0.35.0

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

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

func (LevelDBStorage) NewTx added in v0.35.0

func (db LevelDBStorage) NewTx() CommitStorage

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

func (LevelDBStorage) Put added in v0.35.0

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

Put upserts given key/value pair.

type LevelDBTx added in v0.35.0

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 added in v0.35.0

func (tx LevelDBTx) Commit() error

Commit writes batch atomically.

func (LevelDBTx) Delete added in v0.35.0

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

Delete adds delete operation to associated batch.

func (LevelDBTx) Get added in v0.35.0

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

Get reads from currently committed state.

func (LevelDBTx) NewIterator added in v0.35.0

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

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

func (LevelDBTx) Put added in v0.35.0

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

Put adds key/value to associated batch.

type NamespaceIterator added in v0.35.0

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 added in v0.35.0

func (iter NamespaceIterator) Error() error

Error returns accumulated error.

func (NamespaceIterator) Key added in v0.35.0

func (iter NamespaceIterator) Key() []byte

Key returns key of the current item.

func (NamespaceIterator) Next added in v0.35.0

func (iter NamespaceIterator) Next() bool

Next moves cursor forward.

func (NamespaceIterator) Prev added in v0.35.0

func (iter NamespaceIterator) Prev() bool

Prev moves cursor backward.

func (NamespaceIterator) Value added in v0.35.0

func (iter NamespaceIterator) Value() []byte

Value returns actual value of the current item.

type Storage added in v0.35.0

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 added in v0.35.0

type TopicHistory struct {

	// whisper topic
	Topic types.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 types.Hash
	// contains filtered or unexported fields
}

TopicHistory stores necessary information.

func LoadTopicHistoryFromKey added in v0.35.0

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 added in v0.35.0

func (t TopicHistory) Delete() error

Delete removes topic history from database.

func (TopicHistory) Key added in v0.35.0

func (t TopicHistory) Key() TopicHistoryKey

Key returns unique identifier for this TopicHistory.

func (*TopicHistory) Load added in v0.35.0

func (t *TopicHistory) Load() error

Load TopicHistory from db using key and unmarshalls it.

func (TopicHistory) Pending added in v0.35.0

func (t TopicHistory) Pending() bool

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

func (TopicHistory) SameRange added in v0.35.0

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 added in v0.35.0

func (t TopicHistory) Save() error

Save persists TopicHistory on disk.

func (TopicHistory) Value added in v0.35.0

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

Value marshalls TopicHistory into bytes.

type TopicHistoryKey added in v0.35.0

type TopicHistoryKey [12]byte

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

type TransactionalStorage added in v0.35.0

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