|
| 1 | +(ns blaze.db.impl.search-param.near |
| 2 | + (:require |
| 3 | + [blaze.anomaly :as ba :refer [when-ok]] |
| 4 | + [blaze.coll.core :as coll] |
| 5 | + [blaze.db.api :as d] |
| 6 | + [blaze.db.impl.index.index-handle :as ih] |
| 7 | + [blaze.db.impl.index.resource-as-of :as rao] |
| 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 :as type] |
| 14 | + [blaze.fhir.spec.type.system :as system] |
| 15 | + [clojure.string :as str] |
| 16 | + [cognitect.anomalies :as anom])) |
| 17 | + |
| 18 | +(set! *warn-on-reflection* true) |
| 19 | + |
| 20 | +(defn- near? [{:keys [longitude latitude]} {:keys [distance] :as compiled-value}] |
| 21 | + (let [coordinates {:latitude (type/value latitude) |
| 22 | + :longitude (type/value longitude)} |
| 23 | + actual-dist (spng/haversine-distance coordinates compiled-value)] |
| 24 | + (<= actual-dist distance))) |
| 25 | + |
| 26 | +(defn- matches? [batch-db resource-handle compiled-values] |
| 27 | + (let [{:keys [position]} @(d/pull batch-db resource-handle)] |
| 28 | + (some #(near? position %) compiled-values))) |
| 29 | + |
| 30 | +(defn- missing-param-msg [arg code] |
| 31 | + (format "Missing argument `%s` in search parameter `%s`." arg code)) |
| 32 | + |
| 33 | +(defn- 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-decimal-arg [coord name code] |
| 41 | + (if (str/blank? coord) |
| 42 | + (ba/incorrect (missing-param-msg name code)) |
| 43 | + (-> (system/parse-decimal coord) |
| 44 | + (ba/exceptionally (update-msg (parsing-err-msg name code)))))) |
| 45 | + |
| 46 | +(defn- unsupported-unit-msg [unit code] |
| 47 | + (format "Unsupported unit `%s` in search parameter `%s`. Supported are 'km', 'm'." unit code)) |
| 48 | + |
| 49 | +(defn- parse-distance [dist-str unit code] |
| 50 | + (cond |
| 51 | + (str/blank? dist-str) |
| 52 | + 1000M |
| 53 | + |
| 54 | + (or (nil? unit) (= unit "km")) |
| 55 | + (when-ok [dist (parse-decimal-arg dist-str "distance" code)] |
| 56 | + (* dist 1000)) |
| 57 | + |
| 58 | + (= unit "m") |
| 59 | + (parse-decimal-arg dist-str "distance" code) |
| 60 | + |
| 61 | + :else |
| 62 | + (ba/incorrect (unsupported-unit-msg unit code)))) |
| 63 | + |
| 64 | +(def ^:private ^:const min-latitude -90.0) |
| 65 | + |
| 66 | +(def ^:private ^:const max-latitude 90.0) |
| 67 | + |
| 68 | +(defn invalid-latitude-msg [arg code] |
| 69 | + (format |
| 70 | + "Invalid argument `%s` for latitude in search parameter `%s`, must be between %s and %s." |
| 71 | + arg code min-latitude max-latitude)) |
| 72 | + |
| 73 | +(defn- parse-latitude [val code] |
| 74 | + (when-ok [latitude (parse-decimal-arg val "latitude" code)] |
| 75 | + (if (<= min-latitude latitude max-latitude) |
| 76 | + latitude |
| 77 | + (ba/incorrect (invalid-latitude-msg val code))))) |
| 78 | + |
| 79 | +(def ^:private ^:const min-longitude -180.0) |
| 80 | + |
| 81 | +(def ^:private ^:const max-longitude 180.0) |
| 82 | + |
| 83 | +(defn invalid-longitude-msg [val code] |
| 84 | + (format |
| 85 | + "Invalid argument `%s` for longitude in search parameter `%s`, must be between %s and %s." |
| 86 | + val code min-longitude max-longitude)) |
| 87 | + |
| 88 | +(defn- parse-longitude [val code] |
| 89 | + (when-ok [longitude (parse-decimal-arg val "longitude" code)] |
| 90 | + (if (<= min-longitude longitude max-longitude) |
| 91 | + longitude |
| 92 | + (ba/incorrect (invalid-longitude-msg val code))))) |
| 93 | + |
| 94 | +(defrecord SearchParamNear [index name type code] |
| 95 | + p/SearchParam |
| 96 | + (-validate-modifier [_ modifier] |
| 97 | + (some->> modifier (u/unknown-modifier-anom code))) |
| 98 | + |
| 99 | + (-compile-value [_ _ value] |
| 100 | + (let [[lat long dist unit] (str/split value #"\|" 4)] |
| 101 | + (when-ok [parsed-lat (parse-latitude lat code) |
| 102 | + parsed-long (parse-longitude long code) |
| 103 | + parsed-dist (parse-distance dist unit code)] |
| 104 | + {:latitude parsed-lat |
| 105 | + :longitude parsed-long |
| 106 | + :distance parsed-dist}))) |
| 107 | + |
| 108 | + (-estimated-scan-size [_ batch-db tid _ _] |
| 109 | + (rao/estimated-scan-size (:kv-store batch-db) tid)) |
| 110 | + |
| 111 | + (-supports-ordered-index-handles [_ _ _ _ _] |
| 112 | + true) |
| 113 | + |
| 114 | + (-ordered-index-handles [search-param batch-db tid modifier compiled-values] |
| 115 | + (if (= 1 (count compiled-values)) |
| 116 | + (p/-index-handles search-param batch-db tid modifier (first compiled-values)) |
| 117 | + (let [index-handles #(p/-index-handles search-param batch-db tid modifier %)] |
| 118 | + (u/union-index-handles (map index-handles compiled-values))))) |
| 119 | + |
| 120 | + (-ordered-index-handles [search-param batch-db tid modifier compiled-values start-id] |
| 121 | + (if (= 1 (count compiled-values)) |
| 122 | + (p/-index-handles search-param batch-db tid modifier (first compiled-values) start-id) |
| 123 | + (let [index-handles #(p/-index-handles search-param batch-db tid modifier % start-id)] |
| 124 | + (u/union-index-handles (map index-handles compiled-values))))) |
| 125 | + |
| 126 | + (-index-handles [search-param batch-db tid modifier compiled-value] |
| 127 | + (coll/eduction |
| 128 | + (comp (p/-matcher search-param batch-db modifier [compiled-value]) |
| 129 | + (map ih/from-resource-handle)) |
| 130 | + (p/-type-list batch-db tid))) |
| 131 | + |
| 132 | + (-index-handles [search-param batch-db tid modifier compiled-value start-id] |
| 133 | + (coll/eduction |
| 134 | + (comp (p/-matcher search-param batch-db modifier [compiled-value]) |
| 135 | + (map ih/from-resource-handle)) |
| 136 | + (p/-type-list batch-db tid start-id))) |
| 137 | + |
| 138 | + (-supports-ordered-compartment-index-handles [_ _] |
| 139 | + false) |
| 140 | + |
| 141 | + (-ordered-compartment-index-handles [_ _ _ _ _] |
| 142 | + (ba/unsupported)) |
| 143 | + |
| 144 | + (-ordered-compartment-index-handles [_ _ _ _ _ _] |
| 145 | + (ba/unsupported)) |
| 146 | + |
| 147 | + (-matcher [_ batch-db _ compiled-values] |
| 148 | + (filter #(matches? batch-db % compiled-values))) |
| 149 | + |
| 150 | + (-single-version-id-matcher [search-param batch-db tid modifier compiled-values] |
| 151 | + (comp (map ih/from-single-version-id) |
| 152 | + (u/resource-handle-xf batch-db tid) |
| 153 | + (p/-matcher search-param batch-db modifier compiled-values) |
| 154 | + (map svi/from-resource-handle))) |
| 155 | + |
| 156 | + (-second-pass-filter [_ _ _]) |
| 157 | + |
| 158 | + (-index-values [_ _ _] |
| 159 | + [])) |
| 160 | + |
| 161 | +(defmethod special/special-search-param "near" |
| 162 | + [index _] |
| 163 | + (->SearchParamNear index "near" "special" "near")) |
0 commit comments