-
Notifications
You must be signed in to change notification settings - Fork 28
/
stream.go
254 lines (222 loc) · 5.96 KB
/
stream.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright 2019-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package cbft
import (
"encoding/binary"
"fmt"
"sync"
"github.com/blevesearch/bleve/v2"
"github.com/blevesearch/bleve/v2/search"
"github.com/blevesearch/bleve/v2/search/highlight"
pb "github.com/couchbase/cbft/protobuf"
log "github.com/couchbase/clog"
)
var DefaultStreamBatchSize = 100
var sliceStart = []byte{'['}
var sliceEnd = []byte{']'}
var itemGlue = []byte{','}
// streamHandler interface is for implementations
// which streams the document matches over streams
type streamHandler interface {
write([]byte, []uint64, int) error
}
type streamer struct {
index string
m sync.Mutex
req *bleve.SearchRequest
stream pb.SearchService_SearchServer
sizeSet bool
skipSet bool
total int
curSkip int
curSize int
}
func newStreamHandler(index string, req *bleve.SearchRequest,
outStream pb.SearchService_SearchServer) *streamer {
rv := &streamer{
index: index,
curSize: int(req.Size),
curSkip: int(req.From),
stream: outStream,
req: req,
}
if req.Size > 0 {
rv.sizeSet = true
}
if req.From > 0 {
rv.skipSet = true
}
return rv
}
func (s *streamer) write(b []byte, offsets []uint64, hitsCount int) error {
s.m.Lock()
s.total += hitsCount
if s.curSkip > 0 && s.skipSet {
if hitsCount <= s.curSkip {
s.curSkip -= hitsCount
s.m.Unlock()
return nil
}
// advance the hit bytes by the skip factor
loc := offsets[s.curSkip-1] + 1 // extra 1 to accommodate the glue size
b = b[loc:]
b = append(sliceStart, b...) // TODO: reuse b slice instead of alloc?
offsets = offsets[s.curSkip:]
hitsCount -= s.curSkip
// accounting for sliceStart offset
loc--
// adjusting the offset due to slicing of b
for i := range offsets {
offsets[i] -= loc
}
s.curSkip = 0
}
// we have already streamed the requested page
if s.curSize == 0 && s.sizeSet {
s.m.Unlock()
return nil
}
if s.curSize > 0 && s.sizeSet {
if hitsCount > s.curSize {
b = b[:offsets[s.curSize-1]]
offsets = offsets[:s.curSize]
b = append(b, sliceEnd...)
offsets[len(offsets)-1] = offsets[len(offsets)-1] + 1
s.curSize = 0
} else {
s.curSize -= hitsCount
}
}
// TODO: perf, can hitRes be reused across stream.Send() calls?
hitRes := &pb.StreamSearchResults{
Contents: &pb.StreamSearchResults_Hits{
Hits: &pb.StreamSearchResults_Batch{
Bytes: b,
Offsets: offsets,
Total: uint64(len(offsets)),
},
},
}
if err := s.stream.Send(hitRes); err != nil {
s.m.Unlock()
return err
}
s.m.Unlock()
return nil
}
type docMatchHandler struct {
bhits []byte
offsets []uint64
ctx *search.SearchContext
s *streamer
n int
highlighter highlight.Highlighter
collNameMap map[uint32]string
}
func (dmh *docMatchHandler) documentMatchHandler(hit *search.DocumentMatch) error {
if hit != nil {
/***** remove unnecessary fields *****/
if dmh.s.req.Highlight == nil {
if !dmh.s.req.IncludeLocations {
hit.Locations = nil
}
hit.Fragments = nil
}
if len(dmh.s.req.Fields) == 0 {
hit.Fields = nil
}
if !dmh.s.req.Explain {
hit.Expl = nil
}
/***** removed unnecessary fields *****/
if dmh.s.req.IncludeLocations || dmh.s.req.Highlight != nil {
hit.Complete(nil)
}
if len(dmh.s.req.Fields) > 0 || dmh.highlighter != nil {
bleve.LoadAndHighlightFields(hit, dmh.s.req, "", dmh.ctx.IndexReader,
dmh.highlighter)
}
// If this is a multi collection index, then strip the colelction UID
// from the hit ID.
if dmh.collNameMap != nil {
idBytes := []byte(hit.ID)
cuid := binary.LittleEndian.Uint32(idBytes[:4])
if collName, ok := dmh.collNameMap[cuid]; ok {
hit.ID = string(idBytes[4:])
if hit.Fields == nil {
hit.Fields = make(map[string]interface{})
}
hit.Fields["_$c"] = collName
}
}
// TODO: perf, perhaps encode directly into output buffer
// (dmh.bits?) instead of alloc'ing memory and copying?
b, err := MarshalJSON(hit)
if err != nil {
log.Printf("streamHandler: json marshal err: %v", err)
return err
}
if dmh.n > 0 {
dmh.bhits = append(append(dmh.bhits, itemGlue...), b...)
} else {
dmh.bhits = append(dmh.bhits, b...)
}
// remember the ending offset position
dmh.offsets = append(dmh.offsets, uint64(len(dmh.bhits)))
dmh.n++
if dmh.n < DefaultStreamBatchSize {
return nil
}
}
if len(dmh.bhits) > 1 {
dmh.bhits = append(dmh.bhits, sliceEnd...)
err := dmh.s.write(dmh.bhits, dmh.offsets, dmh.n)
dmh.bhits = dmh.bhits[:1]
dmh.offsets = dmh.offsets[:0]
dmh.n = 0
return err
}
return nil
}
func (s *streamer) MakeDocumentMatchHandler(
ctx *search.SearchContext) (search.DocumentMatchHandler, bool, error) {
var highlighter highlight.Highlighter
if s.req.Highlight != nil {
var err error
// get the right highlighter
highlighter, err = bleve.Config.Cache.HighlighterNamed(
bleve.Config.DefaultHighlighter)
if err != nil {
return nil, false, err
}
if s.req.Highlight.Style != nil {
highlighter, err = bleve.Config.Cache.HighlighterNamed(
*s.req.Highlight.Style)
if err != nil {
return nil, false, err
}
}
if highlighter == nil {
return nil, false, fmt.Errorf("no highlighter named `%s` registered",
*s.req.Highlight.Style)
}
}
dmh := docMatchHandler{
s: s,
ctx: ctx,
bhits: make([]byte, 0, DefaultStreamBatchSize*50),
offsets: make([]uint64, 0, DefaultStreamBatchSize),
highlighter: highlighter,
}
dmh.bhits = append(dmh.bhits, sliceStart...)
if sdm, multiCollIndex :=
metaFieldValCache.getSourceDetailsMap(s.index); multiCollIndex {
dmh.collNameMap = sdm.collUIDNameMap
}
return dmh.documentMatchHandler, true, nil
}