lmdbscan

package
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2015 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package lmdbscan provides a wrapper for lmdb.Cursor to simplify iteration. This package is experimental and it's API may change.

Index

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 New

func New(txn *lmdb.Txn, dbi lmdb.DBI) *Scanner

New allocates and intializes a Scanner for dbi within txn.

func (*Scanner) Close

func (s *Scanner) Close()

Close clears internal structures. Close does not attempt to terminate the enclosing transaction.

Scan must not be called after Close.

func (*Scanner) Del

func (s *Scanner) Del(flags uint) error

Del will delete the key at the current cursor location.

func (*Scanner) Err

func (s *Scanner) Err() error

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) Key

func (s *Scanner) Key() []byte

Key returns the key read during the last call to Scan.

func (*Scanner) Scan

func (s *Scanner) Scan() bool

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

func (s *Scanner) Set(k, v []byte, op uint)

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

func (s *Scanner) SetNext(k, v []byte, opset, opnext uint)

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:

func (*Scanner) Val

func (s *Scanner) Val() []byte

Val returns the value read during the last call to Scan.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL