gdbm

package module
v0.0.0-...-023f8d3 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2013 License: MIT Imports: 2 Imported by: 0

README

go-gdbm

go-gdbm provides Go bindings for the GNU database manager. The go-gdbm project is licensed under the MIT license.

You can view the API documentation here.

Basic Usage

Open a database:

db, err := gdbm.Open("my-stuff.db")
if err != nil {
  panic(err)
}
defer db.Close()

Store some data:

db.Store([]byte("strawberry"), []byte("red"))
db.Store([]byte("banana"), []byte("yellow"))

To delete an entry, call Store with nil as the second parameter.

To retrieve data for a key:

value := db.Fetch([]byte("strawberry"))
if string(value) != "red" {
  panic("oh no!")
}

Documentation

Overview

Package gdbm provides support for the GNU database manager.

Index

Constants

View Source
const (
	// Open as a reader.
	Reader Mode = C.GDBM_READER
	// Open as a writer. The database must already exist.
	Writer = C.GDBM_WRITER
	// Open as a writer and create the database if it does not exist.
	Create = C.GDBM_WRCREAT
	// Open as a writer and create a new database regardless if one already exists.
	ForceCreate = C.GDBM_NEWDB
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Name of the database file.
	File string
	// Permission bits to use when creating a new database file.
	Perm int
	// How to open the database. See the documentation for the Mode type.
	Mode Mode
	// If true, all database operations will be synchronized to the disk.
	Sync bool
	// If true, no file locking will be performed on the database.
	DontLock bool
	// When creating new databases, BlockSize refers to the size of a single
	// transfer from disk to memory. The minimum value is 512. If BlockSize
	// is zero, then the file system default will be used.
	BlockSize int
	// CacheSize sets the size of the internal bucket cache. If zero, the cache size
	// will be set to 100.
	CacheSize int
}

type Database

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

func Open

func Open(file string) (*Database, error)

Opens a database with the default options. The database will be created if it does not exist (with permission bits 0666, before umask).

func OpenConfig

func OpenConfig(config *Config) (*Database, error)

Opens a database using the given configuration options.

func (*Database) Close

func (d *Database) Close()

Closes the database and releases all associated resources.

func (*Database) Exists

func (d *Database) Exists(key []byte) bool

Returns true if the specified key is found in the database.

func (*Database) Fetch

func (d *Database) Fetch(key []byte) (value []byte)

Returns the data associated with a given key, or nil if the key is not present in the database.

func (*Database) File

func (d *Database) File() string

Returns the name of the database file.

func (*Database) Iterate

func (d *Database) Iterate(callback func(key []byte) (cont bool))

Iterates through all the keys in the database. The callback will be called for each key. The callback should return true unless it wants to cancel the iteration. Keys will be traversed in an unspecified order.

func (*Database) Reorganize

func (d *Database) Reorganize()

Reorganizes the database file in order to reduce its size by reusing free space. This should be used very infrequently, and will only be useful after a lot of deletions have been made.

func (*Database) Store

func (d *Database) Store(key, data []byte)

Stores data for a specified key. Any existing data for the key will be replaced. If `data` is nil, then the key will be deleted if it exists in the database.

func (*Database) Sync

func (d *Database) Sync()

Synchronizes the database to disk. Sync will only return once the database has been physically written to the disk.

type Mode

type Mode int

Jump to

Keyboard shortcuts

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