mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
fix mongo logic for project search with packs
This commit is contained in:
parent
7fdce8fc48
commit
1d7f0919a4
1 changed files with 64 additions and 33 deletions
|
@ -168,8 +168,10 @@ module.exports = MongoManager =
|
||||||
|
|
||||||
sort = {}
|
sort = {}
|
||||||
sort['meta.end_ts'] = -1;
|
sort['meta.end_ts'] = -1;
|
||||||
|
|
||||||
|
projection = {"op":false, "pack.op": false}
|
||||||
cursor = db.docHistory
|
cursor = db.docHistory
|
||||||
.find( query, {"op":false, "pack.op": false} ) # no need to return the op only need version info
|
.find( query, projection ) # no need to return the op only need version info
|
||||||
.sort( sort )
|
.sort( sort )
|
||||||
# if we have packs, we will trim the results more later after expanding them
|
# if we have packs, we will trim the results more later after expanding them
|
||||||
if limit?
|
if limit?
|
||||||
|
@ -179,7 +181,6 @@ module.exports = MongoManager =
|
||||||
before = query['meta.end_ts']?['$lt'] # may be null
|
before = query['meta.end_ts']?['$lt'] # may be null
|
||||||
|
|
||||||
updates = [] # used to accumulate the set of results
|
updates = [] # used to accumulate the set of results
|
||||||
extraQuery = _.clone(query)
|
|
||||||
|
|
||||||
cursor.toArray (err, result) ->
|
cursor.toArray (err, result) ->
|
||||||
if err?
|
if err?
|
||||||
|
@ -192,42 +193,61 @@ module.exports = MongoManager =
|
||||||
unpackedSet = unpackedSet.slice(0, limit)
|
unpackedSet = unpackedSet.slice(0, limit)
|
||||||
# find the end time of the last result, we will take all the
|
# find the end time of the last result, we will take all the
|
||||||
# results up to this, and then all the changes at that time
|
# results up to this, and then all the changes at that time
|
||||||
# (without imposing a limit)
|
# (without imposing a limit) and any overlapping packs
|
||||||
cutoff = if unpackedSet.length then unpackedSet[unpackedSet.length-1].meta.end_ts else before
|
cutoff = if unpackedSet.length then unpackedSet[unpackedSet.length-1].meta.end_ts else null
|
||||||
console.log 'before is', before
|
console.log 'before is', before
|
||||||
console.log 'cutoff is', cutoff
|
console.log 'cutoff is', cutoff
|
||||||
console.log 'limit is', limit
|
console.log 'limit is', limit
|
||||||
|
|
||||||
filterFn = (item) ->
|
filterFn = (item) ->
|
||||||
ts = item?.meta?.end_ts
|
ts = item?.meta?.end_ts
|
||||||
|
console.log 'checking', ts, before, cutoff
|
||||||
return false if before? && ts >= before
|
return false if before? && ts >= before
|
||||||
return false if cutoff? && ts < cutoff
|
return false if cutoff? && ts < cutoff
|
||||||
return true
|
return true
|
||||||
|
|
||||||
updates = MongoManager._filterAndLimit(updates, unpackedSet, filterFn, limit)
|
updates = MongoManager._filterAndLimit(updates, unpackedSet, filterFn, limit)
|
||||||
console.log 'initial updates are', updates
|
console.log 'initial updates are', updates
|
||||||
# now find any extra entries which are exactly at the last time (cutoff)
|
|
||||||
# or in packs that overlap it
|
# get all elements on the lower bound (cutoff)
|
||||||
extraQuery['meta.end_ts'] = {"$gte": +cutoff}
|
tailQuery = _.clone(query)
|
||||||
extraQuery['meta.start_ts'] = {"$lte": +cutoff}
|
tailQuery['meta.end_ts'] = cutoff
|
||||||
# we don't specify a limit here, as there could be any number of
|
tail = db.docHistory
|
||||||
# docs at that timestamp
|
.find(tailQuery, projection)
|
||||||
#
|
|
||||||
# NB. need to catch items in original query and followup query for duplicates
|
|
||||||
console.log 'extraQuery is', extraQuery
|
|
||||||
extra = db.docHistory
|
|
||||||
.find(extraQuery, {"op": false, "pack.op": false})
|
|
||||||
.sort(sort)
|
.sort(sort)
|
||||||
extra.toArray (err, result2) ->
|
|
||||||
console.log 'got extra result', err, result
|
console.log 'tailQuery is', tailQuery
|
||||||
if err?
|
|
||||||
return callback err, updates
|
# now find any packs that overlap with the time window
|
||||||
else
|
overlapQuery = _.clone(query)
|
||||||
extraSet = MongoManager._unpackResults(result2)
|
if before? && cutoff?
|
||||||
|
overlapQuery['meta.end_ts'] = {"$gte": before}
|
||||||
|
overlapQuery['pack.0.meta.end_ts'] = {"$lte": before }
|
||||||
|
else if before? && not cutoff?
|
||||||
|
overlapQuery['meta.end_ts'] = {"$gte": before}
|
||||||
|
overlapQuery['pack.0.meta.end_ts'] = {"$lte": before }
|
||||||
|
else if not before? && cutoff?
|
||||||
|
overlapQuery['meta.end_ts'] = {"$gte": cutoff}
|
||||||
|
overlapQuery['pack.0.meta.end_ts'] = {"$gte": 0 }
|
||||||
|
else if not before? && not cutoff?
|
||||||
|
overlapQuery['meta.end_ts'] = {"$gte": 0 }
|
||||||
|
overlapQuery['pack.0.meta.end_ts'] = {"$gte": 0 }
|
||||||
|
overlap = db.docHistory
|
||||||
|
.find(overlapQuery, projection)
|
||||||
|
.sort(sort)
|
||||||
|
|
||||||
|
console.log 'overlapQuery is', overlapQuery
|
||||||
|
|
||||||
|
# we don't specify a limit here, as there could be any number of overlaps
|
||||||
|
# NB. need to catch items in original query and followup query for duplicates
|
||||||
|
|
||||||
|
applyAndUpdate = (result) ->
|
||||||
|
extraSet = MongoManager._unpackResults(result)
|
||||||
# note: final argument is null, no limit applied because we
|
# note: final argument is null, no limit applied because we
|
||||||
# need all the updates at the final time to avoid breaking
|
# need all the updates at the final time to avoid breaking
|
||||||
# the changeset into parts
|
# the changeset into parts
|
||||||
updates = MongoManager._filterAndLimit(updates, extraSet, filterFn, null)
|
updates = MongoManager._filterAndLimit(updates, extraSet, filterFn, null)
|
||||||
|
console.log 'extra updates after filterandlimit', updates
|
||||||
# remove duplicates
|
# remove duplicates
|
||||||
seen = {}
|
seen = {}
|
||||||
updates = updates.filter (item) ->
|
updates = updates.filter (item) ->
|
||||||
|
@ -237,9 +257,20 @@ module.exports = MongoManager =
|
||||||
return false
|
return false
|
||||||
else
|
else
|
||||||
seen[key] = true
|
seen[key] = true
|
||||||
|
return true
|
||||||
console.log 'extra updates are', updates
|
console.log 'extra updates are', updates
|
||||||
callback err, updates
|
|
||||||
|
|
||||||
|
tail.toArray (err, result2) ->
|
||||||
|
if err?
|
||||||
|
return callback err, updates
|
||||||
|
else
|
||||||
|
applyAndUpdate result2
|
||||||
|
overlap.toArray (err, result3) ->
|
||||||
|
if err?
|
||||||
|
return callback err, updates
|
||||||
|
else
|
||||||
|
applyAndUpdate result3
|
||||||
|
callback err, updates
|
||||||
|
|
||||||
_unpackResults: (updates) ->
|
_unpackResults: (updates) ->
|
||||||
# iterate over the updates, if there's a pack, expand it into ops and
|
# iterate over the updates, if there's a pack, expand it into ops and
|
||||||
|
|
Loading…
Reference in a new issue