你能用一个原子计算Clojure STM交易中的重试次数吗?我尝试过如下,它似乎有效:
(def annotatedCounter (agent 0))
(def finishedCounter (agent 0))
(def annotatedSentences (ref {}))
(def corroboratedSentences (ref {}))
(def retryCounter (atom 0))
;; ...
(defn upvote [id]
(dosync
(if (contains? @annotatedSentences id)
(alter annotatedSentences update-in [id :counter] inc)))
(dosync
(if (contains? @annotatedSentences id)
(do
(if (> (get-in @annotatedSentences [id :counter]) 5)
(do
(swap! retryCounter inc)
(alter corroboratedSentences conj {id (get @annotatedSentences id)})
(alter annotatedSentences dissoc annotatedSentences id)
(send annotatedCounter dec)
(send finishedCounter inc)))))))
我在交易的较长时间内添加了上面看到的retryCounter,以计算交易总数。我知道代理只会在事务中发送一次inc函数,无论重试次数如何,但这是否有用,可以计算一个原子的事务数量?