From aab789d15716460c36950ddb3b9f3b7a7c4c8089 Mon Sep 17 00:00:00 2001 From: Ilya Mikheev <54912776+JkLondon@users.noreply.github.com> Date: Mon, 17 Jun 2024 16:20:42 +0400 Subject: [PATCH 1/2] New setval approach (#148) * save * better naming * removed unused --------- Co-authored-by: Ilya Miheev --- mdbx/cursor.go | 54 ++++++++------------------------------------------ 1 file changed, 8 insertions(+), 46 deletions(-) diff --git a/mdbx/cursor.go b/mdbx/cursor.go index 4f10779..035738a 100644 --- a/mdbx/cursor.go +++ b/mdbx/cursor.go @@ -147,15 +147,10 @@ func (c *Cursor) DBI() DBI { // // See mdb_cursor_get. func (c *Cursor) Get(setkey, setval []byte, op uint) (key, val []byte, err error) { - switch { - case len(setkey) == 0 && len(setval) == 0: - err = c.getVal0(op) - case len(setkey) == 0: - err = c.getVal01(setval, op) - case len(setval) == 0: - err = c.getVal1(setkey, op) - default: - err = c.getVal2(setkey, setval, op) + if len(setkey) != 0 || len(setval) != 0 { + err = c.getVal(setkey, setval, op) + } else { + err = c.getValEmpty(op) } if err != nil { c.txn.key = C.MDBX_val{} @@ -184,53 +179,20 @@ func (c *Cursor) Get(setkey, setval []byte, op uint) (key, val []byte, err error return key, val, nil } -// getVal0 retrieves items from the database without using given key or value +// getValEmpty retrieves items from the database without using given key or value // data for reference (Next, First, Last, etc). // // See mdb_cursor_get. -func (c *Cursor) getVal0(op uint) error { +func (c *Cursor) getValEmpty(op uint) error { ret := C.mdbx_cursor_get(c._c, &c.txn.key, &c.txn.val, C.MDBX_cursor_op(op)) return operrno("mdbx_cursor_get", ret) } -// getVal1 retrieves items from the database using key data for reference -// (Set, SetRange, etc). -// -// See mdb_cursor_get. -func (c *Cursor) getVal1(setkey []byte, op uint) error { - var k *C.char - if len(setkey) > 0 { - k = (*C.char)(unsafe.Pointer(&setkey[0])) - } - ret := C.mdbxgo_cursor_get1( - c._c, - k, C.size_t(len(setkey)), - &c.txn.key, - &c.txn.val, - C.MDBX_cursor_op(op), - ) - return operrno("mdbx_cursor_get", ret) -} -func (c *Cursor) getVal01(setval []byte, op uint) error { - var v *C.char - if len(setval) > 0 { - v = (*C.char)(unsafe.Pointer(&setval[0])) - } - ret := C.mdbxgo_cursor_get01( - c._c, - v, C.size_t(len(setval)), - &c.txn.key, - &c.txn.val, - C.MDBX_cursor_op(op), - ) - return operrno("mdbx_cursor_get", ret) -} - -// getVal2 retrieves items from the database using key and value data for +// getVal retrieves items from the database using key and value data for // reference (GetBoth, GetBothRange, etc). // // See mdb_cursor_get. -func (c *Cursor) getVal2(setkey, setval []byte, op uint) error { +func (c *Cursor) getVal(setkey, setval []byte, op uint) error { var k, v *C.char if len(setkey) > 0 { k = (*C.char)(unsafe.Pointer(&setkey[0])) From 250d2208f2e687452eec9bdca3b4d2ba2cbb0a3e Mon Sep 17 00:00:00 2001 From: Ilya Mikheev <54912776+JkLondon@users.noreply.github.com> Date: Tue, 18 Jun 2024 09:50:14 +0400 Subject: [PATCH 2/2] linter fixes + go version to 1.22 + added 1 more linter (#149) * linter fixes + go version to 1.22 + added 1 more linter * changed version to version in test.yml --------- Co-authored-by: Ilya Miheev --- .golangci.yml | 2 ++ Makefile | 5 +++-- go.mod | 2 +- mdbx/cursor_test.go | 8 ++++---- mdbx/env.go | 2 +- mdbx/error.go | 2 +- mdbx/mdbx.go | 2 +- 7 files changed, 13 insertions(+), 10 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index e05a1c9..559054e 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -27,6 +27,8 @@ linters: - depguard - typecheck - misspell + - bodyclose + - noctx linters-settings: gofmt: diff --git a/Makefile b/Makefile index f7630e3..5644f4f 100644 --- a/Makefile +++ b/Makefile @@ -15,11 +15,12 @@ race: go test -race ./mdbx ./exp/mdbxpool lint: - ./build/bin/golangci-lint run --new-from-rev=$(MASTER_COMMIT) ./... + ./build/bin/golangci-lint run ./... +#//--new-from-rev=$(MASTER_COMMIT) ./... lintci-deps: rm -f ./build/bin/golangci-lint - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ./build/bin v1.55.2 + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ./build/bin v1.59.1 clean: cd mdbxdist && make clean diff --git a/go.mod b/go.mod index 8ccd344..ab2cb53 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ module github.com/erigontech/mdbx-go -go 1.15 +go 1.20 require github.com/ianlancetaylor/cgosymbolizer v0.0.0-20240503222823-736c933a666d diff --git a/mdbx/cursor_test.go b/mdbx/cursor_test.go index f85733a..a05331f 100644 --- a/mdbx/cursor_test.go +++ b/mdbx/cursor_test.go @@ -790,7 +790,7 @@ func TestDupCursor_EmptyKeyValues1(t *testing.T) { if !bytes.Equal(v, []byte{}) { panic(fmt.Sprintf("%x", v)) } - k, v, err = cur.Get([]byte{}, nil, Set) + k, _, err = cur.Get([]byte{}, nil, Set) if err == nil { panic("expected 'not found' error") } @@ -865,7 +865,7 @@ func TestDupCursor_EmptyKeyValues2(t *testing.T) { if !bytes.Equal(v, []byte{}) { panic(fmt.Sprintf("%x", v)) } - k, v, err = cur.Get([]byte{}, nil, Set) + k, _, err = cur.Get([]byte{}, nil, Set) if err == nil { panic("expected 'not found' error") } @@ -969,7 +969,7 @@ func TestDupCursor_EmptyKeyValues3(t *testing.T) { if !bytes.Equal(v, []byte{}) { panic(fmt.Sprintf("%x", v)) } - k, v, err = cur.Get([]byte{}, nil, Set) + k, _, err = cur.Get([]byte{}, nil, Set) if err == nil { panic("expected 'not found' error") } @@ -1082,7 +1082,7 @@ func TestDupCursor_EmptyKeyValues(t *testing.T) { if !bytes.Equal(v, []byte{}) { panic(fmt.Sprintf("%x", v)) } - k, v, err = cur.Get([]byte{}, nil, Set) + k, _, err = cur.Get([]byte{}, nil, Set) if err == nil { panic("expected 'not found' error") } diff --git a/mdbx/env.go b/mdbx/env.go index 00296d9..b0d1477 100644 --- a/mdbx/env.go +++ b/mdbx/env.go @@ -287,7 +287,7 @@ type Stat struct { LeafPages uint64 // Number of leaf pages OverflowPages uint64 // Number of overflow pages Entries uint64 // Number of data items - LastTxId uint64 // Transaction ID of commited last modification + LastTxId uint64 // Transaction ID of committed last modification } // Stat returns statistics about the environment. diff --git a/mdbx/error.go b/mdbx/error.go index 3a3eba6..9932bf4 100644 --- a/mdbx/error.go +++ b/mdbx/error.go @@ -40,7 +40,7 @@ func (err *OpError) Error() string { // lmdb.IsErrnoFn(err, os.IsPermission) type Errno C.int -// The most common error codes do not need to be handled explicity. Errors can +// The most common error codes do not need to be handled explicitly. Errors can // be checked through helper functions IsNotFound, IsMapFull, etc, Otherwise // they should be checked using the IsErrno function instead of direct // comparison because they will typically be wrapped with an OpError. diff --git a/mdbx/mdbx.go b/mdbx/mdbx.go index 83cd918..43eba4d 100644 --- a/mdbx/mdbx.go +++ b/mdbx/mdbx.go @@ -109,7 +109,7 @@ they fail get garbage collected. Write transactions (those created without the Readonly flag) must be created in a goroutine that has been locked to its thread by calling the function -runtime.LockOSThread. Futhermore, all methods on such transactions must be +runtime.LockOSThread. Furthermore, all methods on such transactions must be called from the goroutine which created them. This is a fundamental limitation of LMDB even when using the NoTLS flag (which the package always uses). The Env.Update method assists the programmer by calling runtime.LockOSThread