Skip to content

Commit

Permalink
Allow overriding action log batch size via :actionLog.service/batch-size
Browse files Browse the repository at this point in the history
Fixes #22.
  • Loading branch information
candera committed Jun 29, 2015
1 parent d1e532c commit a4aab99
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
6 changes: 6 additions & 0 deletions resources/simulant/schema.edn
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@
:db/doc "How long this action took, in nsec"
:db/cardinality :db.cardinality/one
:db.install/_attribute :db.part/db}
{:db/id #db/id[:db.part/db]
:db/ident :actionLog.service/batch-size
:db/valueType :db.type/long
:db/doc "The batch size to use when transacting action log entries"
:db/cardinality :db.cardinality/one
:db.install/_attribute :db.part/db}
{:db/id #db/id [:db.part/db]
:db/ident :simulant.sim/actionLog}
{:db/id #db/id[:db.part/db]
Expand Down
28 changes: 16 additions & 12 deletions src/simulant/sim.clj
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ process."
;; ActionLogs implement IFn, and expect to be passed transaction data
(def ^:private serializer (agent nil))

(defrecord ActionLogService [conn ^File temp-file writer]
(defrecord ActionLogService [conn ^File temp-file writer batch-size]
Service
(start-service [this process] this)

Expand All @@ -124,7 +124,7 @@ process."
(.close ^Closeable writer)
(with-open [reader (io/reader temp-file)
pbr (PushbackReader. reader)]
(transact-pbatch conn (form-seq pbr) 1000)))
(transact-pbatch conn (form-seq pbr) (or batch-size 1000))))

clojure.lang.IFn
(invoke
Expand All @@ -140,18 +140,22 @@ process."
[conn svc-definition]
(let [f (File/createTempFile "actionLog" "edn")
writer (io/writer f)]
(->ActionLogService conn f writer)))
(->ActionLogService conn f writer (:actionLog.service/batch-size svc-definition))))

(defn create-action-log
"Create an action log service for the sim."
[conn sim]
(let [id (d/tempid :sim)]
(-> @(d/transact conn [{:db/id id
:sim/_services (e sim)
:service/type :service.type/actionLog
:service/constructor (str 'simulant.sim/construct-action-log)
:service/key :simulant.sim/actionLog}])
(tx-ent id))))
"Create an action log service for the sim. `attrs` is an optional
map of attributes to include in the service definition."
([conn sim] (create-action-log conn sim {}))
([conn sim attrs]
(let [id (d/tempid :sim)]
(-> @(d/transact conn [(merge
{:db/id id
:sim/_services (e sim)
:service/type :service.type/actionLog
:service/constructor (str 'simulant.sim/construct-action-log)
:service/key :simulant.sim/actionLog}
attrs)])
(tx-ent id)))))

;; ## Process state service

Expand Down

0 comments on commit a4aab99

Please sign in to comment.