GobDB

package module
v0.0.0-...-9ca1cc2 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2014 License: MIT Imports: 8 Imported by: 0

README

Overview

GobDB is a simple database optimized for convenience in Go. It wraps leveldb to provide persistant key-value storage of gob-compatible types.

Sample Usage

Setup a database and assign a local data file.

db := GobDB.At("example")
db.Open()

Insert persistently key-value pairs. We use strings here, but all gob-compatible values are supported.

db.Put("name", "adam")

Fetch values of key-value pairs. Note that you must provide a pointer of the correct type.

var value string = ""
db.Get("name", &value)

Check if keys are contained within the database.

db.Has("name")
db.Has("3234") 

Close the database and write changes to disk.

db.Close()

Installation

go get github.com/dasmithii/GobDB
go test github.com/dasmithii/GobDB

Documentation

Overview

Package GobDB implements a persistant key-value store of gob-compatible types. This is accomplished with a light wrapper around leveldb and Go's gob encoding library.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

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

DB is a LevelDB wrapper that stores key-value pairs of gob-compatible types.

func At

func At(path string) *DB

At returns an unopened database at with given datafile.

func (*DB) Close

func (db *DB) Close()

Close tears down the internal leveldb, writing all contents to file.

func (*DB) Compact

func (db *DB) Compact() error

Compact performs leveldb.CompactRange on the range of key-value mappings maintained by GobDB. Pairs outside the 'GobDB:' prefix aren't affected.

func (*DB) Delete

func (db *DB) Delete(key interface{}) error

Delete encodes given key via gob, deleting the resulting byte slice from the database's internal leveldb.

func (*DB) Entries

func (db *DB) Entries() int

Entries counts key-value pairs in the database. This includes only pairs written through GobDB.Put.

func (*DB) Get

func (db *DB) Get(key, value interface{}) error

Get ncodes given key via gob, fetches the corresponding value from within leveldb, and decodes that value into parameter two.

func (DB) Has

func (db DB) Has(key interface{}) bool

Has encodes given key via gob and checks if the resulting byte slice exists in the database's internal leveldb.

func (*DB) Internal

func (db *DB) Internal() *leveldb.DB

Internal opens and fetches the underlying leveldb. Clients may use this to perform direct writing of byte slices, or to access leveldb APIs left out of this wrapper.

Note: GobDB stores its mappings in the prefix "GobDB:", so that prefix should be avoided.

Note: closing the parent GobDB will invalidate the returned value of this function.

func (DB) IsOpen

func (db DB) IsOpen() bool

IsOpen checks whether or not the database is open.

func (*DB) Open

func (db *DB) Open() error

Open sets up the internal leveldb if not done already.

func (*DB) Put

func (db *DB) Put(key, value interface{}) error

Put encodes given key and value through gob, inserting resulting byte slices into the database's internal leveldb.

func (*DB) Reset

func (db *DB) Reset()

Reset erases caches and closes leveldb. This way, the db is forced to reload gobbed values as though it had just been opened for the first time.

type Decoder

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

Decoder provides a goroutine-safe method of decoding gobbed objects individually. Assumes that gob type definitions are registered in the necessary order.

NOTE: Decoder pointers to the same address will be forced to use mutex locks when utilized concurrently. On the other hand, if you copy a decoder itself, the two copies may be interacted with concurrently and lock-free.

func (*Decoder) Decode

func (d *Decoder) Decode(data []byte, address interface{}) error

Decode attempts to decode the given data, placing results into <address> if no errors occur. New types are registered implicitly, but they must be used in correct order.

func (*Decoder) Register

func (d *Decoder) Register(data []byte) error

Register informs internal decoder of the given gob type definition and its example object.

type FilteredEncoder

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

FilteredEncoder wraps gob.Encoder in such a way to retain all of gobs internal type definitions, and to return byte slices of type-value pairs without any additional data.

Returns bytes of encoded objects, writes only type definitions and uniquely-typed objects (i.e. one object of each type).

func (*FilteredEncoder) Encode

func (f *FilteredEncoder) Encode(e interface{}) ([]byte, []byte, error)

Encode performs gob.Encode twice, compares the two encodings to deduce whether or not the value had been encoded before. If it has the gob type definition is returned in position one. Regardless, the actual encoded value (without typedef bytes) is returned in position two.

Jump to

Keyboard shortcuts

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