forked from szferi/gomdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursor.go
107 lines (95 loc) · 2.15 KB
/
cursor.go
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package mdb
/*
#cgo CFLAGS: -pthread -W -Wall -Wno-unused-parameter -Wbad-function-cast -O2 -g
#cgo freebsd CFLAGS: -DMDB_DSYNC=O_SYNC
#cgo openbsd CFLAGS: -DMDB_DSYNC=O_SYNC
#cgo netbsd CFLAGS: -DMDB_DSYNC=O_SYNC
#include <stdlib.h>
#include <stdio.h>
#include "lmdb.h"
*/
import "C"
import (
"errors"
)
// MDB_cursor_op
const (
FIRST = iota
FIRST_DUP
GET_BOTH
GET_RANGE
GET_CURRENT
GET_MULTIPLE
LAST
LAST_DUP
NEXT
NEXT_DUP
NEXT_MULTIPLE
NEXT_NODUP
PREV
PREV_DUP
PREV_NODUP
SET
SET_KEY
SET_RANGE
)
func (cursor *Cursor) Close() error {
if cursor._cursor == nil {
return errors.New("Cursor already closed")
}
C.mdb_cursor_close(cursor._cursor)
cursor._cursor = nil
return nil
}
func (cursor *Cursor) Txn() *Txn {
var _txn *C.MDB_txn
_txn = C.mdb_cursor_txn(cursor._cursor)
if _txn != nil {
return &Txn{_txn, cursor.nested}
}
return nil
}
func (cursor *Cursor) DBI() DBI {
var _dbi C.MDB_dbi
_dbi = C.mdb_cursor_dbi(cursor._cursor)
return DBI(_dbi)
}
// Retrieves the low-level MDB cursor.
func (cursor *Cursor) MdbCursor() *C.MDB_cursor {
return cursor._cursor
}
func (cursor *Cursor) Get(set_key, sval []byte, op uint) (key, val []byte, err error) {
ckey, cval, err := cursor.GetVal(set_key, sval, op)
defer ckey.Free()
defer cval.Free()
if err != nil {
return nil, nil, err
}
return ckey.Bytes(), cval.Bytes(), nil
}
func (cursor *Cursor) GetVal(key, val []byte, op uint) (*Val, *Val, error) {
ckey := Wrap(key)
cval := Wrap(val)
ret := C.mdb_cursor_get(cursor._cursor, (*C.MDB_val)(ckey), (*C.MDB_val)(cval), C.MDB_cursor_op(op))
return ckey, cval, errno(ret)
}
func (cursor *Cursor) Put(key, val []byte, flags uint) error {
ckey := Wrap(key)
cval := Wrap(val)
defer ckey.Free()
defer cval.Free()
ret := C.mdb_cursor_put(cursor._cursor, (*C.MDB_val)(ckey), (*C.MDB_val)(cval), C.uint(flags))
return errno(ret)
}
func (cursor *Cursor) Del(flags uint) error {
ret := C.mdb_cursor_del(cursor._cursor, C.uint(flags))
return errno(ret)
}
func (cursor *Cursor) Count() (uint64, error) {
var _size C.mdb_size_t
ret := C.mdb_cursor_count(cursor._cursor, &_size)
if ret != SUCCESS {
return 0, errno(ret)
}
return uint64(_size), nil
}