Guest User

Untitled

a guest
Mar 21st, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. (import '[clojure.lang IAtom IDeref])
  2. (import '[org.apache.ignite IgniteAtomicReference])
  3.  
  4. (defn swap-helper [a f & args]
  5. (let [old-val (deref a)
  6. new-val (apply f old-val args)]
  7. (if (.compareAndSet (.-state a) old-val new-val)
  8. new-val
  9. (recur a f args))))
  10.  
  11. ;; no implementation yet!
  12. (deftype IgniteAtom [^IgniteAtomicReference state]
  13. IAtom
  14. (swap [this f] (swap-helper this f))
  15.  
  16. (swap [this f x] (swap-helper this f x))
  17.  
  18. (swap [this f x y args] (apply swap-helper this f x y args))
  19.  
  20. (compareAndSet [this old-val new-val]
  21. (.compareAndSet state old-val new-val))
  22.  
  23. (reset [this new-val]
  24. (.set state new-val)
  25. new-val)
  26.  
  27. IDeref
  28. (deref [this] (.get state)))
  29.  
  30. ;; function to create
  31. (defn ignite-atom [ignite id init-val]
  32. (->IgniteAtom (.atomicReference ignite id init-val true)))
  33.  
  34. (import '[org.apache.ignite Ignition])
  35.  
  36. ;; start Ignite node
  37. (def ignite (Ignition/start))
  38.  
  39. ;; create or retrieve
  40. (def a (ignite-atom ignite "ignite-atom" 0))
Add Comment
Please, Sign In to add comment