Group toV and fromV by the doc they correspond to

This commit is contained in:
James Allen 2014-03-20 13:37:23 +00:00
parent 5595910122
commit 6aaa7ba8d5
3 changed files with 66 additions and 48 deletions

View file

@ -176,21 +176,29 @@ module.exports = UpdatesManager =
if !userExists
earliestUpdate.meta.users.push update.meta.user
if update.doc_id.toString() not in earliestUpdate.doc_ids
earliestUpdate.doc_ids.push update.doc_id.toString()
doc_id = update.doc_id.toString()
doc = earliestUpdate.docs[doc_id]
if doc?
doc.fromV = Math.min(doc.fromV, update.v)
doc.toV = Math.max(doc.toV, update.v)
else
earliestUpdate.docs[doc_id] =
fromV: update.v
toV: update.v
earliestUpdate.meta.start_ts = Math.min(earliestUpdate.meta.start_ts, update.meta.start_ts)
earliestUpdate.meta.end_ts = Math.max(earliestUpdate.meta.end_ts, update.meta.end_ts)
earliestUpdate.fromV = update.v
else
newUpdate =
meta:
users: []
start_ts: update.meta.start_ts
end_ts: update.meta.end_ts
docs: {}
newUpdate.docs[update.doc_id.toString()] =
fromV: update.v
toV: update.v
doc_ids: [update.doc_id.toString()]
if update.meta.user?
newUpdate.meta.users.push update.meta.user

View file

@ -63,30 +63,30 @@ describe "Getting updates", ->
.should.equal true
it "should return at least the min_count number of summarized updates", ->
docs1 = {}
docs1[@doc_id] = toV: 20, fromV: 19
docs2 = {}
docs2[@doc_id] = toV: 18, fromV: 17
docs3 = {}
docs3[@doc_id] = toV: 16, fromV: 15
expect(@updates.slice(0,3)).to.deep.equal [{
doc_ids: [@doc_id]
docs: docs1
meta:
start_ts: @to - 2 * @minutes
end_ts: @to
users: [@user]
toV: 20
fromV: 19
}, {
doc_ids: [@doc_id]
docs: docs2
meta:
start_ts: @to - 1 * @days - 2 * @minutes
end_ts: @to - 1 * @days
users: [@user]
toV: 18
fromV: 17
}, {
doc_ids: [@doc_id]
docs: docs3
meta:
start_ts: @to - 2 * @days - 2 * @minutes
end_ts: @to - 2 * @days
users: [@user]
toV: 16
fromV: 15
}]
@ -98,21 +98,21 @@ describe "Getting updates", ->
done()
it "should return as many updates as it can", ->
docs1 = {}
docs1[@doc_id] = toV: 4, fromV: 3
docs2 = {}
docs2[@doc_id] = toV: 2, fromV: 1
expect(@updates).to.deep.equal [{
doc_ids: [@doc_id]
docs: docs1
meta:
start_ts: @to - 8 * @days - 2 * @minutes
end_ts: @to - 8 * @days
users: [@user]
toV: 4
fromV: 3
}, {
doc_ids: [@doc_id]
docs: docs2
meta:
start_ts: @to - 9 * @days - 2 * @minutes
end_ts: @to - 9 * @days
users: [@user]
toV: 2
fromV: 1
}]

View file

@ -480,97 +480,107 @@ describe "UpdatesManager", ->
@now = Date.now()
@user_1 = { id: "mock-user-1" }
@user_2 = { id: "mock-user-2" }
@doc_id_1 = "mock-doc-id-1"
@doc_id_2 = "mock-doc-id-2"
it "should concat updates that are close in time", ->
expect(@UpdatesManager._summarizeUpdates [{
doc_id: @doc_id_1
result = @UpdatesManager._summarizeUpdates [{
doc_id: "doc-id-1"
meta:
user: @user_1
start_ts: @now + 20
end_ts: @now + 30
v: 5
}, {
doc_id: @doc_id_1
doc_id: "doc-id-1"
meta:
user: @user_2
start_ts: @now
end_ts: @now + 10
v: 4
}]).to.deep.equal [{
doc_ids: [@doc_id_1]
}]
expect(result).to.deep.equal [{
docs:
"doc-id-1":
fromV: 4
toV: 5
meta:
users: [@user_1, @user_2]
start_ts: @now
end_ts: @now + 30
fromV: 4
toV: 5
}]
it "should leave updates that are far apart in time", ->
oneDay = 1000 * 60 * 60 * 24
expect(@UpdatesManager._summarizeUpdates [{
doc_id: @doc_id_1
result = @UpdatesManager._summarizeUpdates [{
doc_id: "doc-id-1"
meta:
user: @user_2
start_ts: @now + oneDay
end_ts: @now + oneDay + 10
v: 5
}, {
doc_id: @doc_id_1
doc_id: "doc-id-1"
meta:
user: @user_1
start_ts: @now
end_ts: @now + 10
v: 4
}]).to.deep.equal [{
doc_ids: [@doc_id_1]
}]
expect(result).to.deep.equal [{
docs:
"doc-id-1":
fromV: 5
toV: 5
meta:
users: [@user_2]
start_ts: @now + oneDay
end_ts: @now + oneDay + 10
fromV: 5
toV: 5
}, {
doc_ids: [@doc_id_1]
docs:
"doc-id-1":
fromV: 4
toV: 4
meta:
users: [@user_1]
start_ts: @now
end_ts: @now + 10
fromV: 4
toV: 4
}]
it "should concat onto existing summarized updates", ->
expect(@UpdatesManager._summarizeUpdates [{
doc_id: @doc_id_2
result = @UpdatesManager._summarizeUpdates [{
doc_id: "doc-id-2"
meta:
user: @user_1
start_ts: @now + 20
end_ts: @now + 30
v: 5
}, {
doc_id: @doc_id_2
doc_id: "doc-id-2"
meta:
user: @user_2
start_ts: @now
end_ts: @now + 10
v: 4
}], [{
doc_ids: [@doc_id_1]
docs:
"doc-id-1":
fromV: 6
toV: 8
meta:
users: [@user_1]
start_ts: @now + 40
end_ts: @now + 50
fromV: 6
}]
expect(result).to.deep.equal [{
docs:
"doc-id-1":
toV: 8
}]).to.deep.equal [{
doc_ids: [@doc_id_1, @doc_id_2]
fromV: 6
"doc-id-2":
toV: 5
fromV: 4
meta:
users: [@user_1, @user_2]
start_ts: @now
end_ts: @now + 50
fromV: 4
toV: 8
}]