Documentation
¶
Index ¶
- func LogClose(c io.Closer)
- type NameProvider
- type Persist
- type Result
- type Table
- func (t *Table[E]) All(yield func(*E) bool)
- func (t *Table[E]) First(dst *E, accept func(*E) bool) bool
- func (t *Table[E]) Insert(e *E) error
- func (t *Table[E]) Match(accept func(*E) bool) Result[E]
- func (t *Table[E]) SetWriteDelay(sec int)
- func (t *Table[E]) Shutdown()
- func (t *Table[E]) Size() int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
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 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 ¶
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 ¶
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]) Match ¶
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 ¶
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.