Skip to content

Commit

Permalink
adde (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov authored Jul 1, 2021
1 parent 8d7f44f commit 00f85ac
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
19 changes: 19 additions & 0 deletions mdbx/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
47 changes: 47 additions & 0 deletions mdbx/txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 00f85ac

Please sign in to comment.