database

package
v0.0.0-...-1b9c57b Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2024 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyUpdate = errors.New("Update operation does not change any values")

ErrEmptyUpdate indicates that an update operation would not change any values.

View Source
var ErrInvalidSavepoint = errors.New("that save point does not exist")

ErrInvalidSavepoint is returned when a user of the Database uses an unkown (or expired) savepoint name.

View Source
var ErrInvalidValue = errors.New("Invalid value for parameter")

ErrInvalidValue indicates that one or more parameters passed to a method had values that are invalid for that operation.

View Source
var ErrNoTxInProgress = errors.New("There is no transaction in progress")

ErrNoTxInProgress indicates that an attempt was made to finish a transaction when none was active.

View Source
var ErrObjectNotFound = errors.New("object was not found in database")

ErrObjectNotFound indicates that an Object was not found in the database.

View Source
var ErrTxInProgress = errors.New("A Transaction is already in progress")

ErrTxInProgress indicates that an attempt to initiate a transaction failed because there is already one in progress.

Functions

This section is empty.

Types

type Database

type Database struct {
	// contains filtered or unexported fields
}

Database wraps a database connection and associated state.

func Open

func Open(path string) (*Database, error)

Open opens a Database. If the database specified by the path does not exist, yet, it is created and initialized.

func (*Database) Begin

func (db *Database) Begin() error

Begin begins an explicit database transaction. Only one transaction can be in progress at once, attempting to start one, while another transaction is already in progress will yield ErrTxInProgress.

func (*Database) Close

func (db *Database) Close() error

Close closes the database. If there is a pending transaction, it is rolled back.

func (*Database) Commit

func (db *Database) Commit() error

Commit ends the active transaction, making any changes made during that transaction permanent and visible to other connections. If no transaction is active, it returns ErrNoTxInProgress

func (*Database) HostAdd

func (db *Database) HostAdd(h *model.Host) error

HostAdd adds a new Host to the database.

func (*Database) HostGetAll

func (db *Database) HostGetAll() ([]model.Host, error)

HostGetAll fetches all Hosts

func (*Database) HostGetByID

func (db *Database) HostGetByID(id int64) (*model.Host, error)

HostGetByID fetches a Host by its ID.

func (*Database) HostGetByName

func (db *Database) HostGetByName(name string) (*model.Host, error)

HostGetByName fetches a Host by its name.

func (*Database) HostUpdateLastSeen

func (db *Database) HostUpdateLastSeen(h *model.Host, timestamp time.Time) error

HostUpdateLastSeen updates the timestamp a Host was last seen by the Server.

func (*Database) PerformMaintenance

func (db *Database) PerformMaintenance() error

PerformMaintenance performs some maintenance operations on the database. It cannot be called while a transaction is in progress and will block pretty much all access to the database while it is running.

func (*Database) RecordAdd

func (db *Database) RecordAdd(r *model.Record) error

RecordAdd adds a new Record to the Database.

func (*Database) RecordCheckExist

func (db *Database) RecordCheckExist(cksum string) (bool, error)

RecordCheckExist returns true if a record with the given checksum exists in the database

func (*Database) RecordGetByHost

func (db *Database) RecordGetByHost(h *model.Host, max int64) ([]model.Record, error)

RecordGetByHost fetches the <max> most recent records for a given Host.

func (*Database) RecordGetByPeriod

func (db *Database) RecordGetByPeriod(begin, end time.Time) ([]model.Record, error)

RecordGetByPeriod fetches all records for the given period, ordered by their timestamps.

func (*Database) RecordGetMostRecent

func (db *Database) RecordGetMostRecent(hostID int64) (time.Time, error)

RecordGetMostRecent returns the timestamp of the youngest record for a given host.

func (*Database) RecordGetRecent

func (db *Database) RecordGetRecent(max int64) ([]model.Record, error)

RecordGetRecent fetches the <max> most recent Records from the database. A negative number returns ALL records, which should probably be avoided.

func (*Database) RecordGetSources

func (db *Database) RecordGetSources() (map[string]int64, error)

RecordGetSources returns a map of all the distinct sources occurring in the database and their respective frequencies.

func (*Database) RecordSearch

func (db *Database) RecordSearch(search *model.SearchQuery, q chan<- model.Record)

RecordSearch searches ALL Records in the database according to the query.

func (*Database) Rollback

func (db *Database) Rollback() error

Rollback terminates a pending transaction, undoing any changes to the database made during that transaction. If no transaction is active, it returns ErrNoTxInProgress

func (*Database) SavepointCreate

func (db *Database) SavepointCreate(name string) error

SavepointCreate creates a savepoint with the given name.

Savepoints only make sense within a running transaction, and just like with explicit transactions, managing them is the responsibility of the user of the Database.

Creating a savepoint without a surrounding transaction is not allowed, even though SQLite allows it.

For details on how Savepoints work, check the excellent SQLite documentation, but here's a quick guide:

Savepoints are kind-of-like transactions within a transaction: One can create a savepoint, make some changes to the database, and roll back to that savepoint, discarding all changes made between creating the savepoint and rolling back to it. Savepoints can be quite useful, but there are a few things to keep in mind:

  • Savepoints exist within a transaction. When the surrounding transaction is finished, all savepoints created within that transaction cease to exist, no matter if the transaction is commited or rolled back.

  • When the database is recovered after being interrupted during a transaction, e.g. by a power outage, the entire transaction is rolled back, including all savepoints that might exist.

  • When a savepoint is released, nothing changes in the state of the surrounding transaction. That means rolling back the surrounding transaction rolls back the entire transaction, regardless of any savepoints within.

  • Savepoints do not nest. Releasing a savepoint releases it and *all* existing savepoints that have been created before it. Rolling back to a savepoint removes that savepoint and all savepoints created after it.

func (*Database) SavepointRelease

func (db *Database) SavepointRelease(name string) error

SavepointRelease releases the Savepoint with the given name, and all Savepoints created before the one being release.

func (*Database) SavepointRollback

func (db *Database) SavepointRollback(name string) error

SavepointRollback rolls back the running transaction to the given savepoint.

func (*Database) SearchAdd

func (db *Database) SearchAdd(search *model.Search) error

SearchAdd adds a Search to the database, including both the query and the results.

func (*Database) SearchDelete

func (db *Database) SearchDelete(id int64) error

SearchDelete removes a Search from the database.

func (*Database) SearchGetAllID

func (db *Database) SearchGetAllID() ([][2]int64, error)

SearchGetAllID fetches the IDs and the number of results of all searches in the database.

func (*Database) SearchGetByID

func (db *Database) SearchGetByID(id int64) (*model.Search, error)

SearchGetByID fetches a Search by its ID

func (*Database) SearchGetResultCount

func (db *Database) SearchGetResultCount(id int64) (int64, error)

SearchGetResultCount returns the number of Records returned by the given Search.

func (*Database) SearchGetResults

func (db *Database) SearchGetResults(id, offset, cnt int64) ([]model.Record, error)

SearchGetResults fetches the Records that were matched by a Search.

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Pool is a pool of database connections

func NewPool

func NewPool(cnt int) (*Pool, error)

NewPool creates a Pool of database connections. The number of connections to use is given by the parameter cnt.

func (*Pool) Close

func (pool *Pool) Close() error

Close closes all open database connections currently in the pool and empties the pool. Any connections retrieved from the pool that are in use at the time Close is called are unaffected.

func (*Pool) Get

func (pool *Pool) Get() *Database

Get returns a DB connection from the pool. If the pool is empty, it waits for a connection to be returned.

func (*Pool) GetNoWait

func (pool *Pool) GetNoWait() (*Database, error)

GetNoWait returns a DB connection from the pool. If the pool is empty, it creates a new one.

func (*Pool) IsEmpty

func (pool *Pool) IsEmpty() bool

IsEmpty returns true if the pool is currently empty.

func (*Pool) Put

func (pool *Pool) Put(db *Database)

Put returns a DB connection to the pool.

Directories

Path Synopsis
Package query defines symbolic constants to reference database queries.
Package query defines symbolic constants to reference database queries.

Jump to

Keyboard shortcuts

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