Skip to content

Commit 9045660

Browse files
author
alef
committed
feat(core): add memoized_parse edn
1 parent 61fd674 commit 9045660

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

release-js/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"mocha": "^2.3.4"
4040
},
4141
"scripts": {
42+
"build": "cd .. && lein cljsbuild once min && cd release-js",
4243
"test": "mocha --harmony --compilers js:babel-core/register test/*.spec.js"
4344
},
4445
"files": [
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {helpers, mori} from '../datascript-mori'
2+
import {assert} from 'chai'
3+
4+
describe('memoized_parse checks', () => {
5+
it('memoized_parse work as mori.parse', () => {
6+
const query = `[:find ?e1 ?e2 ?e3
7+
:where [?e1 :age ?a1]
8+
[?e2 :age ?a2]
9+
[?e3 :age ?a3]
10+
[(+ ?a1 ?a2) ?a12]
11+
[(= ?a12 ?a3)]]`
12+
13+
assert(
14+
mori.equals(helpers.memoized_parse(query), mori.parse(query)),
15+
'memoized_parse and mori.parse return equal struct'
16+
);
17+
})
18+
});

src/datascript_mori/core.cljs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
[datascript.parser :as dp]
66
[datascript.pull-parser :as dpp]
77
[mori :as m]
8-
[mori.extra :as me]))
8+
[mori.extra :as me]
9+
[cljs.reader :as reader]
10+
[datascript.lru :as dlru]))
911

1012
(def ^:export schema_to_clj djs/schema->clj)
1113
(def ^:export entity_to_clj djs/entity->clj)
@@ -19,6 +21,17 @@
1921
(def ^:export parse_query dp/parse-query)
2022
(def ^:export parse_pull dpp/parse-pull)
2123

24+
(def ^:const lru-cache-size 300)
25+
(def ^:export lru dlru/lru)
26+
(def ^:export cleanup_lru dlru/cleanup-lru)
27+
(def ^:export query_cache (volatile! (dlru/lru lru-cache-size)))
28+
(defn ^:export memoized_parse [q]
29+
(if-let [cached (get @query_cache q nil)]
30+
cached
31+
(let [qp (reader/read-string q)]
32+
(vswap! query_cache assoc q qp)
33+
qp)))
34+
2235
(def ^:export DB_ID :db/id)
2336
(def ^:export DB_FN_CALL :db.fn/call)
2437
(def ^:export DB_BEFORE :db-before)

0 commit comments

Comments
 (0)