Go to the first, previous, next, last section, table of contents.


Persistent predicate database

Author(s): J.M. Gomez, D. Cabeza, and M. Hermenegildo, clip@dia.fi.upm.es, http://www.clip.dia.fi.upm.es/, The CLIP Group, Facultad de Informática, Universidad Politécnica de Madrid.

Version: 1.5#118 (2000/4/19, 18:13:43 CEST)

Version of last change: 0.8#31 (1998/12/27, 19:17:20 MET)

Introduction to persistent predicates

This library implements a generic persistent predicate database. The basic notion implemented by the library is that of a persistent predicate. The persistent predicate concept provides a simple, yet powerful generic persistent data access method [CHGT98,Par97]. A persistent predicate is a special kind of dynamic, data predicate that "resides" in some persistent medium (such as a set of files, a database, etc.) that is typically external to the program using such predicates. The main effect is that any changes made to to a persistent predicate from a program "survive" across executions. I.e., if the program is halted and restarted the predicate that the new process sees is in precisely the same state as it was when the old process was halted (provided no change was made in the meantime to the storage by other processes or the user).

Persistent predicates appear to a program as ordinary predicates, and calls to these predicates can appear in clause bodies in the usual way. However, the definitions of these predicates do not appear in the program. Instead, the library maintains automatically the definitions of predicates which have been declared as persistent in the persistent storage.

Updates to persistent predicates can be made by calling predicates similar to assertz_fact/1 and retract_fact/1. The library makes sure that each update is a transactional update, in the sense that if the update terminates, then the permanent storage has definitely been modified. For example, if the program making the updates is halted just after the update and then restarted, then the updated state of the predicate will be seen. This provides security against possible data loss due to, for example, a system crash. Also, due to the atomicity of the transactions, persistent predicates allow concurrent updates from several programs.

Persistent predicates, files, and relational databases

The concept of persistent predicates provided by this library essentially implements a light-weight, simple, and at the same time powerful form of relational database (a deductive database), and which is standalone, in the sense that it does not require external support, other than the file management capabilities provided by the operating system. This is due to the fact that the persistent predicates are in fact stored in one or more auxiliary files in a given directory.

This type of database is specially useful when building small to medium-sized standalone applications in Prolog which require persistent storage. In many cases it provides a much easier way of implementing such storage than using files under direct program control. For example, interactive applications can use persistent predicates to represent their internal state in a way that is close to the application. The persistence of such predicates then allows automatically restoring the state to that at the end of a previous session. Using persistent predicates amounts to simply declaring some predicates as such and eliminates having to worry about opening files, closing them, recovering from system crashes, etc.

In other cases, however, it may be convenient to use a relational database as persistent storage. This may be the case, for example, when the data already resides in such a database (where it is perhaps accessed also by other applications) or the volume of data is very large. persdb_sql [CCG98] is a companion library which implements the same notion of persistent predicates used herein, but keeping the storage in a relational database. This provides a very natural and transparent way to access SQL database relations from a Prolog program. In that library, facilities are also provided for reflecting more complex views of the database relations as predicates. Such views can be constructed as conjunctions, disjunctions, projections, etc. of database relations, and may include SQL-like aggregation operations.

A nice characteristic of the notion of persistent predicates used in both of these libraries is that it abstracts away how the predicate is actually stored. Thus, a program can use persistent predicates stored in files or in external relational databases interchangeably, and the type of storage used for a given predicate can be changed without having to modify the program (except for replacing the corresponding persistent/2 declarations).

An example application of the persdb and persdb_sql libraries (and also the pillow library [CH97]), is WebDB [GCH98]. WebDB is a generic, highly customizable deductive database engine with an html interface. WebDB allows creating and maintaining Prolog-based databases as well as relational databases (residing in conventional relational database engines) using any standard WWW browser.

Using file-based persistent predicates

Persistent predicates can be declared statically, using persistent/2 declarations (which is the preferred method, when possible), or dynamically via calls to make_persistent/2. Currently, persistent predicates may only contain facts, i.e., they are dynamic predicates of type data/1, and should be declared as such.

Predicates declared as persistent are linked to a directory, and the persistent state of the predicate will be kept in several files in that directory. The files in which the persistent predicates are stored are in readable, plain ASCII format, and in Prolog syntax. One advantage of this approach is that such files can also be created or edited by hand, in a text editor, or even by other applications.

An example definition of a persistent predicate implemented by files follows:

:- persistent(p/3,dbdir).

:- multifile persistent_dir/2.
:- data persistent_dir/2.

persistent_dir(dbdir, '/home/clip/public_html/db').

The first line declares the predicate p/3 persistent. The argument dbdir is a key used to index into a fact of the relation persistent_dir/2, which specifies the directory where the corresponding files will be kept. The effect of the declaration, together with the persistent_dir/2 fact, is that, although the predicate is handled in the same way as a normal data predicate, in addition the system will create and maintain efficiently a persistent version of p/3 via files in the directory /home/clip/public_html/db.

The level of indirection provided by the dbdir argument makes it easy to place the storage of several persistent predicates in a common directory, by specifying the same key for all of them. It also allows changing the directory for several such persistent predicates by modifying only one fact in the program. Furthermore, the persistent_dir/2 predicate can even be dynamic and specified at run-time.

Implementation Issues

We outline the current implementation approach. This implementation attempts to provide at the same time efficiency and security. To this end, up to three files are used for each predicate (the persistence set): the data file, the operations file, and the backup file. In the updated state the facts (tuples) that define the predicate are stored in the data file and the operations file is empty (the backup file, which contains a security copy of the data file, may or may not exist).

While a program using a persistent predicate is running, any insertion (assert) or deletion (retract) operations on the predicate are performed on both the program memory and on the persistence set. However, in order to incurr only a small overhead in the execution, rather than changing the data file directly, a record of each of the insertion and deletion operations is appended to the operations file. The predicate is then in a transient state, in that the contents of the data file do not reflect exactly the current state of the corresponding predicate. However, the complete persistence set does.

When a program starts, when it is halted, or, periodically, when it is idle for some time, all pending operations in the operations file are performed on the data file. A backup of the data file is created first to prevent data loss if the system crashes during this operation. The order in which this updating of files is done ensures that, if at any point the process dies, on restart data will be completely recovered. This process of updating the persistence set can also be triggered at any point in the execution of a program by calling update_files/2.

Using persistent predicates from the top level

Special care must be taken when using persistent predicates from the top level. This includes not only defining persistent predicates on the fly from de top level (which is not really very useful in practice) but also the more frequent case of loading into the top level modules or user files which use persistent predicates. As mentioned before, the persistence set is updated automatically each time a program using the corresponding persistent predicates is run or halted. However, since the top level itself is also a standard program, persistent predicates would only be updated whenever the top level is started, when they are typically still not loaded.

If a program launched from a top level needs to update the persistence sets of any persistent predicate it must be done by calling the update_files/2 method explicitly.

Usage and interface (persdbrt)

Documentation on exports (persdbrt)

PREDICATE: passertz_fact/1:

Meta-predicate with arguments: passertz_fact(fact).

Usage: passertz_fact(Fact)

PREDICATE: pretract_fact/1:

Meta-predicate with arguments: pretract_fact(fact).

Usage: pretract_fact(Fact)

PREDICATE: pcurrent_fact/1:

Meta-predicate with arguments: pcurrent_fact(fact).

Usage: pcurrent_fact(Fact)

PREDICATE: init_persdb/0:

Usage:

PREDICATE: initialize_db/0:

Usage:

PREDICATE: make_persistent/2:

Meta-predicate with arguments: make_persistent(spec,?).

Usage: make_persistent(PredDesc,Keyword)

PREDICATE: update_files/2:

Usage: update_files(PredDesc,Arity)

Documentation on multifiles (persdbrt)

PREDICATE: persistent_dir/2:

The predicate is multifile.

The predicate is of type data.

Usage: persistent_dir(Keyword,Location_Path)

PREDICATE: $is_persistent/2:

No further documentation available for this predicate.

The predicate is multifile.

Documentation on internals (persdbrt)

REGTYPE: keyword/1:

An atom which identifies a fact of the persistent_dir/2 relation. This fact relates this atom to a directory in which the persistent storage for one or more persistent predicates is kept.

Usage: keyword(X)

REGTYPE: directoryname/1:

Usage: directoryname(X)

Known bugs and planned improvements (persdbrt)


Go to the first, previous, next, last section, table of contents.