/
ingester.go
344 lines (288 loc) · 8.97 KB
/
ingester.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package sonic
import (
"fmt"
"strconv"
"strings"
"unicode/utf8"
)
// IngestBulkRecord is the struct to be used as a list in bulk operation.
type IngestBulkRecord struct {
Object, Text string
}
// IngestBulkError represent an error for a given object in a bulk operation.
type IngestBulkError struct {
Object string
Error error
}
// Ingestable is used for altering the search index (push, pop and flush).
type Ingestable interface {
// Push search data in the index.
// Command syntax PUSH <collection> <bucket> <object> "<text>" [LANG(<locale>)]?
Push(collection, bucket, object, text string, lang Lang) (err error)
// BulkPush will execute N (parallelRoutines) goroutines at the same time to
// dispatch the records at best.
// If parallelRoutines <= 0; parallelRoutines will be equal to 1.
// If parallelRoutines > len(records); parallelRoutines will be equal to len(records).
BulkPush(collection, bucket string, parallelRoutines int, records []IngestBulkRecord, lang Lang) []IngestBulkError
// Pop search data from the index.
// Command syntax POP <collection> <bucket> <object> "<text>".
Pop(collection, bucket, object, text string) (err error)
// BulkPop will execute N (parallelRoutines) goroutines at the same time to
// dispatch the records at best.
// If parallelRoutines <= 0; parallelRoutines will be equal to 1.
// If parallelRoutines > len(records); parallelRoutines will be equal to len(records).
BulkPop(collection, bucket string, parallelRoutines int, records []IngestBulkRecord) []IngestBulkError
// Count indexed search data.
// bucket and object are optionals, empty string ignore it.
// Command syntax COUNT <collection> [<bucket> [<object>]?]?.
Count(collection, bucket, object string) (count int, err error)
// FlushCollection Flush all indexed data from a collection.
// Command syntax FLUSHC <collection>.
FlushCollection(collection string) (err error)
// Flush all indexed data from a bucket in a collection.
// Command syntax FLUSHB <collection> <bucket>.
FlushBucket(collection, bucket string) (err error)
// Flush all indexed data from an object in a bucket in collection.
// Command syntax FLUSHO <collection> <bucket> <object>.
FlushObject(collection, bucket, object string) (err error)
// Quit refer to the Base interface
Quit() (err error)
// Ping refer to the Base interface
Ping() (err error)
}
type ingesterCommands string
const (
push ingesterCommands = "PUSH"
pop ingesterCommands = "POP"
count ingesterCommands = "COUNT"
flushb ingesterCommands = "FLUSHB"
flushc ingesterCommands = "FLUSHC"
flusho ingesterCommands = "FLUSHO"
)
type ingesterChannel struct {
*driver
}
// NewIngester create a new driver instance with a ingesterChannel instance.
// Only way to get a Ingestable implementation.
func NewIngester(host string, port int, password string) (Ingestable, error) {
driver := &driver{
Host: host,
Port: port,
Password: password,
channel: Ingest,
}
err := driver.Connect()
if err != nil {
return nil, err
}
return ingesterChannel{
driver: driver,
}, nil
}
func (i ingesterChannel) Push(collection, bucket, object, text string, lang Lang) (err error) {
//
patterns := []struct {
Pattern string
Replacement string
}{{"\\", "\\\\"},
{"\n", "\\n"},
{"\"", "\\\""}}
for _, v := range patterns {
text = strings.Replace(text, v.Pattern, v.Replacement, -1)
}
chunks := splitText(text, i.cmdMaxBytes/2)
// split chunks with partial success will yield single error
for _, chunk := range chunks {
ff := fmt.Sprintf("%s %s %s %s \"%s\""+langFormat(lang), push, collection, bucket, object, chunk, lang)
err = i.write(ff)
if err != nil {
return err
}
// sonic should sent OK
_, err = i.read()
if err != nil {
return err
}
}
return nil
}
func langFormat(lang Lang) string {
if lang != "" {
return " LANG(%s)"
}
return "%s"
}
// Ensure splitting on a valid leading byte
// Slicing the string directly is more efficient than converting to []byte and back because
// since a string is immutable and a []byte isn't,
// the data must be copied to new memory upon conversion,
// taking O(n) time (both ways!),
// whereas slicing a string simply returns a new string header backed by the same array as the original
// (taking constant time).
func splitText(longString string, maxLen int) []string {
var splits []string
var l, r int
for l, r = 0, maxLen; r < len(longString); l, r = r, r+maxLen {
for !utf8.RuneStart(longString[r]) {
r--
}
splits = append(splits, longString[l:r])
}
splits = append(splits, longString[l:])
return splits
}
func (i ingesterChannel) BulkPush(collection, bucket string, parallelRoutines int, records []IngestBulkRecord, lang Lang) (errs []IngestBulkError) {
if parallelRoutines <= 0 {
parallelRoutines = 1
}
// chunk array into N (parallelRoutines) parts
divided := divideIngestBulkRecords(records, parallelRoutines)
bulkErrorChan := make(chan []IngestBulkError)
defer close(bulkErrorChan)
for _, r := range divided {
r := r
go func(driver *driver, collection, bucket string, recs []IngestBulkRecord, bulkErrorChan chan<- []IngestBulkError) {
errs := make([]IngestBulkError, 0)
newIngester, _ := NewIngester(driver.Host, driver.Port, driver.Password)
for _, rec := range recs {
if newIngester == nil {
addBulkError(&errs, rec, ErrClosed)
continue
}
err := newIngester.Push(collection, bucket, rec.Object, rec.Text, lang)
if err != nil {
addBulkError(&errs, rec, err)
}
}
if newIngester != nil {
_ = newIngester.Quit()
}
bulkErrorChan <- errs
}(i.driver, collection, bucket, r, bulkErrorChan)
}
errs = make([]IngestBulkError, 0)
for range divided {
errs = append(errs, <-bulkErrorChan...)
}
return errs
}
func (i ingesterChannel) Pop(collection, bucket, object, text string) (err error) {
err = i.write(fmt.Sprintf("%s %s %s %s \"%s\"", pop, collection, bucket, object, text))
if err != nil {
return err
}
// sonic should sent OK
_, err = i.read()
if err != nil {
return err
}
return nil
}
func (i ingesterChannel) BulkPop(collection, bucket string, parallelRoutines int, records []IngestBulkRecord) (errs []IngestBulkError) {
if parallelRoutines <= 0 {
parallelRoutines = 1
}
// chunk array into N (parallelRoutines) parts
divided := divideIngestBulkRecords(records, parallelRoutines)
bulkErrorChan := make(chan []IngestBulkError)
defer close(bulkErrorChan)
for _, r := range divided {
r := r
go func(driver *driver, collection, bucket string, recs []IngestBulkRecord, bulkErrorChan chan<- []IngestBulkError) {
errs := make([]IngestBulkError, 0)
newIngester, _ := NewIngester(driver.Host, driver.Port, driver.Password)
for _, rec := range recs {
if newIngester == nil {
addBulkError(&errs, rec, ErrClosed)
continue
}
err := newIngester.Pop(collection, bucket, rec.Object, rec.Text)
if err != nil {
addBulkError(&errs, rec, err)
}
}
if newIngester != nil {
_ = newIngester.Quit()
}
bulkErrorChan <- errs
}(i.driver, collection, bucket, r, bulkErrorChan)
}
errs = make([]IngestBulkError, 0)
for range divided {
errs = append(errs, <-bulkErrorChan...)
}
return errs
}
func (i ingesterChannel) Count(collection, bucket, object string) (cnt int, err error) {
err = i.write(fmt.Sprintf("%s %s %s", count, collection, buildCountQuery(bucket, object)))
if err != nil {
return 0, err
}
// RESULT NUMBER
r, err := i.read()
if err != nil {
return 0, err
}
return strconv.Atoi(r[7:])
}
func buildCountQuery(bucket, object string) string {
builder := strings.Builder{}
if bucket != "" {
builder.WriteString(bucket)
if object != "" {
builder.WriteString(" " + object)
}
}
return builder.String()
}
func (i ingesterChannel) FlushCollection(collection string) (err error) {
err = i.write(fmt.Sprintf("%s %s", flushc, collection))
if err != nil {
return err
}
// sonic should sent OK
_, err = i.read()
if err != nil {
return err
}
return nil
}
func (i ingesterChannel) FlushBucket(collection, bucket string) (err error) {
err = i.write(fmt.Sprintf("%s %s %s", flushb, collection, bucket))
if err != nil {
return err
}
// sonic should sent OK
_, err = i.read()
if err != nil {
return err
}
return nil
}
func (i ingesterChannel) FlushObject(collection, bucket, object string) (err error) {
err = i.write(fmt.Sprintf("%s %s %s %s", flusho, collection, bucket, object))
if err != nil {
return err
}
// sonic should sent OK
_, err = i.read()
if err != nil {
return err
}
return nil
}
func divideIngestBulkRecords(records []IngestBulkRecord, parallelRoutines int) [][]IngestBulkRecord {
var divided [][]IngestBulkRecord
chunkSize := (len(records) + parallelRoutines - 1) / parallelRoutines
for i := 0; i < len(records); i += chunkSize {
end := i + chunkSize
if end > len(records) {
end = len(records)
}
divided = append(divided, records[i:end])
}
return divided
}
func addBulkError(e *[]IngestBulkError, record IngestBulkRecord, err error) {
*e = append(*e, IngestBulkError{record.Object, err})
}