|
| 1 | +(ns blaze.db.impl.search-param.near |
| 2 | + (:require |
| 3 | + [blaze.anomaly :as ba :refer [when-ok]] |
| 4 | + [blaze.async.comp :as ac] |
| 5 | + [blaze.coll.core :as coll] |
| 6 | + [blaze.db.api :as d] |
| 7 | + [blaze.db.impl.index.index-handle :as ih] |
| 8 | + [blaze.db.impl.index.single-version-id :as svi] |
| 9 | + [blaze.db.impl.protocols :as p] |
| 10 | + [blaze.db.impl.search-param.near.geo :as spng] |
| 11 | + [blaze.db.impl.search-param.special :as special] |
| 12 | + [blaze.db.impl.search-param.util :as u] |
| 13 | + [blaze.fhir.spec.type.system :as system] |
| 14 | + [clojure.string :as str] |
| 15 | + [cognitect.anomalies :as anom])) |
| 16 | + |
| 17 | +(set! *warn-on-reflection* true) |
| 18 | + |
| 19 | +(defn- matches? [batch-db resource-handle values] |
| 20 | + (-> (d/pull batch-db resource-handle) |
| 21 | + (ac/then-apply |
| 22 | + (fn [{:keys [position]}] |
| 23 | + (some (fn [{:keys [distance] :as value}] |
| 24 | + (spng/near? position value distance)) values))) |
| 25 | + (ac/join))) |
| 26 | + |
| 27 | +(defn- unsupported-unit-msg [unit code] |
| 28 | + (format "Unsupported unit `%s` in search parameter `%s`. Supported are 'km', 'm'." unit code)) |
| 29 | + |
| 30 | +(defn- missing-param-msg [arg code] |
| 31 | + (format "Missing argument `%s` in search parameter `%s`." arg code)) |
| 32 | + |
| 33 | +(defn- param-parsing-err-msg [arg code] |
| 34 | + (format "Error parsing argument `%s` in search parameter `%s`." arg code)) |
| 35 | + |
| 36 | +(defn- update-msg [msg] |
| 37 | + (fn [anomaly] |
| 38 | + (update anomaly ::anom/message #(str msg " " %)))) |
| 39 | + |
| 40 | +(defn- parse-dec-arg [coord name code] |
| 41 | + (when-ok [_ (when (str/blank? coord) |
| 42 | + (ba/incorrect (missing-param-msg name code)))] |
| 43 | + (-> (system/parse-decimal coord) |
| 44 | + (ba/exceptionally (update-msg (param-parsing-err-msg name code)))))) |
| 45 | + |
| 46 | +(defn- distance-in-meters [dist unit code] |
| 47 | + (cond |
| 48 | + (str/blank? dist) |
| 49 | + 1000M |
| 50 | + |
| 51 | + (or (nil? unit) (= unit "km")) |
| 52 | + (when-ok [dist (parse-dec-arg dist "distance" code)] |
| 53 | + (* dist 1000)) |
| 54 | + |
| 55 | + (= unit "m") |
| 56 | + (parse-dec-arg dist "distance" code) |
| 57 | + |
| 58 | + :else |
| 59 | + (ba/incorrect (unsupported-unit-msg unit code)))) |
| 60 | + |
| 61 | +(defrecord SearchParamNear [index name type code] |
| 62 | + p/SearchParam |
| 63 | + (-validate-modifier [_ modifier] |
| 64 | + (some->> modifier (u/unknown-modifier-anom code))) |
| 65 | + |
| 66 | + (-compile-value [_ _ value] |
| 67 | + (let [[lat long dist unit] (str/split value #"\|" 4)] |
| 68 | + (when-ok [parsed-lat (parse-dec-arg lat "latitude" code) |
| 69 | + parsed-long (parse-dec-arg long "longitude" code) |
| 70 | + meters (distance-in-meters dist unit code)] |
| 71 | + {:latitude parsed-lat |
| 72 | + :longitude parsed-long |
| 73 | + :distance meters}))) |
| 74 | + |
| 75 | + (-estimated-scan-size [_ _ _ _ _] |
| 76 | + (ba/unsupported)) |
| 77 | + |
| 78 | + (-supports-ordered-index-handles [_ _ _ _ _] |
| 79 | + true) |
| 80 | + |
| 81 | + (-ordered-index-handles [search-param batch-db tid modifier compiled-values] |
| 82 | + (if (= 1 (count compiled-values)) |
| 83 | + (p/-index-handles search-param batch-db tid modifier (first compiled-values)) |
| 84 | + (let [index-handles #(p/-index-handles search-param batch-db tid modifier %)] |
| 85 | + (u/union-index-handles (map index-handles compiled-values))))) |
| 86 | + |
| 87 | + (-ordered-index-handles [search-param batch-db tid modifier compiled-values start-id] |
| 88 | + (if (= 1 (count compiled-values)) |
| 89 | + (p/-index-handles search-param batch-db tid modifier (first compiled-values) start-id) |
| 90 | + (let [index-handles #(p/-index-handles search-param batch-db tid modifier % start-id)] |
| 91 | + (u/union-index-handles (map index-handles compiled-values))))) |
| 92 | + |
| 93 | + (-index-handles [search-param batch-db tid modifier compiled-value] |
| 94 | + (coll/eduction |
| 95 | + (comp (p/-matcher search-param batch-db modifier [compiled-value]) |
| 96 | + (map ih/from-resource-handle)) |
| 97 | + (d/type-list batch-db tid))) |
| 98 | + |
| 99 | + (-index-handles [search-param batch-db tid modifier compiled-value start-id] |
| 100 | + (coll/eduction |
| 101 | + (comp (p/-matcher search-param batch-db modifier [compiled-value]) |
| 102 | + (map ih/from-resource-handle)) |
| 103 | + (d/type-list batch-db tid start-id))) |
| 104 | + |
| 105 | + (-supports-ordered-compartment-index-handles [_ _] |
| 106 | + false) |
| 107 | + |
| 108 | + (-ordered-compartment-index-handles [_ _ _ _ _] |
| 109 | + (ba/unsupported)) |
| 110 | + |
| 111 | + (-ordered-compartment-index-handles [_ _ _ _ _ _] |
| 112 | + (ba/unsupported)) |
| 113 | + |
| 114 | + (-matcher [_ batch-db _ compiled-values] |
| 115 | + (filter #(matches? batch-db % compiled-values))) |
| 116 | + |
| 117 | + (-single-version-id-matcher [search-param batch-db tid modifier compiled-values] |
| 118 | + (comp (map ih/from-single-version-id) |
| 119 | + (u/resource-handle-xf batch-db tid) |
| 120 | + (p/-matcher search-param batch-db modifier compiled-values) |
| 121 | + (map svi/from-resource-handle))) |
| 122 | + |
| 123 | + (-second-pass-filter [_ _ _]) |
| 124 | + |
| 125 | + (-index-values [_ _ _] |
| 126 | + [])) |
| 127 | + |
| 128 | +(defmethod special/special-search-param "near" |
| 129 | + [index _] |
| 130 | + (->SearchParamNear index "near" "special" "near")) |
0 commit comments