db

package
v0.1.0-beta Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2018 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoSuchKey is returned when Get() was passed a non-existant key
	ErrNoSuchKey = errors.New("This key does not exist")
)

Functions

func CopyKey

func CopyKey(db Database, src, dst []string) error

Types

type Batch

type Batch interface {
	// Put sets `val` at `key`.
	Put(val []byte, key ...string)

	// Clear all contents below and including `key`.
	Clear(key ...string)

	// Erase a key from the database.
	Erase(key ...string)

	// Flush the batch to the database.
	// Only now, all changes will be written to disk.
	Flush() error

	// Rollback will forget all changes without executing them.
	Rollback()
}

type Database

type Database interface {
	// Get retrievies the key `key` out of bucket.
	// If no such key exists, it will return (nil, ErrNoSuchKey)
	// If a badge is currently open, Get() shall still return the
	// most current value currently set by the last Put() call
	// to `key`.
	Get(key ...string) ([]byte, error)

	// Keys returns all keys
	// TODO: Make this look more like a iterator interface?
	Keys(fn func(key []string) error, prefix ...string) error

	// Batch returns a new Batch object, that will allow modifications
	// of the state. Batch() can be called recursive: The changes will
	// only be flushed to disk if batch.Flush() was called equal times
	// to the number Batch() was called.
	Batch() Batch

	// Export backups all database content to `w` in
	// an implemenation specific format that can be read by Import.
	Export(w io.Writer) error

	// Import reads a previously exported db dump by Export from `r`.
	// Existing keys might be overwritten if the dump also contains them.
	Import(r io.Reader) error

	// Close closes the database. Since I/O may happen, an error is returned.
	Close() error

	// Glob finds all existing keys in the store, starting with prefix.
	Glob(prefix []string) ([][]string, error)
}

Database is a key/value store that offers different buckets for storage. Keys are strings, values are arbitary untyped data. TODO: Write down assumptions made (single user database e.g.)

type DiskDatabase

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

DiskDatabase is a database that simply uses the filesystem as storage. Each bucket is one directory. Leaf keys are simple files. The exported form of the database is simply a gzipped .tar of the directory.

Note that this database backends was written for easy debugging. It is currently by no means optimized for fast reads and writes and could be probably made a lot faster if we ever need that.

func NewDiskDatabase

func NewDiskDatabase(basePath string) (*DiskDatabase, error)

NewDiskDatabase creates a new database at `basePath`.

func (*DiskDatabase) Batch

func (db *DiskDatabase) Batch() Batch

func (*DiskDatabase) Clear

func (db *DiskDatabase) Clear(key ...string)

Clear removes all keys below and including `key`.

func (*DiskDatabase) Close

func (db *DiskDatabase) Close() error

Close the database

func (*DiskDatabase) Erase

func (db *DiskDatabase) Erase(key ...string)

func (*DiskDatabase) Export

func (db *DiskDatabase) Export(w io.Writer) error

Export writes all key/values into a gzipped .tar that is written to `w`.

func (*DiskDatabase) Flush

func (db *DiskDatabase) Flush() error

func (*DiskDatabase) Get

func (db *DiskDatabase) Get(key ...string) ([]byte, error)

Get a single value from `bucket` by `key`.

func (*DiskDatabase) Glob

func (db *DiskDatabase) Glob(prefix []string) ([][]string, error)

func (*DiskDatabase) Import

func (db *DiskDatabase) Import(r io.Reader) error

Import a gzipped tar from `r` into the current database.

func (*DiskDatabase) Keys

func (db *DiskDatabase) Keys(fn func(key []string) error, prefix ...string) error

func (*DiskDatabase) Put

func (db *DiskDatabase) Put(val []byte, key ...string)

Put stores a new `val` under `key` at `bucket`. Implementation detail: `key` may contain slashes (/). If used, those keys will result in a nested directory structure.

func (*DiskDatabase) Rollback

func (db *DiskDatabase) Rollback()

type MemoryDatabase

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

MemoryDatabase is a purely in memory database.

func NewMemoryDatabase

func NewMemoryDatabase() *MemoryDatabase

NewMemoryDatabase allocates a new empty MemoryDatabase

func (*MemoryDatabase) Batch

func (mdb *MemoryDatabase) Batch() Batch

Batch is a no-op for a memory database.

func (*MemoryDatabase) Clear

func (mdb *MemoryDatabase) Clear(key ...string)

Clear removes all keys includin and below `key`.

func (*MemoryDatabase) Close

func (mdb *MemoryDatabase) Close() error

Close the memory - a no op.

func (*MemoryDatabase) Erase

func (mdb *MemoryDatabase) Erase(key ...string)

func (*MemoryDatabase) Export

func (mdb *MemoryDatabase) Export(w io.Writer) error

Export encodes the internal memory map to a gob structure, and writes it to `w`.

func (*MemoryDatabase) Flush

func (mdb *MemoryDatabase) Flush() error

Flush is a no-op for a memory database.

func (*MemoryDatabase) Get

func (mdb *MemoryDatabase) Get(key ...string) ([]byte, error)

Get returns `key` of `bucket`.

func (*MemoryDatabase) Glob

func (mdb *MemoryDatabase) Glob(prefix []string) ([][]string, error)

func (*MemoryDatabase) Import

func (mdb *MemoryDatabase) Import(r io.Reader) error

Import imports a previously exported dump and decodes the gob structure.

func (*MemoryDatabase) Keys

func (mdb *MemoryDatabase) Keys(fn func(key []string) error, prefix ...string) error

Keys will return all keys currently stored in the memory map

func (*MemoryDatabase) Put

func (mdb *MemoryDatabase) Put(data []byte, key ...string)

Put sets `key` in `bucket` to `data`.

func (*MemoryDatabase) Rollback

func (mdb *MemoryDatabase) Rollback()

Rollback is a no-op for a memory database

Jump to

Keyboard shortcuts

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