Documentation ¶
Overview ¶
Package database provides a universal interface for interacting with the database.
A Lazy Database ¶
The database system can handle Go structs as well as serialized data by the dsd package. While data is in transit within the system, it does not know which form it currently has. Only when it reaches its destination, it must ensure that it is either of a certain type or dump it.
Record Interface ¶
The database system uses the Record interface to transparently handle all types of structs that get saved in the database. Structs include the Base struct to fulfill most parts of the Record interface.
Boilerplate Code:
type Example struct { record.Base sync.Mutex Name string Score int } var ( db = database.NewInterface(nil) ) // GetExample gets an Example from the database. func GetExample(key string) (*Example, error) { r, err := db.Get(key) if err != nil { return nil, err } // unwrap if r.IsWrapped() { // only allocate a new struct, if we need it new := &Example{} err = record.Unwrap(r, new) if err != nil { return nil, err } return new, nil } // or adjust type new, ok := r.(*Example) if !ok { return nil, fmt.Errorf("record not of type *Example, but %T", r) } return new, nil } func (e *Example) Save() error { return db.Put(e) } func (e *Example) SaveAs(key string) error { e.SetKey(key) return db.PutNew(e) }
Index ¶
- Variables
- func Initialize(dirStructureRoot *utils.DirStructure) error
- func InitializeWithPath(dirPath string) error
- func Maintain() (err error)
- func MaintainRecordStates(ctx context.Context) error
- func MaintainThorough() (err error)
- func Shutdown() (err error)
- type Controller
- func (c *Controller) Get(key string) (record.Record, error)
- func (c *Controller) Injected() bool
- func (c *Controller) Maintain() error
- func (c *Controller) MaintainThorough() error
- func (c *Controller) PushUpdate(r record.Record)
- func (c *Controller) Put(r record.Record) (err error)
- func (c *Controller) PutMany() (chan<- record.Record, <-chan error)
- func (c *Controller) Query(q *query.Query, local, internal bool) (*iterator.Iterator, error)
- func (c *Controller) ReadOnly() bool
- func (c *Controller) Shutdown() error
- type Database
- type Hook
- type HookBase
- type Interface
- func (i *Interface) Delete(key string) error
- func (i *Interface) Exists(key string) (bool, error)
- func (i *Interface) Get(key string) (record.Record, error)
- func (i *Interface) InsertValue(key string, attribute string, value interface{}) error
- func (i *Interface) MakeCrownJewel(key string) error
- func (i *Interface) MakeSecret(key string) error
- func (i *Interface) Put(r record.Record) (err error)
- func (i *Interface) PutMany(dbName string) (put func(record.Record) error)
- func (i *Interface) PutNew(r record.Record) (err error)
- func (i *Interface) Query(q *query.Query) (*iterator.Iterator, error)
- func (i *Interface) SetAbsoluteExpiry(key string, time int64) error
- func (i *Interface) SetRelativateExpiry(key string, duration int64) error
- func (i *Interface) Subscribe(q *query.Query) (*Subscription, error)
- type Options
- type RegisteredHook
- type Subscription
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = errors.New("database entry could not be found") ErrPermissionDenied = errors.New("access to database record denied") ErrReadOnly = errors.New("database is read only") ErrShuttingDown = errors.New("database system is shutting down") ErrNotImplemented = errors.New("not implemented by this storage") )
Errors
Functions ¶
func Initialize ¶
func Initialize(dirStructureRoot *utils.DirStructure) error
Initialize initializes the database at the specified location using a dir structure.
func InitializeWithPath ¶ added in v0.5.0
InitializeWithPath initializes the database at the specified location using a path.
func MaintainRecordStates ¶
MaintainRecordStates runs record state lifecycle maintenance on all storages.
func MaintainThorough ¶
func MaintainThorough() (err error)
MaintainThorough runs the MaintainThorough method on all storages.
Types ¶
type Controller ¶
type Controller struct {
// contains filtered or unexported fields
}
A Controller takes care of all the extra database logic.
func InjectDatabase ¶
func InjectDatabase(name string, storageInt storage.Interface) (*Controller, error)
InjectDatabase injects an already running database into the system.
func (*Controller) Get ¶
func (c *Controller) Get(key string) (record.Record, error)
Get return the record with the given key.
func (*Controller) Injected ¶
func (c *Controller) Injected() bool
Injected returns whether the storage is injected.
func (*Controller) Maintain ¶
func (c *Controller) Maintain() error
Maintain runs the Maintain method on the storage.
func (*Controller) MaintainThorough ¶
func (c *Controller) MaintainThorough() error
MaintainThorough runs the MaintainThorough method on the storage.
func (*Controller) PushUpdate ¶
func (c *Controller) PushUpdate(r record.Record)
PushUpdate pushes a record update to subscribers.
func (*Controller) Put ¶
func (c *Controller) Put(r record.Record) (err error)
Put saves a record in the database.
func (*Controller) PutMany ¶ added in v0.5.0
func (c *Controller) PutMany() (chan<- record.Record, <-chan error)
PutMany stores many records in the database.
func (*Controller) ReadOnly ¶
func (c *Controller) ReadOnly() bool
ReadOnly returns whether the storage is read only.
func (*Controller) Shutdown ¶
func (c *Controller) Shutdown() error
Shutdown shuts down the storage.
type Database ¶
type Database struct { Name string Description string StorageType string PrimaryAPI string Registered time.Time LastUpdated time.Time LastLoaded time.Time }
Database holds information about registered databases
func Register ¶
Register registers a new database. If the database is already registered, only the description and the primary API will be updated and the effective object will be returned.
type Hook ¶
type Hook interface { UsesPreGet() bool PreGet(dbKey string) error UsesPostGet() bool PostGet(r record.Record) (record.Record, error) UsesPrePut() bool PrePut(r record.Record) (record.Record, error) }
Hook describes a hook
type HookBase ¶
type HookBase struct { }
HookBase implements the Hook interface and provides dummy functions to reduce boilerplate.
func (*HookBase) UsesPostGet ¶
UsesPostGet implements the Hook interface and returns false.
func (*HookBase) UsesPreGet ¶
UsesPreGet implements the Hook interface and returns false.
func (*HookBase) UsesPrePut ¶
UsesPrePut implements the Hook interface and returns false.
type Interface ¶
type Interface struct {
// contains filtered or unexported fields
}
Interface provides a method to access the database with attached options.
func NewInterface ¶
NewInterface returns a new Interface to the database.
func (*Interface) InsertValue ¶
InsertValue inserts a value into a record.
func (*Interface) MakeCrownJewel ¶
MakeCrownJewel marks a record as a crown jewel, meaning it will only be accessible locally.
func (*Interface) MakeSecret ¶
MakeSecret marks the record as a secret, meaning interfacing processes, such as an UI, are denied access to the record.
func (*Interface) PutMany ¶ added in v0.5.0
PutMany stores many records in the database. Warning: This is nearly a direct database access and omits many things: - Record locking - Hooks - Subscriptions - Caching
func (*Interface) PutNew ¶
PutNew saves a record to the database as a new record (ie. with new timestamps).
func (*Interface) SetAbsoluteExpiry ¶
SetAbsoluteExpiry sets an absolute record expiry.
func (*Interface) SetRelativateExpiry ¶
SetRelativateExpiry sets a relative (self-updating) record expiry.
type Options ¶
type Options struct { Local bool Internal bool AlwaysMakeSecret bool AlwaysMakeCrownjewel bool AlwaysSetRelativateExpiry int64 AlwaysSetAbsoluteExpiry int64 CacheSize int }
Options holds options that may be set for an Interface instance.
type RegisteredHook ¶
type RegisteredHook struct {
// contains filtered or unexported fields
}
RegisteredHook is a registered database hook.
func RegisterHook ¶
func RegisterHook(q *query.Query, hook Hook) (*RegisteredHook, error)
RegisterHook registers a hook for records matching the given query in the database.
type Subscription ¶
Subscription is a database subscription for updates.
func (*Subscription) Cancel ¶
func (s *Subscription) Cancel() error
Cancel cancels the subscription.