Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Molecule Scala meta-DSL for Datomic

204 views

Published on

Presentation of Molecule, a meta-DSL for Datomic by the creator, Marc Grue. Talk from a Scala group MeetUp in Gothenburg, Sweden on April 25, 2017.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Molecule Scala meta-DSL for Datomic

  1. 1. Scala meta-DSL for Datomic Marc Grue April 2017 Molecule
  2. 2. Molecule Introduction to Datomic Building a DSL with Scala macros Tour of Molecule features Molecule and domain modelling 2 3 1 4
  3. 3. Molecule • Facts • Entities • Immutability • Time • Architecture 1 Datomic
  4. 4. Molecule Key-Value 32 pizza
  5. 5. Molecule Fact Fred likes pizza
  6. 6. Molecule Fact Entity Attribute Value Fred likes pizza
  7. 7. Molecule Fact Entity Attribute Value Time Fred likes pizza 10:31
  8. 8. Molecule Fact Entity Attribute Value Time Op Fred likes pizza 11:16 retract
  9. 9. Molecule Datom Entity Attribute Value Tx (time) Operation fred (eid) likes pizza tx4 assert/retract
  10. 10. Molecule Datom fred likes pizza tx4 assert/retract E A V T Op
  11. 11. Molecule Datom fred :person/likes pizza tx4 assert/retract E A V T Op Namespace
  12. 12. Molecule Schema of attributes eid :person/likes String value … … E A V T Op {":db/ident" ":person/likes"} Attribute Value
  13. 13. Molecule Schema of attributes eid :person/likes String value … … E A V T Op string boolean long bigint float double big dec ref instant (date) uuid uri bytes keyword (enum) Types {":db/ident" ":person/likes" ":db/valueType" ":db.type/string"}
  14. 14. Molecule Schema of attributes eid :person/likes String value … … E A V T Op one many Cardinality{":db/ident" ":person/likes" ":db/valueType" ":db.type/string" ":db/cardinality" ":db.cardinality/one"}
  15. 15. Molecule Schema of attributes eid :person/likes String value … … E A V T Op {":db/ident" ":person/likes" ":db/valueType" ":db.type/string" ":db/cardinality" ":db.cardinality/one" ":db/doc" "What a Person likes"} doc unique/value unique/identity index fulltext isComponent noHistory Options
  16. 16. Molecule 101 :person/name Fred 101 :person/likes pizza 101 :person/age 38 101 :person/addr 102 Common entity id Entity
  17. 17. Molecule Entity 101 :person/name Fred 101 :person/likes pizza 101 :person/age 38 101 :person/addr 102 101 :site/cat customer Common entity id Entities can span any attributes!
  18. 18. Molecule Entity eid 101 :person/name Fred :person/likes pizza :person/age 38 :person/addr 102 :site/cat customer Group of facts with shared entity id
  19. 19. Molecule Relationships eid 102 :addr/street Baker st. :addr/zip 90221 :site/cat identifier eid 101 :person/name Fred :person/likes pizza :person/age 38 :person/addr 102 :site/cat customer
  20. 20. Molecule Relationships eid 104 :person/name Ben :person/age 5 eid 101 :person/name Fred :person/likes pizza :person/age 38 :person/kids 103, 104 :site/cat customer eid 103 :person/name Lisa :person/age 7
  21. 21. Molecule fred :person/likes pizza tx4 (10:31) assert Mutable E A V T Op Immutable facts Id likes fred pizza … … Person table { id: fred likes: “pizza” } Person document
  22. 22. Molecule Id likes created fred pizza 10:31 … … … Person table E A V T Op fred :person/likes pizza tx4 (10:31) assert Create Assert { id: fred likes: “pizza” created: “10:31” } Person document
  23. 23. Molecule fred :person/likes pizza tx5 (12:45) retract fred :person/likes pasta tx5 (12:45) assert Id likes created updated fred pasta 10:31 12:45 … … … fred :person/likes pizza tx4 (10:31) assert Update Person table Retract-Assert Person document { id: fred likes: pasta created: “10:31” updated: “12:45” } E A V T Op
  24. 24. Molecule fred :person/likes pasta tx6 (17:10) retract fred :person/likes pizza tx5 (12:45) retract fred :person/likes pasta tx5 (12:45) assert Id likes created updated fred NULL 10:31 17:10 … … … Delete Person table Retract E A V T Op Person document { id: fred likes: null created: “10:31” updated: “17:10” } fred :person/likes pizza tx4 (10:31) assert
  25. 25. Molecule 101 :person/likes pizza tx4 (10:31) assert 101 :person/likes pizza tx5 (12:45) retract 101 :person/likes pasta tx5 (12:45) assert 101 :person/likes pasta tx6 (17:10) retract Read E A V T Op [:find ?v :where [101 :person/likes ?v]] => Nil (no current likes)
  26. 26. Molecule 101 :person/likes pizza tx4 (10:31) assert 101 :person/likes pizza tx5 (12:45) retract 101 :person/likes pasta tx5 (12:45) assert 101 :person/likes pasta tx6 (17:10) retract asOf E A V T Op [:find ?v :in db.asOf(tx5) :where [101 :person/likes ?v]] => [pasta]
  27. 27. Molecule 101 :person/likes pizza tx4 (10:31) assert 101 :person/likes pizza tx5 (12:45) retract 101 :person/likes pasta tx5 (12:45) assert 101 :person/likes pasta tx6 (17:10) retract History E A V T Op [:find ?v ?t ?op :in db.history :where [101 :person/likes ?v ?t ?op]] => [pizza tx4 assert ] [pasta tx5 retract] [pasta tx5 assert ] [pasta tx6 retract]
  28. 28. Molecule 101 :person/likes pizza tx1 assert 102 :person/likes pasta tx2 assert 103 :person/likes sushi tx3 assert 104 :person/likes burger tx4 assert Since E A V T Op [:find ?v :in db.since(tx2) :where [?e :person/likes ?v]] => [sushi burger] (updated slide)
  29. 29. Molecule 101 :person/likes pizza tx4 (10:31) assert 101 :person/likes pizza tx5 (12:45) retract 101 :person/likes pasta tx5 (12:45) assert 101 :person/likes pasta tx6 (17:10) retract With (the future!) E A V T Op [:find ?v :in db.with(testDataTx) :where [101 :person/likes ?v]] => [penne] 101 :person/likes penne tx7 assert testDataTx = { id: 101 likes: “penne” } As though we had created this fact
  30. 30. Molecule Datomic architecture Expensive connection App process (JVM) App All-in-one Database server
  31. 31. Molecule App process (JVM) Datomic architectureApp Peer Transactor • Separating Reads and writes in 2 processes! • Peer lib in your app process • Multiple Peers possible • Not a client-server (optional) • No network traffic Reads Writes
  32. 32. Molecule App process (JVM) Datomic architectureApp Peer Query Transactor Transactions Storage Service • In-memory • SQL • DynamoDB • Cassandra Reads Writes
  33. 33. Molecule App process (JVM) Datomic architectureApp Peer Commit Query Cache Transactor Transactions Storage Service
  34. 34. Molecule App process (JVM) Datomic architectureApp Peer Commit Query Live Index Cache Transactor TransactionsIndexing Storage Service
  35. 35. Molecule App process (JVM) Datomic architectureApp Peer Commit Query Live Index Cache Transactor TransactionsIndexing Storage Service Memcached cluster
  36. 36. Molecule Datomic 2
  37. 37. Molecule [:find ?name ?age :where [?e :person/name ?name] [?e :person/age ?age]]
  38. 38. Molecule [:find ?name ?age :where [?e :person/name ?name] [?e :person/age ?age]]
  39. 39. Molecule [:find ?name ?age :where [?e :person/name ?name] [?e :person/age ?age]]
  40. 40. Molecule
  41. 41. Molecule (Previously `Seq`)
  42. 42. Molecule
  43. 43. Molecule
  44. 44. Molecule
  45. 45. Molecule Macro should create this:
  46. 46. Molecule
  47. 47. Molecule
  48. 48. Molecule
  49. 49. Molecule
  50. 50. Molecule
  51. 51. Molecule Molecule DSL Molecules Your domain terms Namespace "Person"
  52. 52. Molecule Molecule DSL Molecules Your domain terms Namespace "Person" Attribute :person/name Person1[String]
  53. 53. Molecule Molecule DSL Molecules Your domain terms Namespace "Person" Attribute :person/name Person1[String] No need to implement here - macro implements! 
 Only the return type is needed!
  54. 54. Molecule Molecule DSL Molecules Your domain terms Namespace "Person" Attribute :person/name Person1[String] Attribute :person/age Person2[String, Int]
  55. 55. Molecule Molecule DSL MoleculesRuntime API M2[String, Int] Returns Seq[(String, Int)]
  56. 56. Molecule Molecule DSL MoleculesRuntime API M2[String, Int] Returns Seq[(String, Int)] Implemented by macro
  57. 57. Molecule
  58. 58. Molecule Molecule DSL Molecules A lot of redundant boilerplate code!
  59. 59. Molecule Schema definition Molecule DSL SBT plugin generates Molecules
  60. 60. Molecule Schema definition Molecule DSL SBT plugin generates Molecules Namespaces
  61. 61. Molecule Schema definition Molecule DSL SBT plugin generates Molecules Attributes
  62. 62. Molecule Schema definition Molecule DSL SBT plugin generates Molecules Cardinality /Types
  63. 63. Molecule Schema def DSL Schema definition Molecule DSL SBT plugin generates Molecules Card/type oneString manyString oneInt manyInt one[Address] many[InvoiceLine] ...
  64. 64. Molecule Schema def DSL Schema definition Molecule DSL SBT plugin generates Molecules oneString.fulltext.doc("name of person") manyString oneInt.doc("age of person") manyInt one[Address] many[InvoiceLine].subComponents etc... Card/type Options
  65. 65. Molecule Define schema Sbt compile (once) Molecule DSL Datomic DSL Compose Molecules Get/Set
 Datomic Data Compile Run Schema definition DSL Molecule eco system
  66. 66. Molecule Define schema Sbt compile (once) Molecule DSL Datomic DSL Compose Molecules Get/Set
 Datomic Data Compile Run Schema definition DSL You'll be here most of the time Molecule eco system
  67. 67. Molecule Define schema Sbt compile (once) Molecule DSL Datomic DSL Compose Molecules Get/Set
 Datomic Data Compile Run Schema definition DSL A DSL to create a DSL to create a DSL ... must be a meta-DSL, no? You'll be here most of the time Molecule eco system
  68. 68. Tour... Molecule 3
  69. 69. Molecule Core • Entities • Attributes • Expressions • Relationships • Composites • CRUD Extras • Map attributes • Input molecules • Bidirectional refs Time • Tx data • Tx meta data • asOf • history • since • with
  70. 70. Molecule Entitieseid 101 :person/name Fred :person/likes pizza :person/age 38 :site/cat customer Groups of facts with common entity id
  71. 71. Molecule Entitieseid 101 :person/name Fred :person/likes pizza :person/age 38 :site/cat customer Groups of facts with common entity id How can we know what facts are grouped at runtime? - the schema doesn't tell us...
  72. 72. Molecule Entitieseid 101 :person/name Fred :person/likes pizza :person/age 38 :site/cat customer Entity API
  73. 73. Molecule Entitieseid 101 :person/name Fred :person/likes pizza :person/age 38 :site/cat customer Entity API
  74. 74. Molecule Attributes eid 104 :person/name Ben :person/likes pizza :person/age 5 eid 101 :person/name Fred :person/likes pizza :person/age 38 :site/cat customer eid 103 :person/name Lisa :person/age 7 Ns.attr --> :ns/attr "Person names"
  75. 75. Molecule Attributes eid 104 :person/name Ben :person/likes pizza :person/age 5 eid 101 :person/name Fred :person/likes pizza :person/age 38 :site/cat customer eid 103 :person/name Lisa :person/age 7 Ns.attr.attr2... --> :ns/attr :ns/attr2 ... "Person names and ages"
  76. 76. Molecule Attributes eid 104 :person/name Ben :person/likes pizza :person/age 5 eid 101 :person/name Fred :person/likes pizza :person/age 38 :site/cat customer eid 103 :person/name Lisa :person/age 7 "Person names, ages and likes"
  77. 77. Molecule Attributes eid 104 :person/name Ben :person/likes pizza :person/age 5 eid 101 :person/name Fred :person/likes pizza :person/age 38 :site/cat customer eid 103 :person/name Lisa :person/age 7 • Mandatory ("attr") • Fact exists • Value is returned • Tacet • Optional"Person names, ages and likes"
  78. 78. Molecule Attributes eid 104 :person/name Ben :person/likes pizza :person/age 5 eid 101 :person/name Fred :person/likes pizza :person/age 38 :site/cat customer eid 103 :person/name Lisa :person/age 7 • Mandatory ("attr") • Fact exists • Value returned • Tacet ("attr_") • Fact exists • Value not returned • Optional "Names and ages of Persons that like something"
  79. 79. Molecule Attributes eid 104 :person/name Ben :person/likes pizza :person/age 5 eid 101 :person/name Fred :person/likes pizza :person/age 38 :site/cat customer eid 103 :person/name Lisa :person/age 7 • Mandatory ("attr") • Fact exists • Value returned • Tacet ("attr_") • Fact exists • Value not returned • Optional ("attr$") • Fact may exists • Option[Value] returned "Names, ages and possibly likes of Persons"
  80. 80. Molecule Expressions eid 104 :person/name Ben :person/likes pizza :person/age 5 eid 101 :person/name Fred :person/likes pizza :person/age 38 :site/cat customer eid 103 :person/name Lisa :person/age 7 • Equality • Fulltext search • Negation • Null / Nil • Comparison • Aggregates • Logical OR "Names and likes of Persons that likes pizza"
  81. 81. Molecule Expressions • Equality + tacet • Fulltext search • Negation • Null / Nil • Comparison • Aggregates • Logical OR eid 104 :person/name Ben :person/likes pizza :person/age 5 eid 101 :person/name Fred :person/likes pizza :person/age 38 :site/cat customer eid 103 :person/name Lisa :person/age 7 "Names of persons that like pizza"
  82. 82. Molecule Expressions eid 104 :person/name Ben :person/likes pizza :person/age 5 eid 101 :person/name Fred Ben :person/likes pizza :person/age 38 :site/cat customer eid 103 :person/name Lisa :person/age 7 • Equality • Fulltext search • Negation • Null / Nil • Comparison • Aggregates • Logical OR
  83. 83. Molecule Expressions eid 104 :person/name Ben :person/likes pizza :person/age 5 eid 101 :person/name Fred :person/likes pizza :person/age 38 :site/cat customer eid 103 :person/name Lisa :person/age 7 • Equality • Fulltext search • Negation • Null / Nil • Comparison • Aggregates • Logical OR
  84. 84. Molecule Expressions eid 104 :person/name Ben :person/likes pizza :person/age 5 eid 101 :person/name Fred :person/likes pizza :person/age 38 :site/cat customer eid 103 :person/name Lisa :person/age 7 • Equality • Fulltext search • Negation • Null / Nil • Comparison • Aggregates • Logical OR "Names of Persons that haven't stated what they like" :person/likes fact not asserted for Lisa - or it has been retracted!
  85. 85. Molecule Expressions eid 104 :person/name Ben :person/likes pizza :person/age 5 eid 101 :person/name Fred :person/likes pizza :person/age 38 :site/cat customer eid 103 :person/name Lisa :person/age 7 • Equality • Fulltext search • Negation • Null / Nil • Comparison • Aggregates • Logical OR
  86. 86. Molecule Expressions eid 104 :person/name Ben :person/likes pizza :person/age 5 eid 101 :person/name Fred :person/likes pizza :person/age 38 :site/cat customer eid 103 :person/name Lisa :person/age 7 • Equality • Fulltext search • Negation • Null / Nil • Comparison • Aggregates • Logical OR "Minimum age of all persons" "Count of all persons with an age asserted" "Average age of persons (with age asserted)"
  87. 87. Molecule Expressions eid 104 :person/name Ben :person/likes pizza :person/age 5 eid 101 :person/name Fred :person/likes pizza :person/age 38 :site/cat customer eid 103 :person/name Lisa :person/age 7 • Equality • Fulltext search • Negation • Null / Nil • Comparison • Aggregates • Logical OR "Names and ages of Persons that are 5, 6 or 7 years old"
  88. 88. Molecule Relationships • Card-one • Card-many Defining relationships in schema definition
  89. 89. Molecule Relationships • Card-one • Card-many
  90. 90. Molecule Relationships • Card-one • Card-many eid attr value - :person/name - - :person/home - - :addr/street - - :addr/city -
  91. 91. Molecule Relationships • Card-one • Card-many eid attr value 101 :person/name Fred 101 :person/home 102 102 :addr/street Baker St. 7 102 :addr/city Boston
  92. 92. Molecule Relationships • Card-one • Card-many eid attr value 101 :person/name Fred 101 :person/home 102 102 :addr/street Baker St. 7 102 :addr/city Boston eid 101 :person/name Fred :person/home 102 eid 102 :addr/street Baker St. 7 :addr/city Boston
  93. 93. Molecule Relationships • Card-one • Card-many eid attr value 101 :person/name Fred 101 :person/home 102 102 :addr/street Baker St. 7 102 :addr/city Boston eid 101 :person/name Fred :person/home 102 eid 102 :addr/street Baker St. 7 :addr/city Boston
  94. 94. Molecule Relationships • Card-one • Card-many
  95. 95. Molecule Relationshipseid 101 :person/name Fred :person/home 102 :person/work 103 • Card-one • Card-manyeid 102 :addr/street Baker St. 7 :addr/city Boston eid 103 :addr/street Downtown 1 :addr/city Boston ?
  96. 96. Molecule Relationshipseid 101 :person/name Fred :person/home 102 :person/work 103 • Card-one • Card-manyeid 102 :addr/street Baker St. 7 :addr/city Boston eid 103 :addr/street Downtown 1 :addr/city Boston Back reference (2) 1 2 3
  97. 97. Molecule Relationshipseid 101 :person/name Fred :person/home 102 :person/work 103 • Card-one • Card-manyeid 102 :addr/street Baker St. 7 :addr/city Boston eid 103 :addr/street Downtown 1 :addr/city Boston
  98. 98. Molecule Relationships More "arms" to the molecule... • Card-one • Card-many
  99. 99. Molecule Relationships • Card-one • Card-many eid 101 :order/id 1 :order/items 102, 103 eid 102 :lineItem/qty 3 :lineItem/product Milk :lineItem/price 12.0 eid 103 :lineItem/qty 2 :lineItem/product Coffee :lineItem/price 46.0
  100. 100. Molecule Relationships • Card-one • Card-many eid 101 :order/id 1 :order/items 102, 103 eid 102 :lineItem/qty 3 :lineItem/product Milk :lineItem/price 12.0 eid 103 :lineItem/qty 2 :lineItem/product Coffee :lineItem/price 46.0
  101. 101. Molecule Relationships • Card-one • Card-many eid 101 :order/id 1 :order/items 102, 103 eid 102 :lineItem/qty 3 :lineItem/product Milk :lineItem/price 12.0 eid 103 :lineItem/qty 2 :lineItem/product Coffee :lineItem/price 46.0
  102. 102. Molecule Relationships • Card-one • Card-many eid 101 :order/id 1 :order/items 102, 103 eid 102 :lineItem/qty 3 :lineItem/product Milk :lineItem/price 12.0 eid 103 :lineItem/qty 2 :lineItem/product Coffee :lineItem/price 46.0
  103. 103. Molecule Relationshipseid 101 :person/name Fred :person/likes pizza :person/age 38 :site/cat customer The black sheep
  104. 104. Molecule Relationshipseid 101 :person/name Fred :person/likes pizza :person/age 38 :site/cat customer No relationship...
  105. 105. Molecule Relationshipseid 101 :person/name Fred :person/likes pizza :person/age 38 :person/site 103 eid 103 :site/cat customer
  106. 106. Molecule Relationshipseid 101 :person/name Fred :person/likes pizza :person/age 38 :person/site 103 eid 103 :site/cat customer
  107. 107. Molecule Compositeseid 101 :person/name Fred :person/likes pizza :person/age 38 :site/cat customer "Names, likes, and ages of Persons + their Site category"
  108. 108. Molecule Compositeseid 101 :person/name Fred :person/likes pizza :person/age 38 :site/cat customer Independent & Intrinsic
  109. 109. Molecule [:find ?name ?likes ?age ?cat :where [?e :person/name ?name ] [?e :person/likes ?likes] [?e :person/age ?age ] [?e :site/cat ?cat ] ] => [Fred pizza 38 customer] Compositeseid 101 :person/name Fred :person/likes pizza :person/age 38 :site/cat customer
  110. 110. Molecule [:find ?v1 ?v2 ?v3 ?v4 :where [?e :xlwk/liawj ?v1] [?e :iauw/sokqx ?v2] [?e :kzmm/wwpai ?v3] [?e :ajdg/tybnq ?v4] ] Attributes can have any name Namespaces are not tables Entity ids tie facts together
  111. 111. Molecule Compositeseid 101 :person/name Fred :person/likes pizza :person/age 38 :site/cat customer
  112. 112. Molecule Compositeseid 101 :person/name Fred :person/likes pizza :person/age 38 :site/cat customer :site/status good "Names, likes, and ages of Persons + their Site category and status"
  113. 113. Molecule Compositeseid 101 :person/name Fred :person/likes pizza :person/age 38 :site/cat customer :site/status good :loc/tag city "Names, likes, and ages of Persons + their Site category and status + their Location tag"
  114. 114. Molecule • Create • Save • Read • Update • Delete CRUD
  115. 115. Molecule • Create • Save • Insert • Read • Update • Delete CRUD
  116. 116. Molecule • Create • Save • Insert • 2-step insert • Read • Update • Delete CRUD
  117. 117. Molecule • Create • Save • Insert • 2-step insert • Read • Update • Delete CRUD
  118. 118. Molecule • Create • Save • Insert • 2-step insert • Read • Update • Card-one • Delete CRUD
  119. 119. Molecule • Create • Save • Insert • 2-step insert • Read • Update • Card-one • Delete CRUD fred :person/likes pizza tx4 assert fred :person/likes pizza tx5 retract fred :person/likes pasta tx5 assert E A V T Op
  120. 120. Molecule • Create • Save • Insert • 2-step insert • Read • Update • Card-one • Delete CRUD fred :person/likes pizza tx4 assert fred :person/likes pizza tx5 retract fred :person/likes pasta tx5 assert fred :person/likes pasta tx6 retract E A V T Op
  121. 121. Molecule • Create • Save • Insert • 2-step insert • Read • Update • Card-one • Card-many • Delete CRUD
  122. 122. Molecule • Create • Save • Insert • 2-step insert • Read • Update • Card-one • Card-many • Delete CRUD
  123. 123. Molecule Core • Entities • Attributes • Expressions • Relationships • Composites • CRUD Extras • Map attributes • Input molecules • Bidirectional refs Time • Tx data • Tx meta data • asOf • history • since • with
  124. 124. Molecule Map attributes
  125. 125. Molecule Map attributes
  126. 126. Molecule Map attributes
  127. 127. Molecule Map attributes
  128. 128. Molecule Input molecules
  129. 129. Molecule Input molecules
  130. 130. Molecule Input molecules
  131. 131. Molecule Bidirectional refs
  132. 132. Molecule Bidirectional refs
  133. 133. Molecule Bidirectional refs
  134. 134. Molecule Bidirectional refs
  135. 135. Molecule Bidirectional refs Ann Ben Joe
  136. 136. Molecule Bidirectional refs Ann Ben Joe
  137. 137. Molecule Bidirectional refs Ann :knows/weight 7 Ben :knows/weight 4 Gus :knows/weight 6 Joe
  138. 138. Molecule Core • Entities • Attributes • Expressions • Relationships • Composites • CRUD Extras • Map attributes • Input molecules • Bidirectional refs Time • Tx data • Tx meta data • asOf • history • since • with
  139. 139. Molecule Tx data e5 :person/name Fred tx4 (10:31) assert e5 :person/likes pizza tx4 (10:31) assert E A V T Op
  140. 140. Molecule Tx data e5 :person/name Fred tx4 (10:31) assert e5 :person/likes pizza tx4 (10:31) assert E A V T Op Magic tx4?
  141. 141. Molecule Magic tx4 Tx data e5 :person/name Fred tx4 assert e5 :person/likes pizza tx4 assert tx4 :db/txInstant 10:31 ...? assert E A V T Op Transaction is an entity! (here with entity id tx4...)
  142. 142. Molecule Tx data e5 :person/name Fred tx4 assert e5 :person/likes pizza tx4 assert tx4 :db/txInstant date1 tx4 assert E A V T Op
  143. 143. Molecule Tx data e5 :person/name Fred e4 assert e5 :person/likes pizza e4 assert e4 :db/txInstant date1 e4 assert E A V T Op
  144. 144. Molecule Tx data e5 :person/name Fred tx4 assert e5 :person/likes pizza tx4 assert tx4 :db/txInstant date1 tx4 assert E A V T Op
  145. 145. Molecule Tx data e5 :person/name Fred tx4 assert e5 :person/likes pizza tx4 assert tx4 :db/txInstant date1 tx4 assert E A V T Op List( (17592186045445L, :person/name, "Fred", 13194139534340L, true), (17592186045445L, :person/likes, "pizza", 13194139534340L, true), (13194139534340L, :db/txInstant, "Tue Apr 26 18:35:41 CEST 2017", 13194139534340L, true) )
  146. 146. Molecule Tx data e5 :person/name Fred tx4 assert e5 :person/likes pizza tx4 assert tx4 :db/txInstant date1 tx4 assert E A V T Op List( (17592186045445L, :person/name, "Fred", 13194139534340L, true), (17592186045445L, :person/likes, "pizza", 13194139534340L, true), (13194139534340L, :db/txInstant, "Tue Apr 26 18:35:41 CEST 2017", 13194139534340L, true) ) Long clojure.keyword Object (various types) Long Boolean
  147. 147. Molecule Tx data e5 :person/name Fred tx4 assert e5 :person/likes pizza tx4 assert tx4 :db/txInstant date1 tx4 assert E A V T Op List( (17592186045445L, 147L, "Fred", 13194139534340L, true), (17592186045445L, 148L, "pizza", 13194139534340L, true), (13194139534340L, 18L, "Tue Apr 26 18:35:41 CEST 2017", 13194139534340L, true) ) Long Long Object (various types) Long Boolean
  148. 148. Molecule Tx data e5 :person/name Fred tx4 assert e5 :person/likes pizza tx4 assert tx4 :db/txInstant date1 tx4 assert E A V T Op In what transaction was Freds name asserted?
  149. 149. Molecule Tx data e5 :person/name Fred tx4 assert e5 :person/likes pizza tx4 assert tx4 :db/txInstant date1 tx4 assert E A V T Op When was Freds name asserted?
  150. 150. Molecule Tx meta data e5 :person/name Fred tx4 assert e5 :person/likes pizza tx4 assert tx4 :db/txInstant date1 tx4 assert tx4 ... ... tx4 assert tx4 ... ... tx4 assert E A V T Op
  151. 151. Molecule Tx meta data e5 :person/name Fred tx4 assert e5 :person/likes pizza tx4 assert tx4 :db/txInstant date1 tx4 assert tx4 :audit/user Lisa tx4 assert tx4 ... ... tx4 assert E A V T Op Who did it?
  152. 152. Molecule Tx meta data e5 :person/name Fred tx4 assert e5 :person/likes pizza tx4 assert tx4 :db/txInstant date1 tx4 assert tx4 :audit/user Lisa tx4 assert tx4 :audit/uc survey tx4 assert E A V T Op Who did it? Why?
  153. 153. Molecule Tx meta data e5 :person/name Fred tx4 assert e5 :person/likes pizza tx4 assert tx4 :db/txInstant date1 tx4 assert tx4 :audit/user Lisa tx4 assert tx4 :audit/uc survey tx4 assert E A V T Op Who did it? Why?
  154. 154. Molecule Tx meta data e5 :person/name Fred tx4 assert e5 :person/likes pizza tx4 assert tx4 :db/txInstant date1 tx4 assert tx4 :audit/user Lisa tx4 assert tx4 :audit/uc survey tx4 assert E A V T Op Who did it? Why? "Lisa added Fred as part of a survey"
  155. 155. Molecule Tx meta data e5 :person/name Fred tx4 assert e5 :person/likes pizza tx4 assert tx4 :db/txInstant date1 tx4 assert tx4 :audit/user Lisa tx4 assert tx4 :audit/uc survey tx4 assert E A V T Op When did they do it? Who did it? Why? "Lisa added Fred as part of a survey onTuesday the 26th of April"
  156. 156. Molecule Tx meta data e5 :person/name Fred tx4 assert e5 :person/likes pizza tx4 assert tx4 :db/txInstant date1 tx4 assert tx4 :audit/user Lisa tx4 assert tx4 :audit/uc survey tx4 assert E A V T Op When did they do it? Who did it? Why? "Fred was surveyed"
  157. 157. Molecule Tx meta data e5 :person/name Fred tx4 assert e5 :person/likes pizza tx4 assert tx4 :db/txInstant date1 tx4 assert tx4 :audit/user Lisa tx4 assert tx4 :audit/uc survey tx4 assert E A V T Op When did they do it? Who did it? Why? "Lisas surveyee liked pizza"
  158. 158. Molecule asOf fred :person/name Fred t1 assert fred :person/likes pizza t1 assert E A V T Op
  159. 159. Molecule asOf fred :person/name Fred t1 assert fred :person/likes pizza t1 assert fred :person/likes pizza t2 retract fred :person/likes pasta t2 assert E A V T Op
  160. 160. Molecule asOf fred :person/name Fred t1 assert fred :person/likes pizza t1 assert fred :person/likes pizza t2 retract fred :person/likes pasta t2 assert E A V T Op "What did Fred like before?"
  161. 161. Molecule history fred :person/name Fred t1 TRUE fred :person/likes pizza t1 TRUE fred :person/likes pizza t2 FALSE fred :person/likes pasta t2 TRUE E A V T Added API changed from `.history.get` to `.getHistory` All time filters now have the verb form `get<filter>` This should avoid confusion with attribute names
  162. 162. Molecule history fred :person/name Fred t1 TRUE fred :person/likes pizza t1 TRUE fred :person/likes pizza t2 FALSE fred :person/likes pasta t2 TRUE E A V T Added
  163. 163. Molecule history E A/V T Op Getting all generic data about the `like` attribute history: Generic attributes tie to a single domain attribute
  164. 164. Molecule history E A V T Op ... or go fully generic and ask for any attribute history of an entity:
  165. 165. Molecule history "What has Fred liked?"
  166. 166. Molecule history "What has Fred disliked?"
  167. 167. Molecule history "Who have disliked what and when?" Multiple mandatory attributes not allowed in history queries. It would be like unifying multiple timelines into one timeline which is not useful.
  168. 168. Molecule history "Who have disliked what and when?" Instead, ask for entity ids with the generic `e`.
  169. 169. Molecule history + tx meta data Only one mandatory attribute! ...
  170. 170. Molecule history + tx meta data Use generic `e` to get entity id instead.
  171. 171. Molecule history + tx meta data "Who changed what taste during interviews with Peter?"
  172. 172. Molecule history + tx meta data "Who changed what taste during interviews with Peter and when?" "Who disliked pizza during an interview and when?"
  173. 173. Molecule history + tx meta data Who changed what taste during interviews with Peter and when? Who disliked pizza during an interview and when? Who liked what and when? Who got the info in what use case?
  174. 174. Molecule since 101 :person/likes pizza tx1 TRUE 102 :person/likes pasta tx2 TRUE 103 :person/likes sushi tx3 TRUE 104 :person/likes burger tx4 TRUE E A V T Added (updated slide)
  175. 175. Molecule with What if ...? Would that work? (presented as now deprecated `imagine`)
  176. 176. Molecule with What if we update Freds age?
  177. 177. Molecule with What if we save a new person?
  178. 178. Molecule with What if we insert 2 persons?
  179. 179. Molecule with What if we do all 3 things...?
  180. 180. Molecule with Test modulerized transactions...
  181. 181. Molecule Core • Entities • Attributes • Expressions • Relationships • Composites • CRUD Extras • Map attributes • Input molecules • Bidirectional refs Time • Tx data • Tx meta data • asOf • history • since • with 3 Questions?
  182. 182. Domain modelling Molecule 4
  183. 183. Molecule Person • id • name • born ‣ doThis() ‣ doThat()
  184. 184. Molecule Person • id • name • born ‣ doThis() ‣ doThat() ‣ ctx1a() ‣ ctx1b() ‣ ctx2a() Ctx 1 person1.doThis() person2.ctx1a() person1.ctx1b() Ctx 2 person2.ctx2a() Slow changing Fast changing Runtime
  185. 185. Molecule Person • id • name • born ‣ doThis() ‣ doThat() ‣ ctx1a() ‣ ctx1b() ‣ ctx2a() ‣ ctx2b() ‣ ctx3a() ‣ ctx3b() ‣ ctx4a() ‣ ctx4b() ‣ ctx5a() ‣ ctx5b() ‣ etc... Ctx 1 person1.doThis() person2.ctx1a() person1.ctx1b() Ctx 2 person2.ctx2a() Slow changing Fast changing Ctx 4 person.ctx4b() Runtime Ctx 5 person.ctx5a() Ctx 3 person.ctx3b() Ctx 6 person2.ctx6a() C person2
  186. 186. Molecule Person • id • name • born ‣ doThis() ‣ doThat() Ctx 1 customer.doThis() customer.buy() Ctx 2 user.login() Slow changing Not so fast changing Ctx 3 manager.add(p1) Customer ‣ buy() ‣ complain() User ‣ login() ‣ comment() Manager ‣ add(person) ‣ getReport() Faster changing Runtime
  187. 187. Molecule Person • id • name • born ‣ doThis() ‣ doThat() Ctx 1 customer.doThis() customer.buy() Ctx 2 user.login() Slow changing Not so fast changing Ctx 3 manager.login() manager.add(p1) Customer ‣ buy() ‣ complain() User ‣ login() ‣ comment() Manager ‣ add(person) ‣ getReport() Faster changing Relatively faster changing Runtime
  188. 188. Molecule Person • id • name • born PersonService(p) ‣ p.doThis() ‣ p.doThat() ‣ p.ctx1a() ‣ p.ctx1b() ‣ p.ctx2a() ‣ etc... Ctx 1 personService(p1).doThis() personService(p2).ctx1a() personService(p1).ctx1b() Ctx 2 person2.ctx2a() Fast changing RuntimeSlow changing
  189. 189. Molecule Person • id • name • born PersonCmdCtx1(p) ‣ p.ctx1a() ‣ p.ctx1b() Ctx 1 PersonCmdCtx1(p2).ctx1a() PersonCmdCtx1(p1).ctx1b() Ctx 2 PersonCmdCtx2.ctx2a() PersonCmdCtx2(p) ‣ p.ctx2a() Slow changing Fast changing Runtime
  190. 190. Molecule Entity • id • attr1 • attr2 • etc... Slow changing Fast changing EntityConsumer(e) ‣ action1() ‣ action2() ‣ etc() • Services • Event source commands • Ad hoc classes • Etc..
  191. 191. Molecule EntityConsumer(e) ‣ action1() ‣ action2() ‣ etc() Person • id • name • born Customer • status User • email • passw Manager • clearance • employees Seldom changing Often changing Occassionally changing And more subtypes... Fast changing
  192. 192. Molecule EntityConsumer(e) ‣ action1() ‣ action2() ‣ etc() Person • id • born • name Customer • status User • email • passw Manager • clearance • employees Very seldom changing Often changing Occassionally changing Never changing And more subtypes... Fast changing
  193. 193. Molecule EntityConsumer(e) ‣ changePassw() Molecular EntitiesAtomic Attributes Person variations... eid 101 :person/name :user/email :user/passw :person/id :person/born :person/name :user/email :user/passw :site/status :auth/clearance :mgt/employees :mgt/employees eid 101 :auth/clearance EntityConsumer(e) ‣ authorize(employee) (User) (Manager 1) (Manager 2) eid 101 :auth/clearance :mgt/employees EntityConsumer(e) ‣ add(employee) Entity consumers
  194. 194. Molecule EmployeeAdministration(eid) ‣ add(employee) Authorization(eid) ‣ authorize(employee) PasswChange(eid) ‣ setNew(passw) Atomic Attributes eid 101 :person/name :user/email :user/passw :person/id :person/born :person/name :user/email :user/passw :site/status :auth/clearance :mgt/employees :mgt/employees eid 101 :auth/clearance (User) (Manager 1) (Manager 2)eid 101 :auth/clearance :mgt/employees Entity id consumers - with molecular entities
  195. 195. Molecule EmployeeAdministration(eid) ‣ add(employee) Authorization(eid) ‣ authorize(employee) :person/id :person/born :person/name :user/email :user/passw :site/status :auth/clearance :mgt/employees :mgt/employees eid 101 :auth/clearance (Manager 1) (Manager 2)eid 101 :auth/clearance :mgt/employees no domain classes!? PasswChange(eid) ‣ setNew(passw) eid 101 :person/name :user/email :user/passw (User) Atomic Attributes Entity id consumers - with molecular entities
  196. 196. Molecule Molecular entities PasswChange(eid, newPassw) ‣ setNew(passw) eid 101 :person/name :user/email :user/passw (User)
  197. 197. Molecule PasswChange(eid, newPassw) Get current User data: Check stuff... Save: Molecules
  198. 198. Molecule Molecules• Get data from the app process • No need to cast raw data from a db server to populate rigid domain classes, only to pull the data out again • Get/set exactly the data you need • Always typed data • Easy type-safe migrations • Use your domain terms directly • Absolute minium of ceremony • Maximum runtime performance • Time • Fun! PasswChange(eid, newPassw) Get current User data: Check stuff... Save:
  199. 199. Molecule • Get data from the app process • No need to cast raw data from a db server to populate rigid domain classes, only to pull it out again • Get/set exactly the data you need • Always typed data • Easy type-safe migrations • Use your domain terms directly • Absolute minium of ceremony • Maximum runtime performance • Time • Fun! Thank you! Questions?
  200. 200. Molecule 4 github.com/scalamolecule/molecule scalamolecule.org datomic.com Marc Grue April 2017

×