wrap

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2025 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package wrap provides a thin, opinionated abstraction for the most common LMDB operations.

Index

Constants

View Source
const (
	MaxNamedDBs = 128          // If you need more, you probably shouldn't be using LMDB.
	MapSize     = 10 * 1 << 30 // 10 GB
)

Variables

View Source
var (
	ErrDuplicateDbName = errors.New("duplicate database name")
	ErrDbNameNotFound  = errors.New("database name not found")
	ErrDBClosed        = errors.New("database is closed")
	ErrEmptyKey        = errors.New("empty key")
)

Functions

This section is empty.

Types

type DB

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

DB represents a simple LMDB database wrapper.

func New

func New(dirPath string, dbNames []string) (*DB, int, error)

New creates (or opens) an LMDB environment at the specified directory path and initializes the given databases. If the directory does not exist, it will be created. Remember to call Close() on the returned DB to cleanly shut down the environment. Returns the DB pointer, the number of stale readers cleared, and any error.

func (*DB) Close

func (db *DB) Close()

Close cleanly shuts down the LMDB environment.

func (*DB) Delete

func (db *DB) Delete(dbName string, key []byte) error

Delete removes a key/value pair from the database.

func (*DB) GetDBis added in v1.1.0

func (db *DB) GetDBis() map[string]lmdb.DBI

GetDBis returns a copy of database names to DBI handle mappings.

func (*DB) Read

func (db *DB) Read(dbName string, key []byte) ([]byte, error)

Read retrieves a value from the database.

func (*DB) Update added in v1.1.0

func (db *DB) Update(op lmdb.TxnOp) error

Update runs an LMDB transaction.

Usage:

err := db.Update(func(txn *lmdb.Txn) error {
	dbi := db.GetDBis()["users"]
	data, err := txn.Get(dbi, []byte("user:123"))
	if err != nil {
		return err
	}
	if !shouldUpdate(data) {
		return nil
	}
	return txn.Put(dbi, []byte("user:123"), update(data), 0)
})

func (*DB) View added in v1.2.0

func (db *DB) View(op lmdb.TxnOp) error

View runs a read-only LMDB transaction.

Usage:

err := db.View(func(txn *lmdb.Txn) error {
	dbi := db.GetDBis()["users"]
	data, err := txn.Get(dbi, []byte("user:123"))
	if err != nil {
		return err
	}
	process(data)
	return nil
})

func (*DB) Write

func (db *DB) Write(dbName string, key, value []byte) error

Write inserts a key/value pair into the database.

Jump to

Keyboard shortcuts

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