Debian Codegraph @

lmdbscan/scanner.go

raw symbols SCIP
1 /*
2 Package lmdbscan provides a wrapper for lmdb.Cursor to simplify iteration.
3 */
4 package lmdbscan
5
6 import (
7 "fmt"
8
9 "github.com/bmatsuo/lmdb-go/lmdb"
10 )
11
12 // errClosed is an error returned to the user when attempting to operate on a
13 // closed Scanner.
14 var errClosed = fmt.Errorf("scanner is closed")
15
16 // Scanner is a low level construct for scanning databases inside a
17 // transaction.
18 type Scanner struct {
19 dbi lmdb.DBI
20 cur *lmdb.Cursor
21 op uint
22 key []byte
23 val []byte
24 err error
25 set bool
26 }
27
28 // New allocates and intializes a Scanner for dbi within txn. When the Scanner
29 // returned by New is no longer needed its Close method must be called.
30 func New(txn *lmdb.Txn, dbi lmdb.DBI) *Scanner {
31 s := &Scanner{
32 dbi: dbi,
33 op: lmdb.Next,
34 }
35
36 s.cur, s.err = txn.OpenCursor(dbi)
37 return s
38 }
39
40 // Cursor returns the lmdb.Cursor underlying s. Cursor returns nil if s is
41 // closed.
42 func (s *Scanner) Cursor() *lmdb.Cursor {
43 return s.cur
44 }
45
46 // Del will delete the key at the current cursor location.
47 //
48 // Del is deprecated. Instead use s.Cursor().Del(flags).
49 func (s *Scanner) Del(flags uint) error {
50 if s.cur == nil {
51 return errClosed
52 }
53 return s.cur.Del(flags)
54 }
55
56 // Key returns the key read during the last call to Scan.
57 func (s *Scanner) Key() []byte {
58 return s.key
59 }
60
61 // Val returns the value read during the last call to Scan.
62 func (s *Scanner) Val() []byte {
63 return s.val
64 }
65
66 // Set moves the cursor with s.Cursor().Get(k, v, opset), and sets s.Key(),
67 // s.Val(), and s.Err() accordingly. The cursor will not move in the next call
68 // to Scan.
69 func (s *Scanner) Set(k, v []byte, opset uint) bool {
70 if !s.checkOpen() {
71 return false
72 }
73 s.set = true
74 s.key, s.val, s.err = s.cur.Get(k, v, opset)
75 return s.err == nil
76 }
77
78 // SetNext moves the cursor like s.Set(k, v, opset) for the next call to
79 // s.Scan(). Subsequent calls to s.Scan() move the cursor as c.Get(nil, nil,
80 // opnext)
81 func (s *Scanner) SetNext(k, v []byte, opset, opnext uint) bool {
82 if !s.checkOpen() {
83 return false
84 }
85 ok := s.Set(k, v, opset)
86 s.op = opnext
87 return ok
88 }
89
90 // Scan gets successive key-value pairs using the underlying cursor. Scan
91 // returns false when key-value pairs are exhausted or another error is
92 // encountered.
93 func (s *Scanner) Scan() bool {
94 if !s.checkOpen() {
95 return false
96 }
97 if s.set {
98 s.set = false
99 } else {
100 s.key, s.val, s.err = s.cur.Get(nil, nil, s.op)
101 }
102 return s.err == nil
103 }
104
105 func (s *Scanner) checkOpen() bool {
106 if s.cur != nil {
107 return true
108 }
109 if s.err == nil {
110 s.err = errClosed
111 }
112 return false
113 }
114
115 // Err returns a non-nil error if and only if the previous call to s.Scan()
116 // resulted in an error other than lmdb.ErrNotFound.
117 func (s *Scanner) Err() error {
118 if lmdb.IsNotFound(s.err) {
119 return nil
120 }
121 return s.err
122 }
123
124 // Close closes the cursor underlying s and clears its ows internal structures.
125 // Close does not attempt to terminate the enclosing transaction.
126 //
127 // Scan must not be called after Close.
128 func (s *Scanner) Close() {
129 if s.cur != nil {
130 s.cur.Close()
131 s.cur = nil
132 }
133 }