objectDB

package module
v0.0.0-...-3997da9 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2024 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LogClose

func LogClose(c io.Closer)

Types

type NameProvider

type NameProvider[E any] interface {
	// SameFile returns true if the two objects should be stored in the same file.
	SameFile(e1, e2 *E) bool
	// ToFile returns the name of the file where the object should be stored.
	// If SameFile(e1, e2) is true, ToFile(e1) has to return the same as ToFile(e2).
	ToFile(e *E) string
}

NameProvider is an interface to provide a name for a file based on the object.

func Monthly

func Monthly[E any](prefix string, dateFunc func(*E) time.Time) NameProvider[E]

Monthly returns a NameProvider that stores objects in monthly files. The prefix is added to the file name.

func SingleFile

func SingleFile[E any](filename string) NameProvider[E]

SingleFile returns a NameProvider that stores all objects in the same file.

type Persist

type Persist[E any] interface {
	// Persist stores the objects in a file.
	Persist(name string, items []*E) error
	// Restore reads all available objects
	Restore() ([]*E, error)
}

Persist is an interface to persist and restore objects.

func PersistJSON

func PersistJSON[E any](baseFolder, suffix string) Persist[E]

PersistJSON returns a Persist that stores objects in JSON format.

func PersistSerializer

func PersistSerializer[E any](baseFolder, suffix string, serializer *serialize.Serializer) Persist[E]

PersistSerializer returns a Persist that stores objects in binary format. It is able to persist and restore interfaces. To do that the interface has to be registered with serialize.Register.

type Result

type Result[E any] struct {
	// contains filtered or unexported fields
}

func (*Result[E]) Delete

func (r *Result[E]) Delete(n int) error

func (*Result[E]) Get

func (r *Result[E]) Get(dst *E, n int) error

func (*Result[E]) Iter

func (r *Result[E]) Iter(yield func(*E, error) bool)

func (*Result[E]) Order

func (r *Result[E]) Order(less func(e1, e2 *E) bool) (Result[E], error)

func (*Result[E]) Size

func (r *Result[E]) Size() int

func (*Result[E]) Update

func (r *Result[E]) Update(n int, e *E) error

type Table

type Table[E any] struct {
	// contains filtered or unexported fields
}

func New

func New[E any](nameProvider NameProvider[E], persist Persist[E], deepCopy func(dst *E, src *E), less func(e1, e2 *E) bool) (*Table[E], error)

New creates a new Table. The nameProvider is used to create a file name for each element. The persist parameter is used to store the data on disk. The deepCopy function is used to create a deep copy of an element. If nil, a simple copy is used. The less function is used to sort the elements. If nil, no sorting is done.

func (*Table[E]) All

func (t *Table[E]) All(yield func(*E) bool)

All calls the yield function for each element in the table. No long-running operations should be done in the yield function, as the table is locked during the call. The elements are deep copied before the yield function is called.

func (*Table[E]) First

func (t *Table[E]) First(dst *E, accept func(*E) bool) bool

First returns the first element that matches the accept function. For performance reasons, the accept function is called with the not yet deep copied elements. So the accept function is not allowed to modify the elements. No long-running operations should be done in the accept function, because the table is locked during the call.

func (*Table[E]) Insert

func (t *Table[E]) Insert(e *E) error

Insert adds a new element to the table.

func (*Table[E]) Match

func (t *Table[E]) Match(accept func(*E) bool) Result[E]

Match returns a Result that contains all elements that match the accept function. For performance reasons, the accept function is called with the not yet deep copied elements. So the accept function is not allowed to modify the elements. No long-running operations should be done in the accept function, because the table is locked during the call.

func (*Table[E]) SetWriteDelay

func (t *Table[E]) SetWriteDelay(sec int)

SetWriteDelay sets the delay in seconds for persisting changes to disk. If sec is 0, changes are written immediately. This is the default. If sec is greater than 0, changes are written after sec seconds of inactivity. If this method is called, the Shutdown method must be called before the program exits, otherwise changes may be lost.

func (*Table[E]) Shutdown

func (t *Table[E]) Shutdown()

Shutdown must be called before the program exits, if write delay was used, otherwise changes may be lost. It waits until all changes are written to disk. If the write delay was not used, this method does nothing. After this method is called, the table is still usable, but changes are written immediately.

func (*Table[E]) Size

func (t *Table[E]) Size() int

Size returns the number of elements in the table.

Directories

Path Synopsis
Package serialize is a simple package to serialize data.
Package serialize is a simple package to serialize data.

Jump to

Keyboard shortcuts

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