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
15package collector
16
17import (
18	"container/list"
19
20	"github.com/blevesearch/bleve/search"
21)
22
23type collectStoreList struct {
24	results *list.List
25	compare collectorCompare
26}
27
28func newStoreList(capacity int, compare collectorCompare) *collectStoreList {
29	rv := &collectStoreList{
30		results: list.New(),
31		compare: compare,
32	}
33
34	return rv
35}
36
37func (c *collectStoreList) AddNotExceedingSize(doc *search.DocumentMatch, size int) *search.DocumentMatch {
38	c.add(doc)
39	if c.len() > size {
40		return c.removeLast()
41	}
42	return nil
43}
44
45func (c *collectStoreList) add(doc *search.DocumentMatch) {
46	for e := c.results.Front(); e != nil; e = e.Next() {
47		curr := e.Value.(*search.DocumentMatch)
48		if c.compare(doc, curr) >= 0 {
49			c.results.InsertBefore(doc, e)
50			return
51		}
52	}
53	// if we got to the end, we still have to add it
54	c.results.PushBack(doc)
55}
56
57func (c *collectStoreList) removeLast() *search.DocumentMatch {
58	return c.results.Remove(c.results.Front()).(*search.DocumentMatch)
59}
60
61func (c *collectStoreList) Final(skip int, fixup collectorFixup) (search.DocumentMatchCollection, error) {
62	if c.results.Len()-skip > 0 {
63		rv := make(search.DocumentMatchCollection, c.results.Len()-skip)
64		i := 0
65		skipped := 0
66		for e := c.results.Back(); e != nil; e = e.Prev() {
67			if skipped < skip {
68				skipped++
69				continue
70			}
71
72			rv[i] = e.Value.(*search.DocumentMatch)
73			err := fixup(rv[i])
74			if err != nil {
75				return nil, err
76			}
77			i++
78		}
79		return rv, nil
80	}
81	return search.DocumentMatchCollection{}, nil
82}
83
84func (c *collectStoreList) len() int {
85	return c.results.Len()
86}
87