Skip to content

Instantly share code, notes, and snippets.

@terjesb
Forked from vvvvalvalval/mock-connection.clj
Created December 9, 2015 20:01
Show Gist options
  • Save terjesb/6754fa2d1e1575dfbaf4 to your computer and use it in GitHub Desktop.
Save terjesb/6754fa2d1e1575dfbaf4 to your computer and use it in GitHub Desktop.

Revisions

  1. @vvvvalvalval vvvvalvalval revised this gist Oct 21, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions mock-connection.clj
    Original file line number Diff line number Diff line change
    @@ -55,4 +55,5 @@
    :bouser/email (str "val.vvalval" i "@gmail.com")
    }]))))
    ;; "Elapsed time: 2027.881 msecs". So this is a x10 improvement.
    ;; the schema and fixture data are quite small (35 attributes, 103 datoms), so I expect this will get more compelling as the application grows.
    )
  2. @vvvvalvalval vvvvalvalval revised this gist Oct 21, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions mock-connection.clj
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,7 @@
    (MockConnection. (atom db) (LinkedBlockingDeque.)))

    (comment ;; How fast is it ?
    (def mem-db (bs.dev/local-db)) ;; creates a mem db, loads the schema and some fixture data in it. Takes about 150ms on my dev laptop.
    (def mem-db (bs.dev/local-db)) ;; creates a mem db, loads the schema and some fixture data in it. Takes about 20ms on my dev laptop.
    (time (dotimes [i 1000]
    (let [conn (mock-conn mem-db)]
    @(d/transact conn [{:db/id (d/tempid :db.part/user)
    @@ -54,5 +54,5 @@
    :bouser/level "bandsquare"
    :bouser/email (str "val.vvalval" i "@gmail.com")
    }]))))
    ;; "Elapsed time: 2027.881 msecs"
    ;; "Elapsed time: 2027.881 msecs". So this is a x10 improvement.
    )
  3. @vvvvalvalval vvvvalvalval revised this gist Oct 21, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mock-connection.clj
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@
    Connection
    (db [this] @(.dbAtom this))
    (transact [this tx-data] (doto (datomic.promise/settable-future)
    (deliver (let [tx-res #_(swap! (:db-atom this) d/with tx-data)
    (deliver (let [tx-res
    (loop []
    (let [db-atom (.dbAtom this)
    old-val @db-atom
  4. @vvvvalvalval vvvvalvalval revised this gist Oct 21, 2015. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions mock-connection.clj
    Original file line number Diff line number Diff line change
    @@ -43,3 +43,16 @@
    Sync and housekeeping methods are implemented as noops. #log() is not supported."
    [db]
    (MockConnection. (atom db) (LinkedBlockingDeque.)))

    (comment ;; How fast is it ?
    (def mem-db (bs.dev/local-db)) ;; creates a mem db, loads the schema and some fixture data in it. Takes about 150ms on my dev laptop.
    (time (dotimes [i 1000]
    (let [conn (mock-conn mem-db)]
    @(d/transact conn [{:db/id (d/tempid :db.part/user)
    :bouser/firstName (str "Valentinnnnn_" i)
    :bouser/lastName (str "Valentinnnnn_" i)
    :bouser/level "bandsquare"
    :bouser/email (str "val.vvalval" i "@gmail.com")
    }]))))
    ;; "Elapsed time: 2027.881 msecs"
    )
  5. @vvvvalvalval vvvvalvalval created this gist Oct 21, 2015.
    45 changes: 45 additions & 0 deletions mock-connection.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    (ns bs.utils.mock-connection
    "Utilities for using Datomic"
    (:require [datomic.api :as d])
    (:use clojure.repl clojure.pprint)
    (:import (java.util.concurrent BlockingQueue LinkedBlockingDeque)
    (datomic Connection)))

    (defrecord MockConnection
    [dbAtom, ^BlockingQueue txQueue]

    Connection
    (db [this] @(.dbAtom this))
    (transact [this tx-data] (doto (datomic.promise/settable-future)
    (deliver (let [tx-res #_(swap! (:db-atom this) d/with tx-data)
    (loop []
    (let [db-atom (.dbAtom this)
    old-val @db-atom
    tx-res (d/with old-val tx-data)
    new-val (:db-after tx-res)]
    (if (compare-and-set! db-atom old-val new-val)
    tx-res
    (recur))
    ))]
    (.add ^BlockingQueue (.txQueue this) tx-res)
    tx-res))
    ))
    (transactAsync [this tx-data] (.transact this tx-data))

    (gcStorage [this olderThan])
    (requestIndex [this])
    (release [this])
    (sync [this] (doto (datomic.promise/settable-future)
    (deliver (.db this))))
    (syncExcise [this t] (.sync this))
    (syncIndex [this t] (.sync this))
    (syncSchema [this t] (.sync this))
    (sync [this t] (.sync this))
    (txReportQueue [this] (.txQueue this)))

    (defn ^Connection mock-conn
    "Creates a mocked version of datomic.Connection which uses db/with internally.
    Only supports datomic.api/db, datomic.api/transact and datomic.api/transact-async operations.
    Sync and housekeeping methods are implemented as noops. #log() is not supported."
    [db]
    (MockConnection. (atom db) (LinkedBlockingDeque.)))