Question

I'm having problems with the datomic tutorial at the "Revisiting the past" section http://datomic.com/company/resources/tutorial.html

For the two queries below:

query = "[:find ?c :where [?c :community/name]]";
db_asOf_schema = conn.db().asOf(schema_tx_date);
System.out.println(Peer.q(query, db_asOf_schema).size()); // 0

db_since_data = conn.db().since(data_tx_date);
System.out.println(Peer.q(query, db_since_data).size()); // 0

I have tried these commands in clojure, but cannot get them working as described in the tutorial:

(since (db conn) (java.util.Date.) ) 
;; It should return 0 but returns the whole database instead

(def ts (q '[:find ?when :where [?tx :db/txInstant ?when]] (db conn)))

(count (since (db conn) (ffirst (reverse (sort ts))))))
;; returns 13, but should return 0

(count (as-of (db conn) (ffirst (sort ts)))))
;; returns 13, but should return 0

I'm not too sure is this is the right behaviour, is there anything I'm doing wrong?

Was it helpful?

Solution

If you are working through the Seattle tutorial in Clojure, probably the most important single thing to know is that working Clojure code is included in the Datomic distribution. The filename is samples/seattle/getting-started.clj, and you can simply follow along at the REPL.

Two observations on the Clojure code in your question:

The since function is documented to return a database value, not a number, so the behavior you are seeing is as expected. In order to see what is in the database, you need to issue a query.

Databases do not have any documented semantics for the Clojure count function, so you should not call count on them. Again, if you want to see what is in the database, you need to issue a query, e.g.

;; Find all transaction times, sort them in reverse order
(def tx-instants (reverse (sort (q '[:find ?when :where [_ :db/txInstant ?when]]
                                       (db conn)))))

;; pull out two most recent transactions, most recent loaded
;; seed data, second most recent loaded schema
(def data-tx-date (ffirst tx-instants))
(def schema-tx-date (first (second tx-instants)))

;; make query to find all communities
(def communities-query '[:find ?c :where [?c :community/name]])

;; find all communities as of schema transaction
(let [db-asof-schema (-> conn db (d/as-of schema-tx-date))]
  (println (count (seq (q communities-query db-asof-schema)))))

;; find all communities as of seed data transaction
(let [db-asof-data (-> conn db (d/as-of data-tx-date))]
  (println (count (seq (q communities-query db-asof-data)))))

;; find all communities since seed data transaction
(let [db-since-data (-> conn db (d/since data-tx-date))]
  (println (count (seq (q communities-query db-since-data)))))

Hope this helps. There is also a Datomic google group if you have more questions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top