![]() |
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.
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).
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.
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.
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.
For queries of the database inside body, limit the number of returned values to limit. By default, at most 1000000 identifiers are returned.
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.
Creation date stamp for the entry, as determined by with-time-stamp.
For the field with attribute attr in the entry with identifier id, set the values to vals, a list of strings.
Get the list of values for the field with attribute attr in the entry with identifier id.
Remove the field with attribute attr from the entry with identifier id.
Get the list of attributes for the entry with identifier id.
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.
Get the list of fields for the entry with identifier id.
Remove the entry with identifier id.
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.
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.
Similar to db-search, but skip the first offset candidate entries and return at most limit identifiers.
Get the first value in (db-get-field id attr), or default if there are no values.
Create a new entry in the current database with fields l, and return the identifier of the newly created 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)
Shorthands for (db-search (list (list "name" name))) and (db-search (list (list "owner" owner))).
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)
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.
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.
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).
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).