1//  Copyright (c) 2017 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 segment
16
17import (
18	"fmt"
19
20	"github.com/RoaringBitmap/roaring"
21	"github.com/blevesearch/bleve/index"
22	"github.com/couchbase/vellum"
23)
24
25var ErrClosed = fmt.Errorf("index closed")
26
27// DocumentFieldValueVisitor defines a callback to be visited for each
28// stored field value.  The return value determines if the visitor
29// should keep going.  Returning true continues visiting, false stops.
30type DocumentFieldValueVisitor func(field string, typ byte, value []byte, pos []uint64) bool
31
32type Segment interface {
33	Dictionary(field string) (TermDictionary, error)
34
35	VisitDocument(num uint64, visitor DocumentFieldValueVisitor) error
36
37	DocID(num uint64) ([]byte, error)
38
39	Count() uint64
40
41	DocNumbers([]string) (*roaring.Bitmap, error)
42
43	Fields() []string
44
45	Close() error
46
47	Size() int
48
49	AddRef()
50	DecRef() error
51}
52
53type TermDictionary interface {
54	PostingsList(term []byte, except *roaring.Bitmap, prealloc PostingsList) (PostingsList, error)
55
56	Iterator() DictionaryIterator
57	PrefixIterator(prefix string) DictionaryIterator
58	RangeIterator(start, end string) DictionaryIterator
59	AutomatonIterator(a vellum.Automaton,
60		startKeyInclusive, endKeyExclusive []byte) DictionaryIterator
61	OnlyIterator(onlyTerms [][]byte, includeCount bool) DictionaryIterator
62
63	Contains(key []byte) (bool, error)
64}
65
66type DictionaryIterator interface {
67	Next() (*index.DictEntry, error)
68}
69
70type PostingsList interface {
71	Iterator(includeFreq, includeNorm, includeLocations bool, prealloc PostingsIterator) PostingsIterator
72
73	Size() int
74
75	Count() uint64
76
77	// NOTE deferred for future work
78
79	// And(other PostingsList) PostingsList
80	// Or(other PostingsList) PostingsList
81}
82
83type PostingsIterator interface {
84	// The caller is responsible for copying whatever it needs from
85	// the returned Posting instance before calling Next(), as some
86	// implementations may return a shared instance to reduce memory
87	// allocations.
88	Next() (Posting, error)
89
90	// Advance will return the posting with the specified doc number
91	// or if there is no such posting, the next posting.
92	// Callers MUST NOT attempt to pass a docNum that is less than or
93	// equal to the currently visited posting doc Num.
94	Advance(docNum uint64) (Posting, error)
95
96	Size() int
97}
98
99type Posting interface {
100	Number() uint64
101
102	Frequency() uint64
103	Norm() float64
104
105	Locations() []Location
106
107	Size() int
108}
109
110type Location interface {
111	Field() string
112	Start() uint64
113	End() uint64
114	Pos() uint64
115	ArrayPositions() []uint64
116	Size() int
117}
118
119// DocumentFieldTermVisitable is implemented by various scorch segment
120// implementations with persistence for the un inverting of the
121// postings or other indexed values.
122type DocumentFieldTermVisitable interface {
123	VisitDocumentFieldTerms(localDocNum uint64, fields []string,
124		visitor index.DocumentFieldTermVisitor, optional DocVisitState) (DocVisitState, error)
125
126	// VisitableDocValueFields implementation should return
127	// the list of fields which are document value persisted and
128	// therefore visitable by the above VisitDocumentFieldTerms method.
129	VisitableDocValueFields() ([]string, error)
130}
131
132type DocVisitState interface {
133}
134
135type StatsReporter interface {
136	ReportBytesWritten(bytesWritten uint64)
137}
138