berkeleydb

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2021 License: MIT Imports: 4 Imported by: 0

README

GoDoc Travis

BerkeleyDB Bindings

This package provides BerkeleyDB wrappers for the C library using cgo.

To build, you will need a relatively recent version of BerkeleyDB.

Example

package main

import (
        "fmt"

        "github.com/jsimonetti/berkeleydb"
)

func main() {
        var err error

        db, err := berkeleydb.NewDB()
        if err != nil {
                fmt.Printf("Unexpected failure of CreateDB %s\n", err)
        }

        err = db.Open("./test.db", "", berkeleydb.DbHash, berkeleydb.DbCreate)
        if err != nil {
                fmt.Printf("Could not open test_db.db. Error code %s", err)
                return
        }
        defer db.Close()

        err = db.Put([]byte("key"), byte[]("value"))
        if err != nil {
                fmt.Printf("Expected clean Put: %s\n", err)
        }

        value, err := db.Get([]byte("key"))
        if err != nil {
                fmt.Printf("Unexpected error in Get: %s\n", err)
                return
        }
        fmt.Printf("value: %s\n", string(value))
}

Documentation

Index

Constants

View Source
const (
	DbCreate   = C.DB_CREATE
	DbExcl     = C.DB_EXCL
	DbRdOnly   = C.DB_RDONLY
	DbTruncate = C.DB_TRUNCATE

	// DbInitMpool is used in environment only
	DbInitMpool = C.DB_INIT_MPOOL
)

Flags for opening a database or environment.

View Source
const (
	DbBtree   = C.DB_BTREE
	DbHash    = C.DB_HASH
	DbRecno   = C.DB_RECNO
	DbQueue   = C.DB_QUEUE
	DbUnknown = C.DB_UNKNOWN
)

Database types.

View Source
const (
	CrsFirst = C.DB_FIRST
	CrsNext  = C.DB_NEXT
	CrsPrev  = C.DB_PREV
	CrsLast  = C.DB_LAST
)

Cursor modes.

Variables

This section is empty.

Functions

func Version

func Version() string

Version returns the version of the database and binding

Types

type Cursor

type Cursor struct {
	// contains filtered or unexported fields
}

Cursor holds the current cursor position

func (*Cursor) Get added in v0.0.3

func (cursor *Cursor) Get(mode int) ([]byte, []byte, error)

Get [FIRST|NEXT|PREV|LAST] record from cursor and return the key/value pair

type DBError

type DBError struct {
	Code    int
	Message string
}

DBError contains the database Error

func (*DBError) Error

func (e *DBError) Error() string

Error return the string representation of the error

type Db

type Db struct {
	// contains filtered or unexported fields
}

Db is the structure that holds the database connection

func NewDB

func NewDB() (*Db, error)

NewDB initialises a new bdb connection

func NewDBInEnvironment

func NewDBInEnvironment(env *Environment) (*Db, error)

NewDBInEnvironment initialises a new bdb connection in an environment

func (*Db) Close

func (handle *Db) Close() error

Close the database file

func (*Db) Cursor

func (handle *Db) Cursor() (*Cursor, error)

Cursor returns a handle for the database cursor

func (*Db) Delete

func (handle *Db) Delete(key []byte) error

Delete a value from the database by key

func (*Db) Flags

func (handle *Db) Flags() (C.u_int32_t, error)

Flags returns the flags of the database connection

func (*Db) Get

func (handle *Db) Get(key []byte) ([]byte, error)

Get a value from the database by key

func (*Db) Open

func (handle *Db) Open(filename, dbname string, dbtype C.DBTYPE, flags C.u_int32_t) error

Open a database file

func (*Db) OpenWithTxn

func (handle *Db) OpenWithTxn(filename string, txn *C.DB_TXN, dbtype C.DBTYPE, flags C.u_int32_t) error

OpenWithTxn opens the database in transaction mode (transactions are not yet supported by all funtions)

func (*Db) Put

func (handle *Db) Put(key, value []byte) error

Put a key/value pair into the database

func (*Db) Remove

func (handle *Db) Remove(filename string) error

Remove the database

func (*Db) Rename

func (handle *Db) Rename(oldname, newname string) error

Rename the database filename

type Environment

type Environment struct {
	// contains filtered or unexported fields
}

Environment holds the database environment

func NewEnvironment

func NewEnvironment() (*Environment, error)

NewEnvironment creates a new database environment

func (*Environment) Close

func (env *Environment) Close() error

Close a database in the environment

func (*Environment) Open

func (env *Environment) Open(path string, flags C.u_int32_t, fileMode int) error

Open a database in the environment

type Errno

type Errno int

Errno is the error number

Jump to

Keyboard shortcuts

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