Created
July 17, 2017 15:14
-
-
Save rystsov/48ab8a21d75db2b47b402dac478f13bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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