database

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: May 2, 2020 License: GPL-3.0 Imports: 19 Imported by: 28

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

Constants

This section is empty.

Variables

View Source
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

func InitializeWithPath(dirPath string) error

InitializeWithPath initializes the database at the specified location using a path.

func Maintain

func Maintain() (err error)

Maintain runs the Maintain method on all storages.

func MaintainRecordStates

func MaintainRecordStates(ctx context.Context) error

MaintainRecordStates runs record state lifecycle maintenance on all storages.

func MaintainThorough

func MaintainThorough() (err error)

MaintainThorough runs the MaintainThorough method on all storages.

func Shutdown

func Shutdown() (err error)

Shutdown shuts down the whole database system.

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) Query

func (c *Controller) Query(q *query.Query, local, internal bool) (*iterator.Iterator, error)

Query executes the given query on 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

func Register(new *Database) (*Database, error)

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.

func (*Database) Loaded

func (db *Database) Loaded()

Loaded updates the LastLoaded timestamp.

func (*Database) MigrateTo

func (db *Database) MigrateTo(newStorageType string) error

MigrateTo migrates the database to another storage type.

func (*Database) Updated

func (db *Database) Updated()

Updated updates the LastUpdated timestamp.

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) PostGet

func (b *HookBase) PostGet(r record.Record) (record.Record, error)

PostGet implements the Hook interface.

func (*HookBase) PreGet

func (b *HookBase) PreGet(dbKey string) error

PreGet implements the Hook interface.

func (*HookBase) PrePut

func (b *HookBase) PrePut(r record.Record) (record.Record, error)

PrePut implements the Hook interface.

func (*HookBase) UsesPostGet

func (b *HookBase) UsesPostGet() bool

UsesPostGet implements the Hook interface and returns false.

func (*HookBase) UsesPreGet

func (b *HookBase) UsesPreGet() bool

UsesPreGet implements the Hook interface and returns false.

func (*HookBase) UsesPrePut

func (b *HookBase) UsesPrePut() bool

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

func NewInterface(opts *Options) *Interface

NewInterface returns a new Interface to the database.

func (*Interface) Delete

func (i *Interface) Delete(key string) error

Delete deletes a record from the database.

func (*Interface) Exists

func (i *Interface) Exists(key string) (bool, error)

Exists return whether a record with the given key exists.

func (*Interface) Get

func (i *Interface) Get(key string) (record.Record, error)

Get return the record with the given key.

func (*Interface) InsertValue

func (i *Interface) InsertValue(key string, attribute string, value interface{}) error

InsertValue inserts a value into a record.

func (*Interface) MakeCrownJewel

func (i *Interface) MakeCrownJewel(key string) error

MakeCrownJewel marks a record as a crown jewel, meaning it will only be accessible locally.

func (*Interface) MakeSecret

func (i *Interface) MakeSecret(key string) error

MakeSecret marks the record as a secret, meaning interfacing processes, such as an UI, are denied access to the record.

func (*Interface) Put

func (i *Interface) Put(r record.Record) (err error)

Put saves a record to the database.

func (*Interface) PutMany added in v0.5.0

func (i *Interface) PutMany(dbName string) (put func(record.Record) error)

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

func (i *Interface) PutNew(r record.Record) (err error)

PutNew saves a record to the database as a new record (ie. with new timestamps).

func (*Interface) Query

func (i *Interface) Query(q *query.Query) (*iterator.Iterator, error)

Query executes the given query on the database.

func (*Interface) SetAbsoluteExpiry

func (i *Interface) SetAbsoluteExpiry(key string, time int64) error

SetAbsoluteExpiry sets an absolute record expiry.

func (*Interface) SetRelativateExpiry

func (i *Interface) SetRelativateExpiry(key string, duration int64) error

SetRelativateExpiry sets a relative (self-updating) record expiry.

func (*Interface) Subscribe

func (i *Interface) Subscribe(q *query.Query) (*Subscription, error)

Subscribe subscribes to updates matching the given query.

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.

func (*Options) Apply

func (o *Options) Apply(r record.Record)

Apply applies options to the record metadata.

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.

func (*RegisteredHook) Cancel

func (h *RegisteredHook) Cancel() error

Cancel unhooks the hook.

type Subscription

type Subscription struct {
	Feed chan record.Record
	// contains filtered or unexported fields
}

Subscription is a database subscription for updates.

func (*Subscription) Cancel

func (s *Subscription) Cancel() error

Cancel cancels the subscription.

Directories

Path Synopsis
fstree
Package fstree provides a dead simple file-based database storage backend.
Package fstree provides a dead simple file-based database storage backend.

Jump to

Keyboard shortcuts

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