Skip to content

Commit c89c5fe

Browse files
JkLondonJkLondon
andauthored
FD reviving (#200)
* FD reviving * FD reviving * FD reviving * FD reviving --------- Co-authored-by: JkLondon <me@ilyamikheev.com>
1 parent f8885bc commit c89c5fe

File tree

4 files changed

+54
-31
lines changed

4 files changed

+54
-31
lines changed

mdbx/env.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -173,34 +173,6 @@ func (env *Env) SetStrictThreadMode(mode bool) {
173173

174174
var errNotOpen = errors.New("enivornment is not open")
175175

176-
/* TODO: fix error: cannot convert *mf (variable of type _Ctype_HANDLE) to type uintptr
177-
178-
// FD returns the open file descriptor (or Windows file handle) for the given
179-
// environment. An error is returned if the environment has not been
180-
// successfully Opened (where C API just retruns an invalid handle).
181-
//
182-
// See mdbx_env_get_fd.
183-
func (env *Env) FD() (uintptr, error) {
184-
// fdInvalid is the value -1 as a uintptr, which is used by MDBX in the
185-
// case that env has not been opened yet. the strange construction is done
186-
// to avoid constant value overflow errors at compile time.
187-
const fdInvalid = ^uintptr(0)
188-
189-
mf := new(C.mdbx_filehandle_t)
190-
ret := C.mdbx_env_get_fd(env._env, mf)
191-
err := operrno("mdbx_env_get_fd", ret)
192-
if err != nil {
193-
return 0, err
194-
}
195-
fd := uintptr(*mf)
196-
197-
if fd == fdInvalid {
198-
return 0, errNotOpen
199-
}
200-
return fd, nil
201-
}
202-
*/
203-
204176
// ReaderList dumps the contents of the reader lock table as text. Readers
205177
// start on the second line as space-delimited fields described by the first
206178
// line.

mdbx/env_not_win.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,28 @@ import "C"
2424
// }
2525
// return C.GoString(cpath), nil
2626
//}
27+
28+
// FD returns the open file descriptor (or Windows file handle) for the given
29+
// environment. An error is returned if the environment has not been
30+
// successfully Opened (where C API just retruns an invalid handle).
31+
//
32+
// See mdbx_env_get_fd.
33+
func (env *Env) FD() (uintptr, error) {
34+
// fdInvalid is the value -1 as a uintptr, which is used by MDBX in the
35+
// case that env has not been opened yet. the strange construction is done
36+
// to avoid constant value overflow errors at compile time.
37+
const fdInvalid = ^uintptr(0)
38+
39+
mf := new(C.mdbx_filehandle_t)
40+
ret := C.mdbx_env_get_fd(env._env, mf)
41+
err := operrno("mdbx_env_get_fd", ret)
42+
if err != nil {
43+
return 0, err
44+
}
45+
fd := uintptr(*mf)
46+
47+
if fd == fdInvalid {
48+
return 0, errNotOpen
49+
}
50+
return fd, nil
51+
}

mdbx/env_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package mdbx
22

33
import (
44
"fmt"
5+
"runtime"
6+
"strings"
57
"testing"
68
)
79

@@ -99,12 +101,11 @@ func TestEnv_PreOpen(t *testing.T) {
99101

100102
}
101103

102-
/*
103104
func TestEnv_FD(t *testing.T) {
104105
if runtime.GOOS == "windows" {
105106
t.Skip("FD funcs not supported on windows")
106107
}
107-
env, err1 := NewEnv()
108+
env, err1 := NewEnv(Default)
108109
if err1 != nil {
109110
t.Error(err1)
110111
return
@@ -131,7 +132,6 @@ func TestEnv_FD(t *testing.T) {
131132
t.Errorf("fd: %x", fd)
132133
}
133134
}
134-
*/
135135

136136
func TestEnv_Flags(t *testing.T) {
137137
env, _ := setup(t)

mdbx/env_windows.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package mdbx
44
#include "mdbxgo.h"
55
*/
66
import "C"
7+
import "unsafe"
78

89
// TODO: fix me please
910
//func (env *Env) Path() (string, error) {
@@ -17,3 +18,28 @@ import "C"
1718
// }
1819
// return C.GoString(cpath), nil
1920
//}
21+
22+
// FD returns the open file descriptor (or Windows file handle) for the given
23+
// environment. An error is returned if the environment has not been
24+
// successfully Opened (where C API just retruns an invalid handle).
25+
//
26+
// See mdbx_env_get_fd.
27+
func (env *Env) FD() (uintptr, error) {
28+
// fdInvalid is the value -1 as a uintptr, which is used by MDBX in the
29+
// case that env has not been opened yet. the strange construction is done
30+
// to avoid constant value overflow errors at compile time.
31+
const fdInvalid = ^uintptr(0)
32+
33+
var fh C.mdbx_filehandle_t
34+
ret := C.mdbx_env_get_fd(env._env, &fh)
35+
err := operrno("mdbx_env_get_fd", ret)
36+
if err != nil {
37+
return 0, err
38+
}
39+
fd := uintptr(unsafe.Pointer(fh))
40+
41+
if fd == fdInvalid {
42+
return 0, errNotOpen
43+
}
44+
return fd, nil
45+
}

0 commit comments

Comments
 (0)