You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

list.go 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (c) 2014 Couchbase, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package collector
  15. import (
  16. "container/list"
  17. "github.com/blevesearch/bleve/search"
  18. )
  19. type collectStoreList struct {
  20. results *list.List
  21. compare collectorCompare
  22. }
  23. func newStoreList(cap int, compare collectorCompare) *collectStoreList {
  24. rv := &collectStoreList{
  25. results: list.New(),
  26. compare: compare,
  27. }
  28. return rv
  29. }
  30. func (c *collectStoreList) AddNotExceedingSize(doc *search.DocumentMatch,
  31. size int) *search.DocumentMatch {
  32. c.add(doc)
  33. if c.len() > size {
  34. return c.removeLast()
  35. }
  36. return nil
  37. }
  38. func (c *collectStoreList) add(doc *search.DocumentMatch) {
  39. for e := c.results.Front(); e != nil; e = e.Next() {
  40. curr := e.Value.(*search.DocumentMatch)
  41. if c.compare(doc, curr) >= 0 {
  42. c.results.InsertBefore(doc, e)
  43. return
  44. }
  45. }
  46. // if we got to the end, we still have to add it
  47. c.results.PushBack(doc)
  48. }
  49. func (c *collectStoreList) removeLast() *search.DocumentMatch {
  50. return c.results.Remove(c.results.Front()).(*search.DocumentMatch)
  51. }
  52. func (c *collectStoreList) Final(skip int, fixup collectorFixup) (search.DocumentMatchCollection, error) {
  53. if c.results.Len()-skip > 0 {
  54. rv := make(search.DocumentMatchCollection, c.results.Len()-skip)
  55. i := 0
  56. skipped := 0
  57. for e := c.results.Back(); e != nil; e = e.Prev() {
  58. if skipped < skip {
  59. skipped++
  60. continue
  61. }
  62. rv[i] = e.Value.(*search.DocumentMatch)
  63. err := fixup(rv[i])
  64. if err != nil {
  65. return nil, err
  66. }
  67. i++
  68. }
  69. return rv, nil
  70. }
  71. return search.DocumentMatchCollection{}, nil
  72. }
  73. func (c *collectStoreList) len() int {
  74. return c.results.Len()
  75. }