Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov committed Jun 18, 2024
1 parent 0a39e32 commit 79fd310
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
15 changes: 15 additions & 0 deletions mdbx/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,21 @@ func (env *Env) BeginTxn(parent *Txn, flags uint) (*Txn, error) {
return beginTxn(env, parent, flags)
}

// RunTxn creates a new Txn and calls fn with it as an argument. Run commits
// the transaction if fn returns nil otherwise the transaction is aborted.
// Because RunTxn terminates the transaction goroutines should not retain
// references to it or its data after fn returns.
//
// RunTxn does not call runtime.LockOSThread. Unless the Readonly flag is
// passed the calling goroutine should ensure it is locked to its thread and
// any goroutines started by fn must not call methods on the Txn object it is
// passed.
//
// See mdbx_txn_begin.
func (env *Env) RunTxn(flags uint, fn TxnOp) error {
return env.run(flags, fn)
}

// View creates a readonly transaction with a consistent view of the
// environment and passes it to fn. View terminates its transaction after fn
// returns. Any error encountered by View is returned.
Expand Down
34 changes: 34 additions & 0 deletions mdbx/txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,40 @@ func TestTxn_UpdateLocked(t *testing.T) {
}
}

func TestTxn_RunTxn(t *testing.T) {
env, _ := setup(t)

var dbi DBI
err := env.RunTxn(0, func(txn *Txn) (err error) {
dbi, err = txn.OpenRoot(0)
if err != nil {
return err
}
return txn.Put(dbi, []byte("k0"), []byte("v0"), 0)
})
if err != nil {
t.Error(err)
}

err = env.RunTxn(Readonly, func(txn *Txn) (err error) {
v, err := txn.Get(dbi, []byte("k0"))
if err != nil {
return err
}
if string(v) != "v0" {
return fmt.Errorf("unexpected value: %q (!= %q)", v, "v0")
}
err = txn.Put(dbi, []byte("k1"), []byte("v1"), 0)
if err == nil {
return fmt.Errorf("allowed to Put in a readonly Txn")
}
return nil
})
if err != nil {
t.Error(err)
}
}

func TestTxn_Stat(t *testing.T) {
env, _ := setup(t)

Expand Down

0 comments on commit 79fd310

Please sign in to comment.