forked from tpotlog/unqlitego
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cursor.go
201 lines (160 loc) · 4.76 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package unqlitego
// #include <unqlite.h>
// #include <wrappers.h>
// #include <stdlib.h>
import "C"
import (
"runtime"
"unsafe"
)
// Cursor represents an UnQLite database cursor.
type Cursor struct {
// Database Pointer
db *Database
// Cursor Handle
handle *C.unqlite_kv_cursor
}
// Cursor creates and initializes a new UnQLite database cursor.
func (db *Database) Cursor() (*Cursor, error) {
c := &Cursor{db: db}
res := C.unqlite_kv_cursor_init(db.conn, &c.handle)
if res != C.UNQLITE_OK {
return nil, UnQLiteError(res)
}
runtime.SetFinalizer(c, (*Cursor).Close)
return c, nil
}
// Close closes the open cursor.
func (cr *Cursor) Close() error {
if cr.db.conn != nil && cr.handle != nil {
res := C.unqlite_kv_cursor_release(cr.db.conn, cr.handle)
if res != C.UNQLITE_OK {
return UnQLiteError(res)
}
cr.handle = nil
}
return nil
}
// Seek will search the cursor for an exact match. If the record exists the cursor is left pointing to it.
// Otherwise it is left pointing to EOF and UNQLITE_NOTFOUND is returned.
func (cr *Cursor) Seek(key []byte) error {
var k unsafe.Pointer
if len(key) > 0 {
k = unsafe.Pointer(&key[0])
}
res := C.unqlite_kv_cursor_seek(cr.handle, k, C.int(len(key)), C.UNQLITE_CURSOR_MATCH_EXACT)
if res != C.UNQLITE_OK {
return UnQLiteError(res)
}
return nil
}
// SeekLE will search the cursor and left pointing to the largest key in the database that is smaller than (pKey/nKey).
// If the database contains no keys smaller than (pKey/nKey), the cursor is left at EOF.
// This option have sense only if the underlying key/value storage subsystem support range search (i.e: B+Tree, R+Tree, etc.).
// Otherwise this option is ignored and an exact match is performed.
func (cr *Cursor) SeekLE(key []byte) error {
var k unsafe.Pointer
if len(key) > 0 {
k = unsafe.Pointer(&key[0])
}
res := C.unqlite_kv_cursor_seek(cr.handle, k, C.int(len(key)), C.UNQLITE_CURSOR_MATCH_LE)
if res != C.UNQLITE_OK {
return UnQLiteError(res)
}
return nil
}
// SeekGE will search the cursor and left pointing to the smallest key in the database that is larger than (pKey/nKey).
// If the database contains no keys larger than (pKey/nKey), the cursor is left at EOF.
// This option have sense only if the underlying key/value storage subsystem support range search (i.e: B+Tree, R+Tree, etc.).
// Otherwise this option is ignored and an exact match is performed.
func (cr *Cursor) SeekGE(key []byte) error {
var k unsafe.Pointer
if len(key) > 0 {
k = unsafe.Pointer(&key[0])
}
res := C.unqlite_kv_cursor_seek(cr.handle, k, C.int(len(key)), C.UNQLITE_CURSOR_MATCH_GE)
if res != C.UNQLITE_OK {
return UnQLiteError(res)
}
return nil
}
// First returns the first entry of the cursor.
func (cr *Cursor) First() error {
res := C.unqlite_kv_cursor_first_entry(cr.handle)
if res != C.UNQLITE_OK {
return UnQLiteError(res)
}
return nil
}
// Last returns the last entry of the cursor.
func (cr *Cursor) Last() error {
res := C.unqlite_kv_cursor_last_entry(cr.handle)
if res != C.UNQLITE_OK {
return UnQLiteError(res)
}
return nil
}
// IsValid returns a boolean indicating if the cursor is valid.
func (cr *Cursor) IsValid() bool {
return C.unqlite_kv_cursor_valid_entry(cr.handle) == 1
}
// Next moves the cursor to the next entry.
func (cr *Cursor) Next() error {
res := C.unqlite_kv_cursor_next_entry(cr.handle)
if res != C.UNQLITE_OK {
return UnQLiteError(res)
}
return nil
}
// Prev moves the cursor the the previous entry.
func (cr *Cursor) Prev() error {
res := C.unqlite_kv_cursor_prev_entry(cr.handle)
if res != C.UNQLITE_OK {
return UnQLiteError(res)
}
return nil
}
// Delete current cursor entry.
func (cr *Cursor) Delete() error {
res := C.unqlite_kv_cursor_delete_entry(cr.handle)
if res != C.UNQLITE_OK {
return UnQLiteError(res)
}
return nil
}
// Reset the cursor.
func (cr *Cursor) Reset() error {
res := C.unqlite_kv_cursor_reset(cr.handle)
if res != C.UNQLITE_OK {
return UnQLiteError(res)
}
return nil
}
// Key returns the key at the current cursor location.
func (cr *Cursor) Key() (key []byte, err error) {
var n C.int
res := C.unqlite_kv_cursor_key(cr.handle, nil, &n)
if res != C.UNQLITE_OK {
return nil, UnQLiteError(res)
}
key = make([]byte, int(n))
res = C.unqlite_kv_cursor_key(cr.handle, unsafe.Pointer(&key[0]), &n)
if res != C.UNQLITE_OK {
return nil, UnQLiteError(res)
}
return
}
// Value returns the value at the current cursor position.
func (cr *Cursor) Value() (value []byte, err error) {
var n C.unqlite_int64
res := C.unqlite_kv_cursor_data(cr.handle, nil, &n)
if res != C.UNQLITE_OK {
return nil, UnQLiteError(res)
}
value = make([]byte, int(n))
res = C.unqlite_kv_cursor_data(cr.handle, unsafe.Pointer(&value[0]), &n)
if res != C.UNQLITE_OK {
return nil, UnQLiteError(res)
}
return
}