Skip to content

Instantly share code, notes, and snippets.

@rystsov
Created July 17, 2017 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rystsov/48ab8a21d75db2b47b402dac478f13bd to your computer and use it in GitHub Desktop.
Save rystsov/48ab8a21d75db2b47b402dac478f13bd to your computer and use it in GitHub Desktop.
function fixed_read(db, key) {
var value = db.read(key);
while (true) {
// If we try to write the read value then we
const written = db.updateWhen(key, {
predicate: `x => x.ver==${value.ver}`,
value: { ver: value.ver+1, val: value.val}
});
if (!written.isConflict) {
// either succeed and return the just written value
return written.value;
}
// or fail and use a newer value and repeat the trick.
value = written.conflict;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment