The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Skip reanalyzing search models on every row hook (#78286)
Chris Truter17ff890b7dce
diff --git a/src/metabase/search/spec.clj b/src/metabase/search/spec.cljindex 87891a3ffdf..6060688c1b9 100644--- a/src/metabase/search/spec.clj+++ b/src/metabase/search/spec.clj@@ -2,6 +2,7 @@(:require[buddy.core.codecs :as codecs][buddy.core.hash :as buddy-hash]+ [clojure.core.memoize :as memoize][clojure.set :as set][clojure.string :as str][clojure.walk :as walk]@@ -425,14 +426,24 @@(derive (:model spec#) :hook/search-index)(defmethod spec* ~search-model [~'_] spec#))))-;; TODO we should memoize this for production (based on spec values)+(def ^:private model-hooks*+ ;; Specs are immutable in production, and keying on the methods map also does the useful thing after a+ ;; REPL/test reload.+ ;; The key is only an invalidation token: the value comes from the static search-models list, so the first+ ;; call builds a complete map even though resolving those models registers more methods while it runs.+ ;; Its key is stale by then, which costs one recompute and nothing else.+ (memoize/fifo+ (fn [_spec-methods]+ (->> (specifications)+ vals+ (map search-model-hooks)+ merge-hooks))+ :fifo/threshold 1))+(defn model-hooks"Return an inverted map of data dependencies to search models, used for updating them based on underlying models."[]- (->> (specifications)- vals- (map search-model-hooks)- merge-hooks))+ (model-hooks* (methods spec*)))(defn- instance->db-values"Given a transformed toucan map, get back a mapping to the raw db values that we can use in a query."diff --git a/test/metabase/search/spec_test.clj b/test/metabase/search/spec_test.cljindex e3eb9a217e6..cd58cd3d9ff 100644--- a/test/metabase/search/spec_test.clj+++ b/test/metabase/search/spec_test.clj@@ -4,6 +4,8 @@[metabase.search.spec :as search.spec][toucan2.core :as t2]))+(set! *warn-on-reflection* true)+(deftest ^:parallel test-qualify-column(is (= [:table.column :column] (#'search.spec/qualify-column :table :column)))(is (= :qualified.column (#'search.spec/qualify-column :table :qualified.column)))@@ -146,6 +148,36 @@(testing "... and nothing else does"(is (empty? (sort-by name (remove expected-models actual-models))))))))+(deftest ^:parallel model-hooks-are-cached-test+ ;; The first call resolves lazily loaded models, which registers more spec methods and changes the cache key.+ (search.spec/model-hooks)+ (is (identical? (search.spec/model-hooks) (search.spec/model-hooks))))++(deftest ^:synchronized model-hooks-cache-invalidates-on-spec-redefinition-test+ (testing "replacing a spec method invalidates the cached model-hooks"+ ;; Registering a throwaway spec would derive a fake model into :hook/search-index and break+ ;; every-model-is-hooked-test, so probe by swapping an existing method out and back.+ (let [spec-multifn search.spec/spec*+ ;; The first call resolves lazily loaded models, registering spec methods as it goes, so it caches+ ;; under a key that is stale by the time it returns. The second call warms the settled method table.+ _ (search.spec/model-hooks)+ warm (search.spec/model-hooks)+ original (get-method spec-multifn "card")]+ ;; Without a hit here the probe below would miss whatever the cache key did, proving nothing.+ (is (identical? warm (search.spec/model-hooks)))+ (try+ (.addMethod ^clojure.lang.MultiFn spec-multifn+ "card"+ (fn [_]+ (update (original "card") :render-terms assoc :cache-probe :cache_probe)))+ (is (contains? (->> (get (search.spec/model-hooks) :model/Card)+ (filter #(= "card" (:search-model %)))+ first+ :fields)+ :cache_probe))+ (finally+ (.addMethod ^clojure.lang.MultiFn spec-multifn "card" original))))))+(deftest ^:parallel index-version-hash-test(testing "index-version-hash returns a consistent value"(let [hash1 (search.spec/index-version-hash)