harebrain

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2025 License: MIT Imports: 8 Imported by: 1

README

Harebrain

Harebrain is a simple file-backed db with the following characteristics:

  • every record is a file
  • every record must know how to marshal and unmarshal itself
  • every record has a Hash() function that produces a unique hash, which becomes the filename.

Getting started


import (
    "github.com/sean9999/harebrain"
)

type cat struct {
	Id    int
	Name  string
	Breed string
}

//  if you don't need a bespoke Hash() function, you can use [JsonRecord]
type catRecord = harebrain.JsonRecord[cat]

func main(){

    myCat := &cat{1,"Muffin","Calico"}

    db := harebrain.NewDatabase()
    db.Open("data")
    db.Table("cats").Insert(myCat)

}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrDatabase = fmt.Errorf("%w: database", ErrHareBrain)
View Source
var ErrHareBrain = &Herror{msg: "harebrained"}
View Source
var ErrNoSuchRecord = errors.New("no such record")
View Source
var ErrNoSuchTable = errors.New("no such table")

Functions

This section is empty.

Types

type Database

type Database struct {
	Folder     string
	Filesystem afero.IOFS
}

A Database is a root folder that acts as a container for [Table]s.

func NewDatabase

func NewDatabase() *Database

func (*Database) Open

func (db *Database) Open(folder string) error

func (*Database) Table

func (db *Database) Table(name string) *Table

type EncodeHasher

type EncodeHasher interface {
	Hash() string // unique
	encoding.BinaryMarshaler
	encoding.BinaryUnmarshaler
}

An EncodeHasher is a record in a table in a harebrain database

type Herror added in v0.1.0

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

func (*Herror) Error added in v0.1.0

func (h *Herror) Error() string

type JsonRecord

type JsonRecord[T any] struct {
	Data T
}

JsonRecord is an EncodeHasher that serializes to JSON

func (*JsonRecord[T]) Hash

func (j *JsonRecord[T]) Hash() string

Hash produces random looking hex

func (*JsonRecord[T]) MarshalBinary

func (j *JsonRecord[T]) MarshalBinary() ([]byte, error)

MarshalBinary marshals to JSON

func (*JsonRecord[T]) UnmarshalBinary

func (j *JsonRecord[T]) UnmarshalBinary(p []byte) error

UnmarshalBinary un-marshals from JSON

type Table

type Table struct {
	Folder string
	Db     *Database
}

func (*Table) Delete

func (t *Table) Delete(f string) error

func (*Table) Get added in v0.0.3

func (t *Table) Get(f string) ([]byte, error)

func (*Table) GetAll added in v0.0.3

func (t *Table) GetAll() (map[string][]byte, error)

func (*Table) Insert

func (t *Table) Insert(rec EncodeHasher) error

func (*Table) Path

func (t *Table) Path() string