Documentation
¶
Overview ¶
Package lmdbscan provides a wrapper for lmdb.Cursor to simplify iteration. This package is experimental and it's API may change.
Index ¶
- type Scanner
- func (s *Scanner) Close()
- func (s *Scanner) Cursor() *lmdb.Cursor
- func (s *Scanner) Del(flags uint) error
- func (s *Scanner) Err() error
- func (s *Scanner) Key() []byte
- func (s *Scanner) Scan() bool
- func (s *Scanner) Set(k, v []byte, op uint)
- func (s *Scanner) SetNext(k, v []byte, opset, opnext uint)
- func (s *Scanner) Val() []byte
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Scanner ¶
type Scanner struct {
// contains filtered or unexported fields
}
Scanner is a low level construct for scanning databases inside a transaction.
Example ¶
This example demonstrates basic usage of a Scanner to scan the root database. It is important to always call scanner.Err() which will returned any unexpected error which interrupted scanner.Scan().
package main import ( "log" "github.com/bmatsuo/lmdb-go/exp/lmdbscan" "github.com/bmatsuo/lmdb-go/lmdb" ) var env *lmdb.Env func main() { err := env.View(func(txn *lmdb.Txn) (err error) { dbroot, _ := txn.OpenRoot(0) scanner := lmdbscan.New(txn, dbroot) defer scanner.Close() for scanner.Scan() { log.Printf("k=%q v=%q", scanner.Key(), scanner.Val()) } return scanner.Err() }) if err != nil { panic(err) } }
Output:
func (*Scanner) Close ¶
func (s *Scanner) Close()
Close closes the cursor underlying s and clears its ows internal structures. Close does not attempt to terminate the enclosing transaction.
Scan must not be called after Close.
func (*Scanner) Cursor ¶ added in v1.2.0
Cursor returns the lmdb.Cursor underlying s. Cursor returns nil if the scanner is closed.
func (*Scanner) Del ¶
Del will delete the key at the current cursor location.
Del is deprecated. Instead use s.Cursor().Del(flags).
func (*Scanner) Err ¶
Err returns a non-nil error if and only if the previous call to s.Scan() resulted in an error other than lmdb.ErrNotFound.
func (*Scanner) Scan ¶
Scan gets key-value successive pairs with the underlying cursor until one matches the supplied filters. If all filters return a nil error for the current pair, true is returned. Scan returns false if all key-value pairs where exhausted.
func (*Scanner) Set ¶
Set marks the starting position for iteration. On the next call to s.Scan() the underlying cursor will be moved as
c.Get(k, v, op)
Example ¶
This example demonstrates scanning a key range in the root database. Set is used to move the cursor's starting position to the desired prefix.
package main import ( "bytes" "log" "github.com/bmatsuo/lmdb-go/exp/lmdbscan" "github.com/bmatsuo/lmdb-go/lmdb" ) var env *lmdb.Env func main() { keyprefix := []byte("users:") err := env.View(func(txn *lmdb.Txn) (err error) { dbroot, _ := txn.OpenRoot(0) scanner := lmdbscan.New(txn, dbroot) defer scanner.Close() scanner.Set(keyprefix, nil, lmdb.SetRange) for scanner.Scan() { if !bytes.HasPrefix(scanner.Key(), keyprefix) { break } log.Printf("k=%q v=%q", scanner.Key(), scanner.Val()) } return scanner.Err() }) if err != nil { panic(err) } }
Output:
func (*Scanner) SetNext ¶
SetNext determines the cursor behavior for subsequent calls to s.Scan(). The immediately following call to s.Scan() behaves as if s.Set(k,v,opset) was called. Subsequent calls move the cursor as
c.Get(nil, nil, opnext)
Example ¶
This example demonstrates scanning all values for a key in a root database with the lmdb.DupSort flag set. SetNext is used instead of Set to configure Cursor the to return ErrNotFound (EOF) after all duplicate keys have been iterated.
package main import ( "log" "github.com/bmatsuo/lmdb-go/exp/lmdbscan" "github.com/bmatsuo/lmdb-go/lmdb" ) var env *lmdb.Env func main() { key := []byte("userphone:123") err := env.View(func(txn *lmdb.Txn) (err error) { dbroot, _ := txn.OpenRoot(0) scanner := lmdbscan.New(txn, dbroot) defer scanner.Close() scanner.SetNext(key, nil, lmdb.GetBothRange, lmdb.NextDup) for scanner.Scan() { log.Printf("k=%q v=%q", scanner.Key(), scanner.Val()) } return scanner.Err() }) if err != nil { panic(err) } }
Output: