Skip to content

Commit

Permalink
txn.DBIs method (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov authored May 2, 2021
1 parent 5fb6d08 commit 6854625
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
21 changes: 21 additions & 0 deletions mdbx/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
84 changes: 84 additions & 0 deletions mdbx/txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 6854625

Please sign in to comment.