diff --git a/mdbx/txn.go b/mdbx/txn.go index dc6ff6d..d27d849 100644 --- a/mdbx/txn.go +++ b/mdbx/txn.go @@ -684,3 +684,24 @@ func (txn *Txn) Sequence(dbi DBI, increment uint64) (uint64, error) { } return uint64(res), nil } + +// DBIs - return names of all existing DBIs. Doesn't include Main and GC. +func (txn *Txn) DBIs() ([]string, error) { + var res []string + root, err := txn.OpenRoot(0) + if err != nil { + return nil, err + } + c, err := txn.OpenCursor(root) + if err != nil { + return nil, err + } + defer c.Close() + for k, _, err := c.Get(nil, nil, First); !IsNotFound(err); 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 eafd8c6..4759331 100644 --- a/mdbx/txn_test.go +++ b/mdbx/txn_test.go @@ -974,6 +974,90 @@ func TestTxn_StatOnEmpty(t *testing.T) { } } +func TestTxn_DBIs(t *testing.T) { + env := setup(t) + defer env.Close() + + if err := env.Update(func(txn *Txn) (err error) { + _, err = txn.OpenDBISimple("test1", Create|DupSort) + if err != nil { + return err + } + _, err = txn.OpenDBISimple("test2", Create) + if err != nil { + return err + } + list, err := txn.DBIs() + if err != nil { + return err + } + if len(list) != 2 { + t.Fatalf("unexpected list of dbi's %+v", list) + } + if list[0] != "test1" { + t.Fatalf("unexpected list of dbi's %+v", list) + } + if list[1] != "test2" { + t.Fatalf("unexpected list of dbi's %+v", list) + } + + return nil + }); err != nil { + t.Errorf("%s", err) + return + } + + if err := env.View(func(txn *Txn) (err error) { + list, err := txn.DBIs() + if err != nil { + return err + } + + if len(list) != 2 { + t.Fatalf("unexpected list of dbi's %+v", list) + } + if list[0] != "test1" { + t.Fatalf("unexpected list of dbi's %+v", list) + } + if list[1] != "test2" { + t.Fatalf("unexpected list of dbi's %+v", list) + } + return nil + }); err != nil { + t.Errorf("%s", err) + return + } + + if err := env.Update(func(txn *Txn) (err error) { + dbi, err := txn.OpenDBI("test1", 0, nil, nil) + if err != nil { + return err + } + + err = txn.Drop(dbi, true) + if err != nil { + return err + } + + list, err := txn.DBIs() + if err != nil { + return err + } + + if len(list) != 1 { + t.Fatalf("unexpected list of dbi's %+v", list) + } + if list[0] != "test2" { + t.Fatalf("unexpected list of dbi's %+v", list) + } + return nil + }); err != nil { + t.Errorf("%s", err) + return + } + +} + func TestSequence(t *testing.T) { env := setup(t) path, err := env.Path()