2024年Clojure状态调查中分享您的想法!

欢迎!请查看关于页面,了解有关此操作的更多信息。

0
序列
编辑
(defn- find-story-ids-by-tag [tag-id]

(map :id (db-content-tag/read-contents-by-tag-id (config/db-spec) tag-id)))

(defn- update-story [txn publisher-id story-id tag-id]
(let [{:keys [published-json]} (db-content/find-by-id txn story-id)

    updated-tags (filter (fn [tag] (not= (:id tag) tag-id)) (:tags published-json))
    _ (db-story/update-published-json-without-timestamps txn publisher-id story-id (assoc published-json :tags updated-tags))]
(log/info {:message "[TAG-DELETION] Updated Story Tags in Published JSON"
           :publisher-id publisher-id
           :tag-id tag-id
           :story-id story-id
           :updated-tags-json updated-tags})))

(defn- delete-tag [publisher-id tag-id]
(let [associated-content-ids (find-story-ids-by-tag tag-id)]

(transaction/with-transaction
  [txn (config/db-spec)]
  (do
    (when (seq associated-content-ids)
      (do
        (doseq [story-id associated-content-ids]
          (update-story txn publisher-id story-id tag-id))
        (db-content-tag/delete-batch-by-tag txn tag-id associated-content-ids)
        (log/info {:message "[TAG-DELETION] Deleted from Content Tag"
                   :publisher-id publisher-id
                   :tag-id tag-id
                   :story-ids associated-content-ids})))
    (db-tag/delete txn publisher-id tag-id)
    (log/info {:message "[TAG-DELETION] Deleted Tag"
               :publisher-id publisher-id
               :tag-id tag-id})))))

(defn run [publisher-id tag-ids]
(comment run 123 [4 5 6])
(try

(do
  (log/info {:message "[TAG-DELETION] started"
             :publisher-id publisher-id
             :tag-ids tag-ids})
  (doseq [tag-id tag-ids] (if (db-tag/find-by-id (config/db-spec) publisher-id tag-id)
                            (delete-tag publisher-id tag-id)
                            (log/info {:message "[TAG-DELETION] Tag Not Found"
                                       :publisher-id publisher-id
                                       :tag-id tag-id})))
  (log/info {:message "[TAG-DELETION] completed"
             :publisher-id publisher-id
             :tag-ids tag-ids}))
(catch Exception e
  (log/exception e {:message "[TAG-DELETION] errored"
                    :publisher-id publisher-id
                    :tag-ids tag-ids}))))

如果我有不到100个标签,这可行,但是如果有100000个标签,则非常耗时。如何修改此代码,使其并行运行并节省时间?目前该任务在删除和更新故事时都使用了seq

登录注册后回答此问题。

...