File: cursor.go

package info (click to toggle)
golang-github-pzhin-go-sophia 0.0~git20180715.8bdc218-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 924 kB
  • sloc: ansic: 23,001; makefile: 4
file content (68 lines) | stat: -rw-r--r-- 1,549 bytes parent folder | download
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
package sophia

import (
	"errors"
	"unsafe"
)

// Order string type of sophia cursor order
type Order string

// Constants for sophia cursor order
// They are used while creating cursor to select it's direction
const (
	GreaterThan      Order = ">"
	GT               Order = GreaterThan
	GreaterThanEqual Order = ">="
	GTE              Order = GreaterThanEqual
	LessThan         Order = "<"
	LT               Order = LessThan
	LessThanEqual    Order = "<="
	LTE              Order = LessThanEqual
)

const (
	// CursorPrefix uses for setting cursor prefix
	CursorPrefix = "prefix"
	// CursorOrder uses for setting cursor order
	CursorOrder = "order"
)

// ErrCursorClosed will be returned in case of closed cursor usage
var ErrCursorClosed = errors.New("usage of closed Cursor")

// Cursor iterates over key-values in a database.
type Cursor struct {
	ptr    unsafe.Pointer
	doc    *Document
	closed bool
}

// Close closes the cursor. If a cursor is not closed, future operations
// on the database can hang indefinitely.
// Cursor won't be accessible after this
func (cur *Cursor) Close() error {
	if cur.closed {
		return ErrCursorClosed
	}
	cur.doc.Free()
	cur.closed = true
	if !spDestroy(cur.ptr) {
		return errors.New("cursor: failed to close")
	}
	return nil
}

// Next fetches the next row for the cursor
// Returns next row if it exists else it will return nil
func (cur *Cursor) Next() *Document {
	if cur.closed {
		return nil
	}
	ptr := spGet(cur.ptr, cur.doc.ptr)
	if ptr == nil {
		return nil
	}
	cur.doc.ptr = ptr
	return cur.doc
}