前回のエントリーがなんか1日で5,000アクセス出てて、正直ビビった。
CRUDのUDいらんとか過激なことを言ったからだと思ってる。
これは、Thought Works tech Radar で append only datastore でdatomicのことを知ってて、若干それを意識していたのもあったためでもある。
はてブのコメントは非常にためになるものばかり(ありがとうございます)なので、ちゃんと時間が取れたら(うつ病の人間は普通の人よりも頭の回転が遅いからなかなか時間がとれない。むしろ被害妄想も強い僕の精神だと、はてブされるたびにビクビクしていて、過激なことを言い過ぎて、イット業界干されるはーとうじうじと考えてたので、かなり何も生産しないことについて考えてしまって時間が余計に取れない)ちゃんと読んで、考えてみます。
それはそうと、datomicの存在だけは知っていたので、すこしチュートリアルを試してみた。
myDatomicに登録するのが面倒だったので、とりあえずfree版を使うことにしたのですが、myDatomicに登録していない人はmavenで依存性解決できないので、とりあえず配布されたdatomic-freeに入っているjarを強引にgradleのdependencyで取り込んで、IntelliJ IDEAで触ってる。
groovyscriptで次のコマンドを実行して
println (new File('path/to/datomic-free/libs') .list([accept:{d, s -> s.contains('jar') && !s.contains('groovy')}]) .collect { "'${it.absolutePath}'" }.join(',\n'))
この結果をbuild.gradle
のココ
って書かれた部分にコピペしてbuild.gradle
は完成
dependencies { compile (/* ココ */) }
以下、チュートリアルに書いてあることを実行していくだけ。
class Tutorial { static final def DIR = '/path/to/datomic-free//samples/seattle' static void main(String... args) { def uri = 'datomic:mem://seattle' Peer.createDatabase(uri) Connection connect = Peer.connect(uri) def reader = new FileReader("${DIR}/seattle-schema.edn") def tx = Util.readAll(reader)[0] reader.close() connect.transact(tx).get() def rdr = new FileReader("${DIR}/seattle-data0.edn") def dtx = Util.readAll(rdr)[0] rdr.close() connect.transact(dtx).get() Database db = connect.db() def results = Peer.query('[:find ?c :where [?c :community/name]]', db) println results.class def itr = results.iterator() (1..10).each { def next = itr.next() def id = next[0] def entity = db.entity(id) def attr = entity.get(':community/name') println "results-type-parameter[${next.class}, size: ${next.size()}, as String: ${next}] - type of id[${id.class}] - type of entity[${entity.class}] - type of attr[${attr.class}] - attr value[${attr}]" } } }
seattle-data0.edn
で登録されるデータは150件ほどらしいので、全部表示するのもアレなので、先頭から10件だけを表示してみた。
results-type-parameter[class clojure.lang.PersistentVector, size: 1, as String: [17592186045440]] - type of id[class java.lang.Long] - type of entity[class datomic.query.EntityMap] - type of attr[class java.lang.String] - attr value[15th Ave Community] results-type-parameter[class clojure.lang.PersistentVector, size: 1, as String: [17592186045443]] - type of id[class java.lang.Long] - type of entity[class datomic.query.EntityMap] - type of attr[class java.lang.String] - attr value[Admiral Neighborhood Association] results-type-parameter[class clojure.lang.PersistentVector, size: 1, as String: [17592186045445]] - type of id[class java.lang.Long] - type of entity[class datomic.query.EntityMap] - type of attr[class java.lang.String] - attr value[Alki News] results-type-parameter[class clojure.lang.PersistentVector, size: 1, as String: [17592186045446]] - type of id[class java.lang.Long] - type of entity[class datomic.query.EntityMap] - type of attr[class java.lang.String] - attr value[Alki News/Alki Community Council] results-type-parameter[class clojure.lang.PersistentVector, size: 1, as String: [17592186045449]] - type of id[class java.lang.Long] - type of entity[class datomic.query.EntityMap] - type of attr[class java.lang.String] - attr value[All About Belltown] results-type-parameter[class clojure.lang.PersistentVector, size: 1, as String: [17592186045452]] - type of id[class java.lang.Long] - type of entity[class datomic.query.EntityMap] - type of attr[class java.lang.String] - attr value[All About South Park] results-type-parameter[class clojure.lang.PersistentVector, size: 1, as String: [17592186045454]] - type of id[class java.lang.Long] - type of entity[class datomic.query.EntityMap] - type of attr[class java.lang.String] - attr value[ArtsWest] results-type-parameter[class clojure.lang.PersistentVector, size: 1, as String: [17592186045457]] - type of id[class java.lang.Long] - type of entity[class datomic.query.EntityMap] - type of attr[class java.lang.String] - attr value[At Large in Ballard] results-type-parameter[class clojure.lang.PersistentVector, size: 1, as String: [17592186045460]] - type of id[class java.lang.Long] - type of entity[class datomic.query.EntityMap] - type of attr[class java.lang.String] - attr value[Aurora Seattle] results-type-parameter[class clojure.lang.PersistentVector, size: 1, as String: [17592186045461]] - type of id[class java.lang.Long] - type of entity[class datomic.query.EntityMap] - type of attr[class java.lang.String] - attr value[Ballard Avenue]
PersistentVector
というのが、おそらくJavaでいうResultSet
に相当するものっぽい。
ちょっと眠い状態でやったので、頭がクリアでないので、クエリーの構造を理解するのに時間がかかってる