Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a transactional write operation that will automatically retry #137

Open
deontologician opened this issue Feb 24, 2016 · 1 comment
Open

Comments

@deontologician
Copy link
Contributor

Leaving this to @mlucy to fill out details.

@mlucy
Copy link
Member

mlucy commented Feb 24, 2016

The proposal is basically that you can write something like this:

collection.find(id).atomicReplace(function(obj) {
  obj.a = obj.a + 1
  return obj;
})

(Where the name could obviously be better.)

Every object that Horizon stores in RethinkDB will have a $reql_version$ field (we've already reserved the $reql_*$ namespace for internal use) that is set to a UUID, and every write operation issued by Horizon will also update that version to a new UUID. This version will never be passed to the user of the Horizon client library, we'll strip it out before objects reach them.

atomicReplace will be implemented like so:

  1. Fetch the document, record its version V.
  2. Call the function passed to atomicReplace, get a new document D.
  3. Issue this query:
collection.get(id).replace {|row| 
  r.branch(row['$reql_version$'].eq(V), D.merge({'$reql_version$' => r.uuid()}), r.error("retry"))
}
  1. If we get the "retry" error, go back to step 1. Otherwise we're done.

The advantage of this interface is that it lets you issue a write that will never be clobbered by another concurrent write. So in the example above, if you issue 5 of those increment operations, you know that a will be larger by exactly 5 when they all complete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants