Skip to content

Commit b3ad327

Browse files
committed
Avoid honeysql compilation step for now in one billion cells
1 parent 01d6df6 commit b3ad327

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

examples/billion_cells/src/app/main.clj

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -145,32 +145,30 @@
145145
:border-radius :0.15em
146146
:border "0.15em solid currentColor"
147147
:padding :5px}]])))
148-
149148
(defn get-tab-state [db sid tabid]
150-
(-> (d/q db {:select [:state]
151-
:from :tab
152-
:where [:and [:= :sid sid] [:= :tabid tabid]]})
149+
(-> (d/q db ["SELECT state FROM tab WHERE (sid = ?) AND (tabid = ?)"
150+
sid tabid])
153151
first))
154152

155153
(defn update-tab-state! [db sid tabid update-fn]
156154
(let [old-state (get-tab-state db sid tabid)
157155
new-state (update-fn old-state)]
158156
(if old-state
159-
(d/q db {:update :tab
160-
:set {:state [:lift new-state]}
161-
:where [:and [:= :sid sid] [:= :tabid tabid]]})
162-
(d/q db {:insert-into :tab
163-
:values [{:sid sid
164-
:tabid tabid
165-
:state [:lift (assoc new-state
166-
:sid sid
167-
:tabid tabid)]}]}))))
157+
(d/q db
158+
["UPDATE tab SET state = ? WHERE (sid = ?) AND (tabid = ?)"
159+
new-state sid tabid])
160+
(d/q db
161+
["INSERT INTO tab (sid, tabid, state) VALUES (?, ?, ?)"
162+
sid
163+
tabid
164+
(assoc new-state
165+
:sid sid
166+
:tabid tabid)]))))
168167

169168
(defn update-chunk! [db chunk-cache chunk-id update-fn]
170169
(let [old-chunk (or (@chunk-cache chunk-id)
171-
(-> (d/q db {:select [:chunk]
172-
:from :chunk
173-
:where [:= :id chunk-id]})
170+
(-> (d/q db
171+
["SELECT chunk FROM chunk WHERE id = ?" chunk-id])
174172
first))
175173
new-chunk (update-fn old-chunk)]
176174
(swap! chunk-cache assoc chunk-id new-chunk)))
@@ -286,9 +284,8 @@
286284

287285
(defn UserView [{:keys [x y sid] :or {x 0 y 0}} db]
288286
(->> (d/q db
289-
{:select [:id :chunk]
290-
:from :chunk
291-
:where [:in :id (xy->chunk-ids x y)]})
287+
(into ["SELECT id, chunk FROM chunk WHERE id IN (?, ?, ?, ?, ?, ?, ?, ?, ?)"]
288+
(xy->chunk-ids x y)))
292289
(mapv (fn [[id chunk]]
293290
(Chunk id chunk sid)))))
294291

@@ -359,7 +356,9 @@
359356
" and "
360357
[:a {:href "https://data-star.dev"} "Datastar"]
361358
" - "
362-
[:a {:href "https://github.com/andersmurphy/hyperlith/blob/master/examples/billion_cells/src/app/main.clj" } "source"]]])))
359+
[:a {:href "https://github.com/andersmurphy/hyperlith/blob/master/examples/billion_cells/src/app/main.clj" } "source"]
360+
" - "
361+
[:a {:href "https://checkboxes.andersmurphy.com"} " more like this"]]])))
363362

364363
(def blank-chunk
365364
(-> (repeat (* chunk-size chunk-size) {})
@@ -391,6 +390,14 @@
391390
(when-not (d/q db {:select [:id] :from :chunk :limit 1})
392391
(initial-board-db-state! db)))
393392

393+
(d/format-query
394+
(let [sid 1 tabid 2 new-state {} chunk-id 3 foo [1 2 3 4 5 6 7 8 9]
395+
new-chunk {}]
396+
{:update :chunk
397+
:set {:chunk [:lift new-chunk]}
398+
:where [:= :id chunk-id]}))
399+
400+
394401
(defn ctx-start []
395402
(let [{:keys [writer reader]}
396403
(d/init-db! "database-new.db"
@@ -409,9 +416,8 @@
409416
(run! (fn [thunk] (thunk db chunk-cache)) thunks)
410417
(run! (fn [[chunk-id new-chunk]]
411418
(d/q db
412-
{:update :chunk
413-
:set {:chunk [:lift new-chunk]}
414-
:where [:= :id chunk-id]}))
419+
["UPDATE chunk SET chunk = ? WHERE id = ?"
420+
new-chunk chunk-id]))
415421
@chunk-cache)))
416422
(h/refresh-all!))
417423
{:run-every-ms 100})}))

src/hyperlith/extras/sqlite.clj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
;; (defmacro q [db query & [opts]]
1818
;; `(d/q ~db ~(hsql/format query {:params opts})))
1919

20+
(def format-query hsql/format)
21+
2022
;;; - UTILITY -
2123

2224
(defn table-info [db table-name]

0 commit comments

Comments
 (0)