An embedded relational datalog storage engine inspired by Datomic and DataScript. This is a fork of [Mozilla's unmaintained project by the same name](
Go to file
Gregory Burd 201ec39dd2 updates (#323)
Reviewed-on: #323
Co-authored-by: Greg Burd <>
Co-committed-by: Greg Burd <>
2023-12-07 20:03:13 +00:00
.cargo CLI improvements (#577) r=grisha 2018-03-05 12:52:20 -08:00
.github ignore warnings from clippy 2022-05-04 17:09:30 -04:00
.vscode Simple aggregates. (#584) r=emily 2018-03-12 15:18:50 -07:00
automation [automation] Re-add project-specific Mentat Docker image. 2018-08-03 12:53:01 -07:00
build updates (#323) 2023-12-07 20:03:13 +00:00
core updates (#323) 2023-12-07 20:03:13 +00:00
core-traits updates (#323) 2023-12-07 20:03:13 +00:00
db Merge branch 'master' into dependabot/cargo/indexmap-approx-1.9 2023-11-25 15:59:43 +00:00
db-traits Update rusqlite requirement from ~0.26 to ~0.29 2023-03-27 10:11:21 +00:00
docs Bump nokogiri from 1.11.7 to 1.12.5 in /docs 2021-09-28 05:59:06 +00:00
edn updates (#323) 2023-12-07 20:03:13 +00:00
examples Add an example, more notes. 2020-02-27 12:09:11 -05:00
ffi fix panic macro use 2021-08-23 17:21:51 -04:00
fixtures Add support for using sqlcipher (#737). Fixes #118 2018-06-13 08:49:40 -07:00
public-traits Update rusqlite requirement from ~0.26 to ~0.29 2023-03-27 10:11:21 +00:00
query-algebrizer implement bytes (aka blobs) as native type 2021-08-23 17:25:10 -04:00
query-algebrizer-traits Minor version adjustments and fixes. 2020-05-25 10:51:22 -04:00
query-projector Merge branch 'master' into dependabot/cargo/indexmap-approx-1.9 2023-11-25 15:59:43 +00:00
query-projector-traits Update rusqlite requirement from ~0.26 to ~0.29 2023-03-27 10:11:21 +00:00
query-pull Update rusqlite requirement from ~0.26 to ~0.29 2023-03-27 10:11:21 +00:00
query-pull-traits Minor version adjustments and fixes. 2020-05-25 10:51:22 -04:00
query-sql Update rusqlite requirement from ~0.26 to ~0.29 2023-03-27 10:11:21 +00:00
scripts Adding iOS Build and Test to CI (#804) 2018-08-22 08:43:17 -07:00
sdks Fix broken iOS tests 2018-08-20 14:40:39 -07:00
sql Update rusqlite requirement from ~0.26 to ~0.29 2023-03-27 10:11:21 +00:00
sql-traits Minor version adjustments and fixes. 2020-05-25 10:51:22 -04:00
src warnings cleanup 2021-07-02 20:39:02 -04:00
tests updates (#323) 2023-12-07 20:03:13 +00:00
tolstoy updates (#323) 2023-12-07 20:03:13 +00:00
tolstoy-traits updates (#323) 2023-12-07 20:03:13 +00:00
tools Merge branch 'master' into dependabot/cargo/rusqlite-approx-0.29 2023-11-25 15:59:20 +00:00
transaction Update rusqlite requirement from ~0.26 to ~0.29 2023-03-27 10:11:21 +00:00
.gitignore update gitignore to ignore doc related files 2021-07-02 17:44:41 -04:00
.ignore stop the docs folder from taking over every search result 2019-07-23 09:01:22 -04:00
.taskcluster.yml [automation] Re-add project-specific Mentat Docker image. 2018-08-03 12:53:01 -07:00
.travis.yml Add a few more versions. 2020-10-29 16:01:01 -04:00
Cargo.toml updates (#323) 2023-12-07 20:03:13 +00:00 Bump version to 0.11.1 2018-08-09 13:16:05 -07:00
LICENSE Change license to Apache. Fixes #74. 2016-11-22 11:40:37 -08:00
Makefile updates (#323) 2023-12-07 20:03:13 +00:00
NOTES Add an example, more notes. 2020-02-27 12:09:11 -05:00 Attempting to cleanup with clippy, rustfmt, etc. 2020-01-31 10:55:45 -05:00
rust-toolchain updates (#323) 2023-12-07 20:03:13 +00:00
shell.nix updates (#323) 2023-12-07 20:03:13 +00:00

Project Mentat

Build Status

Project Mentat is a persistent, embedded knowledge base. It draws heavily on DataScript and Datomic.

This project was started by Mozilla, but is no longer being developed or actively maintained by them. Their repository was marked read-only, this fork is an attempt to revive and continue that interesting work. We owe the team at Mozilla more than words can express for inspiring us all and for this project in particular.

Thank you.



Mentat is a flexible relational (not key-value, not document-oriented) store that makes it easy to describe, grow, and reuse your domain schema.

By abstracting away the storage schema, and by exposing change listeners outside the database (not via triggers), we hope to make domain schemas stable, and allow both the data store itself and embedding applications to use better architectures, meeting performance goals in a way that allows future evolution.

Data storage is hard

We've observed that data storage is a particular area of difficulty for software development teams:

  • It's hard to define storage schemas well. A developer must:

    • Model their domain entities and relationships.
    • Encode that model efficiently and correctly using the features available in the database.
    • Plan for future extensions and performance tuning.

    In a SQL database, the same schema definition defines everything from high-level domain relationships through to numeric field sizes in the same smear of keywords. It's difficult for someone unfamiliar with the domain to determine from such a schema what's a domain fact and what's an implementation concession — are all part numbers always 16 characters long, or are we trying to save space? — or, indeed, whether a missing constraint is deliberate or a bug.

    The developer must think about foreign key constraints, compound uniqueness, and nullability. They must consider indexing, synchronizing, and stable identifiers. Most developers simply don't do enough work in SQL to get all of these things right. Storage thus becomes the specialty of a few individuals.

    Which one of these is correct?

    {:db/id          :person/email
     :db/valueType   :db.type/string
     :db/cardinality :db.cardinality/many     ; People can have multiple email addresses.
     :db/unique      :db.unique/identity      ; For our purposes, each email identifies one person.
     :db/index       true}                    ; We want fast lookups by email.
    {:db/id          :person/friend
     :db/valueType   :db.type/ref
     :db/cardinality :db.cardinality/many}    ; People can have many friends.
    CREATE TABLE people (
      id INTEGER PRIMARY KEY,  -- Bug: because of the primary key, each person can have no more than 1 email.
      email VARCHAR(64),       -- Bug?: no NOT NULL, so a person can have no email.
                               -- Bug: nobody will ever have a long email address, right?
    CREATE TABLE friendships (
      FOREIGN KEY person REFERENCES people(id),  -- Bug?: no indexing, so lookups by friend or person will be slow.
      FOREIGN KEY friend REFERENCES people(id),  -- Bug: no compound uniqueness constraint, so we can have dupe friendships.

    They both have limitations — the Mentat schema allows only for an open world (it's possible to declare friendships with people whose email isn't known), and requires validation code to enforce email string correctness — but we think that even such a tiny SQL example is harder to understand and obscures important domain decisions.

  • Queries are intimately tied to structural storage choices. That not only hides the declarative domain-level meaning of the query — it's hard to tell what a query is trying to do when it's a 100-line mess of subqueries and LEFT OUTER JOINs — but it also means a simple structural schema change requires auditing every query for correctness.

  • Developers often capture less event-shaped than they perhaps should, simply because their initial requirements don't warrant it. It's quite common to later want to know when a fact was recorded, or in which order two facts were recorded (particularly for migrations), or on which device an event took place… or even that a fact was ever recorded and then deleted.

  • Common queries are hard. Storing values only once, upserts, complicated joins, and group-wise maxima are all difficult for non-expert developers to get right.

  • It's hard to evolve storage schemas. Writing a robust SQL schema migration is hard, particularly if a bad migration has ever escaped into the wild! Teams learn to fear and avoid schema changes, and eventually they ship a table called metadata, with three TEXT columns, so they never have to write a migration again. That decision pushes storage complexity into application code. (Or they start storing unversioned JSON blobs in the database…)

  • It's hard to share storage with another component, let alone share data with another component. Conway's Law applies: your software system will often grow to have one database per team.

  • It's hard to build efficient storage and querying architectures. Materialized views require knowledge of triggers, or the implementation of bottleneck APIs. Ad hoc caches are often wrong, are almost never formally designed (do you want a write-back, write-through, or write-around cache? Do you know the difference?), and often aren't reusable. The average developer, faced with a SQL database, has little choice but to build a simple table that tries to meet every need.

Comparison to DataScript

DataScript asks the question: "What if creating a database were as cheap as creating a Hashmap?"

Mentat is not interested in that. Instead, it's focused on persistence and performance, with very little interest in immutable databases/databases as values or throwaway use.

One might say that Mentat's question is: "What if a database could store arbitrary relations, for arbitrary consumers, without them having to coordinate an up-front storage-level schema?"

Consider this a practical approach to facts, to knowledge its storage and access, much like SQLite is a practical RDBMS.

(Note that domain-level schemas are very valuable.)

Another possible question would be: "What if we could bake some of the concepts of CQRS and event sourcing into a persistent relational store, such that the transaction log itself were of value to queries?"

Some thought has been given to how databases as values — long-term references to a snapshot of the store at an instant in time — could work in this model. It's not impossible; it simply has different performance characteristics.

Just like DataScript, Mentat speaks Datalog for querying and takes additions and retractions as input to a transaction.

Unlike DataScript, Mentat exposes free-text indexing, thanks to SQLite/FTS.

Comparison to Datomic

Datomic is a server-side, enterprise-grade data storage system. Datomic has a beautiful conceptual model. It's intended to be backed by a storage cluster, in which it keeps index chunks forever. Index chunks are replicated to peers, allowing it to run queries at the edges. Writes are serialized through a transactor.

Many of these design decisions are inapplicable to deployed desktop software; indeed, the use of multiple JVM processes makes Datomic's use in a small desktop app, or a mobile device, prohibitive.

Comparison to SQLite

SQLite is a traditional SQL database in most respects: schemas conflate semantic, structural, and datatype concerns, as described above; the main interface with the database is human-first textual queries; sparse and graph-structured data are 'unnatural', if not always inefficient; experimenting with and evolving data models are error-prone and complicated activities; and so on.

Mentat aims to offer many of the advantages of SQLite — single-file use, embeddability, and good performance — while building a more relaxed, reusable, and expressive data model on top.


Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

See for further notes.

This project is very new, so we'll probably revise these guidelines. Please comment on an issue before putting significant effort in if you'd like to contribute.


You first need to clone the project. To build and test the project, we are using Cargo.

To build all of the crates in the project use:

cargo build

To run tests use:

# Run tests for everything.
cargo test --all

# Run tests for just the query-algebrizer folder (specify the crate, not the folder),
# printing debug output.
cargo test -p mentat_query_algebrizer -- --nocapture

For most cargo commands you can pass the -p argument to run the command just on that package. So, cargo build -p mentat_query_algebrizer will build just the "query-algebrizer" folder.

What are all of these crates?

We use multiple sub-crates for Mentat for four reasons:

  1. To improve incremental build times.
  2. To encourage encapsulation; writing extern crate feels worse than just use mod.
  3. To simplify the creation of targets that don't use certain features: e.g., a build with no syncing, or with no query system.
  4. To allow for reuse (e.g., the EDN parser is essentially a separate library).

So what are they?

Building blocks


Our EDN parser. It uses rust-peg to parse EDN, which is Clojure/Datomic's richer alternative to JSON. edn's dependencies are all either for representing rich values (chrono, uuid, ordered-float) or for parsing (serde, peg).

In addition, this crate turns a stream of EDN values into a representation suitable to be transacted.


This is the lowest-level Mentat crate. It collects together the following things:

  • Fundamental domain-specific data structures like ValueType and TypedValue.
  • Fundamental SQL-related linkages like SQLValueType. These encode the mapping between Mentat's types and values and their representation in our SQLite format.
  • Conversion to and from EDN types (e.g., edn::Keyword to TypedValue::Keyword).
  • Common utilities (some in the util module, and others that should be moved there or broken out) like Either, InternSet, and RcCounter.
  • Reusable lazy namespaced keywords (e.g., DB_TYPE_DOUBLE) that are used by mentat_db and EDN serialization of core structs.



This crate defines the structs and enums that are the output of the query parser and used by the translator and algebrizer. SrcVar, NonIntegerConstant, FnArg… these all live here.


Similarly, this crate defines an abstract representation of a SQL query as understood by Mentat. This bridges between Mentat's types (e.g., TypedValue) and SQL concepts (ColumnOrExpression, GroupBy). It's produced by the algebrizer and consumed by the translator.

Query processing


This is the biggest piece of the query engine. It takes a parsed query, which at this point is independent of a database, and combines it with the current state of the schema and data. This involves translating keywords into attributes, abstract values into concrete values with a known type, and producing an AlgebraicQuery, which is a representation of how a query's Datalog semantics can be satisfied as SQL table joins and constraints over Mentat's SQL schema. An algebrized query is tightly coupled with both the disk schema and the vocabulary present in the store when the work is done.


A Datalog query projects some of the variables in the query into data structures in the output. This crate takes an algebrized query and a projection list and figures out how to get values out of the running SQL query and into the right format for the consumer.


This crate works with all of the above to turn the output of the algebrizer and projector into the data structures defined in mentat_query_sql.


This simple crate turns those data structures into SQL text and bindings that can later be executed by rusqlite.

The data layer: mentat_db

This is a big one: it implements the core storage logic on top of SQLite. This crate is responsible for bootstrapping new databases, transacting new data, maintaining the attribute cache, and building and updating in-memory representations of the storage schema.

The main crate

The top-level main crate of Mentat assembles these component crates into something useful. It wraps up a connection to a database file and the associated metadata into a Store, and encapsulates an in-progress transaction (InProgress). It provides modules for programmatically writing ( and managing vocabulary (


Sync code lives, for referential reasons, in a crate named tolstoy. This code is a work in progress; current state is a proof-of-concept implementation which largely relies on the internal transactor to make progress in most cases and comes with a basic support for timelines. See Tolstoy's documentation for details.

The command-line interface

This is under tools/cli. It's essentially an external consumer of the main mentat crate. This code is ugly, but it mostly works.

SQLite dependencies

Mentat uses partial indices, which are available in SQLite 3.8.0 and higher. It relies on correlation between aggregate and non-aggregate columns in the output, which was added in SQLite 3.7.11.

It also uses FTS4, which is a compile time option.

By default, Mentat specifies the "bundled" feature for rusqlite, which uses a relatively recent version of SQLite. If you want to link against the system version of SQLite, omit "bundled_sqlite3" from Mentat's features.

version = "0.6"
# System sqlite is known to be new.
default-features = false


Project Mentat is currently licensed under the Apache License v2.0. See the LICENSE file for details.