ããæè¿æ¸ãã¦ããDatomicãã¥ã¼ããªã¢ã«ã®ããã¯ãçµæ§ãã©ã¼ãã«ãªæä½ã§æ¸ãã¦ãã¾ãããããã®ã¨ã³ããªã¼ã¯ããªãã«ã¸ã¥ã¢ã«ã«æ¸ãã¾ãã
èªå·±åç §ã¢ããªãã¥ã¼ã
datomic tutorial 15æ¥ç®ã§æ¸ããããã«ãåç §åã¯ã¨ã«ãããªãã§ãã¨ã³ãã£ãã£ãåç §ãããã¨ãã§ãã¾ããããã¨ããã®ã¨ã³ãã£ãã£èªèº«ãåç §ããã¢ããªãã¥ã¼ããä½ããã¨ãã§ããã®ãã©ãã試ãã¦ã¿ããã¨æãããã«ãªãã¾ãã
ããã§ã次ã®ãããªã¹ãã¼ãã®å®ç¾©ããã¦ã¿ã¾ãã
ã¨ã³ãã£ã㣠| ã¢ããªãã¥ã¼ã | å | ã«ã¼ãã£ããªã㣠| ç¹è¨ |
---|---|---|---|---|
person | name | string | one | - |
person | sex | ref(enum) | one | :person.sex/woman, :person.sex/man |
person | birth | long | one | - |
person | self | ref(entity) | one | èªä¿¡ã¸ã®åç § |
ã¹ãã¼ãå®ç¾©ã¯æ¬¡ã®ããã«ãªãã¾ãã
[ {:db/id #db/id[db.part/db -1000000], :db/ident :person/name, :db/valueType :db.type/string, :db/cardinality :db.cardinality/one, :db.install/_attribute db.part/db} {:db/id #db/id[db.part/db -1000001], :db/ident :person/sex, :db/valueType :db.type/ref, :db/cardinality :db.cardinality/one, :db.install/_attribute db.part/db} {:db/id #db/id[db.part/db -1000002], :db/ident :person/birth, :db/valueType :db.type/long, :db/cardinality :db.cardinality/one, :db.install/_attribute db.part/db} {:db/id #db/id[db.part/db -1000003], :db/ident :person/self, :db/valueType :db.type/ref, :db/cardinality :db.cardinality/one, :db.install/_attribute db.part/db} {:db/id #db/id[:db.part/user -1000004], :db/ident :person.sex/woman} {:db/id #db/id[:db.part/user -1000005], :db/ident :person.sex/man} ]
ãã¦ãä¸è¨ã®ã¹ãã¼ãã«æ¬¡ã®ãããªãã¼ã¿ãæå ¥ãã¾ãã
:person/name | :person/sex | :person/birth | :person/self |
---|---|---|---|
å±±ç°å¤ªé | :person.sex/man | 1995 | èªåèªèº« |
å±±æ¬è¯å | :person.sex/woman | 1997 | èªåèªèº« |
ãã®ãã¼ã¿ã¯æ¬¡ã®ãã©ã³ã¶ã¯ã·ã§ã³ãçºçããã¾ãã
[ {:person/name å±±ç°å¤ªé, :person/sex :person.sex/man, :person/birth 1995, :person/self #db/id[:db.part/user -1000006], :db/id #db/id[:db.part/user -1000006]} {:person/name å±±æ¬è¯å, :person/sex :person.sex/woman, :person/birth 1997, :person/self #db/id[:db.part/user -1000007], :db/id #db/id[:db.part/user -1000007]} ]
ã§ããã¼ã¿æå ¥å¾ã®ãã¼ã¿ãã¼ã¹ã«å¯¾ãã¦ã次ã®ã¯ã¨ãªã¼ãæãã¾ãã
[ :find ?p ?self :where [?p :person/self ?self]]
:
ããã§å¤æ°?p
ã«ææãããã®ãã¨ã³ãã£ãã£èªä¿¡ã®id(long
)ã§ããã?self
ã«ææãããã®ã:preson/self
ã«æ ¼ç´ãããèªåèªèº«ã¸ã®åç
§(id)ã¨ãªãã¾ããããã¦ããã®äºã¤ã¯åãå¤ã§ãããã¨ãæ³å®ãããã®ã§ãåå¾ããçµæ(HashSet
)ã«å¯¾ãã¦ã次ã®ããã«assert
ãã¾ãã
results.each {PersistentVector vec -> assert vec[0] == vec[1] }
ã§ããã®ãã¹ãã¯ãã¹ãã¾ããã¾ããå½ç¶ã¨ããã°å½ç¶ã§ããã¨ããããã§ãç®è«è¦ã®ãã®1ã¯å®äºã§ãã
ã¨ã³ãã£ãã£ããã¢ããªãã¥ã¼ããã²ã£ãºããã¦å¥ã®ã¨ã³ãã£ãã£ã«ãã¦ãã¾ã
ã§ãããããã£ãã®ãããã
å ã»ã©ã®ã¹ãã¼ãå®ç¾©ãåæ²ãã¾ãã
ã¨ã³ãã£ã㣠| ã¢ããªãã¥ã¼ã | å | ã«ã¼ãã£ããªã㣠| ç¹è¨ |
---|---|---|---|---|
person | name | string | one | - |
person | sex | ref(enum) | one | :person.sex/woman, :person.sex/man |
person | birth | long | one | - |
person | self | ref(entity) | one | èªä¿¡ã¸ã®åç § |
ããã§ã次ã®ãããªè¦æãçºçãã¾ãã
- ããã親ã«ãã©ãã©ãã¼ã ã¤ãããã¦ãã¤ããæããã¦ãããããååå¤ãããããã
ã¾ããdatomicã®ãã¼ã¿ãã¼ã¹ã¯æéãæã£ã¦ãããããåç´ã«å¤æ´ãã¦ãã¾ãã°ããã ãã®è©±ã§ãããã ãã¾ããRDBè³ãæ¶ãå»ã£ã¦ããªãã®ã§ãå¥ã®ã¨ã³ãã£ãã£ã¸åå²ãããã¨ããçµè«ã«è³ã£ããã¨ã«ãã¾ãããã
ããã§ãã¹ãã¼ãå®ç¾©ã®ãã©ã³ã¶ã¯ã·ã§ã³ãè¦ãã¨ãããä¸ã¤ã®ã¨ããããå½ããåã®äºå®ã«æ°ãä»ãã¾ããããã¯ã
ã¹ãã¼ãå®ç¾©ã®ãã©ã³ã¶ã¯ã·ã§ã³ã£ã¦ãã åã«ãã¼ã¿ãç»é²ãããã©ã³ã¶ã¯ã·ã§ã³ã ãã
ããã¨çãã¦ããã®ãã追å (å®ç¾©)ãããdb
ã¨ã³ãã£ãã£ã®ident
ã¢ããªãã¥ã¼ããæ´æ°ãããã©ã³ã¶ã¯ã·ã§ã³ãçºè¡ããã°ãã¹ãã¼ãã®å®ç¾©ããã¼ã¿éç ´å£ã§å¤æ´ã§ãããã¨ã«ãªãã®ã§ã¯ãªããã¨ããäºæ¸¬ã§ããã¨ããããã§ãæåã®ã¹ãã¼ãå®ç¾©ããã¼ã¿æå
¥ã®ãã¨ã«ã次ã®ãããª:db/ident
ãå¤ãã¦ãã¾ããã©ã³ã¶ã¯ã·ã§ã³ãçºçããã¦ã¿ããã¨æãã¾ãã
ã¾ãã:db/ident
ãåå¾ããã¯ã¨ãªã¼
[ :find [?id ...] :in $ [?attr ...] :where [?id :db/ident ?attr]]
ãã©ã¡ã¼ã¿ã¼
[:person/name :person/self]
ã¹ãã¼ãåå®ç¾©
ãã¦ãåå¾ããidã«å¯¾ãã¦ã次ã®ããã«å¥ã®ã¨ã³ãã£ãã£/ã¢ããªãã¥ã¼ããåå®ç¾©ãã¾ãã
:person/name
â:person.name/value
:person/self
â:person.name/person
ä»åã試ãã«ãã£ãã¨ããããããã®ã¨ã³ãã£ãã£/ã¢ããªãã¥ã¼ãã®:db/ident
ã®idã¯æ¬¡ã®ã¨ããã§ããã
:person/name
â66
:person/self
â63
ãã®çµæãã¹ãã¼ãå¤æ´ããããã©ã³ã¶ã¯ã·ã§ã³ã¯æ¬¡ã®ããã«ãªãã¾ãã
[ {:db/id 66, :db/ident :person.name/person} {:db/id 63, :db/ident :person.name/value} ]
ãã®ãã©ã³ã¶ã¯ã·ã§ã³ããããã¨ãç¹ã«åé¡ã¯çºçãã¾ããã
ã¨ãããã¨ã¯ã¹ãã¼ãã®å¤æ´ãå®äºãã¦ããããã«æãããã
ãã¼ã¿ãéç ´å£ã§ãããã¨ã確èª
ã§ã¯ããã¼ã¿ã¯éç ´å£ãªã®ãã次ã®ã¯ã¨ãªã¼ã§ç¢ºèªãã¾ãã
[ :find ?name ?sex ?birth :where [?p :person/sex ?s] [?s :db/ident ?sex] [?p :person/birth ?birth] [?n :person.name/value ?name] [?n :person.name/person ?p]]
ããã®çµæã次ã®ã³ã¼ãã§è¡¨ç¤ºããã¾ãã
results.each {PersistentVector vec -> log "name: [${vec[0]}], sex: [${vec[1]}], birth: [${vec[2]}]" }
å®è¡ããã¨ããã®ãããªãã°ãåºåããã¦ããã¼ã¿éç ´å£ã§ã¹ãã¼ãå¤æ´ãæåãããã¨ããããã¾ãã
name: [å±±æ¬è¯å], sex: [:person.sex/woman], birth: [1997] name: [å±±ç°å¤ªé], sex: [:person.sex/man], birth: [1995]
æåã«æå ¥ãããã¼ã¿ã¨ã¾ã£ããåãç¶æ ã§åå¾ã§ãã¦ãããã¨ããããã¾ãã
RDBã«æ¯ã¹ã¦ãã«ã©ã (ã¢ããªãã¥ã¼ã)ãå¥ã®ãã¼ãã«(ã¨ã³ãã£ãã£)ã«ç§»åããããã¨ãç°¡åãªæãããã¾ãã