-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconnection.go
More file actions
71 lines (55 loc) · 1.6 KB
/
connection.go
File metadata and controls
71 lines (55 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package wtgo
// Much of design inspired by @jmhodges' levigo
/*
#cgo LDFLAGS: -lwiredtiger
#include <wiredtiger.h>
#include <stdlib.h>
// Wrappers for calling WT methods accessed through function pointers
int wt_connection_close(WT_CONNECTION* conn,
const char* config) {
return conn->close(conn, config);
}
int wt_connection_open_session(WT_CONNECTION *conn,
WT_EVENT_HANDLER *errhandler,
const char* config,
WT_SESSION **session) {
return conn->open_session(conn, errhandler, config, session);
}
*/
import "C"
import (
"unsafe"
)
type Connection struct {
WTConnection *C.WT_CONNECTION
}
// TODO: better API for all configuration options
func Open(home, config string) (*Connection, error) {
wthome := C.CString(home)
wtconfig := C.CString(config)
defer C.free(unsafe.Pointer(wthome))
defer C.free(unsafe.Pointer(wtconfig))
var wtconn *C.WT_CONNECTION
wterr := C.wiredtiger_open(wthome, nil, wtconfig, &wtconn)
if wterr != 0 {
return nil, &WTError{wterr}
}
return &Connection{wtconn}, nil
}
func (conn *Connection) Close() error {
wterr := C.wt_connection_close(conn.WTConnection, nil)
if wterr != 0 {
return &WTError{wterr}
}
return nil
}
func (conn *Connection) OpenSession(config string) (*Session, error) {
wtconfig := C.CString(config)
defer C.free(unsafe.Pointer(wtconfig))
var wtsess *C.WT_SESSION
wterr := C.wt_connection_open_session(conn.WTConnection, nil, wtconfig, &wtsess)
if wterr != 0 {
return nil, &WTError{wterr}
}
return &Session{wtsess}, nil
}