the power of datomic

64
The power of Datomic Konrad Szydlo @ryujinkony

Upload: konrad-szydlo

Post on 15-Jul-2015

321 views

Category:

Software


4 download

TRANSCRIPT

Page 1: The power of datomic

The power of Datomic

Konrad Szydlo@ryujinkony

Page 2: The power of datomic
Page 3: The power of datomic

Independent components

● Storage● Reading● Writes● Query

Page 4: The power of datomic

Main features

● No update-in place● Only assertions and retractions● Retraction != removal● Immutable data● Database as a value● Flexible schema● ACID transactions● Declarative and logic data programming

Page 5: The power of datomic

Datomic's Reality model

● Based on Clojure's reality model● Value● Identity● State● Time

Page 6: The power of datomic

Value

● Never changing property● Stable, immutable● 44● “John”● 44 != 61● Same meaning regardless of context

Page 7: The power of datomic

Identity

● Named entity in a system● May have different values● At different points in time● “John”● “Johnny”● “ElfenLied”

Page 8: The power of datomic

State

● Value of an identity● At a point in time

Page 9: The power of datomic

Time

● By-product of ordering states within an entity● Linear progression● Morning => ElfenLied● Afternoon => John● Evening => ElfenLied

Page 10: The power of datomic

Datom

● Basic unit of operation in Datomic● Also called a fact● Made up of five parts

Page 11: The power of datomic

Datom example

Entity attribute value Tx operation

2439 name John at work add

2439 name John playing retract

2439 name ElfenLied playing add

Page 12: The power of datomic

Not only “now”

● Easy access to past and future states● What was the value of x one week ago?● What was the state of DB before app crashed?● Not so easy in RDBMS

Page 13: The power of datomic

Everything is data

● Datomic operates on data● Lists, maps, vectors, keywords, strings● Even functions, schema is data● Possible to run queries on all data

Page 14: The power of datomic

Transactor

● Single instance● Handles all writes● Second instance on stand-by● Ensures ACID properties● Notifies transaction submitter and all peers

(other applications) when transaction is persisted

Page 15: The power of datomic

Storage

● Not directly in Datomic● Leverages storage services:

– DynamoDB

– Riak

– Infinispan

– SQL storages

● Storing segments of datoms not individual ones● Log storage – chronological● Indices storage – various orders of datoms

Page 16: The power of datomic

Indices

● Entity / Attribute / Value / Transaction● Sorted order● EAVT – all datoms (SQL “row-like” view)● AEVT – all datoms (SQL “column-like” view)● AVET – unique datoms● VAET – reference attributes

Page 17: The power of datomic

EAVT

● EAVT – all datoms (SQL “row-like” view)

134 name Tim 4592 add

586 city 32 1975 add

586 gender male 4592 add

586 name John 4592 add

976 name Rob 4938 add

Page 18: The power of datomic

AEVT

● AEVT – all datoms (SQL “column-like” view)

city 986 32 1975 add

name 134 Tim 4592 add

name 576 Rob 4938 add

name 986 John 4592 add

title 367 Lord Jim 4592 add

Page 19: The power of datomic

AVET

● AVET – unique datoms

city 32 986 1975 add

name John 986 4592 add

name Rob 576 4938 add

name Tim 134 4592 add

title Lord Jim 367 4592 add

Page 20: The power of datomic

VAET

● VAET – reference attributes

15 author 841 1975 add

32 city 134 4592 add

269 city 576 4938 add

517 city 986 4592 add

male gender 986 4592 add

Page 21: The power of datomic

Value of DB

● View of db is a value● Immutable● Graph of entities and their attributes● Direct iterable access to indices● DB value is a param to queries and functions

Page 22: The power of datomic

Datalog

● Default query language● Declarative – WHAT not HOW● Logic – patterns matching● Logical variables

Page 23: The power of datomic

Query structure

● :find – projection clause, similar to SELECT● :in – binding arguments

– Implicit for some queries

● :where – restriction clause● Most of the logic would be in :where clause

Page 24: The power of datomic

Simple schema

person

name Joseph Conrad

name George Orwell

genre

type novel

type fiction

Page 25: The power of datomic

Schema

book

:book/author Joseph Conrad

:book/title Lord Jim

:book/genre novel

Page 26: The power of datomic

Simple Query

● Find titles for all books

[:find ?title

 :where [?book :book/title ?title]]

↓ ↓ ↓

        (Entity Attribute  Value)

● Binding on ?title and ?book

Page 27: The power of datomic

● Find all book titles for Joseph Conrad

[:find ?title

 :where [?book :book/author ?author]

  [?author :person/name “Joseph Conrad”]

  [?book :book/title ?title]]

Page 28: The power of datomic

[:find ?title

 :where [?book :book/author ?author]

  [?author :person/name “Joseph Conrad”]

  [?book :book/title ?title]]

● ?book resolves to the same entity

● ?author resolves to the same entity

Page 29: The power of datomic

Order in query

[:find ?title

 :where 

  [?author :person/name “Joseph Conrad”]

  [?book :book/author ?author]   

  [?book :book/title ?title]]

Page 30: The power of datomic

● Queries are semantically similar● Marginal performance difference● Provide specific datoms first

Page 31: The power of datomic

Even more attributes in query

[:find ?title ?author ?year

 :where

 [?b :book/genre :genre.type/novel] 

 [?b :book/year ?year]

 [?b :book/author ?p]

 [?p :person/name ?author]

 [?b :book/title ?title]]

Page 32: The power of datomic

Pull API

● New addition to Datomic● Allows easy access to datom's attributes● Provides a number of patterns for querying

Page 33: The power of datomic

Wildcard pattern

● Return all attributes with *

(pull

  database­value

  '[*]

  entity­id)

Page 34: The power of datomic

Specific attributes

● Use pattern to return specific attributes

(pull

  database­value

  '[:book/title :book/genre]

  lord­jim)

Page 35: The power of datomic

Schema with reference

{:db/id #db/id[:db.part/db]

:db/ident :book/genre

:db/valueType :db.type/ref

:db/cardinality :db.cardinality/one

:db/doc "Book's genre"

:db.install/_attribute :db.part/db}

Page 36: The power of datomic

Lookup reverse

● Navigate backwards from declared reference● Get fiction books

(pull

  database­value

  '[:book/_genre]

  :genre.type/fiction)

Page 37: The power of datomic

German philosophers

● Kepler (1571 - 1630)● Leibniz (1646 - 1716)● Kant (1724 - 1804)● Hegel (1770 - 1831)● Heidegger (1889 – 1976)

● attribute :person/influenced-by

Page 38: The power of datomic

Recursion

● Who influenced Kant?

(pull

  database­value

  '[:person/name {:person/influenced­by 5}]

  kant)

● Gottfried Leibniz <= Kepler

Page 39: The power of datomic

● Kepler (1571 - 1630)● Leibniz (1646 - 1716)● Kant (1724 - 1804)● Hegel (1770 - 1831)● Heidegger (1889 – 1976)

Page 40: The power of datomic

Recursion + reverse lookup

● Who was influenced by Kant?

(pull

  database­value

  '[:person/name {:person/_influenced­by 5}]

 kant)

● Hegel => Heidegger

Page 41: The power of datomic

Java methods

[:find ?name

 :where 

 [?p :person/name ?name]

 [(.startsWith ?name "M")]]

Page 42: The power of datomic

Functions

● Atomic transformations● Integrity checks● Constraints

Page 43: The power of datomic

DB function components

● Function is a data structure● Declared with :db/fn● Optional docs● Clojure or java● Parameters● Require (Clojure) or import (Java) block● Your code

Page 44: The power of datomic

DB function

{:db/ident :get­k­volume

 :db/doc "Gets number of book copies in 1000s”

 :db/fn #db/fn 

   {:lang "clojure"

    :params [no­of­copies]  

    :requires [[datomic­book.utils :refer [format­k]]]

    :code [(format­k (/ no­of­copies 1000) )]}}

Page 45: The power of datomic

Using db functions

(invoke

  database­value

  :get­k­volume

  4000)

=> ["4 K"]

Page 46: The power of datomic

Validation function

{:db/ident :validate­book

  :db/fn #db/fn

    {:lang "clojure"

     :params [book]

     :requires [[clojure.string :as str]]

     :code (let [required #{:book/title :book/author}

                 missing (remove book required)]

                 (if­let [missing (seq missing)]

                    (throw (RuntimeException. 

                              (str "Missing attributes"

                              (str/join ", " missing))))

                     book))}}

Page 47: The power of datomic

Construction function

{:db/ident :validate­and­construct­book

 :db/fn #db/fn

  {:lang "clojure"

   :params [db m]

   :code (let [book (merge {:db/id

                          (tempid :db.part/user)}

                            m)

               validate (→ (entity db :validate­book)

                            :db/fn)]

               [(validate book)])}}

Page 48: The power of datomic

(transact

   database­value

   [[:validate­and­construct­book

     {:book/title "Victory"

      :book/author joseph­conrad}]])

Page 49: The power of datomic

Database filters

● Filter DB value based on some predicate● Keep only relevant datoms● Built-in filters and custom filters● Filters allow for one set of queries operating on

different db values

Page 50: The power of datomic

As-of

● Returns DB value “as of” particular point in time● Ignores any transactions after that point● Point-in-time could be:

– Transaction id

– java.util.Date instance

– Time-basis (t) of database

● What was the DB last week, month etc?

Page 51: The power of datomic

(find­population

  (as­of database berlin­1850­tx­date)

  berlin)

Page 52: The power of datomic

Since

● Opposite of as-of● Returns value of database that includes only

datoms added after certain point in time

● What were the transactions after X point in time?

Page 53: The power of datomic

(find­population

  (since database berlin­1850­tx­date)

  berlin)

Page 54: The power of datomic

Latest changes

(def tx­time #inst "2014­12­25T10:33:16.436­00:00")

(map :e (datoms (since berlin­1850 tx­time) :eavt))

● Get datoms from filtered db using since

● Get datoms using :eavt index

● Get entity ids from datoms

Page 55: The power of datomic

history

● Present and all of the past unfiltered● Complete history of entity● Or group of entities

Page 56: The power of datomic

(­>> (d/q '[:find ?aname ?v ?inst

            :in $ ?e

            :where [?e ?a ?v ?tx true]

            [?tx :db/txInstant ?inst]

            [?a :db/ident ?aname]]

          (history database) berlin)

     (filter

       #(some #{:city/population} %))

     (sort­by #(nth % 2)))

([:city/population 260000 Wed Dec 31 08:57:09 GMT 2014]

 [:city/population 383000 Thu Jan 01 06:00:55 GMT 2015])

Page 57: The power of datomic

Filtering errors

● Custom filter for:– incorrect datoms

– Not applicable data at some point in time

– Security reasons

Page 58: The power of datomic

● Add “wrong” population

(def berlin­error

  @(transact

     connection

     [{:db/id           berlin

       :city/population 999999}]))

Page 59: The power of datomic

(def error­tx­id

  (:tx (first (berlin­error :tx­data))))

(def error­txes

  "Known bad transactions"

  #{error­tx­id})

Page 60: The power of datomic

(defn correct?

  [_ datom]

  (not (contains? error­txes (:tx datom))))

(def corrected (filter (berlin­history) correct?))

Page 61: The power of datomic

(get­population­history corrected)

; id population tx

=> #{[17592186045421 383000 13194139534329] [17592186045421 139700 13194139534327] [17592186045421 163600 13194139534328] [17592186045421 686000 13194139534331] [17592186045421 63400 13194139534324] [17592186045421 39000 13194139534313]}

Page 62: The power of datomic

● Still more from Datomic:– Negation in query

– Retraction

– Excision – true removal

– Partitioning

– Transactions

– with – state with proposed additions● What would happen if we did x?

– More in depth on covered topics

Page 63: The power of datomic

Resources

● http://docs.datomic.com● http://www.datomic.com/training.html● http://www.datomic.com/videos.html● http://www.learndatalogtoday.org

Page 64: The power of datomic

Thank you

● Datomic is awesome● [email protected]● @ryujinkony