Merge "pkg/search: enable using Around with unsorted blob sources"

This commit is contained in:
Mathieu Lonjaret 2017-07-31 14:56:40 +00:00 committed by Gerrit Code Review
commit bd3514ace1
1 changed files with 22 additions and 1 deletions

View File

@ -910,6 +910,9 @@ func (h *Handler) Query(rawq *SearchQuery) (*SearchResult, error) {
Blob: meta.Ref, Blob: meta.Ref,
}) })
if q.Limit <= 0 || !cands.sorted { if q.Limit <= 0 || !cands.sorted {
if wantAround && !foundAround && q.Around == meta.Ref {
foundAround = true
}
continue continue
} }
if !wantAround || foundAround { if !wantAround || foundAround {
@ -995,9 +998,27 @@ func (h *Handler) Query(rawq *SearchQuery) (*SearchResult, error) {
return nil, errors.New("TODO: unsupported sort+query combination.") return nil, errors.New("TODO: unsupported sort+query combination.")
} }
if q.Limit > 0 && len(res.Blobs) > q.Limit { if q.Limit > 0 && len(res.Blobs) > q.Limit {
if foundAround {
panic("foundAround should never have been set to true with a non sorted source")
}
if wantAround {
aroundPos := sort.Search(len(res.Blobs), func(i int) bool {
return res.Blobs[i].Blob == q.Around
})
lowerBound := aroundPos - q.Limit/2
if lowerBound < 0 {
lowerBound = 0
}
upperBound := aroundPos + q.Limit/2
if upperBound > len(res.Blobs) {
upperBound = len(res.Blobs)
}
res.Blobs = res.Blobs[lowerBound:upperBound]
} else {
res.Blobs = res.Blobs[:q.Limit] res.Blobs = res.Blobs[:q.Limit]
} }
} }
}
if corpus != nil { if corpus != nil {
if !wantAround { if !wantAround {
q.setResultContinue(corpus, res) q.setResultContinue(corpus, res)