memdb

package
v0.0.0-...-73a60f1 Latest Latest
Warning

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

Go to latest
Published: May 30, 2015 License: ISC Imports: 10 Imported by: 0

Documentation

Overview

Package memdb implements an instance of the database package that uses memory for object storage.

This is primary used for testing purposes as normal operations require a persistent object storage mechanism which this is not.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateDB

func CreateDB(args ...interface{}) (database.Db, error)

CreateDB creates, initializes, and opens a database for use.

func OpenDB

func OpenDB(args ...interface{}) (database.Db, error)

OpenDB opens an existing database for use.

Types

type MemDb

type MemDb struct {
	// Embed a mutex for safe concurrent access.
	sync.Mutex
	// contains filtered or unexported fields
}

MemDb is a concrete implementation of the database.Db interface which provides a memory-only database. Since it is memory-only, it is obviously not persistent and is mostly only useful for testing purposes.

func (*MemDb) Close

func (db *MemDb) Close() error

Close cleanly shuts down database. This is part of the database.Db interface implementation.

All data is purged upon close with this implementation since it is a memory-only database.

func (*MemDb) ExistsObject

func (db *MemDb) ExistsObject(hash *wire.ShaHash) (bool, error)

ExistsObject returns whether or not an object with the given inventory hash exists in the database. This is part of the database.Db interface implementation.

func (*MemDb) FetchIdentityByAddress

func (db *MemDb) FetchIdentityByAddress(addr *bmutil.Address) (*identity.Public,
	error)

FetchIdentityByAddress returns identity.Public stored in the form of a PubKey message in the pubkey database. It needs to go through all the public keys in the database to find this. The implementation must thus cache results, if needed.

func (*MemDb) FetchObjectByCounter

func (db *MemDb) FetchObjectByCounter(objType wire.ObjectType,
	counter uint64) ([]byte, error)

FetchObjectByCounter returns an object from the database as a byte array based on the object type and counter value. The implementation may cache the underlying data if desired. This is part of the database.Db interface implementation.

This implementation does not use any additional cache since the entire database is already in memory.

func (*MemDb) FetchObjectByHash

func (db *MemDb) FetchObjectByHash(hash *wire.ShaHash) ([]byte, error)

FetchObjectByHash returns an object from the database as a byte array. It is upto the implementation to decode the byte array. This is part of the database.Db interface implementation.

This implementation does not use any additional cache since the entire database is already in memory.

func (*MemDb) FetchObjectsFromCounter

func (db *MemDb) FetchObjectsFromCounter(objType wire.ObjectType, counter uint64,
	count uint64) ([][]byte, uint64, error)

FetchObjectsFromCounter returns objects from the database, which have a counter value starting from `counter' and a type of `objType` as a slice of byte slices with the counter of the last object. Objects are guaranteed to be returned in order of increasing counter. The implementation may cache the underlying data if desired. This is part of the database.Db interface implementation.

This implementation does not use any additional cache since the entire database is already in memory.

func (*MemDb) FetchRandomInvHashes

func (db *MemDb) FetchRandomInvHashes(count uint64,
	filter func(*wire.ShaHash, []byte) bool) ([]wire.ShaHash, error)

FetchRandomInvHashes returns the specified number of inventory hashes corresponding to random unexpired objects from the database and filtering them by calling filter(invHash, objectData) on each object. A return value of true from filter means that the object would be returned.

Useful for creating inv message, with filter being used to filter out inventory hashes that have already been sent out to a particular node.

WARNING: filter must not mutate the object and/or its inventory hash.

func (*MemDb) FilterObjects

func (db *MemDb) FilterObjects(filter func(hash *wire.ShaHash,
	obj []byte) bool) (map[wire.ShaHash][]byte, error)

FilterObjects returns a map of objects that return true when passed to the filter function. It could be used for grabbing objects of a certain type, like getpubkey requests. This is an expensive operation as it copies data corresponding to anything that matches the filter. Use sparingly and ensure that only a few objects can match.

WARNING: filter must not mutate the object and/or its inventory hash.

func (*MemDb) GetCounter

func (db *MemDb) GetCounter(objType wire.ObjectType) (uint64, error)

GetCounter returns the highest value of counter that exists for objects of the given type.

func (*MemDb) InsertObject

func (db *MemDb) InsertObject(data []byte) (uint64, error)

InsertObject inserts data of the given type and hash into the database. It returns the calculated/stored inventory hash as well as the counter position (if object type has a counter associated with it). If the object is a PubKey, it inserts it into a separate place where it isn't touched by RemoveObject or RemoveExpiredObjects and has to be removed using RemovePubKey.

func (*MemDb) RemoveExpiredObjects

func (db *MemDb) RemoveExpiredObjects() error

RemoveExpiredObjects prunes all objects in the main circulation store whose expiry time has passed (along with a margin of 3 hours). This does not touch the pubkeys stored in the public key collection.

func (*MemDb) RemoveObject

func (db *MemDb) RemoveObject(hash *wire.ShaHash) error

RemoveObject removes the object with the specified hash from the database.

func (*MemDb) RemoveObjectByCounter

func (db *MemDb) RemoveObjectByCounter(objType wire.ObjectType,
	counter uint64) error

RemoveObjectByCounter removes the object with the specified counter value from the database.

func (*MemDb) RemovePubKey

func (db *MemDb) RemovePubKey(tag *wire.ShaHash) error

RemovePubKey removes a PubKey from the PubKey store with the specified tag. Note that it doesn't touch the general object store and won't remove the public key from there.

func (*MemDb) RollbackClose

func (db *MemDb) RollbackClose() error

RollbackClose discards the recent database changes to the previously saved data at last Sync and closes the database. This is part of the database.Db interface implementation.

The database is completely purged on close with this implementation since the entire database is only in memory. As a result, this function behaves no differently than Close.

func (*MemDb) Sync

func (db *MemDb) Sync() error

Sync verifies that the database is coherent on disk and no outstanding transactions are in flight. This is part of the database.Db interface implementation.

This implementation does not write any data to disk, so this function only grabs a lock to ensure it doesn't return until other operations are complete.

Jump to

Keyboard shortcuts

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