db

package
v1.8.2 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewDB

func NewDB(dbVersion int64, path string, logger log.Logger) (*leveldb.DB, error)

newDB creates/opens a leveldb persistent database at the given path. If no path is given, an in-memory, temporary database is constructed.

func NewMemoryDB

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

newMemoryDB creates a new in-memory node database without a persistent backend.

func NewPersistentDB

func NewPersistentDB(dbVersion int64, path string, logger log.Logger) (*leveldb.DB, error)

newPersistentNodeDB creates/opens a leveldb backed persistent database, also flushing its contents in case of a version mismatch.

Types

type GenericDB

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

GenericDB manages a levelDB database

func New

func New(dbVersion int64, path string, logger log.Logger, writeOptions *opt.WriteOptions) (*GenericDB, error)

New will open a new db at the given file path with the given version. If the path is empty, the db will be created in memory. If there is a version mismatch in the existing db, the contents are flushed.

func (*GenericDB) Close

func (gdb *GenericDB) Close() error

Close flushes and closes the database files.

func (*GenericDB) Get

func (gdb *GenericDB) Get(key []byte) ([]byte, error)

Get gets the bytes at a given key in the db

func (*GenericDB) Iterate

func (gdb *GenericDB) Iterate(keyPrefix []byte, onEntry func([]byte, []byte) error) error

Iterate will iterate through each entry in the db whose key has the prefix keyPrefix, and call `onEntry` with the bytes of the key (without the prefix) and the bytes of the value

func (*GenericDB) Upsert

func (gdb *GenericDB) Upsert(
	entries []GenericEntry,
	getExistingEntry func(entry GenericEntry) (GenericEntry, error),
	onUpdatedEntry func(batch *leveldb.Batch, existingEntry GenericEntry, newEntry GenericEntry) error,
	onNewEntry func(batch *leveldb.Batch, entry GenericEntry) error,
) error

Upsert iterates through each provided entry and determines if the entry is new. If there is an existing entry in the db, `onUpdatedEntry` is called. If there is no existing entry, `onNewEntry` is called. Db content modifications are left to those functions by providing a leveldb Batch that is written after all entries are processed.

func (*GenericDB) Write

func (gdb *GenericDB) Write(batch *leveldb.Batch) error

Write writes a Batch to modify the db

type GenericEntry

type GenericEntry interface{}

Jump to

Keyboard shortcuts

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