DB API
The DB library is a high performance NoSQL database for embedded applications. It supports fast, flexible local data access and transparent synchronization of data to the cloud. It is modeled after DynamoDB and the OneTable access library.
DB is a NoSQL database where database items items are documents of arbitrary complexity. Data items are implemented as JSON documents and are organized into entity tables. Application entities are defined via an entity schema that specifies data fields and attributes.
DB uses Red/black binary search indexes and has controllable local persistency to disk and to the cloud on a per-table basis.
The embedded database is a high performance NoSQL management document database.
It offers JSON document items with flexible query API with efficient import and export of database items. The database uses fast red/black binary search indexes.
Extensions
Db | Embedded Database based on DynamoDB. |
Functions
void | dbAddCallback(Db *db, DbCallbackProc proc, cchar *model, void *arg, int events) |
Add a database change trigger callback. | |
void | dbAddContext(Db *db, cchar *name, cchar *value) |
Add global context properties. | |
void | dbClose(Db *db) |
Close a database. | |
const DbItem * | dbCreate(Db *db, cchar *model, Json *props, DbParams *params) |
Create a new item. | |
cchar * | dbField(const DbItem *item, cchar *fieldName) |
Fetch a field value from an item as a string. | |
bool | dbFieldBool(const DbItem *item, cchar *fieldName) |
Fetch a field value from an item as a boolean. | |
Time | dbFieldDate(const DbItem *item, cchar *fieldName) |
Fetch a field value from an item as a date. | |
double | dbFieldDouble(const DbItem *item, cchar *fieldName) |
Fetch a field value from an item as a double. | |
int64 | dbFieldNumber(const DbItem *item, cchar *fieldName) |
Fetch a field value from an item as a number (64 bit). | |
RList * | dbFind(Db *db, cchar *model, Json *props, DbParams *params) |
Find matching items in the database. | |
const DbItem * | dbFindOne(Db *db, cchar *model, Json *props, DbParams *params) |
Find the first matching item. | |
const DbItem * | dbGet(Db *db, cchar *model, Json *props, DbParams *params) |
Read a matching item from the database. | |
const cchar * | dbGetError(Db *db) |
Get an error message for the most recent API call. | |
cchar * | dbGetField(Db *db, cchar *model, cchar *fieldName, Json *props, DbParams *params) |
Get a field from a matching item from the database. | |
DbModel * | dbGetItemModel(Db *db, DbItem *item) |
Get the model object for a data item. | |
DbModel * | dbGetModel(Db *db, cchar *name) |
Get the model object for a model name. | |
cchar * | dbNext(Db *db, RList *list) |
Get the next key when using paginated find requests. | |
Db * | dbOpen(cchar *path, cchar *schema, int flags) |
Open a database. | |
Json * | dbPropsToJson(cchar *props[]) |
Convert a list of keyword / value pairs into a JSON object. | |
int | dbRemove(Db *db, cchar *model, Json *props, DbParams *params) |
Remove matching items in the database. | |
void | dbRemoveCallback(Db *db, DbCallbackProc proc, cchar *model, void *arg) |
Remove a database change trigger callback. | |
int | dbSave(Db *db, cchar *filename) |
Save the database. | |
const DbItem * | dbSetBool(Db *db, cchar *model, cchar *fieldName, bool value, Json *props, DbParams *params) |
Set an item field value as a boolean. | |
const DbItem * | dbSetDate(Db *db, cchar *model, cchar *fieldName, Time value, Json *props, DbParams *params) |
Set an item field value as a date. | |
const DbItem * | dbSetDouble(Db *db, cchar *model, cchar *fieldName, double value, Json *props, DbParams *params) |
Set an item field value as a double. | |
const DbItem * | dbSetField(Db *db, cchar *model, cchar *fieldName, cchar *value, Json *props, DbParams *params) |
Set an item field value. | |
const DbItem * | dbSetNum(Db *db, cchar *model, cchar *fieldName, int64 value, Json *props, DbParams *params) |
Set an item field value as a number. | |
Json * | dbStringToJson(cchar *fmt, ...) |
Parse a string into Json properties. | |
const DbItem * | dbUpdate(Db *db, cchar *model, Json *props, DbParams *params) |
Update an item. |
Typedefs
DbCallbackProc | Database callback on changes. |
DbItem | Database items stored in RB indexes. |
DbModel | Model schema. |
DbParams | Database parameters. |
Defines
#define | DB_INMEM -2 |
Don't persist change to storage - preserve in memory only. | |
#define | DB_JSON |
Macro for supplying API properties as a string in JSON format. | |
#define | DB_MAX_ITEM (256 * 1024) |
Maximum database item length. | |
#define | DB_MAX_KEY 1024 |
Maximum sort key length. | |
#define | DB_MAX_LOG_AGE |
Maximum age of log file. | |
#define | DB_MAX_LOG_SIZE (1024 * 1024) |
Maximum journal size. | |
#define | DB_NODELAY -1 |
Don't delay, persist immediately. | |
#define | DB_OPEN_RESET 0x2 |
Reset (erase) database on open. | |
#define | DB_PARAMS |
Macro for supplying API parameters. | |
#define | DB_PROPS |
Macro for supplying API properties as key/value pairs. | |
#define | DB_READ_ONLY 0x1 |
Database flags. | |
#define | DB_JSON |
Macro for supplying API properties as a string in JSON format. | |
#define | DB_PARAMS |
Macro for supplying API parameters. | |
#define | DB_PROPS |
Macro for supplying API properties as key/value pairs. |
Db
Embedded Database based on DynamoDB.
- Description:
- The DB library is a high performance NoSQL in-memory database for embedded applications modeled on DynamoDB. Data items are implemented as JSON documents and are organized into tables. Application entities are defined via an entity schema that specifies data fields and attributes. Data items are JSON documents and are accessed via a flexible API and dot notation queries. DB uses Red/black binary search indexes and has controllable persistency locally to disk and to the cloud on a per-table basis.
- API Stability:
- Evolving.
- Fields:
-
RList * callbacks Database change notification triggers. RHash * changes Hash of pending changes. int code API error code. REvent commitEvent Timeout for commit event. Json * context Global context properties - overwrites API properties. Ticks due When delayed commits are due. char * error API error message. int flags Reserved. FILE * journal Journal file descriptor. Ticks journalCreated When journal file recreated. bool journalError Journal I/O error. REvent journalEvent Timeout for journal save. char * journalPath On-disk journal filename. ssize journalSize Current size of journal file. Ticks maxJournalAge Maximum age of journal file before saving. ssize maxJournalSize Maximum size of the journal before saving. RHash * models List of schema models. char * path On-disk path. RbTree * primary Red/black tree primary index. Json * schema OneTable schema. bool timestamps Maintain created/updated timestamps (if in schema). cchar * type Item schema type property.
Add a database change trigger callback.
- Description:
- When database items are changed, the trigger callbacks are invoked to notify regarding the change.
- Parameters:
-
db Database instance. proc Database trigger function. model Target model. If null, then match all models. arg Argument to pass to the trigger function. events Events of interest. Set to DB_ON_CHANGE, DB_ON_COMMIT (or both).
- API Stability:
- Evolving.
Add global context properties.
- Description:
- A global context property can be added to the set of properties supplied to each API. These property values are added at the top level only. Dotted notation is not supported.
- Parameters:
-
db Database instance. name Name of the property. value Property value to set.
- API Stability:
- Evolving.
Close a database.
- Description:
- This will immediately save any unsaved changes and then close the database.
- Parameters:
-
db Database instance returned via dbOpen
- API Stability:
- Evolving.
Create a new item.
- Description:
- Create a new item of the required model type.
- Parameters:
-
db Database instance returned via dbOpen model Name of the schema model entity to create. The model determines the valid set of properties for the created item. Set to NULL if no model is required. props JSON object containing the item properties to create. Use the macro DB_PROP(name, value, ...) to specify the properties as a list of keyword / value pairs. Use DB_JSON to provide the properties as a JSON/5 string. For example: DB_PROP("name", name, "address", "12 Wishbury lane") or DB_JSON("{role: 'user'}"). If a model is provided, the properties are validated against the model and unknown property names will not be written to the database. params List of API parameters. Use the macro DB_PARAMS(key = value, ...) to specify.
bool upsert : 1; // Update on create if present. Create on update if missing.
- Returns:
- The created database item. The DbItem "name" property is the indexed key value. The value "json" property contains the item values as a cached JSON object. Caller must not free the returned item. Returns null on errors. Use dbGetError to retrieve an error message.
- API Stability:
- Evolving.
Fetch a field value from an item as a string.
- Description:
- Use to examine an item returned via dbGet or other APIs that return items.
- Parameters:
-
item Database item returned from other APIs. fieldName Name of the field to examine.
- Returns:
- The field value as a string. Caller must not free.
- API Stability:
- Evolving.
Fetch a field value from an item as a boolean.
- Description:
- Use to examine an item returned via dbGet or other APIs that return items. Will return true if the item field value is set to "true" or "1".
- Parameters:
-
item Database item returned from other APIs. fieldName Name of the field to examine.
- Returns:
- The field value as a boolean.
- API Stability:
- Evolving.
Fetch a field value from an item as a date.
- Description:
- Use to examine an item returned via dbGet or other APIs that return items. This requires that the date value be stored as an ISO date string.
- Parameters:
-
item Database item returned from other APIs. fieldName Name of the field to examine.
- Returns:
- The field value as a date in a Time value. This is the time in milliseconds since Jan 1 1970.
- API Stability:
- Evolving.
Fetch a field value from an item as a double.
- Description:
- Use to examine an item returned via dbGet or other APIs that return items. This requires that the date value be stored as an ISO date string.
- Parameters:
-
item Database item returned from other APIs. fieldName Name of the field to examine.
- Returns:
- The field value as a double.
- API Stability:
- Evolving.
Fetch a field value from an item as a number (64 bit).
- Description:
- Use to examine an item returned via dbGet or other APIs that return items.
- Parameters:
-
item Database item returned from other APIs. fieldName Name of the field to examine.
- Returns:
- The field value as a 64 bit integer.
- API Stability:
- Evolving.
Find matching items in the database.
- Parameters:
-
db Database instance returned via dbOpen model Name of the schema model for matching items. Set to NULL if no model is required. props JSON object containing item properties to match. Use the macro DB_PROP(name, value, ...) to specify the properties as a list of keyword / value pairs. Use DB_JSON to provide the properties as a JSON/5 string. For example: DB_PROP("name", name, "address", "12 Wishbury lane") or DB_JSON("{role: 'user'}"). If a model is provided, the properties are validated against the model and unknown property names will not be written to the database. params List of API parameters. Use the macro DB_PARAMS(key = value, ...) to specify.
int limit; // Limit the number of returned or removed items.
cchar *index; // Name of the index to use. Defaults to "primary". Currently only supports "primary".
cchar *next; // Next pagination token to use as the starting point for the next page of results.
DbWhere where; // Where query expression callback function.
- Returns:
- A list of matching items. Items can be enumerated or accessed using ITERATE_ITEMS, rGetNextItem and rGetItem. Caller must free the result using rFreeList.
- API Stability:
- Evolving.
Find the first matching item.
- Parameters:
-
db Database instance returned via dbOpen model Name of the schema model for matching items. Set to NULL if no model is required. props JSON object containing item properties to match. Use the macro DB_PROP(name, value, ...) to specify the properties as a list of keyword / value pairs. Use DB_JSON to provide the properties as a JSON/5 string. For example: DB_PROP("name", name, "address", "12 Wishbury lane") or DB_JSON("{role: 'user'}"). If a model is provided, the properties are validated against the model and unknown property names will not be written to the database. params List of API parameters. Use the macro DB_PARAMS(key = value, ...) to specify.
cchar *index; // Name of the index to use. Defaults to "primary". Currently only supports "primary".
DbWhere where; // Where query expression callback function.
- Returns:
- The first matching item. Returns null if no match found.
- API Stability:
- Evolving.
Read a matching item from the database.
- Parameters:
-
db Database instance returned via dbOpen model Name of the schema model for matching items. props JSON object containing the item properties to match. Use the macro DB_PROP(name, value, ...) to specify the properties as a list of keyword / value pairs. Use DB_JSON to provide the properties as a JSON/5 string. For example: DB_PROP("name", name, "address", "12 Wishbury lane") or DB_JSON("{role: 'user'}"). If a model is provided, the properties are validated against the model and unknown property names will not be written to the database. params List of API parameters. Use the macro DB_PARAMS(key = value, ...) to specify.
cchar *index; // Name of the index to use. Defaults to "primary". Currently only supports "primary".
- Returns:
- The matching database item. The DbItem "name" property is the indexed key. The value "json" property contains the item values as a cached JSON object. If null, the "value" property contains the item's value as an unparsed JSON string. Use dbField to access the individual field values in the item. Caller must not free the returned item.
- API Stability:
- Evolving.
Get an error message for the most recent API call.
- Parameters:
-
db Database instance returned via dbOpen
- Returns:
- A static error message string. Caller must not free.
- API Stability:
- Evolving.
Get a field from a matching item from the database.
- Parameters:
-
db Database instance returned via dbOpen model Name of the schema model for matching items. fieldName Name of the item field to return. props JSON object containing the item properties to match. Use the macro DB_PROP(name, value, ...) to specify the properties as a list of keyword / value pairs. Use DB_JSON to provide the properties as a JSON/5 string. For example: DB_PROP("name", name, "address", "12 Wishbury lane") or DB_JSON("{role: 'user'}"). If a model is provided, the properties are validated against the model and unknown property names will not be written to the database. params List of API parameters. Use the macro DB_PARAMS(key = value, ...) to specify.
cchar *index; // Name of the index to use. Defaults to "primary". Currently only supports "primary".
- Returns:
- A string containing the required field in the data item. Caller must not free.
- API Stability:
- Evolving.
Get the model object for a data item.
- Parameters:
-
db Database instance. item Data item with a model type field.
- Returns:
- A model instance.
- API Stability:
- Evolving.
Get the model object for a model name.
- Parameters:
-
db Database instance. name Model name.
- Returns:
- A model instance.
- API Stability:
- Evolving.
Get the next key when using paginated find requests.
- Parameters:
-
db Database instance. list A list returned from a prior dbFind request.
- Returns:
- A reference into the last item returned in the list.
- API Stability:
- Evolving.
Open a database.
- Parameters:
-
path Filename for from which to load and save the database when calling dbSave. On open, an initial load is performed from the file at path. schema OneTable data schema describing the indexes and data models. flags Reserved. Set to zero.
- API Stability:
- Evolving.
- See Also:
- dbClose
Convert a list of keyword / value pairs into a JSON object.
- Parameters:
-
props NULL terminated array of keyword / value pairs.
- Returns:
- A JSON object containing the supplied property values.
- API Stability:
- Internal.
Remove matching items in the database.
- Parameters:
-
db Database instance returned via dbOpen model Name of the schema model for matching items. props JSON object containing the item properties to match. Use the macro DB_PROP(name, value, ...) to specify the properties as a list of keyword / value pairs. Use DB_JSON to provide the properties as a JSON/5 string. For example: DB_PROP("name", name, "address", "12 Wishbury lane") or DB_JSON("{role: 'user'}"). If a model is provided, the properties are validated against the model and unknown property names will not be written to the database. params List of API parameters. Use the macro DB_PARAMS(key = value, ...) to specify. int limit; // Limit the number of items to remove.
cchar *index; // Name of the index to use. Defaults to "primary". Currently only supports "primary".
- Returns:
- A count of the number of items removed.
- API Stability:
- Evolving.
Remove a database change trigger callback.
- Parameters:
-
db Database instance. proc Database trigger function. model Target model. If null, then match all models. arg Argument to pass to the trigger function.
- API Stability:
- Evolving.
Save the database.
- Parameters:
-
db Database instance. filename Optional filename to save data to. If set to NULL, the data is saved to the name given when opening the database via dbOpen
- API Stability:
- Evolving.
Set an item field value as a boolean.
- Parameters:
-
db Database instance returned via dbOpen model Name of the schema model for the item. Set to NULL if no model is required. fieldName Name of the item field to set. value Value to assign to the item's field. props JSON object containing the item properties to match. Use the macro DB_PROP(name, value, ...) to specify the properties as a list of keyword / value pairs. Use DB_JSON to provide the properties as a JSON/5 string. For example: DB_PROP("name", name, "address", "12 Wishbury lane") or DB_JSON("{role: 'user'}"). If a model is provided, the properties are validated against the model. params List of API parameters. Use the macro DB_PARAMS(key = value, ...) to specify.
cchar *index; // Name of the index to use. Defaults to "primary". Currently only supports "primary".
- Returns:
- The updated item. Caller must not free.
- API Stability:
- Evolving.
Set an item field value as a date.
- Parameters:
-
db Database instance returned via dbOpen model Name of the schema model for the item. Set to NULL if no model is required. fieldName Name of the item field to set. value Value to assign to the item's field. props JSON object containing the item properties to match. Use the macro DB_PROP(name, value, ...) to specify the properties as a list of keyword / value pairs. Use DB_JSON to provide the properties as a JSON/5 string. For example: DB_PROP("name", name, "address", "12 Wishbury lane") or DB_JSON("{role: 'user'}"). If a model is provided, the properties are validated against the model. params List of API parameters. Use the macro DB_PARAMS(key = value, ...) to specify.
cchar *index; // Name of the index to use. Defaults to "primary". Currently only supports "primary".
- Returns:
- The updated item. Caller must not free.
- API Stability:
- Evolving.
Set an item field value as a double.
- Parameters:
-
db Database instance returned via dbOpen model Name of the schema model for the item. Set to NULL if no model is required. fieldName Name of the item field to set. value Value to assign to the item's field. props JSON object containing the item properties to match. Use the macro DB_PROP(name, value, ...) to specify the properties as a list of keyword / value pairs. Use DB_JSON to provide the properties as a JSON/5 string. For example: DB_PROP("name", name, "address", "12 Wishbury lane") or DB_JSON("{role: 'user'}"). If a model is provided, the properties are validated against the model. params List of API parameters. Use the macro DB_PARAMS(key = value, ...) to specify.
cchar *index; // Name of the index to use. Defaults to "primary". Currently only supports "primary".
- Returns:
- The updated item. Caller must not free.
- API Stability:
- Evolving.
Set an item field value.
- Description:
- Update a field in an existing item. The item must already exist.
- Parameters:
-
db Database instance returned via dbOpen model Name of the schema model for the item. Set to NULL if no model is required. fieldName Name of the item field to set. value Value to assign to the item's field. props JSON object containing the item properties to match. Use the macro DB_PROP(name, value, ...) to specify the properties as a list of keyword / value pairs. Use DB_JSON to provide the properties as a JSON/5 string. For example: DB_PROP("name", name, "address", "12 Wishbury lane") or DB_JSON("{role: 'user'}"). If a model is provided, the properties are validated against the model. params List of API parameters. Use the macro DB_PARAMS(key = value, ...) to specify.
cchar *index; // Name of the index to use. Defaults to "primary". Currently only supports "primary".
- Returns:
- The updated item. Caller must not free.
- API Stability:
- Evolving.
Set an item field value as a number.
- Parameters:
-
db Database instance returned via dbOpen model Name of the schema model for the item. Set to NULL if no model is required. fieldName Name of the item field to set. value Value to assign to the item's field. props JSON object containing the item properties to match. Use the macro DB_PROP(name, value, ...) to specify the properties as a list of keyword / value pairs. Use DB_JSON to provide the properties as a JSON/5 string. For example: DB_PROP("name", name, "address", "12 Wishbury lane") or DB_JSON("{role: 'user'}"). If a model is provided, the properties are validated against the model. params List of API parameters. Use the macro DB_PARAMS(key = value, ...) to specify.
cchar *index; // Name of the index to use. Defaults to "primary". Currently only supports "primary".
- Returns:
- The updated item. Caller must not free.
- API Stability:
- Evolving.
Parse a string into Json properties.
- Parameters:
-
fmt Printf style format string. ... Arguments to fmt.
- Returns:
- A JSON object containing the supplied property values.
- API Stability:
- Internal.
Update an item.
- Parameters:
-
db Database instance returned via dbOpen model Name of the schema model for the item. Set to NULL if no model is required. props JSON object containing the item properties to update. Use the macro DB_PROP(name, value, ...) to specify the properties as a list of keyword / value pairs. Use DB_JSON to provide the properties as a JSON/5 string. For example: DB_PROP("name", name, "address", "12 Wishbury lane") or DB_JSON("{role: 'user'}"). If a model is provided, the properties are validated against the model and unknown property names will not be written to the database. params List of API parameters. Use the macro DB_PARAMS(key = value, ...) to specify.
cchar *index; // Name of the index to use. Defaults to "primary". Currently only supports "primary".
- Returns:
- The updated item. Caller must not free. Returns null on errors. Use dbGetError to retrieve an error message.
- API Stability:
- Evolving.
Typedefs
Database callback on changes.
- Parameters:
-
arg User argument provided to dbSetCallback. db Database instance. model DbModel reference describing the item's schema model. item Database item that is changing. params User params provided to the API that caused the change. cmd The nature of the change. Set to "create", "update" or "remove". events Events of interest mask. Set to DB_ON_CHANGE, DB_ON_COMMIT.
- API Stability:
- Evolving.
Database items stored in RB indexes.
- API Stability:
- Evolving.
- Fields:
-
uint allocatedName The name is allocated and must be freed when removed. uint allocatedValue The value is allocated and must be freed when removed. uint delayed Update to journal and cloud delayed. Json * json Parsed JSON value of the item, takes precedence over value. char * key Indexed name of the item. Used as the sort key. char * value Text value of the item (JSON string), may be stale if json set.
Model schema.
- Description:
- The model schema defines an application entity and the supported entity fields.
- Fields:
-
Time delay Time to delay before committing changes. RHash * fields Hash of model fields. uint mem Keep model in-memory and not persisted to storage. cchar * name Name of the model. uint sync Sync model items to the cloud.
- API Stability:
- Evolving.
Database parameters.
- API Stability:
- Evolving.
- Fields:
-
cvoid * arg Argument to where callback. bool bypass Bypass changes. int delay Delay before commmiting changes (Delay in msec, -1 == nocommit). cchar * index Index name. Default to "primary". Currently only supports "primary". int limit Limit the number of returned or removed items. bool log Emit trace information to the log. bool mem Update in memory only. cchar * next Pagination token starting point for the next page of results. bool upsert Update on create if present. Create on update if missing. DbWhere where Where query expression callback.