Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
alecgibson committed May 29, 2024
1 parent 7abe650 commit 5aeaf89
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
10 changes: 9 additions & 1 deletion lib/submit-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var ot = require('./ot');
var projections = require('./projections');
var ShareDBError = require('./error');
var types = require('./types');
var akg = require('../test/akg-debug');

var ERROR_CODE = ShareDBError.CODES;

Expand Down Expand Up @@ -91,17 +92,21 @@ SubmitRequest.prototype.submit = function(callback) {
// Send a special projection so that getSnapshot knows to return all fields.
// With a null projection, it strips document metadata
var fields = {$submit: true};
akg.debug('submit', collection, id, op);

var snapshotOptions = {};
snapshotOptions.agentCustom = request.agent.custom;
backend.db.getSnapshot(collection, id, fields, snapshotOptions, function(err, snapshot) {
if (err) return callback(err);
akg.debug('get snapshot', collection, id, snapshot);

request.snapshot = snapshot;
request._addSnapshotMeta();

if (op.v == null) {
akg.debug('null version');
if (op.create && snapshot.type && op.src) {
akg.debug('check create collision');
// If the document was already created by another op, we will return a
// 'Document already exists' error in response and fail to submit this
// op. However, this could also happen in the case that the op was
Expand All @@ -110,7 +115,8 @@ SubmitRequest.prototype.submit = function(callback) {
// must get the past ops and check their src and seq values to
// differentiate.
request._fetchCreateOpVersion(function(error, version) {
if (err) return callback(err);
akg.debug('fetch create op version returned', error, version);
if (error) return callback(error);
if (version == null) {
callback(request.alreadyCreatedError());
} else {
Expand Down Expand Up @@ -301,6 +307,7 @@ SubmitRequest.prototype._shouldSaveMilestoneSnapshot = function(snapshot) {

SubmitRequest.prototype._fetchCreateOpVersion = function(callback) {
var create = this.snapshot.m._create;
akg.debug('got create meta', create);
if (create) {
var version = (create.src === this.op.src && create.seq === this.op.seq) ? create.v : null;
return callback(null, version);
Expand All @@ -310,6 +317,7 @@ SubmitRequest.prototype._fetchCreateOpVersion = function(callback) {
// This can happen if a client tries to re-create or resubmit a create op to
// a "legacy" snapshot that existed before we started adding the meta (should
// be uncommon) or when using a driver that doesn't support metadata (eg Postgres).
akg.debug('get committed op version...');
this.backend.db.getCommittedOpVersion(this.collection, this.id, this.snapshot, this.op, null, callback);
};

Expand Down
9 changes: 9 additions & 0 deletions test/akg-debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function debug() {
if (!module.exports.enabled) return;
console.log.apply(console, arguments);
}

module.exports = {
enabled: false,
debug: debug
};
13 changes: 10 additions & 3 deletions test/client/submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ var deserializedType = require('./deserialized-type');
var numberType = require('./number-type');
var errorHandler = require('../util').errorHandler;
var richText = require('rich-text');
var MemoryDB = require('../../lib/db/memory');
types.register(deserializedType.type);
types.register(deserializedType.type2);
types.register(numberType.type);
types.register(richText.type);
var akg = require('../akg-debug');

module.exports = function() {
describe('client submit', function() {
Expand Down Expand Up @@ -210,13 +210,17 @@ module.exports = function() {

describe('create', function() {
describe('metadata enabled', function() {
afterEach(function() {
akg.enabled = false;
});

runCreateTests();
});

describe('no snapshot metadata available', function() {
beforeEach(function() {
var getSnapshot = MemoryDB.prototype.getSnapshot;
sinon.stub(MemoryDB.prototype, 'getSnapshot')
var getSnapshot = this.backend.db.getSnapshot;
sinon.stub(this.backend.db, 'getSnapshot')
.callsFake(function() {
var args = Array.from(arguments);
var callback = args.pop();
Expand All @@ -229,6 +233,7 @@ module.exports = function() {
});

afterEach(function() {
akg.enabled = false;
sinon.restore();
});

Expand Down Expand Up @@ -297,6 +302,8 @@ module.exports = function() {
});

it('does not fail when resubmitting a create op on a doc that was deleted', function(done) {
akg.enabled = true;
console.log('> START TEST');
var backend = this.backend;
var connection1 = backend.connect();
var connection2 = backend.connect();
Expand Down

0 comments on commit 5aeaf89

Please sign in to comment.