Permalink
Browse files

[Experimental] Add `:swap/delete` support to swap utils

  • Loading branch information...
ptaoussanis committed Nov 3, 2018
1 parent b1cf02b commit 9773d8a1358943062426c53e4336f1b390150f18
Showing with 82 additions and 56 deletions.
  1. +12 −10 src/lua/cas-hset.lua
  2. +12 −10 src/lua/cas-set.lua
  3. +58 −36 src/taoensso/carmine.clj
@@ -1,18 +1,20 @@
local curr_val = redis.call('hget', _:k, _:field);

local do_swap = false;
if (_:old-?sha ~= '') then
local curr_sha = redis.sha1hex(curr_val);
if (curr_sha == _:old-?sha) then
redis.call('hset', _:k, _:field, _:new-val);
return 1;
else
return 0;
end
if (curr_sha == _:old-?sha) then do_swap = true; end
else
if (curr_val == _:old-?val) then
redis.call('hset', _:k, _:field, _:new-val);
return 1;
if (curr_val == _:old-?val) then do_swap = true; end
end

if do_swap then
if (_:delete == '1') then
redis.call('hdel', _:k, _:field);
else
return 0;
redis.call('hset', _:k, _:field, _:new-val);
end
return 1;
else
return 0;
end
@@ -1,18 +1,20 @@
local curr_val = redis.call('get', _:k);

local do_swap = false;
if (_:old-?sha ~= '') then
local curr_sha = redis.sha1hex(curr_val);
if (curr_sha == _:old-?sha) then
redis.call('set', _:k, _:new-val);
return 1;
else
return 0;
end
if (curr_sha == _:old-?sha) then do_swap = true; end
else
if (curr_val == _:old-?val) then
redis.call('set', _:k, _:new-val);
return 1;
if (curr_val == _:old-?val) then do_swap = true; end
end

if do_swap then
if (_:delete == '1') then
redis.call('del', _:k);
else
return 0;
redis.call('set', _:k, _:new-val);
end
return 1;
else
return 0;
end
@@ -533,8 +533,9 @@
nx? (= ex 0)
[new-val return-val] (enc/-vswapped (f old-val nx?))
cas-success?
(if (= new-val :swap/abort)
true
(case new-val
:swap/abort (do (unwatch) true)
:swap/delete (do (unwatch) (del k) true)
(parse nil
(with-replies
(parse-suppress (multi) (set k new-val))
@@ -557,32 +558,34 @@

(comment (enc/qb 1000 (prep-cas-old-val "hello there")))

(def compare-and-set "Experimental."
(let [script (enc/slurp-resource "lua/cas-set.lua")]
(fn self
([k old-val ?sha new-val]
(let [script (enc/slurp-resource "lua/cas-set.lua")]
(defn compare-and-set "Experimental."
([k old-val new-val]
(let [[?sha raw-bs] (prep-cas-old-val old-val)]
(compare-and-set k raw-bs ?sha new-val)))

([k old-val ?sha new-val]
(let [delete? (= new-val :cas/delete)]
(lua script {:k k}
{:old-?sha (or ?sha "")
:old-?val (if ?sha "" old-val)
:new-val new-val}))
:delete (if delete? 1 0)
:new-val (if delete? "" new-val)})))))

([k old-val new-val]
(let [[?sha raw-bs] (prep-cas-old-val old-val)]
(self k raw-bs ?sha new-val))))))
(let [script (enc/slurp-resource "lua/cas-hset.lua")]
(defn compare-and-hset "Experimental."
([k field old-val new-val]
(let [[?sha raw-bs] (prep-cas-old-val old-val)]
(compare-and-hset k field raw-bs ?sha new-val)))

(def compare-and-hset "Experimental."
(let [script (enc/slurp-resource "lua/cas-hset.lua")]
(fn self
([k field old-val ?sha new-val]
([k field old-val ?sha new-val]
(let [delete? (= new-val :cas/delete)]
(lua script {:k k}
{:field field
:old-?sha (or ?sha "")
:old-?val (if ?sha "" old-val)
:new-val new-val}))

([k field old-val new-val]
(let [[?sha raw-bs] (prep-cas-old-val old-val)]
(self k field raw-bs ?sha new-val))))))
:delete (if delete? 1 0)
:new-val (if delete? "" new-val)})))))

(comment
(wcar {} (del "cas-k") (set "cas-k" 0) (compare-and-set "cas-k" 0 1))
@@ -603,14 +606,22 @@
?sha (when-not (= sha "") sha)
[new-val return-val] (enc/-vswapped (f old-val nx?))
cas-success?
(if (= new-val :swap/abort)
true
(= 1 (parse nil
(with-replies
(if nx?
(setnx k new-val)
(compare-and-set k old-val
?sha new-val))))))]
(case new-val
:swap/abort true
:swap/delete
(if nx?
true
(= 1
(parse nil
(with-replies
(compare-and-set k old-val ?sha :cas/delete)))))

(= 1
(parse nil
(with-replies
(if nx?
(setnx k new-val)
(compare-and-set k old-val ?sha new-val))))))]

(if cas-success?
(return return-val)
@@ -628,22 +639,33 @@
?sha (when-not (= sha "") sha)
[new-val return-val] (enc/-vswapped (f old-val nx?))
cas-success?
(if (= new-val :swap/abort)
true
(= 1 (parse nil
(with-replies
(if nx?
(hsetnx k field new-val)
(compare-and-hset k field old-val
?sha new-val))))))]
(case new-val
:swap/abort true
:swap/delete
(if nx?
true
(= 1
(parse nil
(with-replies
(compare-and-hset k field old-val ?sha :cas/delete)))))

(= 1
(parse nil
(with-replies
(if nx?
(hsetnx k field new-val)
(compare-and-hset k field old-val ?sha new-val))))))]

(if cas-success?
(return return-val)
(if (or (nil? nmax-attempts) (< idx (long nmax-attempts)))
(recur (inc idx))
(return abort-val))))))))

(comment (enc/qb 100 (wcar {} (swap "swap-k" (fn [?old _] ?old)))))
(comment
(enc/qb 100 (wcar {} (swap "swap-k" (fn [?old _] ?old))))
(wcar {} (get "swap-k"))
(wcar {} (swap "swap-k" (fn [?old _] :swap/delete))))

;;;;

0 comments on commit 9773d8a

Please sign in to comment.