-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathunmarshal.go
703 lines (651 loc) · 17.1 KB
/
unmarshal.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
/*
Copyright 2017-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/json"
"fmt"
"io"
"unsafe"
"github.com/couchbase/cbgt"
"github.com/blevesearch/bleve/v2"
"github.com/blevesearch/bleve/v2/geo"
"github.com/blevesearch/bleve/v2/search"
"github.com/blevesearch/bleve/v2/search/query"
jsoniter "github.com/json-iterator/go"
)
// JSONImpl holds the custom json parser impl
var JSONImpl *CustomJSONImpl
// CustomJSONImpl enables the custom json parser
// is an implementation of JSONParserImplType
type CustomJSONImpl struct {
CustomJSONImplType string
//mgr instance for untime flipping through manager options
mgr *cbgt.Manager
}
// SetManager sets the manager instance
func (p *CustomJSONImpl) SetManager(mgr *cbgt.Manager) {
p.mgr = mgr
}
// GetManagerOption gets the requested manager option
func (p *CustomJSONImpl) GetManagerOption(key string) string {
return p.mgr.GetOption(key)
}
// GetParserType returns the custom parser type
func (p *CustomJSONImpl) GetParserType() string {
return p.CustomJSONImplType
}
// Unmarshal abstracts the underlying json lib used
func (p *CustomJSONImpl) Unmarshal(b []byte, v interface{}) error {
return jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(b, v)
}
// Decode abstracts the underlying json lib used
func (p *CustomJSONImpl) Decode(r io.Reader, v interface{}) error {
return jsoniter.ConfigCompatibleWithStandardLibrary.NewDecoder(r).Decode(v)
}
// UnmarshalJSON abstracts the underlying json lib used
func UnmarshalJSON(b []byte, v interface{}) error {
if JSONImpl != nil && JSONImpl.GetManagerOption("jsonImpl") != "std" {
return JSONImpl.Unmarshal(b, v)
}
return json.Unmarshal(b, v)
}
func init() {
// registers the custom json decoders with jsoniter
registerCustomJSONDecoders()
}
func registerCustomJSONDecoders() {
// adding all the custom decoders that bleve has implemented,
// and need to extend as bleve introduces new custom decoders.
jsoniter.RegisterTypeDecoderFunc("query.BleveQueryTime", decodeBleveQueryTime)
jsoniter.RegisterTypeDecoderFunc("query.ConjunctionQuery", decodeConjunctionQuery)
jsoniter.RegisterTypeDecoderFunc("query.BooleanQuery", decodeBooleanQuery)
jsoniter.RegisterTypeDecoderFunc("query.GeoBoundingBoxQuery", decodeGeoBoundingBoxQuery)
jsoniter.RegisterTypeDecoderFunc("query.GeoDistanceQuery", decodeGeoDistanceQuery)
jsoniter.RegisterTypeDecoderFunc("query.DisjunctionQuery", decodeDisjunctionQuery)
jsoniter.RegisterTypeDecoderFunc("bleve.IndexErrMap", decodeBleveIndexErrMap)
jsoniter.RegisterTypeDecoderFunc("bleve.SearchRequest", decodeBleveSearchRequest)
jsoniter.RegisterTypeDecoderFunc("bleve.FacetRequest", decodeFacetRequest)
jsoniter.RegisterTypeDecoderFunc("query.MatchQueryOperator", decodeMatchQueryOperator)
}
func decodeBleveQueryTime(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
timeString := iter.ReadAny().ToString()
dateTimeParser, err := cache.DateTimeParserNamed(query.QueryDateTimeParser)
if err != nil {
iter.Error = err
return
}
t := query.BleveQueryTime{}
t.Time, _, err = dateTimeParser.ParseDateTime(timeString)
if err != nil {
iter.Error = err
return
}
*((*query.BleveQueryTime)(ptr)) = t
}
func decodeConjunctionQuery(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
tmp := struct {
Conjuncts []json.RawMessage `json:"conjuncts"`
Boost *query.Boost `json:"boost,omitempty"`
}{}
iter.ReadVal(&tmp)
if iter.Error != nil {
return
}
q := query.ConjunctionQuery{}
q.Conjuncts = make([]query.Query, len(tmp.Conjuncts))
for i, term := range tmp.Conjuncts {
query1, err := parseQuery(term)
if err != nil {
iter.Error = err
return
}
q.Conjuncts[i] = query1
}
q.BoostVal = tmp.Boost
*((*query.ConjunctionQuery)(ptr)) = q
}
func decodeBooleanQuery(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
tmp := struct {
Must json.RawMessage `json:"must,omitempty"`
Should json.RawMessage `json:"should,omitempty"`
MustNot json.RawMessage `json:"must_not,omitempty"`
Boost *query.Boost `json:"boost,omitempty"`
}{}
iter.ReadVal(&tmp)
if iter.Error != nil {
return
}
var err error
q := query.BooleanQuery{}
if tmp.Must != nil {
q.Must, err = parseQuery(tmp.Must)
if err != nil {
iter.Error = err
return
}
_, isConjunctionQuery := q.Must.(*query.ConjunctionQuery)
if !isConjunctionQuery {
iter.Error = fmt.Errorf("must clause must be conjunction")
return
}
}
if tmp.Should != nil {
q.Should, err = parseQuery(tmp.Should)
if err != nil {
iter.Error = err
return
}
_, isDisjunctionQuery := q.Should.(*query.DisjunctionQuery)
if !isDisjunctionQuery {
iter.Error = fmt.Errorf("should clause must be disjunction")
return
}
}
if tmp.MustNot != nil {
q.MustNot, err = parseQuery(tmp.MustNot)
if err != nil {
iter.Error = err
return
}
_, isDisjunctionQuery := q.MustNot.(*query.DisjunctionQuery)
if !isDisjunctionQuery {
iter.Error = fmt.Errorf("must not clause must be disjunction")
return
}
}
q.BoostVal = tmp.Boost
*((*query.BooleanQuery)(ptr)) = q
}
func decodeGeoBoundingBoxQuery(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
tmp := struct {
TopLeft interface{} `json:"top_left,omitempty"`
BottomRight interface{} `json:"bottom_right,omitempty"`
FieldVal string `json:"field,omitempty"`
BoostVal *query.Boost `json:"boost,omitempty"`
}{}
iter.ReadVal(&tmp)
if iter.Error != nil {
return
}
q := query.GeoBoundingBoxQuery{}
// now use our generic point parsing code from the geo package
lon, lat, found := geo.ExtractGeoPoint(tmp.TopLeft)
if !found {
iter.Error = fmt.Errorf("geo location top_left not in a valid format")
return
}
q.TopLeft = []float64{lon, lat}
lon, lat, found = geo.ExtractGeoPoint(tmp.BottomRight)
if !found {
iter.Error = fmt.Errorf("geo location bottom_right not in a valid format")
return
}
q.BottomRight = []float64{lon, lat}
q.FieldVal = tmp.FieldVal
q.BoostVal = tmp.BoostVal
*((*query.GeoBoundingBoxQuery)(ptr)) = q
}
func decodeGeoDistanceQuery(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
tmp := struct {
Location interface{} `json:"location,omitempty"`
Distance string `json:"distance,omitempty"`
FieldVal string `json:"field,omitempty"`
BoostVal *query.Boost `json:"boost,omitempty"`
}{}
iter.ReadVal(&tmp)
if iter.Error != nil {
return
}
// now use our generic point parsing code from the geo package
lon, lat, found := geo.ExtractGeoPoint(tmp.Location)
if !found {
iter.Error = fmt.Errorf("geo location not in a valid format")
return
}
q := query.GeoDistanceQuery{}
q.Location = []float64{lon, lat}
q.Distance = tmp.Distance
q.FieldVal = tmp.FieldVal
q.BoostVal = tmp.BoostVal
*((*query.GeoDistanceQuery)(ptr)) = q
}
func decodeDisjunctionQuery(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
tmp := struct {
Disjuncts []json.RawMessage `json:"disjuncts"`
Boost *query.Boost `json:"boost,omitempty"`
Min float64 `json:"min"`
}{}
iter.ReadVal(&tmp)
if iter.Error != nil {
return
}
q := query.DisjunctionQuery{}
q.Disjuncts = make([]query.Query, len(tmp.Disjuncts))
for i, term := range tmp.Disjuncts {
query1, err := parseQuery(term)
if err != nil {
iter.Error = err
return
}
q.Disjuncts[i] = query1
}
q.BoostVal = tmp.Boost
q.Min = tmp.Min
*((*query.DisjunctionQuery)(ptr)) = q
}
func decodeBleveIndexErrMap(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
var tmp map[string]string
iter.ReadVal(&tmp)
if iter.Error != nil {
return
}
iem := make(bleve.IndexErrMap, 0)
for k, v := range tmp {
iem[k] = fmt.Errorf("%s", v)
}
*((*bleve.IndexErrMap)(ptr)) = iem
}
func decodeBleveSearchRequest(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
var temp struct {
Q json.RawMessage `json:"query"`
Size *int `json:"size"`
From int `json:"from"`
Highlight *bleve.HighlightRequest `json:"highlight"`
Fields []string `json:"fields"`
Facets bleve.FacetsRequest `json:"facets"`
Explain bool `json:"explain"`
Sort []json.RawMessage `json:"sort"`
IncludeLocations bool `json:"includeLocations"`
Score string `json:"score"`
KNN json.RawMessage `json:"knn"`
KNNOperator json.RawMessage `json:"knn_operator"`
PreSearchData json.RawMessage `json:"pre_search_data"`
}
r := &bleve.SearchRequest{}
iter.ReadVal(&temp)
if iter.Error != nil {
return
}
var err error
if temp.Size == nil {
r.Size = 10
} else {
r.Size = *temp.Size
}
if temp.Sort == nil {
r.Sort = search.SortOrder{&search.SortScore{Desc: true}}
} else {
r.Sort, err = search.ParseSortOrderJSON(temp.Sort)
if err != nil {
iter.Error = err
return
}
}
r.From = temp.From
r.Explain = temp.Explain
r.Highlight = temp.Highlight
r.Fields = temp.Fields
if temp.Facets != nil {
r.Facets = make(map[string]*bleve.FacetRequest, len(temp.Facets))
for k, fr := range temp.Facets {
bfr := (*bleve.FacetRequest)(unsafe.Pointer(fr))
r.AddFacet(k, bfr)
}
}
r.IncludeLocations = temp.IncludeLocations
r.Score = temp.Score
r.Query, err = parseQuery(temp.Q)
if err != nil {
iter.Error = err
return
}
if r.Size < 0 {
r.Size = 10
}
if r.From < 0 {
r.From = 0
}
if temp.PreSearchData != nil {
r.PreSearchData, err = parsePreSearchData(temp.PreSearchData)
if err != nil {
iter.Error = err
return
}
}
if r, err = interpretKNNForRequest(temp.KNN, temp.KNNOperator, r); err != nil {
return
}
*((*bleve.SearchRequest)(ptr)) = *r
}
func decodeFacetRequest(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
var temp facetRequest
iter.ReadVal(&temp)
if iter.Error != nil {
return
}
fr := bleve.NewFacetRequest(temp.Field, temp.Size)
for _, nr := range temp.NumericRanges {
fr.AddNumericRange(nr.Name, nr.Min, nr.Max)
}
for _, dr := range temp.DateTimeRanges {
fr.AddDateTimeRangeStringWithParser(dr.Name, dr.Start, dr.End, dr.DateTimeParser)
}
*((*bleve.FacetRequest)(ptr)) = *fr
}
func decodeMatchQueryOperator(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
var operatorString string
iter.ReadVal(&operatorString)
if iter.Error != nil {
return
}
var o query.MatchQueryOperator
switch operatorString {
case "or":
o = query.MatchQueryOperatorOr
case "and":
o = query.MatchQueryOperatorAnd
default:
iter.Error = fmt.Errorf("cannot unmarshal match operator '%v' from JSON", o)
return
}
*((*query.MatchQueryOperator)(ptr)) = o
}
// parseQuery deserializes a JSON representation of
// a Query object.
func parseQuery(input []byte) (query.Query, error) {
if len(input) == 0 {
// interpret as a match_none query
return query.NewMatchNoneQuery(), nil
}
var tmp map[string]interface{}
err := jsoniter.Unmarshal(input, &tmp)
if err != nil {
return nil, err
}
_, hasFuzziness := tmp["fuzziness"]
_, isMatchQuery := tmp["match"]
_, isMatchPhraseQuery := tmp["match_phrase"]
_, hasTerms := tmp["terms"]
if hasFuzziness && !isMatchQuery && !isMatchPhraseQuery && !hasTerms {
var rv query.FuzzyQuery
err := jsoniter.Unmarshal(input, &rv)
if err != nil {
return nil, err
}
return &rv, nil
}
if isMatchQuery {
var rv query.MatchQuery
err := jsoniter.Unmarshal(input, &rv)
if err != nil {
return nil, err
}
return &rv, nil
}
if isMatchPhraseQuery {
var rv query.MatchPhraseQuery
err := jsoniter.Unmarshal(input, &rv)
if err != nil {
return nil, err
}
return &rv, nil
}
if hasTerms {
var rv query.PhraseQuery
err := jsoniter.Unmarshal(input, &rv)
if err != nil {
// now try multi-phrase
var rv2 query.MultiPhraseQuery
err = jsoniter.Unmarshal(input, &rv2)
if err != nil {
return nil, err
}
return &rv2, nil
}
return &rv, nil
}
_, isTermQuery := tmp["term"]
if isTermQuery {
var rv query.TermQuery
err := jsoniter.Unmarshal(input, &rv)
if err != nil {
return nil, err
}
return &rv, nil
}
_, hasMust := tmp["must"]
_, hasShould := tmp["should"]
_, hasMustNot := tmp["must_not"]
if hasMust || hasShould || hasMustNot {
var rv query.BooleanQuery
err := jsoniter.Unmarshal(input, &rv)
if err != nil {
return nil, err
}
return &rv, nil
}
_, hasConjuncts := tmp["conjuncts"]
if hasConjuncts {
var rv query.ConjunctionQuery
err := jsoniter.Unmarshal(input, &rv)
if err != nil {
return nil, err
}
return &rv, nil
}
_, hasDisjuncts := tmp["disjuncts"]
if hasDisjuncts {
var rv query.DisjunctionQuery
err := jsoniter.Unmarshal(input, &rv)
if err != nil {
return nil, err
}
return &rv, nil
}
_, hasSyntaxQuery := tmp["query"]
if hasSyntaxQuery {
var rv query.QueryStringQuery
err := jsoniter.Unmarshal(input, &rv)
if err != nil {
return nil, err
}
return &rv, nil
}
_, hasMin := tmp["min"].(float64)
_, hasMax := tmp["max"].(float64)
if hasMin || hasMax {
var rv query.NumericRangeQuery
err := jsoniter.Unmarshal(input, &rv)
if err != nil {
return nil, err
}
return &rv, nil
}
_, hasMinStr := tmp["min"].(string)
_, hasMaxStr := tmp["max"].(string)
if hasMinStr || hasMaxStr {
var rv query.TermRangeQuery
err := jsoniter.Unmarshal(input, &rv)
if err != nil {
return nil, err
}
return &rv, nil
}
_, hasStart := tmp["start"]
_, hasEnd := tmp["end"]
if hasStart || hasEnd {
var rv query.DateRangeStringQuery
err := jsoniter.Unmarshal(input, &rv)
if err != nil {
return nil, err
}
return &rv, nil
}
_, hasPrefix := tmp["prefix"]
if hasPrefix {
var rv query.PrefixQuery
err := jsoniter.Unmarshal(input, &rv)
if err != nil {
return nil, err
}
return &rv, nil
}
_, hasRegexp := tmp["regexp"]
if hasRegexp {
var rv query.RegexpQuery
err := jsoniter.Unmarshal(input, &rv)
if err != nil {
return nil, err
}
return &rv, nil
}
_, hasWildcard := tmp["wildcard"]
if hasWildcard {
var rv query.WildcardQuery
err := jsoniter.Unmarshal(input, &rv)
if err != nil {
return nil, err
}
return &rv, nil
}
_, hasMatchAll := tmp["match_all"]
if hasMatchAll {
var rv query.MatchAllQuery
err := jsoniter.Unmarshal(input, &rv)
if err != nil {
return nil, err
}
return &rv, nil
}
_, hasMatchNone := tmp["match_none"]
if hasMatchNone {
var rv query.MatchNoneQuery
err := jsoniter.Unmarshal(input, &rv)
if err != nil {
return nil, err
}
return &rv, nil
}
_, hasDocIds := tmp["ids"]
if hasDocIds {
var rv query.DocIDQuery
err := jsoniter.Unmarshal(input, &rv)
if err != nil {
return nil, err
}
return &rv, nil
}
_, hasBool := tmp["bool"]
if hasBool {
var rv query.BoolFieldQuery
err := jsoniter.Unmarshal(input, &rv)
if err != nil {
return nil, err
}
return &rv, nil
}
_, hasTopLeft := tmp["top_left"]
_, hasBottomRight := tmp["bottom_right"]
if hasTopLeft && hasBottomRight {
var rv query.GeoBoundingBoxQuery
err := jsoniter.Unmarshal(input, &rv)
if err != nil {
return nil, err
}
return &rv, nil
}
_, hasDistance := tmp["distance"]
if hasDistance {
var rv query.GeoDistanceQuery
err := jsoniter.Unmarshal(input, &rv)
if err != nil {
return nil, err
}
return &rv, nil
}
_, hasPoints := tmp["polygon_points"]
if hasPoints {
var rv query.GeoBoundingPolygonQuery
err := jsoniter.Unmarshal(input, &rv)
if err != nil {
return nil, err
}
return &rv, nil
}
_, hasGeo := tmp["geometry"]
if hasGeo {
var rv query.GeoShapeQuery
err := jsoniter.Unmarshal(input, &rv)
if err != nil {
return nil, err
}
return &rv, nil
}
_, hasCidr := tmp["cidr"]
if hasCidr {
var rv query.IPRangeQuery
err := jsoniter.Unmarshal(input, &rv)
if err != nil {
return nil, err
}
return &rv, nil
}
return nil, fmt.Errorf("unknown query type")
}
// parsePreSearchData deserializes a JSON representation of
// a preSearchData object.
func parsePreSearchData(input []byte) (map[string]interface{}, error) {
var rv map[string]interface{}
var tmp map[string]json.RawMessage
err := jsoniter.Unmarshal(input, &tmp)
if err != nil {
return nil, err
}
for k, v := range tmp {
switch k {
case search.KnnPreSearchDataKey:
var value []*search.DocumentMatch
if v != nil {
err := jsoniter.Unmarshal(v, &value)
if err != nil {
return nil, err
}
}
if rv == nil {
rv = make(map[string]interface{})
}
rv[search.KnnPreSearchDataKey] = value
case search.SynonymPreSearchDataKey:
var value search.FieldTermSynonymMap
if v != nil {
err := jsoniter.Unmarshal(v, &value)
if err != nil {
return nil, err
}
}
if rv == nil {
rv = make(map[string]interface{})
}
rv[search.SynonymPreSearchDataKey] = value
case search.BM25PreSearchDataKey:
var value *search.BM25Stats
if v != nil {
err := jsoniter.Unmarshal(v, &value)
if err != nil {
return nil, err
}
}
if rv == nil {
rv = make(map[string]interface{})
}
rv[search.BM25PreSearchDataKey] = value
}
}
return rv, nil
}