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

♻️ Remove usage of db.getCommittedOpVersion() #657

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions lib/submit-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,24 @@ SubmitRequest.prototype.submit = function(callback) {
// 'Document already exists' error in response and fail to submit this
// op. However, this could also happen in the case that the op was
// already committed and the create op was simply resent. In that
// case, we should return a non-fatal 'Op already submitted' error. We
// must get the past ops and check their src and seq values to
// differentiate.
backend.db.getCommittedOpVersion(collection, id, snapshot, op, null, function(err, version) {
// case, we should return a non-fatal 'Op already submitted' error.
//
// To check this, let's see if our create op has already been committed.
// It can be uniquely identified by src+seq, so we can compare that with the
// database.
//
// We fetch the op by version number, which is okay with creates because:
// - version can only be changed by transforming the op by remote ops
// - a create op cannot be transformed by any other op
// - therefore, a create op's version **never** changes between submit and
// commit, so we can fetch by version number
backend.db.getOps(collection, id, op.v, op.v + 1, null, function(err, ops) {
if (err) return callback(err);
if (version == null) {
callback(request.alreadyCreatedError());
} else {
op.v = version;
var committedOp = ops[0];
if (op.src === committedOp.src && op.seq === committedOp.seq) {
callback(request.alreadySubmittedError());
} else {
callback(request.alreadyCreatedError());
}
});
return;
Expand Down
23 changes: 23 additions & 0 deletions test/client/submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,29 @@ module.exports = function() {
});
});

it('does not fail when resubmitting a create op', function(done) {
var backend = this.backend;
var connection = backend.connect();
var submitted = false;
backend.use('submit', function(request, next) {
if (!submitted) {
submitted = true;
connection.close();
backend.connect(connection);
}
next();
});
var count = 0;
backend.use('reply', function(message, next) {
if (!message.request.create) return next();
count++;
next();
if (count === 2) done();
});
var doc = connection.get('dogs', 'fido');
doc.create({age: 3}, errorHandler(done));
});

it('server fetches and transforms by already committed op', function(done) {
var doc = this.backend.connect().get('dogs', 'fido');
var doc2 = this.backend.connect().get('dogs', 'fido');
Expand Down
Loading