database

package
v0.0.0-...-efb87f3 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is returned when the specified record is not saved.
	ErrNotFound = errors.New("not found")
)

Functions

func RegisterType

func RegisterType(v interface{})

RegisterType will register the type with the raft log marshaller.

Types

type Broadcaster

type Broadcaster interface {
	// AddListener adds a new listener for changes.
	RegisterListener(listener Listener)
}

Broadcaster defines the interface that a type must implement to broadcast changes.

type Command

type Command int

Command is used to denote which operation should be carried out as a result of a Raft commit.

const (
	// CommandSave will save an object in the local database.
	CommandSave Command = iota

	// CommandDelete will delete an object in the local database.
	CommandDelete
)

func (Command) String

func (c Command) String() string

String implements Stringer.

type IDSetter

type IDSetter interface {
	SetID()
	GetID() string
}

IDSetter is the interface required by all database write operations.

type Listener

type Listener interface {
	// PostApply will be called in its own goroutine when the node detects
	// a change in the database. If leader is true the current node is
	// leader.
	PostApply(leader bool, command Command, data interface{})
}

Listener is an interface for type capable of listening to changes in the database.

type LogEntry

type LogEntry struct {
	Command Command
	Type    string
	Value   json.RawMessage
}

LogEntry is an entry in the Raft log (?).

func NewLogEntry

func NewLogEntry(cmd Command, value interface{}) *LogEntry

NewLogEntry will return a new LogEntry ready for committing to the Raft log.

func (*LogEntry) Byte

func (e *LogEntry) Byte() []byte

Byte is a simple helper, that will marshal the entry to a byte slice.

func (*LogEntry) Payload

func (e *LogEntry) Payload() interface{}

Payload will return the payload of a logentry - if any. This could be replaced by proper JSON marshal/unmarshal functions.

type Object

type Object struct {
	ID string `json:"id"`
}

Object can be embbed in other types for saving to the database and thereby easily implementing IDSetter.

func (*Object) GetID

func (o *Object) GetID() string

GetID will return the ID of o or an empty string if none is set.

func (*Object) SetID

func (o *Object) SetID()

SetID will assign a random ID to o if none is set.

type ReadWriteBroadcaster

type ReadWriteBroadcaster interface {
	Reader
	Writer
	Broadcaster
}

ReadWriteBroadcaster must be implemented by types able to both read, write and broadcast.

type ReadWriter

type ReadWriter interface {
	Reader
	Writer
}

ReadWriter must be implemented by types able to both read and write.

type Reader

type Reader interface {
	// One will retrieve one record from the database.
	One(fieldName string, value interface{}, to interface{}) error

	// All lists all kinds of a type.
	All(to interface{}, limit int, skip int, reverse bool) error

	// Find Find returns one or more records by the specified index.
	Find(field string, value interface{}, to interface{}, limit int, skip int, reverse bool) error
}

Reader defines the interface that database reader types must implement.

type Validator

type Validator interface {
	Validate(Reader) error
}

Validator is a type that can validate itself.

type Writer

type Writer interface {
	// Save will save an object to the database.
	Save(data interface{}) error

	// Delete an object from the database.
	Delete(data interface{}) error
}

Writer defines the interface that database writers types must implement.

Jump to

Keyboard shortcuts

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