Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov committed Jun 15, 2024
1 parent e921652 commit 0a39e32
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 70 deletions.
6 changes: 2 additions & 4 deletions mdbx/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ func (c *Cursor) Get(setkey, setval []byte, op uint) (key, val []byte, err error
err = c.getVal2(setkey, setval, op)
}
if err != nil {
c.txn.key = C.MDBX_val{}
c.txn.val = C.MDBX_val{}
c.txn.key, c.txn.val = C.MDBX_val{}, C.MDBX_val{}
return nil, nil, err
}

Expand All @@ -178,8 +177,7 @@ func (c *Cursor) Get(setkey, setval []byte, op uint) (key, val []byte, err error

// Clear transaction storage record storage area for future use and to
// prevent dangling references.
c.txn.key = C.MDBX_val{}
c.txn.val = C.MDBX_val{}
c.txn.key, c.txn.val = C.MDBX_val{}, C.MDBX_val{}

return key, val, nil
}
Expand Down
38 changes: 6 additions & 32 deletions mdbx/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ type DBI C.MDBX_dbi
// See MDBX_env.
type Env struct {
_env *C.MDBX_env
ckey *C.MDBX_val
cval *C.MDBX_val

// closeLock is used to allow the Txn finalizer to check if the Env has
// been closed, so that it may know if it must abort.
Expand All @@ -140,8 +138,6 @@ func NewEnv() (*Env, error) {
if ret != success {
return nil, operrno("mdbx_env_create", ret)
}
env.ckey = (*C.MDBX_val)(C.malloc(C.size_t(unsafe.Sizeof(C.MDBX_val{}))))
env.cval = (*C.MDBX_val)(C.malloc(C.size_t(unsafe.Sizeof(C.MDBX_val{}))))
return env, nil
}

Expand Down Expand Up @@ -234,11 +230,6 @@ func (env *Env) Close() {
C.mdbx_env_close(env._env)
env._env = nil
env.closeLock.Unlock()

C.free(unsafe.Pointer(env.ckey))
C.free(unsafe.Pointer(env.cval))
env.ckey = nil
env.cval = nil
}

// CopyFD copies env to the the file descriptor fd.
Expand Down Expand Up @@ -522,21 +513,6 @@ 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(false, 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 All @@ -548,7 +524,7 @@ func (env *Env) RunTxn(flags uint, fn TxnOp) error {
// Any call to Commit, Abort, Reset or Renew on a Txn created by View will
// panic.
func (env *Env) View(fn TxnOp) error {
return env.run(false, Readonly, fn)
return env.run(Readonly, fn)
}

// Update calls fn with a writable transaction. Update commits the transaction
Expand All @@ -575,7 +551,9 @@ func (env *Env) View(fn TxnOp) error {
// Any call to Commit, Abort, Reset or Renew on a Txn created by Update will
// panic.
func (env *Env) Update(fn TxnOp) error {
return env.run(true, 0, fn)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
return env.run(0, fn)
}

// UpdateLocked behaves like Update but does not lock the calling goroutine to
Expand All @@ -596,14 +574,10 @@ func (env *Env) Update(fn TxnOp) error {
// Any call to Commit, Abort, Reset or Renew on a Txn created by UpdateLocked
// will panic.
func (env *Env) UpdateLocked(fn TxnOp) error {
return env.run(false, 0, fn)
return env.run(0, fn)
}

func (env *Env) run(lock bool, flags uint, fn TxnOp) error {
if lock {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
}
func (env *Env) run(flags uint, fn TxnOp) error {
txn, err := beginTxn(env, nil, flags)
if err != nil {
return err
Expand Down
34 changes: 0 additions & 34 deletions mdbx/txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,40 +799,6 @@ 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 0a39e32

Please sign in to comment.