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 ¶
- func CreateDB(args ...interface{}) (database.Db, error)
- func OpenDB(args ...interface{}) (database.Db, error)
- type MemDb
- func (db *MemDb) Close() error
- func (db *MemDb) ExistsObject(hash *wire.ShaHash) (bool, error)
- func (db *MemDb) FetchIdentityByAddress(addr *bmutil.Address) (*identity.Public, error)
- func (db *MemDb) FetchObjectByCounter(objType wire.ObjectType, counter uint64) ([]byte, error)
- func (db *MemDb) FetchObjectByHash(hash *wire.ShaHash) ([]byte, error)
- func (db *MemDb) FetchObjectsFromCounter(objType wire.ObjectType, counter uint64, count uint64) ([][]byte, uint64, error)
- func (db *MemDb) FetchRandomInvHashes(count uint64, filter func(*wire.ShaHash, []byte) bool) ([]wire.ShaHash, error)
- func (db *MemDb) FilterObjects(filter func(hash *wire.ShaHash, obj []byte) bool) (map[wire.ShaHash][]byte, error)
- func (db *MemDb) GetCounter(objType wire.ObjectType) (uint64, error)
- func (db *MemDb) InsertObject(data []byte) (uint64, error)
- func (db *MemDb) RemoveExpiredObjects() error
- func (db *MemDb) RemoveObject(hash *wire.ShaHash) error
- func (db *MemDb) RemoveObjectByCounter(objType wire.ObjectType, counter uint64) error
- func (db *MemDb) RemovePubKey(tag *wire.ShaHash) error
- func (db *MemDb) RollbackClose() error
- func (db *MemDb) Sync() error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.