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

Question about the commit #126

Closed
nieyuyao opened this issue Apr 17, 2022 · 3 comments
Closed

Question about the commit #126

nieyuyao opened this issue Apr 17, 2022 · 3 comments

Comments

@nieyuyao
Copy link

Why write the op before snapshot, is there any consideration?

sharedb-mongo/index.js

Lines 239 to 254 in 9e88c28

ShareDbMongo.prototype.commit = function(collectionName, id, op, snapshot, options, callback) {
var self = this;
var request = createRequestForMiddleware(options, collectionName, op);
this._writeOp(collectionName, id, op, snapshot, function(err, result) {
if (err) return callback(err);
var opId = result.insertedId;
self._writeSnapshot(request, id, snapshot, opId, function(err, succeeded) {
if (succeeded) return callback(err, succeeded);
// Cleanup unsuccessful op if snapshot write failed. This is not
// necessary for data correctness, but it gets rid of clutter
self._deleteOp(request.collectionName, opId, function(removeErr) {
callback(err || removeErr, succeeded);
});
});
});
};

@alecgibson
Copy link
Contributor

I think this is done like this because back in MongoDB 3, there wasn't support for transactions, so something always needed to be written first, and ops were chosen.

There's a bit of a writeup on the behaviour in the README.

@nieyuyao
Copy link
Author

@alecgibson Thanks for your reply. But i still have a question, can i write the snapshot before op?

@alecgibson
Copy link
Contributor

If you're talking about an implementation that you're doing yourself, there's no reason why not, so long as you ensure that you have some way to ensure the op being written is "canonical", and that you don't have conflicting snapshot writes.

As I mentioned above, a new implementation might use MongoDB transactions, although this may not be as performant; there's some (light) discussion on the topic in #127

@nieyuyao nieyuyao closed this as completed May 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants