GNU TeXmacsUser manualReferenceAboutEnglish
The TeXmacs database model

The TeXmacs database manipulation API has mainly been designed for internal use. It is based on a dedicated NoSQL-style database model, using a variant of column data stores. For the moment, we only support a limited number of entry types and field types, although new types can easily be added later. Currently, databases are used for managing remote files, bibliographies, user lists, versions, etc.

The interface has been kept to be as simple as possible, so that our low level implementation can be most easily optimized for efficiency when needed. Furthermore, the routines of our basic API can all be customized a posteriori to add specific features. For instance, the basic API is string-based, so a special additional layer was added to support TeXmacs snippets as values instead of strings. Similarly, an additional layer was added for managing the permissions of specific users. The advantage of this design based on a posteriori customizations is that the routines in the basic API always keep the same semantics, no matter how many additional layers are added.

A TeXmacs database is always a collection of database entries. Each entry consists of a unique identifier and a list of fields. Each field consists of an attribute, a list of values, a creation date and an expiration date. The creation and expiration dates cannot be manipulated directly, but it is possible to specify an alternative time for database queries, which make it possible to easily recover any past state of the database.

The basic Scheme API is implemented in

database/db-base.scm

on top of a few glued C++ routines (tmdb-set-field, tmdb-get-field, tmdb-set-entry, tmdb-query, etc.) whose implementation can be found in

src/src/Plugins/Database/

. The extensions described in the next sections are implemented in further files of the database/ directory by overloading the basic routines with tm-define. The internals of the database engine (storage on disk, indexation, queries) are described in more detail in the TeXmacs database.

Macros for context specification
(with-database db . body)
(specify a database)

Execute body with db as the current database. Here db should be an URL with extension .tmdb. The database db will be used by all routines of the database API called from within body. If no database has been specified, then these routines raise an error. The variant (with-database* db . body) in addition switches off the recording of the history of modifications of db (see tmdb-keep-history).

(with-time t . body)
(specify a time)

Execute body with t as the current time. All database queries inside body become relative to the time t, which allows for the inspection of past states of the database. The parameter t is a number (or a string containing a number) representing a UNIX time stamp, :now (the default) or :always. The special value :always disables the time filter, so that queries take into account all past and present values. Modifications of the database are normally made at the current time :now.

(with-time-stamp on? . body)
(add date field to new entries)

Whenever on? holds, a date attribute (with the current time as its value) will automatically be added by db-set-entry to all entries which do not already contain a date field. For entries which circulate among several users, this allows you to determine when they were created for the first time.

(with-extra-fields l . body)
(add fields to entries)

Whenever an entry with fields new-l is set using db-set-entry (or created using db-create-entry) inside body, the list of fields l is automatically added to new-l. Fields of new-l whose attributes occur in l are discarded, so the extra fields take precedence. Nested uses of with-extra-fields accumulate their field lists.

(with-limit limit . body)
(limit number of return values)

For queries of the database inside body, limit the number of returned values to limit. By default, at most 1000000 identifiers are returned.

Special attributes
name

A name (or key) for the entry, by which it can be referred to. Values of name fields are indexed separately for completion (see index-get-name-completions), and an entry is considered to exist if and only if it has a name field.

date

Creation date stamp for the entry, as determined by with-time-stamp.

Main routines of the database API
(db-set-field id attr vals)
(set values for a given field)

For the field with attribute attr in the entry with identifier id, set the values to vals, a list of strings.

(db-get-field id attr)
(get all values for a given field)

Get the list of values for the field with attribute attr in the entry with identifier id.

(db-remove-field id attr)
(remove a field)

Remove the field with attribute attr from the entry with identifier id.

(db-get-attributes id)
(get the list of attributes)

Get the list of attributes for the entry with identifier id.

(db-set-entry id l)
(fill out a complete entry)

For the entry with identifier id, set the list of fields to l. Each field is a list (attr val1 … valn) of an attribute and its values.

(db-get-entry id)
(retrieve a complete entry)

Get the list of fields for the entry with identifier id.

(db-remove-entry id)
(remove a complete entry)

Remove the entry with identifier id.

(db-create-id)
(create a unique identifier)

Create an identifier which does not yet exist in the database. If no current database has been specified, then a unique identifier is returned without any further checks.

(db-search q)
(search for a list of fields)

Return the list of identifiers of entries which match a given query q. The query q is a list of constraints of the form (attr val1 … valn). Each constraint is interpreted as “the attribute attr of the entry is one of the values val1, \(\ldots\), valn”. In addition to these basic constraints, extensions of the database API may implement additional kinds of constraints. Such supplementary constraints are always formed by taking a special keyword for attr.

The basic API already implements one type of supplementary constraint of the form (:order attr asc?), where attr is an attribute and asc? a boolean value. This kind of supplementary constraint is always satisfied and has the effect of ordering the output of the query on the attribute attr in ascending or descending order, depending on asc?. Another supplementary constraint (:modified t1 t2) restricts the output to entries which were modified between the times t1 and t2 (strings containing integers); it is typically used in combination with (with-time :always …). Finally, the keyword constraints :match and :prefix (with the synonyms :contains and :completes) are described in the section about indexation.

(db-search-paginate q limit offset)
(search with pagination)

Similar to db-search, but skip the first offset candidate entries and return at most limit identifiers.

Other useful routines
(db-get-field-first id attr default)
(get first value for a given field)

Get the first value in (db-get-field id attr), or default if there are no values.

(db-create-entry l)
(create a new entry)

Create a new entry in the current database with fields l, and return the identifier of the newly created entry.

(db-entry-exists? id)
(test existence of entry)

Test whether there exists an entry with identifier id, i.e. whether the entry has a non-empty name field.

(db-search-name name)

(db-search-owner owner)
(search by name or owner)

Shorthands for (db-search (list (list "name" name))) and (db-search (list (list "owner" owner))).

(db-reset)
(reset the database context)

Reset the current database, the current time and the other context variables of the database API (and its extensions) to their default values.

(global-database)

(db-get-time)
(context)

The URL of the database $TEXMACS_HOME_PATH/server/global.tmdb, resp. the time used by the queries (see with-time), as a floating point number.

(db-inspect-history name)
(debugging)

Print on the standard output the identifiers of the entries whose name field has, or had, the value name, with the creation and expiration dates of these fields.

Glued routines

The routines above call the following glued routines, which take the URL db of the database and, for most of them, the time t of the query explicitly: (tmdb-set-field db id attr vals t), (tmdb-get-field db id attr t), (tmdb-remove-field db id attr t), (tmdb-get-attributes db id t), (tmdb-set-entry db id l t), (tmdb-get-entry db id t), (tmdb-remove-entry db id t), (tmdb-query db q t limit offset), (tmdb-keep-history db flag), (tmdb-inspect-history db name), (tmdb-get-completions db prefix) and (tmdb-get-name-completions db prefix).

Other modules

The other files of the database/ directory build on this API:

db-users.scm

Users and permissions (see users, groups and permissions), and the database of each user and each kind of data: (user-database kind) returns the database of the current user for kind (such as "bib"), which is stored with (set-preferred-database uid kind db) and retrieved with (get-preferred-database uid kind); (use-database db) makes db the database of the current kind and (add-user pseudo name) registers a user.

db-convert.scm

Conversion between the entries of the database and TeXmacs markup: (db-load-entry id) returns an entry as a db-entry tree, (db-load) all the entries of the current database as a document, and (db-save doc) stores the entries of a document. The predicate (db-url? u) tests whether u is a database (.tmdb file or tmfs://db/ url).

db-tmfs.scm

The buffers tmfs://db/kind/file, which show the entries of a database, with the search, order and limit of the query kept in preferences (db-get-query-preference and db-set-query-preference).

bib-db.scm

The bibliographic database (bib-database), that is (user-database "bib"), and the conversions (db->bib t) and (bib->db t) between database entries and BibTEX entries (see bibliography styles).

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".