本站源代码
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.

78 lines
2.0KB

  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 "github.com/blevesearch/bleve/search"
  16. type collectStoreSlice struct {
  17. slice search.DocumentMatchCollection
  18. compare collectorCompare
  19. }
  20. func newStoreSlice(capacity int, compare collectorCompare) *collectStoreSlice {
  21. rv := &collectStoreSlice{
  22. slice: make(search.DocumentMatchCollection, 0, capacity),
  23. compare: compare,
  24. }
  25. return rv
  26. }
  27. func (c *collectStoreSlice) AddNotExceedingSize(doc *search.DocumentMatch,
  28. size int) *search.DocumentMatch {
  29. c.add(doc)
  30. if c.len() > size {
  31. return c.removeLast()
  32. }
  33. return nil
  34. }
  35. func (c *collectStoreSlice) add(doc *search.DocumentMatch) {
  36. // find where to insert, starting at end (lowest)
  37. i := len(c.slice)
  38. for ; i > 0; i-- {
  39. cmp := c.compare(doc, c.slice[i-1])
  40. if cmp >= 0 {
  41. break
  42. }
  43. }
  44. // insert at i
  45. c.slice = append(c.slice, nil)
  46. copy(c.slice[i+1:], c.slice[i:])
  47. c.slice[i] = doc
  48. }
  49. func (c *collectStoreSlice) removeLast() *search.DocumentMatch {
  50. var rv *search.DocumentMatch
  51. rv, c.slice = c.slice[len(c.slice)-1], c.slice[:len(c.slice)-1]
  52. return rv
  53. }
  54. func (c *collectStoreSlice) Final(skip int, fixup collectorFixup) (search.DocumentMatchCollection, error) {
  55. for i := skip; i < len(c.slice); i++ {
  56. err := fixup(c.slice[i])
  57. if err != nil {
  58. return nil, err
  59. }
  60. }
  61. if skip <= len(c.slice) {
  62. return c.slice[skip:], nil
  63. }
  64. return search.DocumentMatchCollection{}, nil
  65. }
  66. func (c *collectStoreSlice) len() int {
  67. return len(c.slice)
  68. }
上海开阖软件有限公司 沪ICP备12045867号-1