ä»æ¥ã¯ãã¼ã¿ã®è¿½å ãããã¾ãã
ãã©ã³ã¶ã¯ã·ã§ã³ã®ãã¼ã¿ã¹ãã©ã¯ãã£ã¼
ãã©ã³ã¶ã¯ã·ã§ã³ããããããã¼ã¿ã¹ãã©ã¯ãã£ã¼ã¯æ¬¡ã®ãããªå½¢ã«ãªãã¾ãã
[ [data-structure-list] [data-structure-list] ... ]
ãããã¯
[ {data-structure-map} {data-structure-map} ... ]
åã ã®ãã¼ã¿æä½ãè¡ããªã¹ããããããè¦ç´ ã«æã¤ãªã¹ãã§ããããã¯ãã¼ã¿ã®è¿½å ãæ´æ°ãåé¤ã«ããã¦ãã¹ã¦å ±éããæ§é ã§ãã®ã§ã確å®ã«è¦ãã¦ãããã»ããè¯ãã§ãã
ãã¼ã¿è¿½å ã®data structure
ãã¼ã¿ã追å ããéã®data structureã¯æ¬¡ã®äºã¤ãããã¾ãã
ãªã¹ãå½¢å¼ã®å ´å:
[:db/add new-entity-id attribute-name attribute-value]
ãªã¹ãå½¢å¼ã®å ´åãè¤æ°ã®å±æ§ã«å¤ãè¨å®ãããã¨ã¯ã§ãã¾ãããå±æ§ï¼ã¤ãã¤ä¸¹å¿µã«å¿ãããã¦:db/add
ãã¦ããã¾ãã
ãããå½¢å¼ã®å ´å:
{ :db/id new-entity-id attribute-name attribute-value attribute-name attribute-value attribute-name attribute-value ... }
ãããå½¢å¼ã®å ´åã®å¼ã¯ãå
é¨çã«ã¯ãªã¹ãå½¢å¼ã«å¤æããã¦å®è¡ããã¾ãã丹念ã«å¿ãããã¦æ¸ãã¦ãã:db/add
ã¯æ©æ¢°çã«å®è¡ãããããã«ãªãã¾ãã
ä¸æID
å
ã«ç¤ºãããªã¹ã/ãããã®è¿½å ç¨ã®data structureã§new-entity-id
ã®ã¨ããã«ãæ¢åã®ã¨ã³ãã£ãã£ã®IDãæå®ããã¨ããã¼ã¿ã®æ´æ°ã«ãªãã¾ãããããã£ã¦ãæ°ãããã¼ã¿ã追å ããå ´åã¯ä¸æIDãå©ç¨ããªãã¨ããã¾ããã
ä¸æIDãåå¾ããå¼ã¯æ¬¡ã®ã¨ããã«ãªãã¾ãã
#db/id[partition-name long-value]
ä¸è¨ã®å¼ã§long-value
ã¯ãªãã·ã§ã³ã®ãããçç¥ãå¯è½ã§ããããããä»ã®æ°ããã¨ã³ãã£ãã£ã¸ã®åç
§ãç»é²ããå¿
è¦ãããå ´åã«ã¯ããã®ã¨ã³ãã£ãã£ãåç
§ã§ããããã«ããããã«ãlong-value
ãæå®ãã¦ãããã»ããããã§ãããã
partition-name
ã¯ãã¼ã¿ãæå±ãããã¼ãã£ã·ã§ã³ã®ååãæå®ãã¾ããçµåãé »ç¹ã«è¡ãã¨ã³ãã£ãã£å士ã¯åããã¼ãã£ã·ã§ã³ã«æå±ãã¦ããæ¹ããã¼ã¿ã¢ã¯ã»ã¹ã®å¹çããããªãã¾ããããã©ã«ãã§ããã¤ãã®ãã¼ãã£ã·ã§ã³ãæä¾ããã¦ãã¾ãããå½é¢ã¯:db.part/user
ã¨ãããã¼ãã£ã·ã§ã³ã使ãã¾ãã
ãªã¹ãå½¢å¼ã§ã®ãã¼ã¿è¿½å
ãã¥ã¼ããªã¢ã«ã®ãã¼ã¿æ§é ãå©ç¨ãã¦ããµã³ãã«ã®ãã¼ã¿ãç»é²ãã¾ãã
ç»é²ãããã¼ã¿ã¯æ¬¡ã®ãããªãã¼ã¿ã§ãã
ã¨ã³ãã£ãã£:community
å±æ§å | å 容 |
---|---|
:community/name |
"Hoge" |
:community/url |
"http://localhost:8000" |
:community/category |
["foo", "bar", "baz"] |
:community/type |
:community.type/twitter |
:community/orgtype |
:community.orgtype/community |
:community/neighborhood |
ä¸è¨ãåç § |
ã¨ã³ãã£ãã£:neighborhood
å±æ§å | å 容 |
---|---|
:neighborhood/name |
"Bar" |
:neighborhood/district |
ä¸è¨ãåç § |
ã¨ã³ãã£ãã£:district
å±æ§å | å 容 |
---|---|
:district/name |
"Foo" |
:district/region |
:region/ne |
ä¸è¨ã®å 容ã追å ãããã©ã³ã¶ã¯ã·ã§ã³ãã¼ã¿ã¯æ¬¡ã®ããã«ãªãã¾ãã
datomic-tutorial8-add-as-list.edn
[ [:db/add #db/id[:db.part/user -1] :district/name "Foo"] [:db/add #db/id[:db.part/user -1] :district/region :region/ne] [:db/add #db/id[:db.part/user -2] :neighborhood/name "Bar"] [:db/add #db/id[:db.part/user -2] :neighborhood/district #db/id[:db.part/user -1]] [:db/add #db/id[:db.part/user -3] :community/name "Hoge"] [:db/add #db/id[:db.part/user -3] :community/url "http://localhost:8000"] [:db/add #db/id[:db.part/user -3] :community/category "foo"] [:db/add #db/id[:db.part/user -3] :community/category "bar"] [:db/add #db/id[:db.part/user -3] :community/category "baz"] [:db/add #db/id[:db.part/user -3] :community/orgtype :community.orgtype/community] [:db/add #db/id[:db.part/user -3] :community/type :community.type/twitter] [:db/add #db/id[:db.part/user -3] :community/neighborhood #db/id[:db.part/user -2]] ]
ãããæµãè¾¼ãã³ã¼ãã¯æ¬¡ã®ããã«ãªãã¾ãã
ClassLoader loader = getClass().classLoader def countQuery = '[:find ?c :where [?c :community/name]]' @Test void æ°è¦ãã¼ã¿ãtransactionDataStructureã®dbAddã§è¿½å () { def url = loader.getResource('datomic-tutorial8-add-as-list.edn') assert url != null def tx = DatomicUtil.from(url) def dbAfter = connection.transact(tx[0]).get().get(Connection.DB_AFTER) def before = Peer.query(countQuery, db).size() def after = Peer.query(countQuery, dbAfter).size() assert after == before + 1 }
追å åã®Database
ã§ããdb
ã¨è¿½å å¾ã®Databse
ã®dbAfter
ã«å¯¾ãã¦[:find ?c [?c :community/name]]
ãå®è¡ããã¨ãdbAfter
ã®æ¹ãã¬ã³ã¼ãæ°ã1件å¤ããã¨ãæ³å®ããã¾ãã®ã§ããã®ãããªassert
ã«ãªã£ã¦ãã¾ãã
ãããå½¢å¼ã§ã®ãã¼ã¿è¿½å
ãªã¹ãå½¢å¼ã§è¿½å ããã®ã¨åããããªãã¼ã¿ããããå½¢å¼ã§è¿½å ãããã©ã³ã¶ã¯ã·ã§ã³ãã¼ã¿ã¯æ¬¡ã®ããã«ãªãã¾ãã
datomic-tutorial8-add-as-map.edn
[ {:db/id #db/id[db.part/user -1], :district/name "Foo", :district/region :region/ne} {:db/id #db/id[db.part/user -2], :neighborhood/name "Bar", :neighborhood/district #db/id[db.part/user -1]} {:db/id #db/id[db.part/user -3], :community/name "Hoge", :community/url "http://localhost:8000", :community/category ["foo" "bar" "baz"], :community/orgtype :community.orgtype/community, :community/type :community.type/twitter, :community/neighborhood #db/id[:db.part/user -2]} ]
å®éã«ãããæµãè¾¼ãã³ã¼ãã¯å ã»ã©ã®ã³ã¼ãã¨å¤ããã¾ãããèªã¿è¾¼ããªã½ã¼ã¹ãç°ãªãã ãã§ãã
ãªã¹ãå½¢å¼ã®data structureãAPIçµç±ã§è¿½å ãã
ä¸è¨ã®ãµã³ãã«ã§ã¯ednå½¢å¼ã§ãã¼ã¿ã追å ããæ¹æ³ãæ¸ãã¾ããããDatomicã§ã¯Java APIãæä¾ããã¦ãã¾ãã
@Test void æ°è¦ãã¼ã¿ãdbAddãç¨ãã¦APIçµç±ã§è¿½å ãã() { def tempId = {long id -> Peer.tempid(':db.part/user', id) } def dbAdd = ':db/add' def list = [ //districtã®ãã¼ã¿ [dbAdd, tempId(-1), ':district/name', 'Foo'], [dbAdd, tempId(-1), ':district/region', ':region/ne'], //neighborhoodã®ãã¼ã¿ [dbAdd, tempId(-2), ':neighborhood/name', 'Bar'], [dbAdd, tempId(-2), ':neighborhood/district', tempId(-1)], //communityã®ãã¼ã¿ [dbAdd, tempId(-3), ':community/name', 'hoge'], [dbAdd, tempId(-3), ':community/url', 'http://localhost:8000'], [dbAdd, tempId(-3), ':community/category', '["foo", "bar", "baz"]'], [dbAdd, tempId(-3), ':community/orgtype', ':community.orgtype/community'], [dbAdd, tempId(-3), ':community/type', ':community.type/twitter'], [dbAdd, tempId(-3), ':community/neighborhood', tempId(-2)] ] def dbAfter = connection.transact(list).get().get(Connection.DB_AFTER) def before = Peer.query(countQuery, db).size() def after = Peer.query(countQuery, dbAfter).size() assert after == before + 1 }
åç´ã«ãã¼ã¿æä½ããããªã¹ãã®ãªã¹ããä½ã£ã¦ãConnection#transact(List)
ã«æ¸¡ãã ãã§ããä¸æIDã¯Peer#tempid(String, long)
ãããã¯Peer#tempid(String)
ã«ããä½æã§ãã¾ãã
ãããå½¢å¼ã®data structureãAPIçµç±ã§è¿½å ãã
ãªã¹ãã§ã§ãããã¨ã¯ãããã§ãã§ãã¾ãã
@Test void æ°è¦ãã¼ã¿ãAPIãã追å ãã() { // ãã³ãã©ãªã¼IDãçºçããããã¼ãã£ã·ã§ã³ def tempId = {long id -> Peer.tempid(':db.part/user', id) } //追å ãã¼ã¿ def lists = [ [//districtã®ãã¼ã¿ ':db/id': tempId(-1), ':district/name': 'Foo', ':district/region': ':region/ne'], [//neighborhoodã®ãã¼ã¿ ':db/id': tempId(-2), ':neighborhood/name': 'Bar', ':neighborhood/district': tempId(-1)], [//communityã®ãã¼ã¿ ':db/id': tempId(-3), ':community/name': 'Hoge', ':community/url': 'http://localhost:8000', ':community/category': ['foo', 'bar', 'baz'], ':community/orgtype': ':community.orgtype/community', ':community/type': ':community.type/twitter', ':community/neighborhood': tempId(-2)] ] def dbAfter = connection.transact(list).get().get(Connection.DB_AFTER) def before = Peer.query(countQuery, db).size() def after = Peer.query(countQuery, dbAfter).size() assert after == before + 1 }
ãªã¹ãå½¢å¼ã§ã¯:db/add
ããå§ã¾ããªã¹ãã§ãããããããå½¢å¼ã§ã¯':db/id': Peer.tempid(':db.part/user')
ãå«ããããã§ãã¼ã¿ã追å ã§ãã¾ãã
ãã¼ã¿æä½ã®ãã¥ã¼ããªã¢ã«ãå¾®å¦ã«ãããã¥ããã£ãã®ã§ããã¼ã¿ã®è¿½å ã調ã¹ãã ãã§1æ¥ã«è²»ãããæéã使ã£ã¦ãã¾ãã¾ãã(´ã»Ïã»ï½)
次åã¯ãã¼ã¿ã®æ´æ°ã»åé¤ãããããã¨æãã¾ãã