Skip to content

Instantly share code, notes, and snippets.

@zenon
Created January 16, 2013 22:32
Show Gist options
  • Save zenon/4551589 to your computer and use it in GitHub Desktop.
Save zenon/4551589 to your computer and use it in GitHub Desktop.
Datomic_zero - What is in an empty Datomic database (and how to find out)?
(ns datomic-zero
(:use [datomic.api :only [q db entity] :as d])
(:use (clojure pprint)))
;; URI as identifyer of the db
(def uri "datomic:mem://trials")
;; create database
(d/create-database uri)
;; connect to database
(def conn (d/connect uri))
;; type: datomic.db.Db
;; get a snapshot of empty DB
(def db-zero (db conn))
;; this asks for all entity having an ident.
;; (there may be others, I just don't know how to find them.)
(def orig-entities (sort-by first
(seq (q '[:find ?c ?i :where [?c :db/ident ?i ]] db-zero))))
(pprint orig-entities)
;; Prints as follows:
([0 :db.part/db]
[1 :db/add]
[2 :db/retract]
[3 :db.part/tx]
[4 :db.part/user]
[10 :db/ident]
[11 :db.install/partition]
[12 :db.install/valueType]
[13 :db.install/attribute]
[14 :db.install/function]
[20 :db.type/ref]
[21 :db.type/keyword]
[22 :db.type/long]
[23 :db.type/string]
[24 :db.type/boolean]
[25 :db.type/instant]
[26 :db.type/fn]
[27 :db.type/bytes]
[35 :db.cardinality/one]
[36 :db.cardinality/many]
[37 :db.unique/value]
[38 :db.unique/identity]
[39 :fressian/tag]
[40 :db/valueType]
[41 :db/cardinality]
[42 :db/unique]
[43 :db/isComponent]
[44 :db/index]
[45 :db/noHistory]
[46 :db/lang]
[47 :db/code]
[48 :db.lang/clojure]
[49 :db.lang/java]
[50 :db/txInstant]
[51 :db/fulltext]
[52 :db/fn]
[53 :db.fn/retractEntity]
[54 :db.fn/cas]
[55 :db.type/uuid]
[56 :db.type/double]
[57 :db.type/float]
[58 :db.type/uri]
[59 :db.type/bigint]
[60 :db.type/bigdec]
[61 :db/doc])
;; I now want to know the attributes too.
(def overview0 (map (fn [[i key]] (let [e (d/entity db-zero i)]
(conj [i key]
(map #(vector % (% e)) (keys e)))))
orig-entities))
(pprint overview0)
;; prints as
([0
:db.part/db
([:db/doc
"Name of the system partition. The system partition includes the core of datomic, as well as user schemas: type definitions, attribute definitions, partition definitions, and data function definitions."]
[:db.install/function #{:db.fn/cas :db.fn/retractEntity}]
[:db.install/attribute
#{:db/noHistory :db.install/partition :db/cardinality
:db.install/attribute :db/index :db/unique :db/fulltext
:db/txInstant :db/lang :db/doc :db.install/valueType :db/code
:db/isComponent :db/fn :db.install/function :db/valueType
:db/ident :fressian/tag}]
[:db.install/valueType
#{:db.type/uuid :db.type/bigint :db.type/uri :db.type/ref
:db.type/keyword :db.type/bytes :db.type/string :db.type/instant
:db.type/fn :db.type/long :db.type/bigdec :db.type/boolean
:db.type/double :db.type/float}]
[:db.install/partition #{:db.part/db}]
[:db/ident :db.part/db])]
[1
:db/add
([:db/doc
"Primitive assertion. All transactions eventually reduce to a collection of primitive assertions and retractions of facts, e.g. [:db/add fred :age 42]."]
[:db/ident :db/add])]
[2
:db/retract
([:db/doc
"Primitive retraction. All transactions eventually reduce to a collection of assertions and retractions of facts, e.g. [:db/retract fred :age 42]."]
[:db/ident :db/retract])]
[3
:db.part/tx
([:db/doc
"Partition used to store data about transactions. Transaction data always includes a :db/txInstant which is the transaction's timestamp, and can be extended to store other information at transaction granularity."]
[:db/ident :db.part/tx])]
[4
:db.part/user
([:db/doc
"Name of the user partition. The user partition is analogous to the default namespace in a programming language, and should be used as a temporary home for data during interactive development."]
[:db/ident :db.part/user])]
[10
:db/ident
([:db/doc "Attribute used to uniquely name an entity."]
[:db/unique :db.unique/identity]
[:db/cardinality :db.cardinality/one]
[:db/valueType :db.type/keyword]
[:db/ident :db/ident])]
[11
:db.install/partition
([:db/doc
"System attribute with type :db.type/ref. Asserting this attribute on :db.part/db with value v will install v as a partition."]
[:db/cardinality :db.cardinality/many]
[:db/valueType :db.type/ref]
[:db/ident :db.install/partition])]
[12
:db.install/valueType
([:db/doc
"System attribute with type :db.type/ref. Asserting this attribute on :db.part/db with value v will install v as a value type."]
[:db/cardinality :db.cardinality/many]
[:db/valueType :db.type/ref]
[:db/ident :db.install/valueType])]
[13
:db.install/attribute
([:db/doc
"System attribute with type :db.type/ref. Asserting this attribute on :db.part/db with value v will install v as an attribute."]
[:db/cardinality :db.cardinality/many]
[:db/valueType :db.type/ref]
[:db/ident :db.install/attribute])]
[14
:db.install/function
([:db/doc
"System attribute with type :db.type/ref. Asserting this attribute on :db.part/db with value v will install v as a data function."]
[:db/cardinality :db.cardinality/many]
[:db/valueType :db.type/ref]
[:db/ident :db.install/function])]
[20
:db.type/ref
([:db/doc
"Value type for references. All references from one entity to another are through attributes with this value type."]
[:fressian/tag :ref]
[:db/ident :db.type/ref])]
[21
:db.type/keyword
([:db/doc
"Value type for keywords. Keywords are used as names, and are interned for efficiency. Keywords map to the native interned-name type in languages that support them."]
[:fressian/tag :key]
[:db/ident :db.type/keyword])]
[22
:db.type/long
([:db/doc
"Fixed integer value type. Same semantics as a Java long: 64 bits wide, two's complement binary representation."]
[:fressian/tag :int]
[:db/ident :db.type/long])]
[23
:db.type/string
([:db/doc "Value type for strings."]
[:fressian/tag :string]
[:db/ident :db.type/string])]
[24
:db.type/boolean
([:db/doc "Boolean value type."]
[:fressian/tag :bool]
[:db/ident :db.type/boolean])]
[25
:db.type/instant
([:db/doc
"Value type for instants in time. Stored internally as a number of milliseconds since midnight, January 1, 1970 UTC. Representation type will vary depending on the language you are using."]
[:fressian/tag :inst]
[:db/ident :db.type/instant])]
[26
:db.type/fn
([:db/doc
"Value type for database functions. See Javadoc for Peer.function."]
[:fressian/tag :datomic/fn]
[:db/ident :db.type/fn])]
[27
:db.type/bytes
([:db/doc
"Value type for small binaries. Maps to byte array on the JVM."]
[:fressian/tag :bytes]
[:db/ident :db.type/bytes])]
[35
:db.cardinality/one
([:db/doc
"One of two legal values for the :db/cardinality attribute. Specify :db.cardinality/one for single-valued attributes, and :db.cardinality/many for many-valued attributes."]
[:db/ident :db.cardinality/one])]
[36
:db.cardinality/many
([:db/doc
"One of two legal values for the :db/cardinality attribute. Specify :db.cardinality/one for single-valued attributes, and :db.cardinality/many for many-valued attributes."]
[:db/ident :db.cardinality/many])]
[37
:db.unique/value
([:db/doc
"Specifies that an attribute's value is unique. Attempts to create a new entity with a colliding value for a :db.unique/value will fail."]
[:db/ident :db.unique/value])]
[38
:db.unique/identity
([:db/doc
"Specifies that an attribute's value is unique. Attempts to create a new entity with a colliding value for a :db.unique/value will become upserts."]
[:db/ident :db.unique/identity])]
[39
:fressian/tag
([:db/doc
"Keyword-valued attribute of a value type that specifies the underlying fressian type used for serialization."]
[:db/index true]
[:db/cardinality :db.cardinality/one]
[:db/valueType :db.type/keyword]
[:db/ident :fressian/tag])]
[40
:db/valueType
([:db/doc
"Property of an attribute that specifies the attribute's value type. Built-in value types include, :db.type/keyword, :db.type/string, :db.type/ref, :db.type/instant, :db.type/long, :db.type/bigdec, :db.type/boolean, :db.type/float, :db.type/uuid, :db.type/double, :db.type/bigint, :db.type/uri."]
[:db/cardinality :db.cardinality/one]
[:db/valueType :db.type/ref]
[:db/ident :db/valueType])]
[41
:db/cardinality
([:db/doc
"Property of an attribute. Two possible values: :db.cardinality/one for single-valued attributes, and :db.cardinality/many for many-valued attributes. Defaults to :db.cardinality/one."]
[:db/cardinality :db.cardinality/one]
[:db/valueType :db.type/ref]
[:db/ident :db/cardinality])]
[42
:db/unique
([:db/doc
"Property of an attribute. If value is :db.unique/value, then attribute value is unique to each entity. Attempts to insert a duplicate value for a temporary entity id will fail. If value is :db.unique/identity, then attribute value is unique, and upsert is enabled. Attempting to insert a duplicate value for a temporary entity id will cause all attributes associated with that temporary id to be merged with the entity already in the database. Defaults to nil."]
[:db/cardinality :db.cardinality/one]
[:db/valueType :db.type/ref]
[:db/ident :db/unique])]
[43
:db/isComponent
([:db/doc
"Property of attribute whose vtype is :db.type/ref. If true, then the attribute is a component of the entity referencing it. When you query for an entire entity, components are fetched automatically. Defaults to nil."]
[:db/cardinality :db.cardinality/one]
[:db/valueType :db.type/boolean]
[:db/ident :db/isComponent])]
[44
:db/index
([:db/doc
"Property of an attribute. If true, create an AVET index for the attribute. Defaults to false."]
[:db/cardinality :db.cardinality/one]
[:db/valueType :db.type/boolean]
[:db/ident :db/index])]
[45
:db/noHistory
([:db/doc
"Property of an attribute. If true, past values of the attribute are not retained. Defaults to false."]
[:db/cardinality :db.cardinality/one]
[:db/valueType :db.type/boolean]
[:db/ident :db/noHistory])]
[46
:db/lang
([:db/doc
"Attribute of a data function. Value is a keyword naming the implementation language of the function. Legal values are :db.lang/java and :db.lang/clojure"]
[:db/cardinality :db.cardinality/one]
[:db/valueType :db.type/ref]
[:db/ident :db/lang])]
[47
:db/code
([:db/doc
"String-valued attribute of a data function that contains the function's source code."]
[:db/fulltext true]
[:db/cardinality :db.cardinality/one]
[:db/valueType :db.type/string]
[:db/ident :db/code])]
[48
:db.lang/clojure
([:db/doc
"Value of :db/lang attribute, specifying that a data function is implemented in Clojure."]
[:db/ident :db.lang/clojure])]
[49
:db.lang/java
([:db/doc
"Value of :db/lang attribute, specifying that a data function is implemented in Java."]
[:db/ident :db.lang/java])]
[50
:db/txInstant
([:db/doc
"Attribute whose value is a :db.type/instant. A :db/txInstant is recorded automatically with every transaction."]
[:db/index true]
[:db/cardinality :db.cardinality/one]
[:db/valueType :db.type/instant]
[:db/ident :db/txInstant])]
[51
:db/fulltext
([:db/doc
"Property of an attribute. If true, create a fulltext search index for the attribute. Defaults to false."]
[:db/cardinality :db.cardinality/one]
[:db/valueType :db.type/boolean]
[:db/ident :db/fulltext])]
[52
:db/fn
([:db/doc
"A function-valued attribute for direct use by transactions and queries."]
[:db/cardinality :db.cardinality/one]
[:db/valueType :db.type/fn]
[:db/ident :db/fn])]
[53
:db.fn/retractEntity
([:db/doc
"Retract all facts about an entity, including references from other entities and component attributes recursively."]
[:db/code
"(clojure.core/fn [db e] (datomic.builtins/build-retract-args db e))"]
[:db/lang :db.lang/clojure]
[:db/ident :db.fn/retractEntity])]
[54
:db.fn/cas
([:db/doc "Compare and swap the value of an entity's attribute."]
[:db/code
"(clojure.core/fn [db e a ov nv] (datomic.builtins/compare-and-swap db e a ov nv))"]
[:db/lang :db.lang/clojure]
[:db/ident :db.fn/cas])]
[55
:db.type/uuid
([:db/doc "Value type for UUIDs. Maps to java.util.UUID on the JVM."]
[:fressian/tag :uuid]
[:db/ident :db.type/uuid])]
[56
:db.type/double
([:db/doc
"Floating point value type. Same semantics as a Java double: double-precision 64-bit IEEE 754 floating point."]
[:fressian/tag :double]
[:db/ident :db.type/double])]
[57
:db.type/float
([:db/doc
"Floating point value type. Same semantics as a Java float: single-precision 32-bit IEEE 754 floating point."]
[:fressian/tag :float]
[:db/ident :db.type/float])]
[58
:db.type/uri
([:db/doc "Value type for URIs. Maps to java.net.URI on the JVM."]
[:fressian/tag :uri]
[:db/ident :db.type/uri])]
[59
:db.type/bigint
([:db/doc
"Value type for arbitrary precision integers. Maps to java.math.BigInteger on the JVM."]
[:fressian/tag :bigint]
[:db/ident :db.type/bigint])]
[60
:db.type/bigdec
([:db/doc
"Value type for arbitrary precision floating point numbers. Maps to java.math.BigDecimal on the JVM."]
[:fressian/tag :bigdec]
[:db/ident :db.type/bigdec])]
[61
:db/doc
([:db/doc "Documentation string for an entity."]
[:db/fulltext true]
[:db/cardinality :db.cardinality/one]
[:db/valueType :db.type/string]
[:db/ident :db/doc])])
;;-------------------
;; If we want to search for entities without a certain attribute
(defn attribute-not-set? [db id attr]
(nil? (attr (d/entity db id))))
; without ident, but with doc (I did not find some ..)
(def no-ident (seq (q '[:find ?e
:where
[?e :db/doc]
[(DatomicTrials.core/attribute-not-set? $ ?e :db/ident)]]
(db conn))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment