Use version numbers for sorting and querying, not dates

This commit is contained in:
James Allen 2014-03-06 10:45:51 +00:00
parent 2e1307bd82
commit aadce232a1
6 changed files with 33 additions and 30 deletions

View file

@ -4,16 +4,16 @@ DiffGenerator = require "./DiffGenerator"
logger = require "logger-sharelatex"
module.exports = DiffManager =
getLatestDocAndUpdates: (project_id, doc_id, fromDate, toDate, callback = (error, lines, version, updates) ->) ->
UpdatesManager.getUpdates doc_id, from: fromDate, to: toDate, (error, updates) ->
getLatestDocAndUpdates: (project_id, doc_id, fromVersion, toVersion, callback = (error, lines, version, updates) ->) ->
UpdatesManager.getUpdates doc_id, from: fromVersion, to: toVersion, (error, updates) ->
return callback(error) if error?
DocumentUpdaterManager.getDocument project_id, doc_id, (error, lines, version) ->
return callback(error) if error?
callback(null, lines, version, updates)
getDiff: (project_id, doc_id, fromDate, toDate, callback = (error, diff) ->) ->
logger.log project_id: project_id, doc_id: doc_id, from: fromDate, to: toDate, "getting diff"
DiffManager.getLatestDocAndUpdates project_id, doc_id, fromDate, null, (error, lines, version, updates) ->
getDiff: (project_id, doc_id, fromVersion, toVersion, callback = (error, diff) ->) ->
logger.log project_id: project_id, doc_id: doc_id, from: fromVersion, to: toVersion, "getting diff"
DiffManager.getLatestDocAndUpdates project_id, doc_id, fromVersion, null, (error, lines, version, updates) ->
return callback(error) if error?
logger.log lines: lines, version: version, updates: updates, "got doc and updates"
@ -24,7 +24,7 @@ module.exports = DiffManager =
updatesToApply = []
for update in updates.reverse()
if update.meta.start_ts <= toDate
if update.v <= toVersion
updatesToApply.push update
logger.log project_id: project_id, doc_id: doc_id, updatesToApply: updatesToApply, "got updates to apply"

View file

@ -43,13 +43,15 @@ module.exports = MongoManager =
query =
doc_id: ObjectId(doc_id.toString())
if options.from?
query["meta.end_ts"] = { $gte: options.from }
query["v"] ||= {}
query["v"]["$gte"] = options.from
if options.to?
query["meta.start_ts"] = { $lte: options.to }
query["v"] ||= {}
query["v"]["$lte"] = options.to
cursor = db.docHistory
.find( query )
.sort( "meta.end_ts": -1 )
.sort( v: -1 )
if options.limit?
cursor.limit(options.limit)
@ -57,5 +59,5 @@ module.exports = MongoManager =
cursor.toArray callback
ensureIndices: (callback = (error) ->) ->
db.docHistory.ensureIndex { doc_id: 1, "meta.start_ts": 1, "meta.end_ts": 1 }, callback
db.docHistory.ensureIndex { doc_id: 1, v: 1 }, callback

View file

@ -30,11 +30,11 @@ describe "Getting a diff", ->
}, {
op: [{ i: "two ", p: 4 }]
meta: { ts: @from + twoMinutes, user_id: @user_id }
v: 4
v: @fromVersion = 4
}, {
op: [{ i: "three ", p: 8 }]
meta: { ts: @to - twoMinutes, user_id: @user_id }
v: 5
v: @toVersion = 5
}, {
op: [{ i: "four", p: 14 }]
meta: { ts: @to + twoMinutes, user_id: @user_id }
@ -53,7 +53,7 @@ describe "Getting a diff", ->
TrackChangesClient.pushRawUpdates @doc_id, @updates, (error) =>
throw error if error?
TrackChangesClient.getDiff @project_id, @doc_id, @from, @to, (error, diff) =>
TrackChangesClient.getDiff @project_id, @doc_id, @fromVersion, @toVersion, (error, diff) =>
throw error if error?
@diff = diff.diff
done()

View file

@ -30,7 +30,7 @@ describe "Getting updates", ->
}, {
op: [{ i: "three ", p: 8 }]
meta: { ts: @to, user_id: @user_id }
v: 5
v: @toVersion = 5
}, {
op: [{ i: "four", p: 14 }]
meta: { ts: @to + 2 * @minutes, user_id: @user_id }
@ -39,7 +39,7 @@ describe "Getting updates", ->
TrackChangesClient.pushRawUpdates @doc_id, @updates, (error) =>
throw error if error?
TrackChangesClient.getUpdates @project_id, @doc_id, { to: @to, limit: 2 }, (error, body) =>
TrackChangesClient.getUpdates @project_id, @doc_id, { to: @toVersion, limit: 2 }, (error, body) =>
throw error if error?
@updates = body.updates
done()

View file

@ -53,6 +53,8 @@ describe "DiffManager", ->
{ op: "mock-2", v: 40, meta: { start_ts: new Date(@to.getTime() - 10)} }
{ op: "mock-1", v: 39, meta: { start_ts: new Date(@to.getTime() - 20)} }
]
@fromVersion = 39
@toVersion = 40
@diffed_updates = @updates.slice(2)
@rewound_content = "rewound-content"
@diff = [ u: "mock-diff" ]
@ -62,11 +64,11 @@ describe "DiffManager", ->
@DiffManager.getLatestDocAndUpdates = sinon.stub().callsArgWith(4, null, @lines, @version, @updates)
@DiffGenerator.rewindUpdates = sinon.stub().returns(@rewound_content)
@DiffGenerator.buildDiff = sinon.stub().returns(@diff)
@DiffManager.getDiff @project_id, @doc_id, @from, @to, @callback
@DiffManager.getDiff @project_id, @doc_id, @fromVersion, @toVersion, @callback
it "should get the latest doc and version with all recent updates", ->
@DiffManager.getLatestDocAndUpdates
.calledWith(@project_id, @doc_id, @from, null)
.calledWith(@project_id, @doc_id, @fromVersion, null)
.should.equal true
it "should rewind the diff", ->
@ -87,7 +89,7 @@ describe "DiffManager", ->
@version = 50
@updates = [ { op: "mock-1", v: 40 }, { op: "mock-1", v: 39 } ]
@DiffManager.getLatestDocAndUpdates = sinon.stub().callsArgWith(4, null, @lines, @version, @updates)
@DiffManager.getDiff @project_id, @doc_id, @from, @to, @callback
@DiffManager.getDiff @project_id, @doc_id, @fromVersion, @toVersion, @callback
it "should call the callback with an error", ->
@callback
@ -98,7 +100,7 @@ describe "DiffManager", ->
beforeEach ->
@DiffManager.getLatestDocAndUpdates = sinon.stub().callsArgWith(4, null, @lines, @version, @updates)
@DiffGenerator.rewindUpdates = sinon.stub().throws(@error = new Error("inconsistent!"))
@DiffManager.getDiff @project_id, @doc_id, @from, @to, @callback
@DiffManager.getDiff @project_id, @doc_id, @fromVersion, @toVersion, @callback
it "should call the callback with an error", ->
@callback

View file

@ -141,25 +141,24 @@ describe "MongoManager", ->
@db.docHistory.limit = sinon.stub().returns @db.docHistory
@db.docHistory.toArray = sinon.stub().callsArgWith(0, null, @updates)
@from = new Date(Date.now())
@to = new Date(Date.now() + 100000)
@from = 42
@to = 55
describe "with a toDate", ->
describe "with a to version", ->
beforeEach ->
@MongoManager.getUpdates @doc_id, from: @from, to: @to, @callback
it "should find the all updates between the to and from date", ->
it "should find the all updates between the to and from versions", ->
@db.docHistory.find
.calledWith({
doc_id: ObjectId(@doc_id)
"meta.end_ts": { $gte: @from }
"meta.start_ts": { $lte: @to }
v: { $gte: @from, $lte: @to }
})
.should.equal true
it "should sort in descending timestamp order", ->
it "should sort in descending version order", ->
@db.docHistory.sort
.calledWith("meta.end_ts": -1)
.calledWith("v": -1)
.should.equal true
it "should not limit the results", ->
@ -169,15 +168,15 @@ describe "MongoManager", ->
it "should call the call back with the updates", ->
@callback.calledWith(null, @updates).should.equal true
describe "without a todo date", ->
describe "without a to version", ->
beforeEach ->
@MongoManager.getUpdates @doc_id, from: @from, @callback
it "should find the all updates after the from date", ->
it "should find the all updates after the from version", ->
@db.docHistory.find
.calledWith({
doc_id: ObjectId(@doc_id)
"meta.end_ts": { $gte: @from }
v: { $gte: @from }
})
.should.equal true