SHARE
TWEET

Untitled

a guest Jul 17th, 2017 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function fixed_read(db, key) {
  2.   var value = db.read(key);
  3.   while (true) {
  4.     // If we try to write the read value then we
  5.     const written = db.updateWhen(key, {
  6.       predicate: `x => x.ver==${value.ver}`,
  7.       value: { ver: value.ver+1, val: value.val}
  8.     });
  9.     if (!written.isConflict) {
  10.       // either succeed and return the just written value
  11.       return written.value;
  12.     }
  13.     // or fail and use a newer value and repeat the trick.
  14.     value = written.conflict;
  15.   }
  16. }
RAW Paste Data
Top