فهرست منبع

Use version numbers for sorting and querying, not dates

James Allen 12 سال پیش
والد
کامیت
aadce232a1

+ 6 - 6
services/track-changes/app/coffee/DiffManager.coffee

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

+ 6 - 4
services/track-changes/app/coffee/MongoManager.coffee

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

+ 3 - 3
services/track-changes/test/acceptance/coffee/GettingADiffTests.coffee

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

+ 2 - 2
services/track-changes/test/acceptance/coffee/GettingUpdatesTests.coffee

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

+ 6 - 4
services/track-changes/test/unit/coffee/DiffManager/DiffManagerTests.coffee

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

+ 10 - 11
services/track-changes/test/unit/coffee/MongoManager/MongoManagerTests.coffee

@@ -141,25 +141,24 @@ describe "MongoManager", ->
 			@db.docHistory.limit = sinon.stub().returns @db.docHistory
 			@db.docHistory.limit = sinon.stub().returns @db.docHistory
 			@db.docHistory.toArray = sinon.stub().callsArgWith(0, null, @updates)
 			@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 ->
 			beforeEach ->
 				@MongoManager.getUpdates @doc_id, from: @from, to: @to, @callback
 				@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
 				@db.docHistory.find
 					.calledWith({
 					.calledWith({
 						doc_id: ObjectId(@doc_id)
 						doc_id: ObjectId(@doc_id)
-						"meta.end_ts": { $gte: @from }
-						"meta.start_ts": { $lte: @to }
+						v: { $gte: @from, $lte: @to }
 					})
 					})
 					.should.equal true
 					.should.equal true
 
 
-			it "should sort in descending timestamp order", ->
+			it "should sort in descending version order", ->
 				@db.docHistory.sort
 				@db.docHistory.sort
-					.calledWith("meta.end_ts": -1)
+					.calledWith("v": -1)
 					.should.equal true
 					.should.equal true
 
 
 			it "should not limit the results", ->
 			it "should not limit the results", ->
@@ -169,15 +168,15 @@ describe "MongoManager", ->
 			it "should call the call back with the updates", ->
 			it "should call the call back with the updates", ->
 				@callback.calledWith(null, @updates).should.equal true
 				@callback.calledWith(null, @updates).should.equal true
 
 
-		describe "without a todo date", ->
+		describe "without a to version", ->
 			beforeEach ->
 			beforeEach ->
 				@MongoManager.getUpdates @doc_id, from: @from, @callback
 				@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
 				@db.docHistory.find
 					.calledWith({
 					.calledWith({
 						doc_id: ObjectId(@doc_id)
 						doc_id: ObjectId(@doc_id)
-						"meta.end_ts": { $gte: @from }
+						v: { $gte: @from }
 					})
 					})
 					.should.equal true
 					.should.equal true