sharedb
Version:
JSON OT database backend
188 lines (160 loc) • 6.41 kB
JavaScript
// This contains the master OT functions for the database. They look like
// ot-types style operational transform functions, but they're a bit different.
// These functions understand versions and can deal with out of bound create &
// delete operations.
var types = require('./types').map;
var ShareDBError = require('./error');
var ERROR_CODE = ShareDBError.CODES;
// Returns an error string on failure. Rockin' it C style.
exports.checkOp = function(op) {
if (op == null || typeof op !== 'object') {
return new ShareDBError(ERROR_CODE.ERR_OT_OP_BADLY_FORMED, 'Op must be an object');
}
if (op.create != null) {
if (typeof op.create !== 'object') {
return new ShareDBError(ERROR_CODE.ERR_OT_OP_BADLY_FORMED, 'Create data must be an object');
}
var typeName = op.create.type;
if (typeof typeName !== 'string') {
return new ShareDBError(ERROR_CODE.ERR_OT_OP_BADLY_FORMED, 'Missing create type');
}
var type = types[typeName];
if (type == null || typeof type !== 'object') {
return new ShareDBError(ERROR_CODE.ERR_DOC_TYPE_NOT_RECOGNIZED, 'Unknown type');
}
} else if (op.del != null) {
if (op.del !== true) return new ShareDBError(ERROR_CODE.ERR_OT_OP_BADLY_FORMED, 'del value must be true');
} else if (op.op == null) {
return new ShareDBError(ERROR_CODE.ERR_OT_OP_BADLY_FORMED, 'Missing op, create, or del');
}
if (op.src != null && typeof op.src !== 'string') {
return new ShareDBError(ERROR_CODE.ERR_OT_OP_BADLY_FORMED, 'src must be a string');
}
if (op.seq != null && typeof op.seq !== 'number') {
return new ShareDBError(ERROR_CODE.ERR_OT_OP_BADLY_FORMED, 'seq must be a string');
}
if (
(op.src == null && op.seq != null) ||
(op.src != null && op.seq == null)
) {
return new ShareDBError(ERROR_CODE.ERR_OT_OP_BADLY_FORMED, 'Both src and seq must be set together');
}
if (op.m != null && typeof op.m !== 'object') {
return new ShareDBError(ERROR_CODE.ERR_OT_OP_BADLY_FORMED, 'op.m must be an object or null');
}
};
// Takes in a string (type name or URI) and returns the normalized name (uri)
exports.normalizeType = function(typeName) {
return types[typeName] && types[typeName].uri;
};
// This is the super apply function that takes in snapshot data (including the
// type) and edits it in-place. Returns an error or null for success.
exports.apply = function(snapshot, op) {
if (typeof snapshot !== 'object') {
return new ShareDBError(ERROR_CODE.ERR_APPLY_SNAPSHOT_NOT_PROVIDED, 'Missing snapshot');
}
if (snapshot.v != null && op.v != null && snapshot.v !== op.v) {
return new ShareDBError(ERROR_CODE.ERR_APPLY_OP_VERSION_DOES_NOT_MATCH_SNAPSHOT, 'Version mismatch');
}
// Create operation
if (op.create) {
if (snapshot.type) return new ShareDBError(ERROR_CODE.ERR_DOC_ALREADY_CREATED, 'Document already exists');
// The document doesn't exist, although it might have once existed
var create = op.create;
var type = types[create.type];
if (!type) return new ShareDBError(ERROR_CODE.ERR_DOC_TYPE_NOT_RECOGNIZED, 'Unknown type');
try {
snapshot.data = type.create(create.data);
snapshot.type = type.uri;
snapshot.v++;
} catch (err) {
return err;
}
// Delete operation
} else if (op.del) {
snapshot.data = undefined;
snapshot.type = null;
snapshot.v++;
// Edit operation
} else if (op.op) {
var err = applyOpEdit(snapshot, op.op);
if (err) return err;
snapshot.v++;
// No-op, and we don't have to do anything
} else {
snapshot.v++;
}
};
function applyOpEdit(snapshot, edit) {
if (!snapshot.type) return new ShareDBError(ERROR_CODE.ERR_DOC_DOES_NOT_EXIST, 'Document does not exist');
if (edit == null) return new ShareDBError(ERROR_CODE.ERR_OT_OP_NOT_PROVIDED, 'Missing op');
var type = types[snapshot.type];
if (!type) return new ShareDBError(ERROR_CODE.ERR_DOC_TYPE_NOT_RECOGNIZED, 'Unknown type');
try {
snapshot.data = type.apply(snapshot.data, edit);
} catch (err) {
return err;
}
}
exports.transform = function(type, op, appliedOp) {
// There are 16 cases this function needs to deal with - which are all the
// combinations of create/delete/op/noop from both op and appliedOp
if (op.v != null && op.v !== appliedOp.v) {
return new ShareDBError(ERROR_CODE.ERR_OP_VERSION_MISMATCH_DURING_TRANSFORM, 'Version mismatch');
}
if (appliedOp.del) {
if (op.create || op.op) {
return new ShareDBError(ERROR_CODE.ERR_DOC_WAS_DELETED, 'Document was deleted');
}
} else if (
(appliedOp.create && (op.op || op.create || op.del)) ||
(appliedOp.op && op.create)
) {
// If appliedOp.create is not true, appliedOp contains an op - which
// also means the document exists remotely.
return new ShareDBError(ERROR_CODE.ERR_DOC_ALREADY_CREATED, 'Document was created remotely');
} else if (appliedOp.op && op.op) {
// If we reach here, they both have a .op property.
if (!type) return new ShareDBError(ERROR_CODE.ERR_DOC_DOES_NOT_EXIST, 'Document does not exist');
if (typeof type === 'string') {
type = types[type];
if (!type) return new ShareDBError(ERROR_CODE.ERR_DOC_TYPE_NOT_RECOGNIZED, 'Unknown type');
}
try {
op.op = type.transform(op.op, appliedOp.op, 'left');
} catch (err) {
return err;
}
}
if (op.v != null) op.v++;
};
/**
* Apply an array of ops to the provided snapshot.
*
* @param snapshot - a Snapshot object which will be mutated by the provided ops
* @param ops - an array of ops to apply to the snapshot
* @return an error object if applicable
*/
exports.applyOps = function(snapshot, ops) {
var type = null;
if (snapshot.type) {
type = types[snapshot.type];
if (!type) return new ShareDBError(ERROR_CODE.ERR_DOC_TYPE_NOT_RECOGNIZED, 'Unknown type');
}
for (var index = 0; index < ops.length; index++) {
var op = ops[index];
snapshot.v = op.v + 1;
if (op.create) {
type = types[op.create.type];
if (!type) return new ShareDBError(ERROR_CODE.ERR_DOC_TYPE_NOT_RECOGNIZED, 'Unknown type');
snapshot.data = type.create(op.create.data);
snapshot.type = type.uri;
} else if (op.del) {
snapshot.data = undefined;
type = null;
snapshot.type = null;
} else {
snapshot.data = type.apply(snapshot.data, op.op);
}
}
};