Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
clj-util-datomic-peer/src/main/clojure/de/mpg/shh/util_datomic_peer/datom.clj
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
64 lines (58 sloc)
2.29 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns de.mpg.shh.util-datomic-peer.datom | |
(:require [clojure.tools.logging :refer [info error]] | |
[clojure.string :as str] | |
[clojure.pprint :as pp] | |
[datomic.api :as d]) | |
(:import [java.util UUID] | |
[java.util.concurrent ExecutionException] | |
[datomic.db DbId])) | |
(defn flatten-eid-pull | |
[e] | |
(letfn [(extract-eid [accumulator [k v]] | |
(if (= :db/id k) | |
(conj accumulator v) | |
(cond | |
(and (coll? v) (map? v)) | |
(reduce extract-eid accumulator v) | |
(sequential? v) | |
(concat accumulator (flatten (map #(reduce extract-eid [] %) v))) | |
:else (throw (Exception. (str/join "" ["Don't know how to extract eid from: " v]))))))] | |
(reduce extract-eid [] e))) | |
(defn datoms-from-query | |
"Return raw datoms from pull query results" | |
[db pull-results] | |
(let [eids-only (into #{} (flatten (map flatten-eid-pull pull-results)))] | |
(flatten (map #(vec (d/datoms db :eavt %)) eids-only)))) | |
(defn attr-filtered-datoms | |
[db e-id attr-ids] | |
(vec (concat (apply #(d/datoms db :eavt e-id %) attr-ids)))) | |
(defn datoms-from-query-limit-attr | |
"Return raw datoms from pull query results" | |
[db pull-results attrs] | |
(let [attr-ids (map :db/id (d/q '[:find [(pull ?e [:db/id]) ...] | |
:in $ [?ident ...] | |
:where [?e :db/ident ?ident]] | |
db | |
attrs)) | |
eids-only (into #{} (flatten (map flatten-eid-pull pull-results)))] | |
(flatten (map #(attr-filtered-datoms db % attr-ids) eids-only)))) | |
(defn trunc | |
"Return a string rep of x, shortened to n chars or less" | |
[x n] | |
(let [s (str x)] | |
(if (<= (count s) n) | |
s | |
(str (subs s 0 (- n 3)) "...")))) | |
(defn datom-table | |
"Print a collection of datoms in an org-mode compatible table." | |
[db datoms] | |
(->> datoms | |
(map | |
(fn [{:keys [e a v tx added]}] | |
{"part" (d/part e) | |
"e" (format "0x%016x" e) | |
"a" (d/ident db a) | |
"v" (trunc v 24) | |
"tx" (format "0x%x" tx) | |
"added" added})) | |
(pp/print-table ["part" "e" "a" "v" "tx" "added"]))) |