Skip to content

Commit

Permalink
Merge branch 'master' into e35_mdbx_v0_13
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov committed Jun 28, 2024
2 parents 9fc3dad + c531783 commit affb875
Show file tree
Hide file tree
Showing 15 changed files with 199 additions and 42 deletions.
5 changes: 2 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ linters:
- govet
- stylecheck
- staticcheck
# - goerr113
# - err113
- unconvert
- unparam
- nakedret
- prealloc
- gosimple
- gosec
- ineffassign
- depguard
- typecheck
- misspell
- bodyclose
Expand All @@ -48,7 +47,7 @@ issues:
- golint
text: "should be"
- linters:
- goerr113
- err113
text: "do not define dynamic errors"
- linters:
- stylecheck
Expand Down
1 change: 0 additions & 1 deletion dbg/pprof_cgo.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build debug
// +build debug

package dbg

Expand Down
2 changes: 1 addition & 1 deletion exp/mdbxpool/put.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !race
//go:build !race

package mdbxpool

Expand Down
1 change: 0 additions & 1 deletion exp/mdbxpool/putrace.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build race
// +build race

package mdbxpool

Expand Down
2 changes: 1 addition & 1 deletion keep.go
Original file line number Diff line number Diff line change
@@ -1 +1 @@
package mdbx_go
package mdbxgo
6 changes: 2 additions & 4 deletions mdbx/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ func (c *Cursor) Get(setkey, setval []byte, op uint) (key, val []byte, err error
err = c.getValEmpty(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 @@ -175,8 +174,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
96 changes: 94 additions & 2 deletions mdbx/cursor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/binary"
"encoding/hex"
"fmt"
"math/rand"
"os"
"reflect"
"runtime"
Expand Down Expand Up @@ -1566,7 +1567,7 @@ func BenchmarkCursor_Renew(b *testing.B) {
})
}

func BenchmarkCursor_SetRange(b *testing.B) {
func BenchmarkCursor_Set_OneKey(b *testing.B) {
env, _ := setup(b)

var db DBI
Expand Down Expand Up @@ -1596,7 +1597,98 @@ func BenchmarkCursor_SetRange(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, err := c.Get(k, nil, SetRange)
_, _, err := c.Get(k, nil, Set)
if err != nil {
return err
}
}
return nil
}); err != nil {
b.Errorf("put: %v", err)
}
}

func BenchmarkCursor_Set_Sequence(b *testing.B) {
env, _ := setup(b)

var db DBI
keys := make([][]byte, b.N, b.N)
for i := range keys {
keys[i] = make([]byte, 8)
binary.BigEndian.PutUint64(keys[i], uint64(i))
}

if err := env.Update(func(txn *Txn) (err error) {
db, err = txn.OpenRoot(0)
if err != nil {
return err
}
for _, k := range keys {
err = txn.Put(db, k, k, 0)
if err != nil {
return err
}
}
return nil
}); err != nil {
b.Errorf("dbi: %v", err)
return
}

if err := env.View(func(txn *Txn) (err error) {
c, err := txn.OpenCursor(db)
if err != nil {
return err
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, err = c.Get(keys[i], nil, Set)
if err != nil {
return err
}
}
return nil
}); err != nil {
b.Errorf("put: %v", err)
}
}

func BenchmarkCursor_Set_Random(b *testing.B) {
env, _ := setup(b)

var db DBI
keys := make([][]byte, b.N, b.N)
for i := range keys {
keys[i] = make([]byte, 8)
binary.BigEndian.PutUint64(keys[i], uint64(rand.Intn(100*b.N)))
}

if err := env.Update(func(txn *Txn) (err error) {
db, err = txn.OpenRoot(0)
if err != nil {
return err
}
for _, k := range keys {
err = txn.Put(db, k, k, 0)
if err != nil {
return err
}
}
return nil
}); err != nil {
b.Errorf("dbi: %v", err)
return
}

if err := env.View(func(txn *Txn) (err error) {
b.ResetTimer()
c, err := txn.OpenCursor(db)
if err != nil {
return err
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, err = c.Get(keys[i], nil, Set)
if err != nil {
return err
}
Expand Down
25 changes: 7 additions & 18 deletions mdbx/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,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 @@ -141,8 +139,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 @@ -235,11 +231,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 @@ -551,7 +542,7 @@ func (env *Env) BeginTxn(parent *Txn, flags uint) (*Txn, error) {
//
// See mdbx_txn_begin.
func (env *Env) RunTxn(flags uint, fn TxnOp) error {
return env.run(false, flags, fn)
return env.run(flags, fn)
}

// View creates a readonly transaction with a consistent view of the
Expand All @@ -565,7 +556,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 @@ -592,7 +583,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 @@ -613,14 +606,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
1 change: 0 additions & 1 deletion mdbx/env_not_win.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !windows
// +build !windows

package mdbx

Expand Down
4 changes: 2 additions & 2 deletions mdbx/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ func setupFlags(t testing.TB, flags uint) (env *Env, path string) {
t.Fatalf("setmaxdbs: %v", err)
}
const pageSize = 4096
err = env.SetGeometry(-1, -1, 64*1024*pageSize, -1, -1, pageSize)
err = env.SetGeometry(-1, -1, 256*64*1024*pageSize, -1, -1, pageSize)
if err != nil {
t.Fatalf("setmaxdbs: %v", err)
}
Expand Down Expand Up @@ -584,7 +584,7 @@ func TestEnv_CloseDBI(t *testing.T) {
return
}

//nolint:goerr113
//nolint:err113
if stat.Entries != numdb {
t.Errorf("unexpected entries: %d (not %d)", stat.Entries, numdb)
}
Expand Down
6 changes: 3 additions & 3 deletions mdbx/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ func IsMapFull(err error) bool {

// IsErrno returns true if err's errno is the given errno.
func IsErrno(err error, errno Errno) bool {
return IsErrnoFn(err, func(err error) bool { return err == errno }) //nolint:goerr113
return IsErrnoFn(err, func(err error) bool { return err == errno }) //nolint
}

// IsErrnoSys returns true if err's errno is the given errno.
func IsErrnoSys(err error, errno syscall.Errno) bool {
return IsErrnoFn(err, func(err error) bool { return err == errno }) //nolint:goerr113
return IsErrnoFn(err, func(err error) bool { return err == errno }) //nolint
}

// IsErrnoFn calls fn on the error underlying err and returns the result. If
Expand All @@ -143,7 +143,7 @@ func IsErrnoFn(err error, fn func(error) bool) bool {
if err == nil {
return false
}
if err, ok := err.(*OpError); ok {
if err, ok := err.(*OpError); ok { //nolint
return fn(err.Errno)
}
return fn(err)
Expand Down
4 changes: 2 additions & 2 deletions mdbx/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ func TestErrno(t *testing.T) {
t.Errorf("errno(0) != nil: %#v", zeroerr)
}
syserr := _operrno("testop", int(syscall.EINVAL))
//nolint:goerr113
//nolint:err113
if syserr.(*OpError).Errno != syscall.EINVAL { // fails if error is Errno(syscall.EINVAL)
t.Errorf("errno(syscall.EINVAL) != syscall.EINVAL: %#v", syserr)
}
mdberr := _operrno("testop", int(KeyExist))
//nolint:goerr113
//nolint:err113
if mdberr.(*OpError).Errno != KeyExist {
t.Errorf("errno(ErrKeyExist) != ErrKeyExist: %#v", syserr)
}
Expand Down
1 change: 0 additions & 1 deletion mdbx/error_unix.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !windows
// +build !windows

package mdbx

Expand Down
2 changes: 1 addition & 1 deletion mdbx/msgfunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type msgfunc func(string) error
// not contain pointers in their struct fields. See the following language
// proposal which discusses the restrictions on passing pointers to C.
//
// https://github.com/golang/proposal/blob/master/design/12416-cgo-pointers.md
// https://github.com/golang/proposal/blob/master/design/12416-cgo-pointers.md
type msgctx uintptr
type _msgctx struct {
fn msgfunc
Expand Down
Loading

0 comments on commit affb875

Please sign in to comment.