Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support for datomic

  • Loading branch information...
commit 20839711c628111909ada720a35c59bc63b89629 1 parent 02e3030
Paulo Suzart authored
26 datomic/clact-schema.dtm
View
@@ -0,0 +1,26 @@
+[
+ ;; community
+
+ {:db/id #db/id[:db.part/db]
+ :db/ident :clact/author
+ :db/valueType :db.type/string
+ :db/cardinality :db.cardinality/one
+ :db/doc "The author of a given fact"
+ :db.install/_attribute :db.part/db}
+
+ {:db/id #db/id[:db.part/db]
+ :db/ident :clact/via
+ :db/valueType :db.type/string
+ :db/cardinality :db.cardinality/one
+ :db/doc "Who noticed the fact"
+ :db.install/_attribute :db.part/db}
+
+ {:db/id #db/id[:db.part/db]
+ :db/ident :clact/fact
+ :db/valueType :db.type/string
+ :db/cardinality :db.cardinality/one
+ :db/fulltext true
+ :db/doc "The fact itself"
+ :db.install/_attribute :db.part/db}
+
+ ]
9 datomic/transactor.properties
View
@@ -0,0 +1,9 @@
+########### free mode config ###############
+protocol=free
+host=localhost
+#free mode will use 3 ports starting with this one:
+port=4334
+
+## optional overrides if you don't want ./data and ./log
+#data-dir=<embedded db data and fulltext temp work goes here>
+#log-dir=<logs go here>
3  project.clj
View
@@ -7,5 +7,6 @@
[org.clojure/java.jdbc "0.2.3"]
[org.xerial/sqlite-jdbc "3.6.16"]
[org.clojure/tools.cli "0.2.1"]
- [org.clojure/clojure "1.3.0"]]
+ [com.datomic/datomic-free "0.8.3438"]
+ [org.clojure/clojure "1.4.0"]]
:main clacts.core)
7 src/clacts/core.clj
View
@@ -29,7 +29,7 @@
"List the facts for a given author. * means all authors."
[author ch storage]
(let [res (find-by-author storage author)]
- (if res
+ (if (seq res)
(doseq [r res]
(enqueue ch ["LSR" (str (:date r))
(str (:author r))
@@ -63,8 +63,9 @@
[& args]
(let [[opts _ ban] (cli args
["-p" "--port" "Port to listen to connections"
- :default 10200 :parse-fn #(Integer/parseInt %)]
- ["-s" "--storage" "Type of storage. Use sqlite or datomic"]
+ :default 10200 :parse-fn #(Integer/parseInt %)]
+ ["-s" "--storage" "Type of storage. Use sqlite or datomic"
+ :default "sqlite"]
["-h" "--help" "Show this help" :default false :flag true])]
(when (:help opts)
(println ban)
63 src/clacts/storage.clj
View
@@ -3,15 +3,17 @@
with-connection
insert-record
with-query-results]]
- [clojure.java.io]))
+ [clojure.java.io]
+ [datomic.api :only [q db] :as d]))
-(defprotocol PersistentMedium
+(defprotocol StorageMediumP
(setup [this])
(put-fact [this author via fact])
(find-by-author [this author]))
+;;============ SQLite
-(def db
+(def sqlite-db
{:classname "org.sqlite.JDBC"
:subprotocol "sqlite"
:subname "db/database.db"})
@@ -25,14 +27,14 @@
"Access SQLite at sturt up, so users don't have to wait
for connecting to ig."
[q]
- (with-connection db
+ (with-connection sqlite-db
(with-query-results r q)))
(defn setup-db
"Setup procedure. DDL the facts table."
[]
- (with-connection db
+ (with-connection sqlite-db
(create-table :facts
[:date :text]
[:author :text]
@@ -41,9 +43,9 @@
(println "Database created with table facts."))
-(defrecord SQLitePersistence
+(defrecord SQLiteStorage
[dbfilechk setupf]
- PersistentMedium
+ StorageMediumP
;;setup sqlite
(setup [_]
@@ -58,7 +60,7 @@
;; insert a fact into sqlite
(put-fact [_ author via fact]
- (with-connection db
+ (with-connection sqlite-db
(insert-record :facts
{:date (str (System/currentTimeMillis))
:author author
@@ -69,7 +71,7 @@
(find-by-author [_ author]
(println "finding " author)
(let [q "select date, author, via, fact from facts"
- listq #(with-connection db
+ listq #(with-connection sqlite-db
(with-query-results rs %
(into [] rs)))]
(or
@@ -77,8 +79,49 @@
(listq [(str q " where author = ?") author])))))
+; =============== Datomic
+
+(defonce uri "datomic:free://localhost:4334/clact")
+(defonce datomic-schema "datomic/clact-schema.dtm")
+
+(defn load-schema [s conn]
+ (let [s-tx (read-string (slurp s))]
+ @(d/transact conn s-tx)))
+
+(defrecord DatomicStorage
+ [schema transactor-config conn]
+ StorageMediumP
+
+ (setup [_]
+ (load-schema schema conn))
+
+ (put-fact [_ author via fact]
+ @(d/transact conn [{:clact/via via
+ :clact/author author
+ :clact/fact fact
+ :db/id #db/id[:db.part/user]}]))
+
+ (find-by-author [_ author]
+ (let [db (db conn)
+ res (q '[:find ?e :in $ ?a :where [?e :clact/author ?a]] db author)
+ extract (fn [e] {:author (:clact/author e)
+ :via (:clact/via e)
+ :date nil
+ :fact (:clact/fact e)})
+ as-e #(d/entity db (first %))]
+ (map #(extract (as-e %)) res))))
+
+;;Auxiliary function to generation a connection before the Storage itself
+
+(defn- make-datomic [schema transactor-config]
+ (d/create-database uri)
+ (DatomicStorage. schema transactor-config (d/connect uri)))
+
+; =============== The rest
+
(defn make-storage [s]
(condp = s
- "sqlite" (SQLitePersistence. db-check-file setup-db)
+ "sqlite" (SQLiteStorage. db-check-file setup-db)
+ "datomic" (make-datomic datomic-schema nil)
nil))
Please sign in to comment.
Something went wrong with that request. Please try again.