gokabinet: github.com/fsouza/gokabinet/kc Index | Files

package kc

import "github.com/fsouza/gokabinet/kc"

Package kc provides types, methods and functions to interact with an Kyoto Cabinet database.

It's just a binding to the Kyoto Cabinet C library, so make sure you have installed Kyoto Cabinet in your system.


Package Files

apply.go doc.go kc.go


const (

type ApplyFunc

type ApplyFunc func(key string, value interface{}, args ...interface{})

ApplyFunc, function used as parameter to the Apply and AsyncApply methods. The function receives three parameters:

key: the record key
value: the record value
args: extra arguments passed to Apply method

type ApplyResult

type ApplyResult struct {
    // contains filtered or unexported fields

func NewApplyResult

func NewApplyResult() *ApplyResult

func (*ApplyResult) Wait

func (r *ApplyResult) Wait()

type DB

type DB struct {

    // Path to the database file.
    Path string
    // contains filtered or unexported fields

DB is the basic type for the gokabinet library. Holds an unexported instance of the database, for interactions.

func Open

func Open(dbfilepath string, mode int) (*DB, error)

Open opens a database.

There are constants for the modes: READ and WRITE.

READ indicates read-only access to the database, and WRITE indicates read and write access to the database (there is no write-only mode).

func (*DB) Append

func (d *DB) Append(key, value string) error

Append appends a string to the end of the value of a string record. It can't append any value to a numeric record.

Returns a KCError instance when trying to append a string to a numeric record and when trying to append a string in read-only mode.

If the append is successful, the method returns nil.

func (*DB) Apply

func (d *DB) Apply(f ApplyFunc, args ...interface{})

Applies a function to all records in the database

The function is called with the key and the value as parameters. All extra arguments passed to Apply are used in the call to f

func (*DB) AsyncApply

func (d *DB) AsyncApply(f ApplyFunc, args ...interface{}) Waiter

Similar to Apply, but asynchronous. Returns a Waiter object, so you can wait for the applying to finish

func (*DB) BeginTransaction

func (d *DB) BeginTransaction(hard bool) error

BeginTransaction begins a new transaction. It accepts a boolean flag that indicates whether the transaction should be hard or not. A hard transaction is a transaction that provides physical synchronization in the device, while a non-hard transaction provides logical synchronization with the file system.

func (*DB) Clear

func (d *DB) Clear() error

Clear removes all records from the database.

Returns a KCError in case of failure.

func (*DB) Close

func (d *DB) Close()

Close closes the database, make sure you always call this method after using the database.

You can do it using the defer statement:

db := Open("my_db.kch", WRITE)
defer db.Close()

func (*DB) Commit

func (d *DB) Commit() error

Commit commits the current transaction.

func (*DB) CompareAndSwap

func (d *DB) CompareAndSwap(key, old, new string) error

CompareAndSwap performs a compare-and-swap operation, receiving three parameters: key, old and new.

If the value corresponding to key is equal to old, then it is set to new. If the operation fails, this method returns a non-nil error.

func (*DB) Count

func (d *DB) Count() (int, error)

Count returns the number of records in the database.

func (*DB) Get

func (d *DB) Get(key string) (string, error)

Get retrieves a record in the database by its key.

Returns the string value and nil in case of success, in case of errors, return a zero-valued string and an KCError instance (including when the key doesn't exist in the database).

func (*DB) GetGob

func (d *DB) GetGob(key string, e interface{}) error

GetGob gets a record in the database by its key, decoding from gob format.

Returns nil in case of success. In case of errors, it returns a KCError instance explaining what happened.

func (*DB) GetInt

func (d *DB) GetInt(key string) (int, error)

GetInt gets a numeric record from the database.

In case of errors (e.g.: when the given key refers to a non-numeric record), returns 0 and a KCError instance.

func (*DB) Increment

func (d *DB) Increment(key string, number int) (int, error)

Increment increments the value of a numeric record by a given number, and return the incremented value.

In case of errors, returns 0 and a KCError instance with detailed error message.

func (*DB) LastError

func (d *DB) LastError() error

LastError returns a KCError instance representing the last occurred error in the database.

func (*DB) MatchPrefix

func (d *DB) MatchPrefix(prefix string, max int64) ([]string, error)

MatchPrefix returns a list of keys that matches a prefix or an error in case of failure.

func (*DB) MatchRegex

func (d *DB) MatchRegex(regex string, max int64) ([]string, error)

MatchRegex returns a list of keys that matches a regular expression string or an error in case of failure.

func (*DB) Remove

func (d *DB) Remove(key string) error

Remove removes a record from the database by its key.

Returns a KCError instance if there is no record for the given key, or in case of other errors. The error instance contains a message describing what happened

func (*DB) Rollback

func (d *DB) Rollback() error

Rollback aborts the current transaction.

func (*DB) Set

func (d *DB) Set(key, value string) error

Set adds a record to the database. Currently, it's able to store only string values.

Returns a KCError instance in case of errors, otherwise, returns nil.

func (*DB) SetGob

func (d *DB) SetGob(key string, e interface{}) error

SetGob adds a record to the database, stored in gob format.

Returns a KCError instance in case of errors, otherwise, returns nil.

func (*DB) SetInt

func (d *DB) SetInt(key string, number int) error

SetInt defines the value of an integer record, creating it when there is no record corresponding to the given key.

Returns an KCError in case of errors setting the value.

func (*DB) Size

func (d *DB) Size() (int64, error)

Size returns the size of the database file.

type KCError

type KCError string

KCError is used for errors using the gokabinet library. It implements the builtin error interface.

func (KCError) Error

func (err KCError) Error() string

type Waiter

type Waiter interface {

Waiter interface, provides the Wait method

This is the interface of the value returned by the AsyncApply method

Package kc imports 7 packages (graph) and is imported by 5 packages. Updated 2014-03-17. Refresh now. Tools for package owners.