Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,37 @@ Contributions are welcome.

## Installation

From a local source checkout:
lmdbnav is written in Go and embeds the LMDB database engine (written in C),
so building it requires two things:

1. **Go 1.21 or newer.** Download it from <https://go.dev/dl/>, or use a
package manager: `brew install go` (macOS) or `sudo apt install golang`
(Debian/Ubuntu). Verify with `go version`.
2. **A C compiler.** On macOS run `xcode-select --install`, on Debian/Ubuntu
`sudo apt install build-essential`, on Fedora `sudo dnf install gcc`.

Then this single command downloads, builds and installs lmdbnav:

```
$ GOBIN=/path/to/target/bin go install ./cmd/lmdbnav
$ go install github.com/PowerDNS/lmdbnav/cmd/lmdbnav@latest
```

Or directly, without any local checkout:
The binary is installed as `~/go/bin/lmdbnav`. Either add `~/go/bin` to your
`PATH`, run it by its full path, or point `GOBIN` at a directory of your
choice, e.g.:

```
$ GOBIN=/path/to/target/bin go install github.com/PowerDNS/lmdbnav/cmd/lmdbnav@latest
$ GOBIN=/usr/local/bin go install github.com/PowerDNS/lmdbnav/cmd/lmdbnav@latest
```

From a local source checkout, use the same commands with the package path
replaced by `./cmd/lmdbnav`.

> **Note:** while lmdbnav depends on an unreleased lmdb-go v2 through a
> `replace` directive, the remote `go install` form above does not work.
> Until lmdb-go v2 is released, clone this repository and install from the
> local source checkout instead.

## Usage

```
Expand Down
61 changes: 53 additions & 8 deletions cmd/lmdbnav/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
"os"
"path/filepath"
"strings"
"syscall"
"time"
"unicode/utf8"

"github.com/PowerDNS/lmdb-go/lmdb"
"github.com/PowerDNS/lmdb-go/lmdbscan"
"github.com/PowerDNS/lmdb-go/v2/lmdb"
"github.com/PowerDNS/lmdb-go/v2/lmdbscan"
"github.com/dustin/go-humanize"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
Expand All @@ -27,7 +28,10 @@ var (
footer *tview.TextView
env *lmdb.Env

withLS = pflag.Bool("ls", false, "Enable LightningStream header support")
withLS = pflag.Bool("ls", false, "Enable LightningStream header support")
prevSnapshot = pflag.Bool("prev-snapshot", false,
"Open the previous snapshot instead of the latest one, e.g. when the "+
"last transaction is corrupted (LMDB 1.0 format databases only)")
)

const (
Expand Down Expand Up @@ -61,6 +65,9 @@ func run(lmdbPath string) error {
return err
}
var envFlags uint = lmdb.Readonly
if *prevSnapshot {
envFlags |= lmdb.PrevSnapshot
}
lmdbFilePath := lmdbPath
if !fileStat.IsDir() {
dir, fname := filepath.Split(lmdbPath)
Expand Down Expand Up @@ -95,6 +102,9 @@ func run(lmdbPath string) error {

err = env.Open(lmdbPath, envFlags, 0666)
if err != nil {
if *prevSnapshot && lmdb.IsErrnoSys(err, syscall.EINVAL) {
return fmt.Errorf("%w (--prev-snapshot only works with LMDB 1.0 format databases)", err)
}
return err
}
defer closeWithLog(env)
Expand Down Expand Up @@ -123,9 +133,17 @@ func run(lmdbPath string) error {
scanner := lmdbscan.New(txn, rootDBI)
defer scanner.Close()
for scanner.Scan() {
name := string(scanner.Key())
if len(scanner.Val()) != 48 {
continue // not a DBI record, see databasesView
}
name, ok := dbiKeyName(scanner.Key())
if !ok {
continue
}
dbi, err := txn.OpenDBI(name, 0)

if err != nil {
continue
}
st, err := txn.Stat(dbi)
if err != nil {
return err
Expand All @@ -139,6 +157,15 @@ func run(lmdbPath string) error {
return err
}

engineVer, err := env.EngineVersion()
if err != nil {
return err
}
writef(footer, "LMDB %d.%d.%d", engineVer.Major, engineVer.Minor, engineVer.Patch)
if *prevSnapshot {
writef(footer, " (prev snapshot)")
}
writef(footer, " | ")
writef(footer, "MapSize %s", humanize.IBytes(uint64(info.MapSize)))
writef(footer, " | ")
writef(footer, "Used %s / %.1f %%",
Expand Down Expand Up @@ -595,7 +622,8 @@ func databasesView() error {
addDBI(RootDBIName, rootDBI)
for scanner.Scan() {
// DBI entries appear to always have 48 bytes, corresponding to
// the MDB_db struct, but there is no truly reliable way to
// the MDB_db struct (identical layout in the 0.9 and 1.0
// formats), but there is no truly reliable way to
// check if they are real DBIs. The LMDB lib only performs
// very light checks when you ask it to open a DBI, so this
// can still cause trouble if you have garbage of the right size in
Expand All @@ -606,7 +634,10 @@ func databasesView() error {
if len(scanner.Val()) != 48 {
continue
}
name := string(scanner.Key())
name, ok := dbiKeyName(scanner.Key())
if !ok {
continue
}
dbi, err := txn.OpenDBI(name, 0)
if err != nil {
continue
Expand Down Expand Up @@ -701,14 +732,28 @@ func inspectView(kv KV) {

func closeWithLog(c io.Closer) {
if err := c.Close(); err != nil {
log.Printf("Error closing %v: %w", c, err)
log.Printf("Error closing %v: %v", c, err)
}
}

func sizeBytes(st *lmdb.Stat) uint64 {
return uint64(st.PSize) * (st.BranchPages + st.LeafPages + st.OverflowPages)
}

// dbiKeyName returns the database name for a key in the root DBI, or
// ok=false if the key cannot be a database name on the current engine.
// LMDB 1.0 stores the name's terminating null byte in the key, LMDB 0.9
// stores the bare name.
func dbiKeyName(key []byte) (name string, ok bool) {
if env.Format() == lmdb.V10 {
if len(key) == 0 || key[len(key)-1] != 0 {
return "", false
}
key = key[:len(key)-1]
}
return string(key), true
}

func writef(w io.Writer, format string, args ...interface{}) {
_, _ = fmt.Fprintf(w, format, args...)
}
Expand Down
17 changes: 11 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
module github.com/PowerDNS/lmdbnav

go 1.17
go 1.21

require github.com/rivo/tview v0.0.0-20220129131435-1f7581b67bd1
require (
github.com/PowerDNS/lmdb-go/v2 v2.0.0
github.com/dustin/go-humanize v1.0.0
github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1
github.com/rivo/tview v0.0.0-20220129131435-1f7581b67bd1
github.com/spf13/pflag v1.0.5
)

require (
github.com/PowerDNS/lmdb-go v1.9.0 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
golang.org/x/text v0.3.6 // indirect
)

// lmdb-go v2 is under development and not published yet
replace github.com/PowerDNS/lmdb-go/v2 => github.com/wojas/lmdb-go/v2 v2.0.0-20260828152212-7d658404dccc
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/PowerDNS/lmdb-go v1.9.0 h1:pvdI8lzdeAfWJdkXc3XPZXCewX508awqfe5yMjSKNTE=
github.com/PowerDNS/lmdb-go v1.9.0/go.mod h1:5beHlX2aYqXfMBI0+BBX3LDhV3YGHU5JgoDsirAFPlA=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
Expand All @@ -16,14 +14,12 @@ github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/net v0.0.0-20210415231046-e915ea6b2b7d/go.mod h1:9tjilg8BloeKEkVJvy7fQ90B1CfIiPueXVOjqfkSzI8=
github.com/wojas/lmdb-go/v2 v2.0.0-20260828152212-7d658404dccc h1:gj29sH/8kfHGNh6iKNKr+Nj+oH+pf61C61DDduBvBNQ=
github.com/wojas/lmdb-go/v2 v2.0.0-20260828152212-7d658404dccc/go.mod h1:aMrVsdgQprZdFwjBETUVX+38d5Io4jrQf2m+yxRwCgA=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44 h1:Bli41pIlzTzf3KEY06n+xnzK/BESIg2ze4Pgfh/aI8c=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 h1:rm+CHSpPEEW2IsXUib1ThaHIjuBVZjxNgSKmBLFfD4c=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d h1:SZxvLBoTP5yHO3Frd4z4vrF+DBX9vMVanchswa69toE=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down