Skip to content

Commit

Permalink
CMR-9498 Fixing PR suggestions.
Browse files Browse the repository at this point in the history
  • Loading branch information
eereiter committed Nov 9, 2023
1 parent a220aec commit 56ca1c7
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
[cmr.common.hash-cache :as hash-cache]
[cmr.common.jobs :refer [defjob]]
[cmr.common.log :as log :refer [info]]
[cmr.common.util :as u]
[cmr.common.util :as c-util]
[cmr.metadata-db.services.concept-service :as metadata-db]
[cmr.umm-spec.versioning :as umm-version]))

Expand Down Expand Up @@ -48,13 +48,13 @@
(let [mdb-context (cmn-coll-metadata-cache/context->metadata-db-context context)
concepts (doall (metadata-db/get-concepts mdb-context concept-tuples true))
concepts (cmn-coll-metadata-cache/concepts-without-xml-processing-inst concepts)
rfms (u/fast-map #(crfm/compress
(crfm/concept->revision-format-map context
%
(cached-formats)
metadata-transformer/transform-to-multiple-formats
true))
concepts)]
rfms (c-util/fast-map #(crfm/compress
(crfm/concept->revision-format-map context
%
(cached-formats)
metadata-transformer/transform-to-multiple-formats
true))
concepts)]
(reduce #(assoc %1 (:concept-id %2) %2) {} rfms)))

(defn update-cache
Expand Down Expand Up @@ -119,5 +119,4 @@

(comment
(refresh-cache {:system (get-in user/system [:apps :bootstrap])})
(cmn-coll-metadata-cache/prettify-cache (get-in user/system [:apps :bootstrap :caches cmn-coll-metadata-cache/cache-key]))
)
(cmn-coll-metadata-cache/prettify-cache (get-in user/system [:apps :bootstrap :caches cmn-coll-metadata-cache/cache-key])))
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[cmr.common.log :as log :refer (debug info warn error)]
[cmr.common.mime-types :as mt]
[cmr.common.services.errors :as errors]
[cmr.common.util :as u]
[cmr.common.util :as c-util]
[cmr.common.xml :as cx]
[cmr.umm-spec.migration.version.core :as vm]
[cmr.umm-spec.umm-json :as umm-json]
Expand Down Expand Up @@ -42,22 +42,32 @@
{}
target-formats)))

(defn transform-with-strategy-migrate-umm-json-to-target-format
"Adds to the translated-map the concept migrated to the passed in target-format."
[context concept-type source-version metadata translated-map target-format]
(assoc translated-map target-format
(umm-json/umm->json
(c-util/remove-nils-empty-maps-seqs
(vm/migrate-umm context
concept-type
source-version
(umm-spec/umm-json-version concept-type
target-format)
(json/decode metadata true))))))

(defn- transform-with-strategy-migrate-umm-json
[context concept _ target-formats]
(let [{concept-mime-type :format, metadata :metadata, concept-type :concept-type} concept
source-version (umm-spec/umm-json-version concept-type concept-mime-type)
[t result] (u/time-execution (reduce (fn [translated-map target-format]
(assoc translated-map target-format
(umm-json/umm->json
(u/remove-nils-empty-maps-seqs
(vm/migrate-umm context
concept-type
source-version
(umm-spec/umm-json-version concept-type
target-format)
(json/decode metadata true))))))
{}
target-formats))]
[t result] (c-util/time-execution (reduce #(transform-with-strategy-migrate-umm-json-to-target-format
context
concept-type
source-version
metadata
%1
%2)
{}
target-formats))]
(info "transform-with-strategy migrate-umm-json: "
"time: " t
"concept-mime-type: " concept-mime-type
Expand Down Expand Up @@ -88,21 +98,25 @@
(format "Unexpected transform strategy [%s] from concept of type [%s] to [%s]"
strategy (:format concept) (pr-str target-formats)))))

(defn transform-to-multiple-formats-with-strategy
"Transforms a concept into a map of formats dictated by the passed target-formats list."
[context concept ignore-exceptions? [k v]]
(if ignore-exceptions?
(try
(transform-with-strategy context concept k v)
(catch Throwable e
(log/error
e
(str "Ignoring exception while trying to transform metadata for concept "
(:concept-id concept) " with revision " (:revision-id concept) " error: "
(.getMessage e)))))
(transform-with-strategy context concept k v)))

(defn transform-to-multiple-formats
"Transforms the concept into multiple different formats. Returns a map of target format to metadata."
[context concept target-formats ignore-exceptions?]
{:pre [(not (:deleted concept))]}
(->> target-formats
(group-by #(transform-strategy concept %))
(keep (fn [[k v]]
(if ignore-exceptions?
(try
(transform-with-strategy context concept k v)
(catch Throwable e
(log/error
e
(str "Ignoring exception while trying to transform metadata for concept "
(:concept-id concept) " with revision " (:revision-id concept) " error: "
(.getMessage e)))))
(transform-with-strategy context concept k v))))
(keep #(transform-to-multiple-formats-with-strategy context concept ignore-exceptions? %))
(reduce into {})))
13 changes: 4 additions & 9 deletions common-app-lib/src/cmr/common_app/api/routes.clj
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@
(acl/verify-ingest-management-permission request-context :read)
(let [cache (cache/context->cache request-context (keyword cache-name))]
(when cache
(let [result (case (str (type cache))
"class cmr.redis_utils.redis_hash_cache.RedisHashCache" (map #(hcache/get-keys cache %1) (:keys-to-track cache))
(let [result (if (instance? cmr.redis_utils.redis_hash_cache.RedisHashCache cache)
(map #(hcache/get-keys cache %) (:keys-to-track cache))
(cache/get-keys cache))]
{:status 200
:body (json/generate-string result)}))))
Expand All @@ -131,14 +131,9 @@
headers :headers}
(acl/verify-ingest-management-permission request-context :read)
(let [cache-key (keyword cache-key)
;; caches (get-in request-context [:system :caches])
;; _ (println "CACHES")
;; _ (clojure.pprint/pprint caches)
;; _ (println "GETTING CACHE " (keyword cache-name))
cache (cache/context->cache request-context (keyword cache-name))
;; _ (when (nil? cache) (println "CACHE IS NIL"))
result (case (str (type cache))
"class cmr.redis_utils.redis_hash_cache.RedisHashCache" (hcache/get-map cache cache-key)
result (if (instance? cmr.redis_utils.redis_hash_cache.RedisHashCache cache)
(hcache/get-map cache cache-key)
(cache/get-value cache cache-key))]
(if result
{:status 200
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
incremental-since-refresh-date (hash-cache/get-value cache
cache-key
incremental-since-refresh-date-key)

query (q/query {:concept-type :collection
:condition (data-range-condition (or incremental-since-refresh-date "1600-01-01T00:00:00"))
:skip-acls? true
Expand All @@ -92,9 +91,8 @@
"Returns a simplified version of the cache to help with debugging cache problems."
[cache]
(let [cache-map (hash-cache/get-map cache cache-key)
incremental-since-refresh-date-key (hash-cache/get-value cache
cache-key
incremental-since-refresh-date-key)
cache-map (dissoc cache-map incremental-since-refresh-date-key)]
(println incremental-since-refresh-date-key incremental-since-refresh-date-key)
(u/map-values crfm/prettify cache-map)))
incremental-since-refresh-date (get cache-map incremental-since-refresh-date-key)
revision-format-map (dissoc cache-map incremental-since-refresh-date-key)]
(assoc (u/map-values crfm/prettify revision-format-map)
incremental-since-refresh-date-key
incremental-since-refresh-date)))
8 changes: 2 additions & 6 deletions common-lib/src/cmr/common/cache.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
(ns cmr.common.cache
"Defines the core caching protocol for the CMR."
(:require
[clojure.string :as string]))
"Defines the core caching protocol for the CMR.")

(defn context->cache
"Get the cache for the given key from the context"
Expand Down Expand Up @@ -38,9 +36,7 @@
"Function that takes a cache and checks to see if the
cache is cmr.common.cache or cmr.common.hash-cache."
[cache]
(if (string/includes? (str (type cache)) "hash_cache")
false
true))
(not (instance? cmr.redis_utils.redis_hash_cache.RedisHashCache cache)))

(defn reset-caches
"Clear all caches found in the system, this includes the caches of embedded systems."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
(:require
[clj-time.core :as t]
[clojure.set :as cset]
[clojure.string :as string]
[cmr.common-app.data.metadata-retrieval.collection-metadata-cache :as cmn-coll-metadata-cache]
[cmr.common-app.data.metadata-retrieval.revision-format-map :as crfm]
[cmr.common.config :refer [defconfig]]
Expand Down Expand Up @@ -109,10 +108,15 @@
new-cache-value)
(info "Metadata cache update complete. Cache Size:" (hash-cache/cache-size cache cmn-coll-metadata-cache/cache-key))))

(defn in-memory-db?
"Checks to see if the database in the context is an in-memory db."
[system]
(instance? cmr.common.memory_db.connection.MemoryStore
(get-in {:system system} [:system :embedded-systems :metadata-db :db])))

(defjob RefreshCollectionsMetadataCache
[ctx system]
(when (string/includes? (str (type (get-in {:system system} [:system :embedded-systems :metadata-db :db])))
"cmr.common.memory_db.connection.MemoryStore")
(when (in-memory-db? system)
(refresh-cache {:system system})))

(defn refresh-collections-metadata-cache-job
Expand All @@ -123,8 +127,7 @@

(defjob UpdateCollectionsMetadataCache
[ctx system]
(when (string/includes? (str (type (get-in {:system system} [:system :embedded-systems :metadata-db :db])))
"cmr.common.memory_db.connection.MemoryStore")
(when (in-memory-db? system)
(update-cache-job {:system system})))

(defn update-collections-metadata-cache-job
Expand Down

0 comments on commit 56ca1c7

Please sign in to comment.