diff options
author | Rich Hickey <richhickey@gmail.com> | 2009-06-30 11:22:39 -0400 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2009-06-30 11:22:39 -0400 |
commit | 6201f5e2ddd52f1b483d75563b0380deba59777c (patch) | |
tree | bd7688bb8f124f6acadc26693a7afce1058e90e7 /src/clj | |
parent | 5e34989a7098a55ca014030d7446d2dc20ebc33b (diff) |
added ref min/max history control - refs #138
Diffstat (limited to 'src/clj')
-rw-r--r-- | src/clj/clojure/core.clj | 39 |
1 files changed, 37 insertions, 2 deletions
diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj index ba674763..38c58daf 100644 --- a/src/clj/clojure/core.clj +++ b/src/clj/clojure/core.clj @@ -1268,14 +1268,30 @@ :validator validate-fn + :min-history (default 0) + :max-history (default 10) + If metadata-map is supplied, it will be come the metadata on the ref. validate-fn must be nil or a side-effect-free fn of one argument, which will be passed the intended new state on any state change. If the new state is unacceptable, the validate-fn should return false or throw an exception. validate-fn will be called on - transaction commit, when all refs have their final values." + transaction commit, when all refs have their final values. + + Normally refs accumulate history dynamically as needed to deal with + read demands. If you know in advance you will need history you can + set :min-history to ensure it will be available when first needed (instead + of after a read fault). History is limited, and the limit can be set + with :max-history." ([x] (new clojure.lang.Ref x)) - ([x & options] (setup-reference (ref x) options))) + ([x & options] + (let [r #^clojure.lang.Ref (setup-reference (ref x) options) + opts (apply hash-map options)] + (when (:max-history opts) + (.setMaxHistory r (:max-history opts))) + (when (:min-history opts) + (.setMinHistory r (:min-history opts))) + r))) (defn deref "Also reader macro: @ref/@agent/@var/@atom/@delay/@future. Within a transaction, @@ -1383,6 +1399,25 @@ [#^clojure.lang.Ref ref val] (. ref (set val))) +(defn ref-history-count + "Returns the history count of a ref" + [#^clojure.lang.Ref ref] + (.getHistoryCount ref)) + +(defn ref-min-history + "Gets the min-history of a ref, or sets it and returns the ref" + ([#^clojure.lang.Ref ref] + (.getMinHistory ref)) + ([#^clojure.lang.Ref ref n] + (.setMinHistory ref n))) + +(defn ref-max-history + "Gets the max-history of a ref, or sets it and returns the ref" + ([#^clojure.lang.Ref ref] + (.getMaxHistory ref)) + ([#^clojure.lang.Ref ref n] + (.setMaxHistory ref n))) + (defn ensure "Must be called in a transaction. Protects the ref from modification by other transactions. Returns the in-transaction-value of |