clerk

package module
v0.0.0-...-2fad0b3 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2018 License: MIT Imports: 7 Imported by: 0

README

clerk GoDoc Build Coverage Go Report Status Stable

Package clerk provides file persistence for your Go structures.

This can replace external databases for small projects that can keep data in memory and do few writes.

Installing

  1. Get package:

    go get -u github.com/arthurwhite/clerk
    
  2. Import it in your code:

    import "github.com/arthurwhite/clerk"
    

Usage

Init

Make a new database structure by embedding DB and initiate it with the destination file name used on saving.
Only exported fields can be saved.

Use DB.Rebase to retreive the data from an existent file at startup.

var db = new(struct {
	clerk.DB
	Data interface{}
})

func init() {
	clerk.Init("data.gob", db)
	if err := db.Rebase(); err != nil {
		panic(err)
	}
}
Modification

DB embeds a sync.RWMutex that should be used when accessing the data from multiple goroutines.

db.Lock()
defer db.Unlock()

db.Data = "one"
Saving

After a data change and while the concurrent access is still locked, use DB.Save to encode the source with gob and save it in the destination file:

if err := db.Save(); err != nil {
	panic(err)
}

Documentation

Overview

Package clerk provides file persistence for your Go structures.

It can replace external databases for small projects that can keep data in memory and don't make a lot of writes.

Example
package main

import "github.com/arthurwhite/clerk"

type User struct {
	Name string
}

var db = new(struct {
	clerk.DB
	Users []*User
})

func init() {
	clerk.Init("example.gob", db)
}

func main() {
	db.Lock()
	defer db.Unlock()

	db.Users = append(db.Users, &User{"Crowe"}, &User{"Jones"}, &User{"Owen"})

	if err := db.Save(); err != nil {
		panic(err)
	}
}

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrDBInited     = errors.New("clerk: database inited multiple times")
	ErrDBNotInited  = errors.New("clerk: database not inited")
	ErrOldMigration = errors.New("clerk: database in file is newer than migration timestamp")
)

Errors.

Functions

func Init

func Init(filename string, db DBInterface)

Init sets the filename for source saving.

Types

type DB

type DB struct {
	UpdatedAt time.Time // UpdatedAt is the timestamp of the last data modification (by calling DB.Lock).
	// contains filtered or unexported fields
}

DB provides methods to persist your data.

func (*DB) Lock

func (db *DB) Lock()

Lock locks database for reading and writing.

func (*DB) Migrate

func (db *DB) Migrate(timestamp string, oldDB DBInterface, do func() error) error

Migrate runs a migration if provided date is after the one registered in the data file. It rebases the database file in oldDB, runs function do and saves database. timestamp format is:

2006-01-02 15:04:05 -07

func (*DB) MigrateOrRebase

func (db *DB) MigrateOrRebase(timestamp string, oldDB DBInterface, do func() error)

MigrateOrRebase runs Migrate. If the migration has not been used, database is rebased. It panics on error.

func (*DB) RLock

func (db *DB) RLock()

RLock locks database for reading.

func (*DB) RUnlock

func (db *DB) RUnlock()

RUnlock unlocks database for reading.

func (*DB) Rebase

func (db *DB) Rebase() error

Rebase replaces the data with the content of the file set on init. Be sure the database is locked for writing.

func (*DB) Remove

func (db *DB) Remove() error

Remove deletes the database file.

func (*DB) Save

func (db *DB) Save() error

Save persists the database in the file set on init. Be sure the database is locked for writing.

func (*DB) Touch

func (db *DB) Touch()

Touch updates the UpdatedAt field.

func (*DB) Unlock

func (db *DB) Unlock()

Unlock unlocks database for reading and writing.

type DBInterface

type DBInterface interface {
	Touch()
	Save() error
	Migrate(string, DBInterface, func() error) error
	MigrateOrRebase(string, DBInterface, func() error)
	Rebase() error
	Remove() error
	Lock()
	RLock()
	Unlock()
	RUnlock()
	// contains filtered or unexported methods
}

DBInterface represents an underlying database.

Jump to

Keyboard shortcuts

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