diff --git a/mdbx/txn.go b/mdbx/txn.go index b78bead..249a3e9 100644 --- a/mdbx/txn.go +++ b/mdbx/txn.go @@ -684,3 +684,22 @@ func (txn *Txn) Sequence(dbi DBI, increment uint64) (uint64, error) { } return uint64(res), nil } + +// ListDBI - return all dbi names. they stored as keys of un-named (main) dbi +func (txn *Txn) ListDBI() (res []string, err error) { + root, err := txn.OpenRoot(0) + if err != nil { + return nil, err + } + c, err := txn.OpenCursor(root) + if err != nil { + return nil, err + } + for k, _, err := c.Get(nil, nil, First); k != nil; k, _, err = c.Get(nil, nil, Next) { + if err != nil { + return nil, err + } + res = append(res, string(k)) + } + return res, nil +} diff --git a/mdbx/txn_test.go b/mdbx/txn_test.go index d4405fe..8866bc7 100644 --- a/mdbx/txn_test.go +++ b/mdbx/txn_test.go @@ -982,6 +982,53 @@ func TestSequence(t *testing.T) { } } +func TestListDBI(t *testing.T) { + env := setup(t) + + if err := env.View(func(txn *Txn) (err error) { + v, err := txn.ListDBI() + if err != nil { + return err + } + if len(v) != 0 { + t.Errorf("unexpected value: %d (expected %d)", len(v), 0) + } + + return nil + }); err != nil { + t.Errorf("%s", err) + } + + if err := env.Update(func(txn *Txn) (err error) { + _, err = txn.OpenDBISimple("testdb", Create) + if err != nil { + return err + } + _, err = txn.OpenDBISimple("testdb2", Create) + return err + }); err != nil { + t.Errorf("%s", err) + return + } + + if err := env.View(func(txn *Txn) (err error) { + v, err := txn.ListDBI() + if err != nil { + return err + } + if v == nil { + t.Errorf("unexpected nil") + } + if len(v) != 2 { + t.Errorf("unexpected value: %d (expected %d)", len(v), 2) + } + + return nil + }); err != nil { + t.Errorf("%s", err) + } +} + func BenchmarkTxn_abort(b *testing.B) { env := setup(b)