Debian Codegraph @

lmdb/cursor.go

raw symbols SCIP
1 package lmdb
2
3 /*
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include "lmdb.h"
7 #include "lmdbgo.h"
8 */
9 import "C"
10 import (
11 "runtime"
12 "unsafe"
13 )
14
15 // These flags are used exclusively for Cursor.Get.
16 const (
17 // Flags for Cursor.Get
18 //
19 // See MDB_cursor_op.
20
21 First = C.MDB_FIRST // The first item.
22 FirstDup = C.MDB_FIRST_DUP // The first value of current key (DupSort).
23 GetBoth = C.MDB_GET_BOTH // Get the key as well as the value (DupSort).
24 GetBothRange = C.MDB_GET_BOTH_RANGE // Get the key and the nearsest value (DupSort).
25 GetCurrent = C.MDB_GET_CURRENT // Get the key and value at the current position.
26 GetMultiple = C.MDB_GET_MULTIPLE // Get up to a page dup values for key at current position (DupFixed).
27 Last = C.MDB_LAST // Last item.
28 LastDup = C.MDB_LAST_DUP // Position at last value of current key (DupSort).
29 Next = C.MDB_NEXT // Next value.
30 NextDup = C.MDB_NEXT_DUP // Next value of the current key (DupSort).
31 NextMultiple = C.MDB_NEXT_MULTIPLE // Get key and up to a page of values from the next cursor position (DupFixed).
32 NextNoDup = C.MDB_NEXT_NODUP // The first value of the next key (DupSort).
33 Prev = C.MDB_PREV // The previous item.
34 PrevDup = C.MDB_PREV_DUP // The previous item of the current key (DupSort).
35 PrevNoDup = C.MDB_PREV_NODUP // The last data item of the previous key (DupSort).
36 Set = C.MDB_SET // The specified key.
37 SetKey = C.MDB_SET_KEY // Get key and data at the specified key.
38 SetRange = C.MDB_SET_RANGE // The first key no less than the specified key.
39 )
40
41 // The MDB_MULTIPLE and MDB_RESERVE flags are special and do not fit the
42 // calling pattern of other calls to Put. They are not exported because they
43 // require special methods, PutMultiple and PutReserve in which the flag is
44 // implied and does not need to be passed.
45 const (
46 // Flags for Txn.Put and Cursor.Put.
47 //
48 // See mdb_put and mdb_cursor_put.
49
50 Current = C.MDB_CURRENT // Replace the item at the current key position (Cursor only)
51 NoDupData = C.MDB_NODUPDATA // Store the key-value pair only if key is not present (DupSort).
52 NoOverwrite = C.MDB_NOOVERWRITE // Store a new key-value pair only if key is not present.
53 Append = C.MDB_APPEND // Append an item to the database.
54 AppendDup = C.MDB_APPENDDUP // Append an item to the database (DupSort).
55 )
56
57 // Cursor operates on data inside a transaction and holds a position in the
58 // database.
59 //
60 // See MDB_cursor.
61 type Cursor struct {
62 txn *Txn
63 _c *C.MDB_cursor
64 }
65
66 func openCursor(txn *Txn, db DBI) (*Cursor, error) {
67 c := &Cursor{txn: txn}
68 ret := C.mdb_cursor_open(txn._txn, C.MDB_dbi(db), &c._c)
69 if ret != success {
70 return nil, operrno("mdb_cursor_open", ret)
71 }
72 return c, nil
73 }
74
75 // Renew associates readonly cursor with txn.
76 //
77 // See mdb_cursor_renew.
78 func (c *Cursor) Renew(txn *Txn) error {
79 ret := C.mdb_cursor_renew(txn._txn, c._c)
80 err := operrno("mdb_cursor_renew", ret)
81 if err != nil {
82 return err
83 }
84 c.txn = txn
85 return nil
86 }
87
88 func (c *Cursor) close() bool {
89 if c._c != nil {
90 if c.txn._txn == nil && !c.txn.readonly {
91 // the cursor has already been released by LMDB.
92 } else {
93 C.mdb_cursor_close(c._c)
94 }
95 c.txn = nil
96 c._c = nil
97 return true
98 }
99 return false
100 }
101
102 // Close the cursor handle and clear the finalizer on c. Cursors belonging to
103 // write transactions are closed automatically when the transaction is
104 // terminated.
105 //
106 // See mdb_cursor_close.
107 func (c *Cursor) Close() {
108 if c.close() {
109 runtime.SetFinalizer(c, nil)
110 }
111 }
112
113 // Txn returns the cursor's transaction.
114 func (c *Cursor) Txn() *Txn {
115 return c.txn
116 }
117
118 // DBI returns the cursor's database handle. If c has been closed than an
119 // invalid DBI is returned.
120 func (c *Cursor) DBI() DBI {
121 // dbiInvalid is an invalid DBI (the max value for the type). it shouldn't
122 // be possible to create a database handle with value dbiInvalid because
123 // the process address space would be exhausted. it is also impractical to
124 // have many open databases in an environment.
125 const dbiInvalid = ^DBI(0)
126
127 // mdb_cursor_dbi segfaults when passed a nil value
128 if c._c == nil {
129 return dbiInvalid
130 }
131 return DBI(C.mdb_cursor_dbi(c._c))
132 }
133
134 // Get retrieves items from the database. If c.Txn().RawRead is true the slices
135 // returned by Get reference readonly sections of memory that must not be
136 // accessed after the transaction has terminated.
137 //
138 // In a Txn with RawRead set to true the Set op causes the returned key to
139 // share its memory with setkey (making it writable memory). In a Txn with
140 // RawRead set to false the Set op returns key values with memory distinct from
141 // setkey, as is always the case when using RawRead.
142 //
143 // Get ignores setval if setkey is empty.
144 //
145 // See mdb_cursor_get.
146 func (c *Cursor) Get(setkey, setval []byte, op uint) (key, val []byte, err error) {
147 switch {
148 case len(setkey) == 0:
149 err = c.getVal0(op)
150 case len(setval) == 0:
151 err = c.getVal1(setkey, op)
152 default:
153 err = c.getVal2(setkey, setval, op)
154 }
155 if err != nil {
156 *c.txn.key = C.MDB_val{}
157 *c.txn.val = C.MDB_val{}
158 return nil, nil, err
159 }
160
161 // When MDB_SET is passed to mdb_cursor_get its first argument will be
162 // returned unchanged. Unfortunately, the normal slice copy/extraction
163 // routines will be bad for the Go runtime when operating on Go memory
164 // (panic or potentially garbage memory reference).
165 if op == Set {
166 if c.txn.RawRead {
167 key = setkey
168 } else {
169 p := make([]byte, len(setkey))
170 copy(p, setkey)
171 key = p
172 }
173 } else {
174 key = c.txn.bytes(c.txn.key)
175 }
176 val = c.txn.bytes(c.txn.val)
177
178 // Clear transaction storage record storage area for future use and to
179 // prevent dangling references.
180 *c.txn.key = C.MDB_val{}
181 *c.txn.val = C.MDB_val{}
182
183 return key, val, nil
184 }
185
186 // getVal0 retrieves items from the database without using given key or value
187 // data for reference (Next, First, Last, etc).
188 //
189 // See mdb_cursor_get.
190 func (c *Cursor) getVal0(op uint) error {
191 ret := C.mdb_cursor_get(c._c, c.txn.key, c.txn.val, C.MDB_cursor_op(op))
192 return operrno("mdb_cursor_get", ret)
193 }
194
195 // getVal1 retrieves items from the database using key data for reference
196 // (Set, SetRange, etc).
197 //
198 // See mdb_cursor_get.
199 func (c *Cursor) getVal1(setkey []byte, op uint) error {
200 ret := C.lmdbgo_mdb_cursor_get1(
201 c._c,
202 (*C.char)(unsafe.Pointer(&setkey[0])), C.size_t(len(setkey)),
203 c.txn.key, c.txn.val,
204 C.MDB_cursor_op(op),
205 )
206 return operrno("mdb_cursor_get", ret)
207 }
208
209 // getVal2 retrieves items from the database using key and value data for
210 // reference (GetBoth, GetBothRange, etc).
211 //
212 // See mdb_cursor_get.
213 func (c *Cursor) getVal2(setkey, setval []byte, op uint) error {
214 ret := C.lmdbgo_mdb_cursor_get2(
215 c._c,
216 (*C.char)(unsafe.Pointer(&setkey[0])), C.size_t(len(setkey)),
217 (*C.char)(unsafe.Pointer(&setval[0])), C.size_t(len(setval)),
218 c.txn.key, c.txn.val,
219 C.MDB_cursor_op(op),
220 )
221 return operrno("mdb_cursor_get", ret)
222 }
223
224 func (c *Cursor) putNilKey(flags uint) error {
225 ret := C.lmdbgo_mdb_cursor_put2(c._c, nil, 0, nil, 0, C.uint(flags))
226 return operrno("mdb_cursor_put", ret)
227 }
228
229 // Put stores an item in the database.
230 //
231 // See mdb_cursor_put.
232 func (c *Cursor) Put(key, val []byte, flags uint) error {
233 if len(key) == 0 {
234 return c.putNilKey(flags)
235 }
236 vn := len(val)
237 if vn == 0 {
238 val = []byte{0}
239 }
240 ret := C.lmdbgo_mdb_cursor_put2(
241 c._c,
242 (*C.char)(unsafe.Pointer(&key[0])), C.size_t(len(key)),
243 (*C.char)(unsafe.Pointer(&val[0])), C.size_t(len(val)),
244 C.uint(flags),
245 )
246 return operrno("mdb_cursor_put", ret)
247 }
248
249 // PutReserve returns a []byte of length n that can be written to, potentially
250 // avoiding a memcopy. The returned byte slice is only valid in txn's thread,
251 // before it has terminated.
252 func (c *Cursor) PutReserve(key []byte, n int, flags uint) ([]byte, error) {
253 if len(key) == 0 {
254 return nil, c.putNilKey(flags)
255 }
256
257 c.txn.val.mv_size = C.size_t(n)
258 ret := C.lmdbgo_mdb_cursor_put1(
259 c._c,
260 (*C.char)(unsafe.Pointer(&key[0])), C.size_t(len(key)),
261 c.txn.val,
262 C.uint(flags|C.MDB_RESERVE),
263 )
264 err := operrno("mdb_cursor_put", ret)
265 if err != nil {
266 *c.txn.val = C.MDB_val{}
267 return nil, err
268 }
269 b := getBytes(c.txn.val)
270 *c.txn.val = C.MDB_val{}
271 return b, nil
272 }
273
274 // PutMulti stores a set of contiguous items with stride size under key.
275 // PutMulti panics if len(page) is not a multiple of stride. The cursor's
276 // database must be DupFixed and DupSort.
277 //
278 // See mdb_cursor_put.
279 func (c *Cursor) PutMulti(key []byte, page []byte, stride int, flags uint) error {
280 if len(key) == 0 {
281 return c.putNilKey(flags)
282 }
283 if len(page) == 0 {
284 page = []byte{0}
285 }
286
287 vn := WrapMulti(page, stride).Len()
288 ret := C.lmdbgo_mdb_cursor_putmulti(
289 c._c,
290 (*C.char)(unsafe.Pointer(&key[0])), C.size_t(len(key)),
291 (*C.char)(unsafe.Pointer(&page[0])), C.size_t(vn), C.size_t(stride),
292 C.uint(flags|C.MDB_MULTIPLE),
293 )
294 return operrno("mdb_cursor_put", ret)
295 }
296
297 // Del deletes the item referred to by the cursor from the database.
298 //
299 // See mdb_cursor_del.
300 func (c *Cursor) Del(flags uint) error {
301 ret := C.mdb_cursor_del(c._c, C.uint(flags))
302 return operrno("mdb_cursor_del", ret)
303 }
304
305 // Count returns the number of duplicates for the current key.
306 //
307 // See mdb_cursor_count.
308 func (c *Cursor) Count() (uint64, error) {
309 var _size C.size_t
310 ret := C.mdb_cursor_count(c._c, &_size)
311 if ret != success {
312 return 0, operrno("mdb_cursor_count", ret)
313 }
314 return uint64(_size), nil
315 }