Create processUncompressedUpdates method

This commit is contained in:
James Allen 2014-02-25 16:48:42 +00:00
parent 34d3847fe4
commit f33a3bde3e
2 changed files with 105 additions and 70 deletions

View file

@ -1,4 +1,5 @@
MongoManager = require "./MongoManager"
RedisManager = require "./RedisManager"
UpdateCompressor = require "./UpdateCompressor"
logger = require "logger-sharelatex"
@ -26,9 +27,15 @@ module.exports = HistoryManager =
logger.log doc_id: doc_id, rawUpdatesLength: length, compressedUpdatesLength: compressedUpdates.length, "compressed doc updates"
callback()
REDIS_READ_BATCH_SIZE: 100
processUncompressedUpdates: (doc_id, callback = (error) ->) ->
# Get lock - here or elsewhere?
# Get batch from Redis left hand side (oldest)
# pass batch to compressAndSaveRawUpdates
# Delete batch from redis
# release lock
RedisManager.getOldestRawUpdates doc_id, HistoryManager.REDIS_READ_BATCH_SIZE, (error, rawUpdates) ->
return callback(error) if error?
HistoryManager.compressAndSaveRawUpdates doc_id, rawUpdates, (error) ->
return callback(error) if error?
RedisManager.deleteOldestRawUpdates doc_id, HistoryManager.REDIS_READ_BATCH_SIZE, (error) ->
return callback(error) if error?
callback()
processUncompressUpdatesWithLock: (doc_id, callback = (error) ->) ->

View file

@ -10,10 +10,12 @@ describe "HistoryManager", ->
@HistoryManager = SandboxedModule.require modulePath, requires:
"./UpdateCompressor": @UpdateCompressor = {}
"./MongoManager" : @MongoManager = {}
"./RedisManager" : @RedisManager = {}
"logger-sharelatex": { log: sinon.stub() }
@doc_id = "doc-id-123"
@callback = sinon.stub()
describe "compressAndSaveRawUpdates", ->
describe "when there are no raw ops", ->
beforeEach ->
@MongoManager.popLastCompressedUpdate = sinon.stub()
@ -108,4 +110,30 @@ describe "HistoryManager", ->
.calledWith(new Error("Tried to apply raw op at version 13 to last compressed update with version 11"))
.should.equal true
describe "processUncompressedUpdates", ->
beforeEach ->
@updates = ["mock-update"]
@RedisManager.getOldestRawUpdates = sinon.stub().callsArgWith(2, null, @updates)
@HistoryManager.compressAndSaveRawUpdates = sinon.stub().callsArgWith(2)
@RedisManager.deleteOldestRawUpdates = sinon.stub().callsArg(2)
@HistoryManager.processUncompressedUpdates @doc_id, @callback
it "should get the oldest updates", ->
@RedisManager.getOldestRawUpdates
.calledWith(@doc_id, @HistoryManager.REDIS_READ_BATCH_SIZE)
.should.equal true
it "should compress and save the updates", ->
@HistoryManager.compressAndSaveRawUpdates
.calledWith(@doc_id, @updates)
.should.equal true
it "should delete the batch of uncompressed updates that was just processed", ->
@RedisManager.deleteOldestRawUpdates
.calledWith(@doc_id, @HistoryManager.REDIS_READ_BATCH_SIZE)
.should.equal true
it "should call the callback", ->
@callback.called.should.equal true