jsondb

package module
v0.0.0-...-c427f81 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2013 License: MIT Imports: 7 Imported by: 0

README

jsondb

Very dumb, stupid, crappy JSON-based file database. No gaurentees for performance, correctness, robustness or usability given.

Install

go get github.com/jagregory/jsondb

Usage

db := jsondb.New("./data", func() string {
  return generateNextUuid()
})

var entry Foo
db.Read("abcd", &foo)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsNotFound

func IsNotFound(err error) bool

Types

type Entry

type Entry interface {
	// Assigns a generated id for a new entry, or assigns
	// the existing id when an entry is read
	AssignId(id string)

	// Sets the created time for a new entry
	Created(at time.Time)

	// Sets the modified time when an entry is updated
	Modified(at time.Time)
}

A database entry representation needs to implement this interface.

type IdGenerator

type IdGenerator func() string

func for generating a new identifier. Use your uuid of choice

type JsonDb

type JsonDb interface {
	// Create a new entry and write it to the database. Returns
	// the newly assigned id or an error.
	Create(entry Entry) (string, error)

	// Delete an existing entry. Will return NotFoundError if the
	// record isn't in the database.
	Delete(id string) error

	// Read an entry from the database. Returns a NotFoundError
	// if an entry with the supplied id can't be found. Will
	// also return any serialisation errors if they occur.
	Read(id string, entry Entry) error

	// Update an existing entry. Will return NotFoundError if the
	// record isn't in the database.
	Update(id string, entry Entry) error
	// contains filtered or unexported methods
}

func Cache

func Cache(db JsonDb) JsonDb

Create a memory caching wrapper around a JsonDb. Unlike an uncached JsonDb, a cache won't be able to deal with other processes changing the underlying files. Don't use if you've got that scenario.

func New

func New(dir string, newid IdGenerator) JsonDb

Create a new database client pointed at a directory. Will create the directory if it doesn't exist.

type NotFoundError

type NotFoundError struct {
	EntityId string
}

func (NotFoundError) Error

func (err NotFoundError) Error() string

type Scanner

type Scanner interface {
	// Scan to see if there's another record after the
	// current one
	Scan() bool

	// Read the current record, see Db.Read for
	// error behaviour.
	Read(entry Entry) error

	// The length of the underlying dataset
	Length() int
}

Database record scanner. Used for iterating over all the records in a database.

func NewScanner

func NewScanner(db JsonDb) (Scanner, error)

Jump to

Keyboard shortcuts

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