torsdag 17 juli 2014

Voyuerism in Datomic

Datomic is powerful. Did you know you can see everything you have ever done to your database?

(ns datomic.voyuer
  (:require [datomic.api :as d :refer [db q]]
            [clojure.pprint :refer [pprint]]))

(def uri "datomic:mem://showoff")

(def schema [{:db/id (d/tempid :db.part/db)
               :db/ident :person/name
               :db/valueType :db.type/string
               :db/cardinality :db.cardinality/one
               :db/doc "A persons name"
               :db.install/_attribute :db.part/db}
              {:db/id (d/tempid :db.part/db)
               :db/ident :person/email
               :db/unique :db.unique/identity
               :db/valueType :db.type/string
               :db/cardinality :db.cardinality/one
               :db/doc "A persons email, must be unique"
               :db.install/_attribute :db.part/db}])


(d/create-database uri)
(def conn (d/connect uri))
(d/transact conn schema)

(d/transact conn [{:db/id (d/tempid :db.part/user)
                   :person/name "Linus"
                   :person/email "itisnot@linusericsson.se"}])

(pprint (seq (d/datoms (db conn) :eavt)))


And the result is a sequence of Datoms which can be seen as entities.

These datoms can be decoded as

[Entity id , Attribute (as a pointer to the attributes entity id), Value (as a clojure literal), Tx (as a pointer to the tx-entity), boolean added?].

I did just add empty lines between the datom-numbers and some titles where apropiate. Everything from the system partition the added schema and data is in the list. It's even well documented!

(#datom[0 10 :db.part/db 13194139533312 true] ;; datom 0 is the System partition
 #datom[0 11 0 13194139533366 true] ;;
13194139533366 a txInstant @ beginning of epoch
 #datom[0 11 3 13194139533366 true]
 #datom[0 11 4 13194139533366 true]
 #datom[0 12 20 13194139533366 true]
 #datom[0 12 21 13194139533366 true]
 #datom[0 12 22 13194139533366 true]
 #datom[0 12 23 13194139533366 true]
 #datom[0 12 24 13194139533366 true]
 #datom[0 12 25 13194139533366 true]
 #datom[0 12 26 13194139533366 true]
 #datom[0 12 27 13194139533366 true]
 #datom[0 12 56 13194139533368 true]
 #datom[0 12 57 13194139533368 true]
 #datom[0 12 58 13194139533368 true]
 #datom[0 12 59 13194139533368 true]
 #datom[0 12 60 13194139533368 true]
 #datom[0 12 61 13194139533368 true]
 #datom[0 13 10 13194139533366 true]
 #datom[0 13 11 13194139533366 true]
 #datom[0 13 12 13194139533366 true]
 #datom[0 13 13 13194139533366 true]
 #datom[0 13 14 13194139533366 true]
 #datom[0 13 15 13194139533366 true]
 #datom[0 13 16 13194139533366 true]
 #datom[0 13 17 13194139533366 true]
 #datom[0 13 18 13194139533366 true]
 #datom[0 13 19 13194139533366 true]
 #datom[0 13 39 13194139533366 true]
 #datom[0 13 40 13194139533366 true]
 #datom[0 13 41 13194139533366 true]
 #datom[0 13 42 13194139533366 true]
 #datom[0 13 43 13194139533366 true]
 #datom[0 13 44 13194139533366 true]
 #datom[0 13 45 13194139533366 true]
 #datom[0 13 46 13194139533366 true]
 #datom[0 13 47 13194139533366 true]
 #datom[0 13 50 13194139533366 true]
 #datom[0 13 51 13194139533366 true]
 #datom[0 13 52 13194139533366 true]
 #datom[0 13 62 13194139533368 true]
 #datom[0 13 63 13194139534312 true]
 #datom[0 13 64 13194139534312 true]
 #datom[0 14 54 13194139533366 true]
 #datom[0 14 55 13194139533366 true]
 #datom[0 62 "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." 13194139533375 true]



 #datom[1 10 :db/add 13194139533312 true]
 #datom[1 62 "Primitive assertion. All transactions eventually reduce to a collection of primitive assertions
 and retractions of facts, e.g. [:db/add fred :age 42]." 13194139533375 true]


 #datom[2 10 :db/retract 13194139533312 true]
 #datom[2 62 "Primitive retraction. All transactions eventually reduce to a collection of assertions and retractions of facts, e.g. [:db/retract fred :age 42]." 13194139533375 true]


 #datom[3 10 :db.part/tx 13194139533312 true]
 #datom[3 62 "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." 13194139533375 true]


 #datom[4 10 :db.part/user 13194139533312 true]
 #datom[4 62 "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." 13194139533375 true]


 #datom[10 10 :db/ident 13194139533312 true]
 #datom[10 40 21 13194139533366 true]
 #datom[10 41 35 13194139533366 true]
 #datom[10 42 38 13194139533366 true]
 #datom[10 62 "Attribute used to uniquely name an entity." 13194139533375 true]


 #datom[11 10 :db.install/partition 13194139533312 true]
 #datom[11 40 20 13194139533366 true]
 #datom[11 41 36 13194139533366 true]
 #datom[11 62 "System attribute with type :db.type/ref. Asserting this attribute on :db.part/db with value v will install v as a partition." 13194139533375 true]


 #datom[12 10 :db.install/valueType 13194139533312 true]
 #datom[12 40 20 13194139533366 true]
 #datom[12 41 36 13194139533366 true]
 #datom[12 62 "System attribute with type :db.type/ref. Asserting this attribute on :db.part/db with value v will install v as a value type." 13194139533375 true]


 #datom[13 10 :db.install/attribute 13194139533312 true]
 #datom[13 40 20 13194139533366 true]
 #datom[13 41 36 13194139533366 true]
 #datom[13 62 "System attribute with type :db.type/ref. Asserting this attribute on :db.part/db with value v will install v as an attribute." 13194139533375 true]


 #datom[14 10 :db.install/function 13194139533312 true]
 #datom[14 40 20 13194139533366 true]
 #datom[14 41 36 13194139533366 true]
 #datom[14 62 "System attribute with type :db.type/ref. Asserting this attribute on :db.part/db with value v will install v as a data function." 13194139533375 true]


 #datom[15 10 :db/excise 13194139533312 true]
 #datom[15 40 20 13194139533366 true]
 #datom[15 41 35 13194139533366 true]


 #datom[16 10 :db.excise/attrs 13194139533312 true]
 #datom[16 40 20 13194139533366 true]
 #datom[16 41 36 13194139533366 true]


 #datom[17 10 :db.excise/beforeT 13194139533312 true]
 #datom[17 40 22 13194139533366 true]
 #datom[17 41 35 13194139533366 true]


 #datom[18 10 :db.excise/before 13194139533312 true]
 #datom[18 40 25 13194139533366 true]
 #datom[18 41 35 13194139533366 true]


 #datom[19 10 :db.alter/attribute 13194139533312 true]
 #datom[19 40 20 13194139533366 true]
 #datom[19 41 36 13194139533366 true]
 #datom[19 62 "System attribute with type :db.type/ref. Asserting this attribute on :db.part/db with value v will alter the definition o
f existing attribute v." 13194139533375 true]


 #datom[20 10 :db.type/ref 13194139533312 true]
 #datom[20 39 :ref 13194139533366 true]
 #datom[20 62 "Value type for references. All references from one entity to another are through attributes with this value type." 13194139533375 true]


 #datom[21 10 :db.type/keyword 13194139533312 true]
 #datom[21 39 :key 13194139533366 true]
 #datom[21 62 "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." 13194139533375 true]


 #datom[22 10 :db.type/long 13194139533312 true]
 #datom[22 39 :int 13194139533366 true]
 #datom[22 62 "Fixed integer value type. Same semantics as a Java long: 64 bits wide, two's complement binary representation." 13194139533375 true]


 #datom[23 10 :db.type/string 13194139533312 true]
 #datom[23 39 :string 13194139533366 true]
 #datom[23 62 "Value type for strings." 13194139533375 true]


 #datom[24 10 :db.type/boolean 13194139533312 true]
 #datom[24 39 :bool 13194139533366 true]
 #datom[24 62 "Boolean value type." 13194139533375 true]


 #datom[25 10 :db.type/instant 13194139533312 true]
 #datom[25 39 :inst 13194139533366 true]
 #datom[25 62 "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." 13194139533375 true]


 #datom[26 10 :db.type/fn 13194139533312 true]
 #datom[26 39 :datomic/fn 13194139533366 true]
 #datom[26 62 "Value type for database functions. See Javadoc for Peer.function." 13194139533375 true]


 #datom[27 10 :db.type/bytes 13194139533312 true]
 #datom[27 39 :bytes 13194139533366 true]
 #datom[27 62 "Value type for small binaries. Maps to byte array on the JVM." 13194139533375 true]


 #datom[35 10 :db.cardinality/one 13194139533312 true]
 #datom[35 62 "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 a
ttributes." 13194139533375 true]


 #datom[36 10 :db.cardinality/many 13194139533312 true]
 #datom[36 62 "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." 13194139533375 true]


 #datom[37 10 :db.unique/value 13194139533312 true]
 #datom[37 62 "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." 13194139533375 true]


 #datom[38 10 :db.unique/identity 13194139533312 true]
 #datom[38 62 "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." 13194139533375 true]


 #datom[39 10 :fressian/tag 13194139533312 true]
 #datom[39 40 21 13194139533366 true]
 #datom[39 41 35 13194139533366 true]
 #datom[39 44 true 13194139533366 true]
 #datom[39 62 "Keyword-valued attribute of a value type that specifies the underlying fressian type
used for serialization." 13194139533375 true]


 #datom[40 10 :db/valueType 13194139533312 true]
 #datom[40 40 20 13194139533366 true]
 #datom[40 41 35 13194139533366 true]
 #datom[40 62 "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." 13194139533375 true]


 #datom[41 10 :db/cardinality 13194139533312 true]
 #datom[41 40 20 13194139533366 true]
 #datom[41 41 35 13194139533366 true]
 #datom[41 62 "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." 13194139533375 true]


 #datom[42 10 :db/unique 13194139533312 true]
 #datom[42 40 20 13194139533366 true]
 #datom[42 41 35 13194139533366 true]
 #datom[42 62 "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." 13194139533375 true]


 #datom[43 10 :db/isComponent 13194139533312 true]
 #datom[43 40 24 13194139533366 true]
 #datom[43 41 35 13194139533366 true]
 #datom[43 62 "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." 13194139533375 true]


 #datom[44 10 :db/index 13194139533312 true]
 #datom[44 40 24 13194139533366 true]
 #datom[44 41 35 13194139533366 true]
 #datom[44 62 "Property of an attribute. If true, create an AVET index for th
e attribute. Defaults to false." 13194139533375 true]


 #datom[45 10 :db/noHistory 13194139533312 true]
 #datom[45 40 24 13194139533366 true]
 #datom[45 41 35 13194139533366 true]
 #datom[45 62 "Property of an attribute. If true, past values of the attribute are not retained after indexing. Defaults to false." 13194139533375 true]


 #datom[46 10 :db/lang 13194139533312 true]
 #datom[46 40 20 13194139533366 true]
 #datom[46 41 35 13194139533366 true]
 #datom[46 62 "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" 13194139533375 true]


 #datom[47 10 :db/code 13194139533312 true]
 #datom[47 40 23 13194139533366 true]
 #datom[47 41 35 13194139533366 true]
 #datom[47 51 true 13194139533366 true]
 #datom[47 62 "String-valued attribute of a data function that contains the function's source code." 13194139533375 true]


 #datom[48 10 :db.lang/clojure 13194139533312 true]
 #datom[48 62 "Value of :db/lang attribute, spec
ifying that a data function is implemented in Clojure." 13194139533375 true]


 #datom[49 10 :db.lang/java 13194139533312 true]
 #datom[49 62 "Value of :db/lang attribute, specifying that a data function is implemented in Java." 13194139533375 true]


 #datom[50 10 :db/txInstant 13194139533312 true]
 #datom[50 40 25 13194139533366 true]
 #datom[50 41 35 13194139533366 true]
 #datom[50 44 true 13194139533366 true]
 #datom[50 62 "Attribute whose value is a :db.type/instant. A :db/txInstant is recorded automatically with every transaction." 13194139533375 true]


 #datom[51 10 :db/fulltext 13194139533312 true]
 #datom[51 40 24 13194139533366 true]
 #datom[51 41 35 13194139533366 true]
 #datom[51 62 "Property of an attribute. If true, create a fulltext search index for the attribute. Defaults to false." 13194139533375 true]


 #datom[52 10 :db/fn 13194139533312 true]
 #datom[52 40 26 13194139533366 true]
 #datom[52 41 35 13194139533366 true]
 #datom[52 62 "A function-valued attribute for direct use by transactions and qu
eries." 13194139533375 true]


 #datom[53 10 :db.bootstrap/part 13194139533312 true]


 #datom[54 10 :db.fn/retractEntity 13194139533366 true]
 #datom[54 46 48 13194139533366 true]
 #datom[54 47 "(clojure.core/fn [db e] (datomic.builtins/build-retract-args db e))" 13194139533366 true]
 #datom[54 62 "Retract all facts about an entity, including references from other entities and component attributes recursively." 13194139533375 true]


 #datom[55 10 :db.fn/cas 13194139533366 true]
 #datom[55 46 48 13194139533366 true]
 #datom[55 47 "(clojure.core/fn [db e a ov nv] (datomic.builtins/compare-and-swap db e a ov nv))" 13194139533366 true]
 #datom[55 62 "Compare and swap the value of an entity's attribute." 13194139533375 true]

Database value types: 

#datom[56 10 :db.type/uuid 13194139533368 true]
 #datom[56 39 :uuid 13194139533368 true]
 #datom[56 62 "Value type for UUIDs. Maps to java.util.UUID on the JVM." 13194139533375 true]


 #datom[57 10 :db.type/double 13194139533368 true]
 #datom[57 39 :double 13194139533368 true]
 #datom[57 62 "Float
ing point value type. Same semantics as a Java double: double-precision 64-bit IEEE 754 floating point." 13194139533375 true]


 #datom[58 10 :db.type/float 13194139533368 true]
 #datom[58 39 :float 13194139533368 true]
 #datom[58 62 "Floating point value type. Same semantics as a Java float: single-precision 32-bit IEEE 754 floating point." 13194139533375 true]


 #datom[59 10 :db.type/uri 13194139533368 true]
 #datom[59 39 :uri 13194139533368 true]
 #datom[59 62 "Value type for URIs. Maps to java.net.URI on the JVM." 13194139533375 true]


 #datom[60 10 :db.type/bigint 13194139533368 true]
 #datom[60 39 :bigint 13194139533368 true]
 #datom[60 62 "Value type for arbitrary precision integers. Maps to java.math.BigInteger on the JVM." 13194139533375 true]


 #datom[61 10 :db.type/bigdec 13194139533368 true]
 #datom[61 39 :bigdec 13194139533368 true]
 #datom[61 62 "Value type for arbitrary precision floating point numbers. Maps to java.math.BigDecimal on the JVM." 13194139533375 true]


 #datom[62 10 :db/doc 13194139533368 true]
 #datom[62 40 23 13194139533368 true]
 #datom[62 41 35 13194139533368 true]
 #datom[62 51 true 13194139533368 true]
 #datom[62 62 "Documentation string for an entity." 13194139533375 true]

The added schema:

 #datom[63 10 :person/name 13194139534312 true]
 #datom[
63 40 23 13194139534312 true]
 #datom[
63 41 35 13194139534312 true]
 #datom[
63 62 "A persons name" 13194139534312 true]

 #datom[64 10 :person/email 13194139534312 true]
 #datom[
64 40 23 13194139534312 true]
 #datom[
64 41 35 13194139534312 true]
 #datom[
64 42 38 13194139534312 true]
 #datom[
64 62 "A persons email, must be unique" 13194139534312 true]

TxInstants, starts at entity id 13194139533366:

 #datom[13194139533366 50 #inst "1970-01-01T00:00:00.000-00:00" 13194139533366 true]
 #datom[13194139533368 50 #inst "1970-01-01T00:00:00.000-00:00" 13194139533368 true]
 #datom[13194139533375 50 #inst "1970-01-01T00:00:00.000-00:00" 13194139533375 true]
 #datom[13194139534312 50 #inst "2014-07-17T16:30:23.535-00:00" 13194139534312 true]
 #datom[13194139534313 50 #inst "2014-07-17T16:30:23.541-00:00" 13194139534313 true]

Added facts

 #datom[17592186045418 63 "Linus" 13194139534313 true]
 #datom[17592186045418 64 "itisnot@linusericsson.se" 13194139534313 true])

Inga kommentarer:

Skicka en kommentar