Using Sicstus Prolog with LMDB

Written by on October 3, 2021 in Programming, Prolog with 0 Comments

In an earlier article, I showed how Sicstus Prolog allows us to use Berkeley DB for storage and retrieval of terms using the file system. Interestingly, the latest release of Sicstus Prolog, version 4.7.0, deprecates the Berekely DB library, while recommending an alternative implementation based on Lightning Memory-Mapped Database Manager (LMDB). The library interfaces are quite similar, so it is easy to port from BDB to LMDB. 

For this article, I am going to use the same Homeopathic Remedies example that I used in the earlier article. The file “remedies.pl” contains simple facts about a few homeopathic remedies:

Facts About Homeopathic Remedies

Facts About Homeopathic Remedies

Here is the code to create a new database, given an input Prolog file and corresponding Index specification:

Code to Create New Database

Code to Create New Database

As you might notice, one difference from the BDB library is the need to call “lmdb_create” explicitly.

Here is how we can create the actual DB in Sicstus Prolog’s IDE:

Create the Database

Create the Database

What if we wish to add some facts after creating the database? Here is the code that facilitates updates:

Code to Update the Database

Code to Update the Database

Using it is straightforward:

Adding New Facts

Adding New Fact

The new fact to insert is passed as the second argument.

A majority of the times, we would want to open the database, perform some operations, and then close the database. The code for opening and closing are given below:

Cde to Open and Close the DB

Code to Open and Close the DB

One useful functionality is to retrieve all terms that match a template:

Code to Fetch Terms from the DB

Code to Fetch Terms from the DB

Obviously, this feature is useful only when we are expecting a “manageable” number of terms as result. In general, it is a good idea to be able to perform some “operation” on “matching” facts in the database.

Code to Traverse the DB

Code to Traverse the DB

Let us put the above ideas to work.

Since we know that our toy DB contains very few facts, let us retrieve all of them in one go:

Fetch All Terms from the DB

Fetch All Terms from the DB

This time, let us fetch terms that correspond to “abbrev”:

Fetch Matching Terms

Fetch Matching Terms

This also works as expected.

To illustrate the use of “traversal”, let us define a predicate to print any given term:

Code to Print Any Term

Code to Print Any Term

First, let us visit all the “abbrev” terms and print them:

Traversal - 1

Traversal – 1

Great. Now traverse all terms in the database:

Traversal - 2

Traversal – 2

Everything seems to work as expected. 

One additional useful feature is the ability to export the contents of the database to a text file. Here is the code:

Code to Export the DB

Code to Export the DB

We can now export our toy database thus:

Exporting Our Database

Exporting Our Database

Here is the resulting text file:

Exported Data

Exported Data

This text file can be “imported” into another database if needed. That part is quite easy to implement and I am not detailing the code here. 

Finally, we have to close the database:

Closing the DB

Closing the DB

Well, that is it! Using the new LMDB library poses no surprises.

Here is the ZIP file that contains the source, sample data and the exported text file.

Have a nice weekend!

Tags: , , ,

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *

Top