Database
A database stores entities and their transactions.
Documents and expression programs can operate without a database.
The example opens a database, lists its included plugins, and closes it.
The plugins option selects the plugins that the database enables. The reactors plugin runs live mutations.
Example
Open a database and retain an independent native document
opened, err := stardust.Open(databasePath, stardust.Options{})
if err != nil {
return err
}
// Closing the database leaves the plugin document valid.
result, err := stardust.Plugins()
opened.Close()
if err != nil {
return err
}
defer result.Close()[{name reactors
description Live mutations: stored mutations with live: true react to commits
version '1'
requires []
definitionTypes []
hooks []
indexProviders []
databaseHooks true}]Locking
Opening a database requires the file lock that protects its recorded changes.
The lock_timeout option sets how the call waits for that lock:
lock_timeout | Result |
|---|---|
| Zero | If the lock is unavailable, the call fails immediately. |
| Positive | The call waits up to that duration, rounded up to whole milliseconds. |
| Negative | The call waits without limit. |
When the wait ends without the lock, the call fails with the locked status. The options table gives the default and the type in your language.
Operations
| Operation | Returns | Summary |
|---|---|---|
| Open | database | open(path, options) opens a database. |
| Database.Close | none | Release the database handle. |
Open
open(path, options) opens a database.
- The create option defaults to true.
- A false value refuses a missing file.
- A missing parent directory reports not_found.
The lock_timeout option is a duration.
- Zero fails at once.
- A representable negative value waits without limit.
An absent plugin selection enables all included plugins.
- An empty selection enables none.
func Open(path string, options Options) (*Database, error)- Concurrency: Other calls can overlap this call, except the release of a handle that this call uses.
- Ownership: The caller owns the returned handle and releases it.
Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
path | string | Value | Required | The database file path. |
options | Options | Value | Required options record. See field defaults | The operation's options record. See its field defaults. |
Errors
| Status | When |
|---|---|
StatusNotFound | the parent directory is missing, or the file is missing and create is false |
StatusLocked | another process holds the lock after lock_timeout |
StatusInvalidArgument | the file is not a database or an option is invalid |
StatusOutOfMemory | native memory is exhausted |
Examples
Open a database and retain an independent native document
opened, err := stardust.Open(databasePath, stardust.Options{})
if err != nil {
return err
}
// Closing the database leaves the plugin document valid.
result, err := stardust.Plugins()
opened.Close()
if err != nil {
return err
}
defer result.Close()[{name reactors
description Live mutations: stored mutations with live: true react to commits
version '1'
requires []
definitionTypes []
hooks []
indexProviders []
databaseHooks true}]Open with explicit native options
create := true
result := stardust.Options{Create: &create, PageSize: 0}
database, err := stardust.Open(databasePath, result)
if err != nil {
return err
}
defer database.Close(){create true
clear false
pin_memory false
page_size 0
lock_timeout 0
initial_map_bytes 0
commit_batch 0
plugins null}Wait for the database lock without a limit
create := true
result := stardust.Options{Create: &create, PageSize: 0, LockTimeout: -time.Nanosecond}
database, err := stardust.Open(databasePath, result)
if err != nil {
return err
}
defer database.Close(){create true
clear false
pin_memory false
page_size 0
lock_timeout null
initial_map_bytes 0
commit_batch 0
plugins null}C exports: stardust_db_open
Database.Close
Release the database handle.
close(handle) releases the handle.
- Do not use it after closure.
- Result documents remain usable after the handle that produces them closes.
func (d *Database) Close()- Concurrency: This call is the last use of the receiver and ends its ownership.
Examples
Open a database and retain an independent native document
opened, err := stardust.Open(databasePath, stardust.Options{})
if err != nil {
return err
}
// Closing the database leaves the plugin document valid.
result, err := stardust.Plugins()
opened.Close()
if err != nil {
return err
}
defer result.Close()[{name reactors
description Live mutations: stored mutations with live: true react to commits
version '1'
requires []
definitionTypes []
hooks []
indexProviders []
databaseHooks true}]C exports: stardust_db_close
options
This record supplies options for open. The zero value selects every default.
Create
Creates a missing file. false refuses it with not_found.
- Type:
*bool - Default: true
- Absent: nil
Clear
Discards all existing database contents after acquiring the exclusive file lock. Use true only to start an empty database.
- Type:
bool - Default: false
PinMemory
Locks the mapped database pages in RAM to prevent paging to disk. This also applies when the mapping grows. Opening or growing the mapping fails if the operating system refuses the memory lock.
- Type:
bool - Default: false
PageSize
Storage page size in bytes for a new database file. 0 selects the operating system page size.
- Type:
uint32 - Unit: Bytes
LockTimeout
Waits for the lock of another process. 0 fails at once. A positive wait rounds up to whole milliseconds.
- Type:
time.Duration - Unit: Host duration (100 ns ticks at the ABI)
- Range: Tick range
- Default: 0
- Absent: A negative duration
InitialMapBytes
Zero selects the library default.
- Type:
int64 - Unit: Bytes
CommitBatch
Batch window of the committer. Zero selects the library default and a negative duration causes invalid_argument.
- Type:
time.Duration - Unit: Host duration (100 ns ticks at the ABI)
- Range: Tick range
Plugins
Plugins to enable. An empty list enables none.
- Type:
[]string - Absent: nil