harebrain

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2024 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
}

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 = &HarebrainError{msg: "harebrain"}
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 realfs.WritableFs
}

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
	encoding.BinaryMarshaler
	encoding.BinaryUnmarshaler
}

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

type HarebrainError

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

func (*HarebrainError) Error

func (h *HarebrainError) 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, plus a ".json" extension

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 unmarshals 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

Jump to

Keyboard shortcuts

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