|
24 | 24 | (and subscriptions-enabled? |
25 | 25 | (= :granule concept-type))) |
26 | 26 |
|
27 | | -(defn remove-from-existing-mode |
28 | | - "Depending on the passed in new-mode ['New' 'Update'] remove from the structure |
29 | | - {Collection concept id: {\"New\" 1 |
30 | | - \"Update\" 1}} |
31 | | - either the count or the mode if count is 0." |
32 | | - [existing-mode new-mode] |
33 | | - (loop [ms new-mode |
34 | | - result existing-mode] |
35 | | - (let [mode (first ms) |
36 | | - mode-count (if (and result |
37 | | - (result mode)) |
38 | | - (result mode) 0) |
39 | | - compare-to-one (compare mode-count 1)] |
40 | | - (if mode |
41 | | - (when (seq result) |
42 | | - (if (or (= 0 compare-to-one) |
43 | | - (= -1 compare-to-one)) |
44 | | - (recur (rest ms) (dissoc result mode)) |
45 | | - (recur (rest ms) (assoc result mode (dec mode-count))))) |
46 | | - result)))) |
47 | | - |
48 | | -(defn delete-subscription |
49 | | - "When a subscription is deleted, the collection-concept-id must be removed |
50 | | - from the subscription cache. Decrement the count if more than 1 subscription |
51 | | - uses the collection-concept-id." |
52 | | - [context concept-type revisioned-tombstone] |
53 | | - (when (subscription-concept? concept-type revisioned-tombstone) |
54 | | - (let [coll-concept-id (:collection-concept-id (:extra-fields revisioned-tombstone)) |
55 | | - existing-value (subscription-cache/get-value context coll-concept-id) |
56 | | - mode (:mode (:extra-fields revisioned-tombstone)) |
57 | | - new-mode (remove-from-existing-mode existing-value mode)] |
58 | | - (if (seq new-mode) |
59 | | - (subscription-cache/set-value context coll-concept-id new-mode) |
60 | | - (subscription-cache/remove-value context coll-concept-id))))) |
| 27 | +(defn ^:dynamic get-subscriptions-from-db |
| 28 | + "Get the subscriptions from the database. This function primarily exists so that |
| 29 | + it can be stubbed out for unit tests." |
| 30 | + ([context] |
| 31 | + (mdb-search/find-concepts context {:latest true |
| 32 | + :concept-type :subscription |
| 33 | + :subscription-type "granule"})) |
| 34 | + ([context coll-concept-id] |
| 35 | + (mdb-search/find-concepts context {:latest true |
| 36 | + :concept-type :subscription |
| 37 | + :collection-concept-id coll-concept-id}))) |
61 | 38 |
|
62 | 39 | (defn add-to-existing-mode |
63 | | - "Depending on the passed in new-mode ['New' 'Update'] create a structure that looks like |
64 | | - {Collection concept id: {\"New\" 1 |
65 | | - \"Update\" 1}}" |
66 | | - [existing-mode new-mode] |
67 | | - (loop [ms new-mode |
68 | | - result existing-mode] |
69 | | - (let [mode (first ms) |
70 | | - mode-count (if (and result |
71 | | - (result mode)) |
72 | | - (result mode) |
73 | | - 0)] |
74 | | - (if mode |
| 40 | + "Depending on the passed in new-mode [\"New\" \"Update\"] create a structure that merges |
| 41 | + the new mode to the existing mode. The result looks like [\"New\" \"Update\"]" |
| 42 | + [existing-modes new-modes] |
| 43 | + (loop [ms new-modes |
| 44 | + result existing-modes] |
| 45 | + (let [mode (first ms)] |
| 46 | + (if (nil? mode) |
| 47 | + result |
75 | 48 | (if result |
76 | | - (recur (rest ms) (assoc result mode (inc mode-count))) |
77 | | - (recur (rest ms) (assoc {} mode (inc mode-count)))) |
78 | | - result)))) |
| 49 | + (if (some #(= mode %) result) |
| 50 | + (recur (rest ms) result) |
| 51 | + (recur (rest ms) (merge result mode))) |
| 52 | + (recur (rest ms) [mode])))))) |
| 53 | + |
| 54 | +(defn merge-modes |
| 55 | + "Go through the list of subscriptions to see if any exist that match the |
| 56 | + passed in collection-concept-id. Return true if any subscription exists |
| 57 | + otherwise return false." |
| 58 | + [subscriptions] |
| 59 | + (loop [subs subscriptions |
| 60 | + result []] |
| 61 | + (let [sub (first subs)] |
| 62 | + (if (nil? sub) |
| 63 | + result |
| 64 | + (recur (rest subs) (add-to-existing-mode result (get-in sub [:extra-fields :mode]))))))) |
79 | 65 |
|
80 | | -(defn add-subscription |
81 | | - "When a subscription is added, the collection-concept-id must be put into |
82 | | - the subscription cache. If the collection-concept-id already exists then another |
83 | | - subscription is also using it so increment the count. The count is used to determine |
84 | | - whether or not the entry can be deleted. The modes are concatenated so that the user |
85 | | - of the cache knows which modes to key off of." |
| 66 | +(defn change-subscription |
| 67 | + "When a subscription is added or deleted, the collection-concept-id must be put into |
| 68 | + or deleted from the subscription cache. Get the subscriptions that match the collection-concept-id |
| 69 | + from the database and rebuild the modes list." |
86 | 70 | [context concept-type concept] |
87 | 71 | (when (subscription-concept? concept-type concept) |
88 | 72 | (let [coll-concept-id (:collection-concept-id (:extra-fields concept)) |
89 | | - mode (:mode (:extra-fields concept)) |
90 | | - existing-mode (subscription-cache/get-value context coll-concept-id) |
91 | | - new-value (add-to-existing-mode existing-mode mode)] |
92 | | - (subscription-cache/set-value context coll-concept-id new-value)))) |
| 73 | + subs (filter #(subscription-concept? (get % :concept-type) %) |
| 74 | + (get-subscriptions-from-db context coll-concept-id))] |
| 75 | + (if (seq subs) |
| 76 | + (subscription-cache/set-value context coll-concept-id (merge-modes subs)) |
| 77 | + (subscription-cache/remove-value context coll-concept-id))))) |
93 | 78 |
|
94 | 79 | (defn create-subscription-cache-contents-for-refresh |
95 | 80 | "Go through all of the subscriptions and find the ones that are |
96 | 81 | ingest subscriptions. Create the mode values for each collection-concept-id |
97 | 82 | and put those into a map. The end result looks like: |
98 | | - {Collection concept id 1: {\"New\" 1 |
99 | | - \"Update\" 1} |
100 | | - Collection concept id 2: {\"New\" 2 |
101 | | - \"Update\" 1 |
102 | | - \"Delete\" 3} |
| 83 | + {Collection concept id 1: [\"New\" \"Update\"] |
| 84 | + Collection concept id 2: [\"New\" \"Update\" \"Delete\"] |
103 | 85 | ...}" |
104 | 86 | [result sub] |
105 | 87 | (let [metadata (:metadata sub) |
|
111 | 93 | mode (:Mode metadata-edn)] |
112 | 94 | (if concept-map |
113 | 95 | (update result coll-concept-id #(add-to-existing-mode % mode)) |
114 | | - (assoc concept-map coll-concept-id (add-to-existing-mode nil mode)))) |
| 96 | + (assoc result coll-concept-id (add-to-existing-mode nil mode)))) |
115 | 97 | result))) |
116 | 98 |
|
117 | | -(defn ^:dynamic get-subscriptions-from-db |
118 | | - "Get the subscriptions from the database. This function primarily exists so that |
119 | | - it can be stubbed out for unit tests." |
120 | | - [context] |
121 | | - (mdb-search/find-concepts context {:latest true |
122 | | - :concept-type :subscription})) |
123 | | - |
124 | 99 | (defn refresh-subscription-cache |
125 | 100 | "Go through all of the subscriptions and create a map of collection concept ids and |
126 | 101 | their mode values. Get the old keys from the cache and if the keys exist in the new structure |
|
0 commit comments