Skip to content

Commit 7a11834

Browse files
ErrNotFound pre-alloc (#113)
1 parent 447919a commit 7a11834

5 files changed

Lines changed: 19 additions & 11 deletions

File tree

mdbx/cursor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func TestCursor_Get_KV(t *testing.T) {
250250
}
251251

252252
_, _, err = cur.Get([]byte("k0"), []byte("v0"), GetBothRange)
253-
if !IsErrno(err, NotFound) {
253+
if !IsNotFound(err) {
254254
t.Errorf("unexpected error: %s", err)
255255
}
256256

mdbx/error.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package mdbx
88
import "C"
99

1010
import (
11+
"fmt"
1112
"os"
1213
"syscall"
1314
)
@@ -33,10 +34,10 @@ func (err *OpError) Error() string {
3334
// Most often helper functions such as IsNotFound may be used instead of
3435
// dealing with Errno values directly.
3536
//
36-
// lmdb.IsNotFound(err)
37-
// lmdb.IsErrno(err, lmdb.TxnFull)
38-
// lmdb.IsErrnoSys(err, syscall.EINVAL)
39-
// lmdb.IsErrnoFn(err, os.IsPermission)
37+
// lmdb.IsNotFound(err)
38+
// lmdb.IsErrno(err, lmdb.TxnFull)
39+
// lmdb.IsErrnoSys(err, syscall.EINVAL)
40+
// lmdb.IsErrnoFn(err, os.IsPermission)
4041
type Errno C.int
4142

4243
// The most common error codes do not need to be handled explicity. Errors can
@@ -76,6 +77,8 @@ const (
7677
// other values may still be produced.
7778
const minErrno, maxErrno C.int = C.MDBX_KEYEXIST, C.MDBX_LAST_ADDED_ERRCODE
7879

80+
var ErrNotFound = fmt.Errorf("key not found")
81+
7982
// App can re-define this messages from init() func
8083
var CorruptErrorHardwareRecommendations = "Maybe free space is over on disk. Otherwise it's hardware failure. Before creating issue please use tools like https://www.memtest86.com to test RAM and tools like https://www.smartmontools.org to test Disk. To handle hardware risks: use ECC RAM, use RAID of disks, run multiple application instances (or do backups). If hardware checks passed - check FS settings - 'fsync' and 'flock' must be enabled. "
8184
var CorruptErrorBacktraceRecommendations = "Otherwise - please create issue in Application repo." // with backtrace or coredump. To create coredump set compile option 'MDBX_FORCE_ASSERTIONS=1' and env variable 'GOTRACEBACK=crash'."
@@ -84,8 +87,9 @@ var CorruptErrorMessage = CorruptErrorHardwareRecommendations + " " + CorruptErr
8487

8588
func (e Errno) Error() string {
8689
if e == Corrupted {
87-
return "MDBX_CORRUPTED: " + CorruptErrorMessage
88-
} else if e == Panic {
90+
return "MDBX_FATAL: " + CorruptErrorMessage
91+
}
92+
if e == Panic {
8993
return "MDBX_PANIC: " + CorruptErrorMessage
9094
}
9195
return C.GoString(C.mdbx_strerror(C.int(e)))
@@ -99,9 +103,7 @@ func _operrno(op string, ret int) error {
99103
// IsNotFound returns true if the key requested in Txn.Get or Cursor.Get does
100104
// not exist or if the Cursor reached the end of the database without locating
101105
// a value (EOF).
102-
func IsNotFound(err error) bool {
103-
return IsErrno(err, NotFound)
104-
}
106+
func IsNotFound(err error) bool { return err == ErrNotFound } //nolint
105107

106108
func IsKeyExists(err error) bool {
107109
return IsErrno(err, KeyExist)

mdbx/error_unix.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ func operrno(op string, ret C.int) error {
1515
if ret == C.MDBX_SUCCESS || ret == C.MDBX_RESULT_TRUE {
1616
return nil
1717
}
18+
if ret == C.MDBX_NOTFOUND {
19+
return ErrNotFound
20+
}
1821
if minErrno <= ret && ret <= maxErrno {
1922
return &OpError{Op: op, Errno: Errno(ret)}
2023
}

mdbx/error_windows.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ func operrno(op string, ret C.int) error {
1111
if ret == C.MDBX_SUCCESS || ret == C.MDBX_RESULT_TRUE {
1212
return nil
1313
}
14+
if ret == C.MDBX_NOTFOUND {
15+
return ErrNotFound
16+
}
1417
if minErrno <= ret && ret <= maxErrno {
1518
return &OpError{Op: op, Errno: Errno(ret)}
1619
}

mdbx/txn_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ func TestTxn_OpenDBI_emptyName(t *testing.T) {
399399
_, err = txn.OpenDBISimple("", 0)
400400
return err
401401
})
402-
if !IsErrno(err, NotFound) {
402+
if !IsNotFound(err) {
403403
t.Errorf("mdb_dbi_open: %v", err)
404404
}
405405

0 commit comments

Comments
 (0)