sod

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2021 License: GPL-3.0 Imports: 14 Imported by: 0

README

build coverage

Go Simple Object Database

A simple database model to store Go structure (on disk) and search across them. It has features close to what an ORM framework can provide but has the advantage of being:

  • in pure Go (de facto portable)
  • does not depend on any DB engine (SQL, SQLite, Mongo ...) to do its job
  • everything is kept simple (one file per structure and eventually an index) It supports structure fields indexing to speed up searches on important fields.

What you should use this project for:

  • you want to implement Go struct persistency in a simple way
  • you want to be able to DB engine like operations on those structures (Update, Delete, Search ...)
  • you don't want to deploy an ORM framework

What you should not use this project for:

  • even though performances are not so bad, I don't think you can rely on it for high troughput DB operations

Examples

See examples directory for all examples.

Documentation

Index

Constants

View Source
const (
	SchemaFilename = "schema.json"
)

Variables

View Source
var (
	ErrUnkownField          = errors.New("unknown object field")
	ErrFieldNotIndexed      = errors.New("field not indexed")
	ErrFieldUnique          = errors.New("unique constraint on field")
	ErrUnkownSearchOperator = errors.New("unknown search operator")
)
View Source
var (
	ErrBadSchema       = errors.New("schema must be a file")
	ErrMissingObjIndex = errors.New("schema is missing object index")

	DefaultSchema = Schema{Extension: ".json"}
)
View Source
var (
	DefaultPermissions = fs.FileMode(0700)
	LowercaseNames     = true
)
View Source
var (
	ErrEOI = errors.New("end of iterator")
)
View Source
var (
	ErrNoObjectFound = errors.New("no object found")
)
View Source
var (
	ErrUnknownKeyType = errors.New("unknown key type")
)

Functions

func IsNoObjectFound added in v1.3.0

func IsNoObjectFound(err error) bool

func IsUnique added in v1.3.0

func IsUnique(err error) bool

func NewFieldIndex

func NewFieldIndex(desc FieldDescriptor, opts ...int) *fieldIndex

NewFieldIndex returns an empty initialized slice. Opts takes len and cap in order to initialize the underlying slice

func UnmarshalJsonFile

func UnmarshalJsonFile(path string, i interface{}) (err error)

Types

type DB

type DB struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func Open

func Open(root string) *DB

Open opens a Simple Object Database

func (*DB) All

func (db *DB) All(of Object) (out []Object, err error)

All returns all Objects in the DB

func (*DB) Close

func (db *DB) Close() (last error)

func (*DB) Commit

func (db *DB) Commit(o Object) (err error)

Commit object schema on the disk. This method must be called after Insert/Delete operations.

func (*DB) Count

func (db *DB) Count(of Object) (n int, err error)

Count the number of Object in the database

func (*DB) Create

func (db *DB) Create(o Object, s Schema) (err error)

Create a schema for an Object

func (*DB) Delete

func (db *DB) Delete(o Object) (err error)

Delete deletes a single Object from the database. If object is indexed DB must be committed to make the changes of the index persistent

func (*DB) DeleteAll

func (db *DB) DeleteAll(of Object) (err error)

DeleteAll deletes all Objects of the same type. If object is indexed, DB must be committed to make the changes of the index persistent

func (*DB) Drop

func (db *DB) Drop() (err error)

Drop drops all the database

func (*DB) Exist

func (db *DB) Exist(o Object) (ok bool, err error)

Exist returns true if the object exist.

func (*DB) Get

func (db *DB) Get(o Object) (err error)

Get gets a single Object from the DB

func (*DB) InsertOrUpdate

func (db *DB) InsertOrUpdate(o Object) (err error)

InsertOrUpdate inserts or updates a single Object and commits changes. This method is not suited for bulk insertions as each insert will trigger a write overhead. For bulk insertion use InsertOrUpdateBulk function

func (*DB) InsertOrUpdateBulk

func (db *DB) InsertOrUpdateBulk(in chan Object) (err error)

InsertOrUpdateBulk inserts objects in bulk in the DB. This function is locking the DB and will terminate only when input channel is closed

func (*DB) Iterator

func (db *DB) Iterator(of Object) (it *Iterator, err error)

Iterator returns an Object Iterator

func (*DB) Schema

func (db *DB) Schema(of Object) (s *Schema, err error)

Schema retrieves the schema of an Object

func (*DB) Search

func (db *DB) Search(o Object, field, operator string, value interface{}) *Search

Search Object where field matches value according to an operator

type FieldDescriptor added in v1.3.0

type FieldDescriptor struct {
	Name   string
	Index  bool
	Unique bool
}

type Index

type Index struct {
	Fields map[string]*fieldIndex
	// mapping ObjectId -> Object UUID
	ObjectIds map[uint64]string
	// contains filtered or unexported fields
}

func NewIndex

func NewIndex(fields ...FieldDescriptor) *Index

func (*Index) Delete

func (in *Index) Delete(o Object)

func (*Index) InsertOrUpdate

func (in *Index) InsertOrUpdate(o Object) (err error)

func (*Index) Len

func (in *Index) Len() int

func (*Index) MarshalJSON

func (in *Index) MarshalJSON() ([]byte, error)

func (*Index) Search

func (in *Index) Search(o Object, field string, operator string, value interface{}, constrain []*IndexedField) ([]*IndexedField, error)

func (*Index) UnmarshalJSON

func (in *Index) UnmarshalJSON(data []byte) error

type IndexedField

type IndexedField struct {
	// the value we want to index
	Value interface{}
	// the ObjectId of the object in the list of object
	// it must be unique accross one ObjectId
	ObjectId uint64
}

func NewIndexedField

func NewIndexedField(value interface{}, objid uint64) (*IndexedField, error)

func (*IndexedField) DeepEqual

func (f *IndexedField) DeepEqual(other *IndexedField) bool

func (*IndexedField) Equal

func (f *IndexedField) Equal(other *IndexedField) bool

func (*IndexedField) Evaluate

func (f *IndexedField) Evaluate(operator string, other *IndexedField) bool

func (*IndexedField) Greater

func (f *IndexedField) Greater(other *IndexedField) bool

func (*IndexedField) Less

func (f *IndexedField) Less(other *IndexedField) bool

func (*IndexedField) MarshalJSON

func (f *IndexedField) MarshalJSON() ([]byte, error)

func (*IndexedField) String

func (f *IndexedField) String() string

func (*IndexedField) UnmarshalJSON

func (f *IndexedField) UnmarshalJSON(data []byte) error

func (*IndexedField) ValueTypeFromString

func (f *IndexedField) ValueTypeFromString(t string)

func (*IndexedField) ValueTypeString

func (f *IndexedField) ValueTypeString() string

type Item

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

func (*Item) Initialize

func (o *Item) Initialize(uuid string)

func (*Item) UUID

func (o *Item) UUID() string

type Iterator

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

func (*Iterator) Len

func (it *Iterator) Len() int

func (*Iterator) Next

func (it *Iterator) Next() (o Object, err error)

func (*Iterator) Reverse added in v1.2.0

func (it *Iterator) Reverse() *Iterator

type Object

type Object interface {
	UUID() string
	Initialize(string)
}

type Schema

type Schema struct {
	Extension    string `json:"extension"`
	ObjectsIndex *Index `json:"index"`
	// contains filtered or unexported fields
}

func (*Schema) Index

func (s *Schema) Index(o Object) error

func (*Schema) Initialize

func (s *Schema) Initialize(o Object)

func (*Schema) Unindex

func (s *Schema) Unindex(o Object)
type Search struct {
	// contains filtered or unexported fields
}

Search helper structure to easily build search queries on objects and retrieve the results

func (*Search) And

func (s *Search) And(field, operator string, value interface{}) *Search

And performs a new Search while "ANDing" search results

func (*Search) Collect

func (s *Search) Collect() (out []Object, err error)

Collect all the objects resulting from the search. If a search has been made on an indexed field, results will be in descending order by default. If you want to change result order, call Reverse before. NB: only search on indexed field(s) will be garanteed to be ordered according to the last field searched.

func (*Search) Err

func (s *Search) Err() error

Err return any error encountered while searching

func (*Search) Iterator

func (s *Search) Iterator() (it *Iterator, err error)

Iterator returns an Iterator convenient to iterate over the objects resulting from the search

func (*Search) Len

func (s *Search) Len() int

Len returns the number of data returned by the search

func (*Search) Limit added in v1.2.0

func (s *Search) Limit(limit uint64) *Search

Limit the number of results collected by Collect function

func (*Search) One added in v1.3.0

func (s *Search) One() (o Object, err error)

One returns the first result found calling Collect function. If no Object is found, ErrNoObjectFound is returned

func (*Search) Or

func (s *Search) Or(field, operator string, value interface{}) *Search

And performs a new Search while "ORing" search results

func (*Search) Reverse added in v1.1.0

func (s *Search) Reverse() *Search

Reverse the order the results are collected by Collect function

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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