Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query to list all partitions in Datomic

What is a query to list all partitions of a Datomic database?

This should return

[[:db.part/db] [:db.part/tx] [:db.part/user] .... ]

where .... is all of the user defined partitions.

like image 491
MRocklin Avatar asked Sep 16 '12 18:09

MRocklin


2 Answers

You should be able to get a list of all partitions in the database by searching for all entities associated with the :db.part/db entity via the :db.install/partition attribute:

(ns myns
  (:require [datomic.api :as d]))

(defn get-partitions [db]
  (d/q '[:find ?ident :where [:db.part/db :db.install/partition ?p]
                             [?p :db/ident ?ident]]
       db))

Note

The current version of Datomic (build 0.8.3524) has a shortcoming such that :db.part/tx and :db.part/user (two of the three built-in partitions) are treated specially and aren't actually associated with :db.part/db via :db.install/partition, so the result of the above query function won't include the two.

This problem is going to be addressed in one of the future builds of Datomic. In the meantime, you should take care of including :db.part/tx and :db.part/user in the result set yourself.

like image 65
mtyaka Avatar answered Oct 29 '22 23:10

mtyaka


1st method - using query

=> (q '[:find ?i :where 
       [:db.part/db :db.install/partition ?p] [?p :db/ident ?i]]
     (db conn))

2nd method - from db object

(filter #(instance? datomic.db.Partition %) (:elements (db conn)))

The second method returns sequence of datomic.db.Partition objects which may be useful if we want to get additional info about the partition.

Both methods have known bug/inconsistency: they don't return :db.part/tx and :db.part/user built-in partitions.

like image 29
Grzegorz Luczywo Avatar answered Oct 29 '22 23:10

Grzegorz Luczywo