Don't store and send version data

This commit is contained in:
James Allen 2014-05-16 13:06:35 +01:00
parent 79457227e1
commit 7cca4f0368
11 changed files with 31 additions and 95 deletions

View file

@ -21,15 +21,15 @@ module.exports = DocManager =
return callback(error) if error?
return callback null, docs
updateDoc: (project_id, doc_id, lines, version, callback = (error, modified, rev) ->) ->
updateDoc: (project_id, doc_id, lines, callback = (error, modified, rev) ->) ->
DocManager.getDoc project_id, doc_id, (error, doc, mongoPath) ->
return callback(error) if error?
return callback new Errors.NotFoundError("No such project/doc: #{project_id}/#{doc_id}") if !doc?
if _.isEqual(doc.lines, lines) and doc.version == version
if _.isEqual(doc.lines, lines)
logger.log {
project_id: project_id, doc_id: doc_id, rev: doc.rev, version: doc.version
}, "doc lines and version have not changed"
project_id: project_id, doc_id: doc_id, rev: doc.rev
}, "doc lines have not changed"
return callback null, false, doc.rev
else
logger.log {
@ -38,10 +38,8 @@ module.exports = DocManager =
oldDocLines: doc.lines
newDocLines: lines
rev: doc.rev
oldVersion: doc.version
newVersion: version
}, "updating doc lines"
MongoManager.updateDoc project_id, mongoPath, lines, version, (error) ->
MongoManager.updateDoc project_id, mongoPath, lines, (error) ->
return callback(error) if error?
callback null, true, doc.rev + 1 # rev will have been incremented in mongo by MongoManager.updateDoc

View file

@ -30,15 +30,14 @@ module.exports = HttpController =
project_id = req.params.project_id
doc_id = req.params.doc_id
lines = req.body?.lines
version = req.body?.version
if !lines? or lines not instanceof Array
logger.error project_id: project_id, doc_id: doc_id, "no doc lines provided"
res.send 400 # Bad Request
return
logger.log project_id: project_id, doc_id: doc_id, version: version, "updating doc"
DocManager.updateDoc project_id, doc_id, lines, version, (error, modified, rev) ->
logger.log project_id: project_id, doc_id: doc_id, "updating doc"
DocManager.updateDoc project_id, doc_id, lines, (error, modified, rev) ->
return next(error) if error?
res.json {
modified: modified
@ -58,5 +57,4 @@ module.exports = HttpController =
_id: doc._id.toString()
lines: doc.lines
rev: doc.rev
version: doc.version
}

View file

@ -5,12 +5,11 @@ module.exports = MongoManager =
db.projects.find _id: ObjectId(project_id.toString()), {}, (error, projects = []) ->
callback error, projects[0]
updateDoc: (project_id, docPath, lines, version, callback = (error) ->) ->
updateDoc: (project_id, docPath, lines, callback = (error) ->) ->
update =
$set: {}
$inc: {}
update.$set["#{docPath}.lines"] = lines
update.$set["#{docPath}.version"] = version if version?
update.$inc["#{docPath}.rev"] = 1
db.projects.update _id: ObjectId(project_id), update, callback

View file

@ -13,7 +13,7 @@ describe "Deleting a doc", ->
@version = 42
DocstoreClient.createProject @project_id, (error) =>
throw error if error?
DocstoreClient.createDoc @project_id, @doc_id, @lines, @version, (error) =>
DocstoreClient.createDoc @project_id, @doc_id, @lines, (error) =>
throw error if error?
done()

View file

@ -12,24 +12,21 @@ describe "Getting all docs", ->
@docs = [{
_id: ObjectId()
lines: ["one"]
version: 1
rev: 2
}, {
_id: ObjectId()
lines: ["two"]
version: 3
rev: 4
}, {
_id: ObjectId()
lines: ["three"]
version: 5
rev: 6
}]
DocstoreClient.createProject @project_id, (error) =>
throw error if error?
jobs = for doc in @docs
do (doc) =>
(callback) => DocstoreClient.createDoc @project_id, doc._id, doc.lines, doc.version, callback
(callback) => DocstoreClient.createDoc @project_id, doc._id, doc.lines, callback
async.series jobs, done
afterEach (done) ->

View file

@ -10,10 +10,9 @@ describe "Getting a doc", ->
@project_id = ObjectId()
@doc_id = ObjectId()
@lines = ["original", "lines"]
@version = 42
DocstoreClient.createProject @project_id, (error) =>
throw error if error?
DocstoreClient.createDoc @project_id, @doc_id, @lines, @version, (error) =>
DocstoreClient.createDoc @project_id, @doc_id, @lines, (error) =>
throw error if error?
done()
@ -24,7 +23,6 @@ describe "Getting a doc", ->
it "should get the doc lines and version", (done) ->
DocstoreClient.getDoc @project_id, @doc_id, (error, res, doc) =>
doc.lines.should.deep.equal @lines
doc.version.should.equal @version
done()
describe "when the doc does not exist", ->

View file

@ -11,11 +11,9 @@ describe "Applying updates to a doc", ->
@doc_id = ObjectId()
@originalLines = ["original", "lines"]
@newLines = ["new", "lines"]
@originalVersion = 42
@newVersion = 53
DocstoreClient.createProject @project_id, (error) =>
throw error if error?
DocstoreClient.createDoc @project_id, @doc_id, @lines, @version, (error) =>
DocstoreClient.createDoc @project_id, @doc_id, @lines, (error) =>
throw error if error?
done()
@ -24,7 +22,7 @@ describe "Applying updates to a doc", ->
describe "when the content has changed", ->
beforeEach (done) ->
DocstoreClient.updateDoc @project_id, @doc_id, @newLines, @newVersion, (error, res, @body) =>
DocstoreClient.updateDoc @project_id, @doc_id, @newLines, (error, res, @body) =>
done()
it "should return modified = true", ->
@ -33,12 +31,11 @@ describe "Applying updates to a doc", ->
it "should update the doc in the API", (done) ->
DocstoreClient.getDoc @project_id, @doc_id, (error, res, doc) =>
doc.lines.should.deep.equal @newLines
doc.version.should.deep.equal @newVersion
done()
describe "when the content has not been updated", ->
beforeEach (done) ->
DocstoreClient.updateDoc @project_id, @doc_id, @originalLines, @originalVersion, (error, res, @body) =>
DocstoreClient.updateDoc @project_id, @doc_id, @originalLines, (error, res, @body) =>
done()
it "should return modified = false", ->
@ -47,13 +44,12 @@ describe "Applying updates to a doc", ->
it "should not update the doc in the API", (done) ->
DocstoreClient.getDoc @project_id, @doc_id, (error, res, doc) =>
doc.lines.should.deep.equal @originalLines
doc.version.should.deep.equal @originalVersion
done()
describe "when the doc does not exist", ->
beforeEach (done) ->
missing_doc_id = ObjectId()
DocstoreClient.updateDoc @project_id, missing_doc_id, @originalLines, @newVersion, (error, @res, @body) =>
DocstoreClient.updateDoc @project_id, missing_doc_id, @originalLines, (error, @res, @body) =>
done()
it "should return a 404", ->
@ -62,7 +58,7 @@ describe "Applying updates to a doc", ->
describe "when the project does not exist", ->
beforeEach (done) ->
missing_project_id = ObjectId()
DocstoreClient.updateDoc missing_project_id, @doc_id, @originalLines, @newVersion, (error, @res, @body) =>
DocstoreClient.updateDoc missing_project_id, @doc_id, @originalLines, (error, @res, @body) =>
done()
it "should return a 404", ->
@ -71,7 +67,7 @@ describe "Applying updates to a doc", ->
describe "when malformed doc lines are provided", ->
describe "when the lines are not an array", ->
beforeEach (done) ->
DocstoreClient.updateDoc @project_id, @doc_id, { foo: "bar" }, @newVersion, (error, @res, @body) =>
DocstoreClient.updateDoc @project_id, @doc_id, { foo: "bar" }, (error, @res, @body) =>
done()
it "should return 400", ->
@ -84,7 +80,7 @@ describe "Applying updates to a doc", ->
describe "when the lines are not present", ->
beforeEach (done) ->
DocstoreClient.updateDoc @project_id, @doc_id, null, @newVersion, (error, @res, @body) =>
DocstoreClient.updateDoc @project_id, @doc_id, null, (error, @res, @body) =>
done()
it "should return 400", ->
@ -99,7 +95,7 @@ describe "Applying updates to a doc", ->
beforeEach (done) ->
line = new Array(1025).join("x") # 1kb
@largeLines = Array.apply(null, Array(1024)).map(() -> line) # 1mb
DocstoreClient.updateDoc @project_id, @doc_id, @largeLines, @newVersion, (error, res, @body) =>
DocstoreClient.updateDoc @project_id, @doc_id, @largeLines, (error, res, @body) =>
done()
it "should return modified = true", ->
@ -108,6 +104,5 @@ describe "Applying updates to a doc", ->
it "should update the doc in the API", (done) ->
DocstoreClient.getDoc @project_id, @doc_id, (error, res, doc) =>
doc.lines.should.deep.equal @largeLines
doc.version.should.deep.equal @newVersion
done()

View file

@ -8,7 +8,7 @@ module.exports = DocstoreClient =
rootFolder: [{ docs: [] }]
}, callback
createDoc: (project_id, doc_id, lines, version, callback = (error) ->) ->
createDoc: (project_id, doc_id, lines, callback = (error) ->) ->
db.projects.update {
_id: project_id
}, {
@ -16,7 +16,6 @@ module.exports = DocstoreClient =
"rootFolder.0.docs": {
_id: doc_id
lines: lines
version: version
}
}
}, callback
@ -36,12 +35,11 @@ module.exports = DocstoreClient =
json: true
}, callback
updateDoc: (project_id, doc_id, lines, version, callback = (error, res, body) ->) ->
updateDoc: (project_id, doc_id, lines, callback = (error, res, body) ->) ->
request.post {
url: "http://localhost:3016/project/#{project_id}/doc/#{doc_id}"
json:
lines: lines
version: version
}, callback
deleteDoc: (project_id, doc_id, callback = (error, res, body) ->) ->

View file

@ -149,15 +149,15 @@ describe "DocManager", ->
beforeEach ->
@oldDocLines = ["old", "doc", "lines"]
@newDocLines = ["new", "doc", "lines"]
@doc = { _id: @doc_id, lines: @oldDocLines, rev: @rev = 5, version: @version = 42 }
@doc = { _id: @doc_id, lines: @oldDocLines, rev: @rev = 5 }
@mongoPath = "mock.mongo.path"
@MongoManager.updateDoc = sinon.stub().callsArg(4)
@MongoManager.updateDoc = sinon.stub().callsArg(3)
describe "when the doc lines have changed", ->
beforeEach ->
@DocManager.getDoc = sinon.stub().callsArgWith(2, null, @doc, @mongoPath)
@DocManager.updateDoc @project_id, @doc_id, @newDocLines, @version, @callback
@DocManager.updateDoc @project_id, @doc_id, @newDocLines, @callback
it "should get the existing doc", ->
@DocManager.getDoc
@ -166,7 +166,7 @@ describe "DocManager", ->
it "should update the doc with the new doc lines", ->
@MongoManager.updateDoc
.calledWith(@project_id, @mongoPath, @newDocLines, @version)
.calledWith(@project_id, @mongoPath, @newDocLines)
.should.equal true
it "should log out the old and new doc lines", ->
@ -177,41 +177,6 @@ describe "DocManager", ->
oldDocLines: @oldDocLines
newDocLines: @newDocLines
rev: @doc.rev
oldVersion: @version
newVersion: @version
"updating doc lines"
)
.should.equal true
it "should return the callback with the new rev", ->
@callback.calledWith(null, true, @rev + 1).should.equal true
describe "when the version has changed", ->
beforeEach ->
@newVersion = 1003
@DocManager.getDoc = sinon.stub().callsArgWith(2, null, @doc, @mongoPath)
@DocManager.updateDoc @project_id, @doc_id, @oldDocLines, @newVersion, @callback
it "should get the existing doc", ->
@DocManager.getDoc
.calledWith(@project_id, @doc_id)
.should.equal true
it "should update the doc with the new version", ->
@MongoManager.updateDoc
.calledWith(@project_id, @mongoPath, @oldDocLines, @newVersion)
.should.equal true
it "should log out the old and new doc lines", ->
@logger.log
.calledWith(
project_id: @project_id
doc_id: @doc_id
oldDocLines: @oldDocLines
newDocLines: @oldDocLines
rev: @doc.rev
oldVersion: @version
newVersion: @newVersion
"updating doc lines"
)
.should.equal true
@ -222,7 +187,7 @@ describe "DocManager", ->
describe "when the doc lines have not changed", ->
beforeEach ->
@DocManager.getDoc = sinon.stub().callsArgWith(2, null, @doc, @mongoPath)
@DocManager.updateDoc @project_id, @doc_id, @oldDocLines.slice(), @version, @callback
@DocManager.updateDoc @project_id, @doc_id, @oldDocLines.slice(), @callback
it "should not update the doc", ->
@MongoManager.updateDoc.called.should.equal false
@ -233,7 +198,7 @@ describe "DocManager", ->
describe "when the doc does not exist", ->
beforeEach ->
@DocManager.getDoc = sinon.stub().callsArgWith(2, null, null, null)
@DocManager.updateDoc @project_id, @doc_id, @newDocLines, @version, @callback
@DocManager.updateDoc @project_id, @doc_id, @newDocLines, @callback
it "should not try to update the doc", ->
@MongoManager.updateDoc.called.should.equal false

View file

@ -42,7 +42,6 @@ describe "HttpController", ->
_id: @doc_id
lines: @doc.lines
rev: @doc.rev
version: @doc.version
})
.should.equal true
@ -54,12 +53,10 @@ describe "HttpController", ->
@docs = [{
_id: ObjectId()
lines: ["mock", "lines", "one"]
version: 1
rev: 2
}, {
_id: ObjectId()
lines: ["mock", "lines", "two"]
version: 3
rev: 4
}]
@DocManager.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
@ -76,12 +73,10 @@ describe "HttpController", ->
_id: @docs[0]._id.toString()
lines: @docs[0].lines
rev: @docs[0].rev
version: @docs[0].version
}, {
_id: @docs[1]._id.toString()
lines: @docs[1].lines
rev: @docs[1].rev
version: @docs[1].version
}])
.should.equal true
@ -92,14 +87,12 @@ describe "HttpController", ->
@docs = [{
_id: ObjectId()
lines: ["mock", "lines", "one"]
version: 1
rev: 2
},
null,
{
_id: ObjectId()
lines: ["mock", "lines", "two"]
version: 3
rev: 4
}]
@DocManager.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
@ -111,12 +104,10 @@ describe "HttpController", ->
_id: @docs[0]._id.toString()
lines: @docs[0].lines
rev: @docs[0].rev
version: @docs[0].version
}, {
_id: @docs[2]._id.toString()
lines: @docs[2].lines
rev: @docs[2].rev
version: @docs[2].version
}])
.should.equal true
@ -139,13 +130,12 @@ describe "HttpController", ->
beforeEach ->
@req.body =
lines: @lines = ["hello", "world"]
version: @version = 42
@DocManager.updateDoc = sinon.stub().callsArgWith(4, null, true, @rev = 5)
@DocManager.updateDoc = sinon.stub().callsArgWith(3, null, true, @rev = 5)
@HttpController.updateDoc @req, @res, @next
it "should update the document", ->
@DocManager.updateDoc
.calledWith(@project_id, @doc_id, @lines, @version)
.calledWith(@project_id, @doc_id, @lines)
.should.equal true
it "should return a modified status", ->
@ -157,7 +147,7 @@ describe "HttpController", ->
beforeEach ->
@req.body =
lines: @lines = ["hello", "world"]
@DocManager.updateDoc = sinon.stub().callsArgWith(4, null, false, @rev = 5)
@DocManager.updateDoc = sinon.stub().callsArgWith(3, null, false, @rev = 5)
@HttpController.updateDoc @req, @res, @next
it "should return a modified status", ->

View file

@ -31,11 +31,10 @@ describe "MongoManager", ->
describe "updateDoc", ->
beforeEach ->
@version = 42
@lines = ["mock-lines"]
@docPath = "rootFolder.0.folders.1.docs.0"
@db.projects.update = sinon.stub().callsArg(2)
@MongoManager.updateDoc @project_id, @docPath, @lines, @version, @callback
@MongoManager.updateDoc @project_id, @docPath, @lines, @callback
it "should update the doc lines and increment the TPDS rev", ->
@db.projects.update
@ -44,7 +43,6 @@ describe "MongoManager", ->
}, {
$set:
"rootFolder.0.folders.1.docs.0.lines": @lines
"rootFolder.0.folders.1.docs.0.version": @version
$inc:
"rootFolder.0.folders.1.docs.0.rev": 1
})