mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 08:21:11 +00:00
rename internals for clarity
This commit is contained in:
parent
df8cfed125
commit
f0a6ceb837
4 changed files with 44 additions and 34 deletions
|
|
@ -119,7 +119,7 @@
|
||||||
compiled-routes)
|
compiled-routes)
|
||||||
lookup (impl/fast-map nl)
|
lookup (impl/fast-map nl)
|
||||||
matcher (trie/linear-matcher compiler pl)
|
matcher (trie/linear-matcher compiler pl)
|
||||||
match-by-path (trie/matcher matcher compiler)
|
match-by-path (trie/path-matcher matcher compiler)
|
||||||
routes (impl/uncompile-routes compiled-routes)]
|
routes (impl/uncompile-routes compiled-routes)]
|
||||||
^{:type ::router}
|
^{:type ::router}
|
||||||
(reify
|
(reify
|
||||||
|
|
@ -213,7 +213,7 @@
|
||||||
[nil {}]
|
[nil {}]
|
||||||
compiled-routes)
|
compiled-routes)
|
||||||
matcher (trie/compile pl compiler)
|
matcher (trie/compile pl compiler)
|
||||||
match-by-path (trie/matcher matcher compiler)
|
match-by-path (trie/path-matcher matcher compiler)
|
||||||
lookup (impl/fast-map nl)
|
lookup (impl/fast-map nl)
|
||||||
routes (impl/uncompile-routes compiled-routes)]
|
routes (impl/uncompile-routes compiled-routes)]
|
||||||
^{:type ::router}
|
^{:type ::router}
|
||||||
|
|
|
||||||
|
|
@ -25,15 +25,15 @@
|
||||||
(wild-matcher [this key end matcher])
|
(wild-matcher [this key end matcher])
|
||||||
(catch-all-matcher [this key params data])
|
(catch-all-matcher [this key params data])
|
||||||
(linear-matcher [this matchers])
|
(linear-matcher [this matchers])
|
||||||
(prettify [this matcher])
|
(-pretty [this matcher])
|
||||||
(path-matcher [this matcher]))
|
(-path-matcher [this matcher]))
|
||||||
|
|
||||||
(defn assoc-param [match k v]
|
(defn- assoc-param [match k v]
|
||||||
(let [params (:params match)]
|
(let [params (:params match)]
|
||||||
(assoc match :params (assoc params k v))))
|
(assoc match :params (assoc params k v))))
|
||||||
|
|
||||||
;; https://stackoverflow.com/questions/8033655/find-longest-common-prefix
|
;; https://stackoverflow.com/questions/8033655/find-longest-common-prefix
|
||||||
(defn common-prefix [s1 s2]
|
(defn- common-prefix [s1 s2]
|
||||||
(let [max (min (count s1) (count s2))]
|
(let [max (min (count s1) (count s2))]
|
||||||
(loop [i 0]
|
(loop [i 0]
|
||||||
(cond
|
(cond
|
||||||
|
|
@ -177,7 +177,7 @@
|
||||||
(update :children dissoc ""))
|
(update :children dissoc ""))
|
||||||
node')))
|
node')))
|
||||||
|
|
||||||
(defn decode [path start end percent?]
|
(defn- decode [path start end percent?]
|
||||||
(let [param (subs path start end)]
|
(let [param (subs path start end)]
|
||||||
(if percent?
|
(if percent?
|
||||||
#?(:cljs (js/decodeURIComponent param)
|
#?(:cljs (js/decodeURIComponent param)
|
||||||
|
|
@ -254,9 +254,9 @@
|
||||||
(view [_] (mapv view matchers))
|
(view [_] (mapv view matchers))
|
||||||
(depth [_] (inc (apply max 0 (map depth matchers))))
|
(depth [_] (inc (apply max 0 (map depth matchers))))
|
||||||
(length [_]))))
|
(length [_]))))
|
||||||
(prettify [_ matcher]
|
(-pretty [_ matcher]
|
||||||
(view matcher))
|
(view matcher))
|
||||||
(path-matcher [_ matcher]
|
(-path-matcher [_ matcher]
|
||||||
(fn [path]
|
(fn [path]
|
||||||
(if-let [match (match matcher 0 (count path) path)]
|
(if-let [match (match matcher 0 (count path) path)]
|
||||||
(->Match (:params match) (:data match)))))))
|
(->Match (:params match) (:data match)))))))
|
||||||
|
|
@ -275,9 +275,9 @@
|
||||||
(Trie/catchAllMatcher key params data))
|
(Trie/catchAllMatcher key params data))
|
||||||
(linear-matcher [_ matchers]
|
(linear-matcher [_ matchers]
|
||||||
(Trie/linearMatcher matchers))
|
(Trie/linearMatcher matchers))
|
||||||
(prettify [_ matcher]
|
(-pretty [_ matcher]
|
||||||
(-> matcher str read-string eval))
|
(-> matcher str read-string eval))
|
||||||
(path-matcher [_ matcher]
|
(-path-matcher [_ matcher]
|
||||||
(fn [path]
|
(fn [path]
|
||||||
(if-let [match ^Trie$Match (Trie/lookup ^Trie$Matcher matcher ^String path)]
|
(if-let [match ^Trie$Match (Trie/lookup ^Trie$Matcher matcher ^String path)]
|
||||||
(->Match (.params match) (.data match))))))))
|
(->Match (.params match) (.data match))))))))
|
||||||
|
|
@ -287,6 +287,7 @@
|
||||||
;;
|
;;
|
||||||
|
|
||||||
(defn insert
|
(defn insert
|
||||||
|
"Returns a trie with routes added to it."
|
||||||
([routes]
|
([routes]
|
||||||
(insert nil routes))
|
(insert nil routes))
|
||||||
([node routes]
|
([node routes]
|
||||||
|
|
@ -299,11 +300,14 @@
|
||||||
params (zipmap (->> parts (remove string?) (map :value)) (repeat nil))]
|
params (zipmap (->> parts (remove string?) (map :value)) (repeat nil))]
|
||||||
(-insert (or node (-node {})) (split-path path) params data))))
|
(-insert (or node (-node {})) (split-path path) params data))))
|
||||||
|
|
||||||
(defn compiler []
|
(defn compiler
|
||||||
|
"Returns a default [[TrieCompiler]]."
|
||||||
|
[]
|
||||||
#?(:cljs (clojure-trie-compiler)
|
#?(:cljs (clojure-trie-compiler)
|
||||||
:clj (java-trie-compiler)))
|
:clj (java-trie-compiler)))
|
||||||
|
|
||||||
(defn compile
|
(defn compile
|
||||||
|
"Returns a compiled trie, to be used with [[pretty]] or [[path-matcher]]."
|
||||||
([options]
|
([options]
|
||||||
(compile options (compiler)))
|
(compile options (compiler)))
|
||||||
([{:keys [data params children wilds catch-all] :or {params {}}} compiler]
|
([{:keys [data params children wilds catch-all] :or {params {}}} compiler]
|
||||||
|
|
@ -325,16 +329,18 @@
|
||||||
:else (data-matcher compiler {} nil)))))
|
:else (data-matcher compiler {} nil)))))
|
||||||
|
|
||||||
(defn pretty
|
(defn pretty
|
||||||
([trie]
|
"Returns a simplified EDN structure of a compiled trie for printing purposes."
|
||||||
(pretty trie (compiler)))
|
([compiled-trie]
|
||||||
([trie compiler]
|
(pretty compiled-trie (compiler)))
|
||||||
(prettify compiler trie)))
|
([compiled-trie compiler]
|
||||||
|
(-pretty compiler compiled-trie)))
|
||||||
|
|
||||||
(defn matcher
|
(defn path-matcher
|
||||||
([trie]
|
"Returns a function of `path -> Match` from a compiled trie."
|
||||||
(matcher trie (compiler)))
|
([compiled-trie]
|
||||||
([trie compiler]
|
(path-matcher compiled-trie (compiler)))
|
||||||
(path-matcher compiler trie)))
|
([compiled-trie compiler]
|
||||||
|
(-path-matcher compiler compiled-trie)))
|
||||||
|
|
||||||
;;
|
;;
|
||||||
;; spike
|
;; spike
|
||||||
|
|
|
||||||
|
|
@ -70,7 +70,7 @@
|
||||||
nil routes))
|
nil routes))
|
||||||
|
|
||||||
(def trie-matcher
|
(def trie-matcher
|
||||||
(trie/matcher
|
(trie/path-matcher
|
||||||
(trie/compile
|
(trie/compile
|
||||||
(reduce
|
(reduce
|
||||||
(fn [acc [p d]]
|
(fn [acc [p d]]
|
||||||
|
|
@ -80,8 +80,10 @@
|
||||||
(defn bench! []
|
(defn bench! []
|
||||||
|
|
||||||
;; 2.3µs
|
;; 2.3µs
|
||||||
#_(cc/quick-bench
|
;; 2.1µs (28.2.2019)
|
||||||
(p/lookup pedestal-tree "/v1/orgs/1/topics"))
|
(cc/with-progress-reporting
|
||||||
|
(cc/bench
|
||||||
|
(p/lookup pedestal-tree "/v1/orgs/1/topics")))
|
||||||
|
|
||||||
;; 3.1µs
|
;; 3.1µs
|
||||||
;; 2.5µs (string equals)
|
;; 2.5µs (string equals)
|
||||||
|
|
@ -99,7 +101,7 @@
|
||||||
;; 0.8µs (return route-data)
|
;; 0.8µs (return route-data)
|
||||||
;; 0.8µs (fix payloads)
|
;; 0.8µs (fix payloads)
|
||||||
#_(cc/quick-bench
|
#_(cc/quick-bench
|
||||||
(trie/matcher reitit-tree "/v1/orgs/1/topics" {}))
|
(trie/path-matcher reitit-tree "/v1/orgs/1/topics" {}))
|
||||||
|
|
||||||
;; 0.9µs (initial)
|
;; 0.9µs (initial)
|
||||||
;; 0.5µs (protocols)
|
;; 0.5µs (protocols)
|
||||||
|
|
@ -111,11 +113,13 @@
|
||||||
#_(cc/quick-bench
|
#_(cc/quick-bench
|
||||||
(segment/lookup segment-matcher "/v1/orgs/1/topics"))
|
(segment/lookup segment-matcher "/v1/orgs/1/topics"))
|
||||||
|
|
||||||
;; 0.32µs (initial)
|
;; 0.320µs (initial)
|
||||||
;; 0.30µs (iterate arrays)
|
;; 0.300µs (iterate arrays)
|
||||||
;; 0.28µs (list-params)
|
;; 0.280µs (list-params)
|
||||||
(cc/quick-bench
|
;; 0.096µs (trie)
|
||||||
(trie-matcher "/v1/orgs/1/topics")))
|
(cc/with-progress-reporting
|
||||||
|
(cc/bench
|
||||||
|
(trie-matcher "/v1/orgs/1/topics"))))
|
||||||
|
|
||||||
(comment
|
(comment
|
||||||
(bench!))
|
(bench!))
|
||||||
|
|
|
||||||
|
|
@ -17,21 +17,21 @@
|
||||||
(is (= (trie/->Match {} {:a 1})
|
(is (= (trie/->Match {} {:a 1})
|
||||||
((-> (trie/insert nil "/foo" {:a 1})
|
((-> (trie/insert nil "/foo" {:a 1})
|
||||||
(trie/compile)
|
(trie/compile)
|
||||||
(trie/matcher)) "/foo")))
|
(trie/path-matcher)) "/foo")))
|
||||||
|
|
||||||
(is (= (trie/->Match {} {:a 1})
|
(is (= (trie/->Match {} {:a 1})
|
||||||
((-> (trie/insert nil "/foo" {:a 1})
|
((-> (trie/insert nil "/foo" {:a 1})
|
||||||
(trie/insert "/foo/*bar" {:b 1})
|
(trie/insert "/foo/*bar" {:b 1})
|
||||||
(trie/compile)
|
(trie/compile)
|
||||||
(trie/matcher)) "/foo")))
|
(trie/path-matcher)) "/foo")))
|
||||||
|
|
||||||
(is (= (trie/->Match {:bar "bar"} {:b 1})
|
(is (= (trie/->Match {:bar "bar"} {:b 1})
|
||||||
((-> (trie/insert nil "/foo" {:a 1})
|
((-> (trie/insert nil "/foo" {:a 1})
|
||||||
(trie/insert "/foo/*bar" {:b 1})
|
(trie/insert "/foo/*bar" {:b 1})
|
||||||
(trie/compile)
|
(trie/compile)
|
||||||
(trie/matcher)) "/foo/bar")))
|
(trie/path-matcher)) "/foo/bar")))
|
||||||
|
|
||||||
(is (= (trie/->Match {} {:a 1})
|
(is (= (trie/->Match {} {:a 1})
|
||||||
((-> (trie/insert nil "" {:a 1})
|
((-> (trie/insert nil "" {:a 1})
|
||||||
(trie/compile)
|
(trie/compile)
|
||||||
(trie/matcher)) ""))))
|
(trie/path-matcher)) ""))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue