Skip to content
erigontechPublic

Repository files navigation

mdbx-go

Test Lint CodeQL Release libmdbx Go Reference

Go bindings to the libmdbx: https://libmdbx.dqdkfa.ru

Notice: directory ./mdbx contains the Go bindings package and a small cgo helper (mdbxgo.c / mdbxgo.h). The libmdbx amalgamation (mdbx.h, mdbx.c), produced by libmdbx's make dist command, lives in ./libmdbx/. License is also there.

Most of articles in internet about LMDB are applicable to MDBX. But mdbx has more features.

For deeper DB understanding please read through mdbx.h

Min Requirements

C language Compilers compatible with GCC or CLANG (mingw 10 on windows) Golang: 1.25

Packages

Functionality is logically divided into several packages. Applications will usually need to import mdbx but may import other packages on an as needed basis.

Packages in the exp/ directory are not stable and may change without warning. That said, they are generally usable if application dependencies are managed and pinned by tag/commit.

Developers concerned with package stability should consult the documentation.

mdbx GoDoc stable

import "github.com/erigontech/mdbx-go/mdbx"

Core bindings allowing low-level access to MDBX.

exp/mdbxpool GoDoc experimental

import "github.com/erigontech/mdbx-go/exp/mdbxpool"

A utility package which facilitates reuse of mdbx.Txn objects using a sync.Pool. Naively storing mdbx.Txn objects in sync.Pool can be troublesome. And the mdbxpool.TxnPool type has been defined as a complete pooling solution and as reference for applications attempting to write their own pooling implementation.

The mdbxpool package is relatively new. But it has a lot of potential utility. And once the mdbxpool API has been ironed out, and the implementation hardened through use by real applications it can be integrated directly into the mdbx package for more transparent integration. Please test this package and provide feedback to speed this process up.

Quickstart

Install:

go get github.com/erigontech/mdbx-go/mdbx

A minimal example — open an environment, write a key, read it back:

package main

import (
	"fmt"
	"log"

	"github.com/erigontech/mdbx-go/mdbx"
)

func main() {
	env, err := mdbx.NewEnv(mdbx.Default)
	if err != nil {
		log.Fatal(err)
	}
	defer env.Close()

	// pageSize=4096, growth=1MiB, shrink=-1 (default), upper=1GiB
	if err := env.SetGeometry(-1, -1, 1<<30, 1<<20, -1, 4096); err != nil {
		log.Fatal(err)
	}
	if err := env.Open("./mydb", 0, 0o664); err != nil {
		log.Fatal(err)
	}

	var dbi mdbx.DBI
	if err := env.Update(func(txn *mdbx.Txn) error {
		var err error
		dbi, err = txn.OpenRoot(0)
		if err != nil {
			return err
		}
		return txn.Put(dbi, []byte("hello"), []byte("world"), 0)
	}); err != nil {
		log.Fatal(err)
	}

	if err := env.View(func(txn *mdbx.Txn) error {
		v, err := txn.Get(dbi, []byte("hello"))
		if err != nil {
			return err
		}
		fmt.Printf("hello = %s\n", v)
		return nil
	}); err != nil {
		log.Fatal(err)
	}
}

More examples — see *_test.go files in ./mdbx.

Key Features

Idiomatic API