-
Notifications
You must be signed in to change notification settings - Fork 0
/
selector.go
839 lines (791 loc) · 23.8 KB
/
selector.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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
/*
* Copyright (c) 2019. Aberic - All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package lily
import (
"errors"
"github.com/aberic/gnomon/log"
"github.com/aberic/lily/api"
"reflect"
"strings"
)
// Selector 检索选择器
//
// 查询顺序 scope -> match -> conditions -> skip -> sort -> limit
type Selector struct {
Conditions []*condition `json:"conditions"` // Conditions 条件查询
Skip uint32 `json:"skip"` // Skip 结果集跳过数量
Sort *sort `json:"sort"` // Sort 排序方式
Limit uint32 `json:"limit"` // Limit 结果集顺序数量
database Database // database 数据库对象
formName string // formName 表名
delete bool // 是否删除检索结果
}
// condition 条件查询
//
// 查询过程中不满足条件的记录将被移除出结果集
type condition struct {
// 参数名,由对象结构层级字段通过'.'组成,如
//
// ref := &ref{
// i: 1,
// s: "2",
// in: refIn{
// i: 3,
// s: "4",
// },
// }
//
// key可取'i','in.s'
Param string `json:"param"`
Cond string `json:"cond"` // 条件 gt/lt/eq/dif 大于/小于/等于/不等
Value interface{} `json:"value"` // 比较对象,支持int、string、float和bool
}
// sort 排序方式
type sort struct {
// 参数名,由对象结构层级字段通过'.'组成,如
//
// ref := &ref{
// i: 1,
// s: "2",
// in: refIn{
// i: 3,
// s: "4",
// },
// }
//
// key可取'i','in.s'
Param string `json:"param"`
ASC bool `json:"asc"` // 是否升序
}
func (s *Selector) exec() (int32, []interface{}, error) {
var (
index Index
leftQuery bool
nc *nodeCondition
pcs map[string]*paramCondition
count int32
is []interface{}
err error
)
if index, leftQuery, nc, pcs, err = s.getIndex(); nil != err {
return 0, nil, err
}
log.Debug("query", log.Field("index", index.getKeyStructure()))
if s.Limit == 0 {
s.Limit = 1000
}
if leftQuery {
count, is = s.leftQueryIndex(index, nc, pcs)
} else {
count, is = s.rightQueryIndex(index, nc, pcs)
}
return count, is, nil
}
// getIndex 根据检索条件获取使用索引对象
//
// index 已获取索引对象
//
// leftQuery 是否顺序查询
//
// cond 条件对象
func (s *Selector) getIndex() (index Index, leftQuery bool, nc *nodeCondition, pcs map[string]*paramCondition, err error) {
var idx Index
// 优先尝试采用条件作为索引,缩小索引范围以提高检索效率
idx, leftQuery, nc, pcs, err = s.getIndexCondition()
if idx != nil { // 如果存在条件查询,则优先条件查询
return idx, leftQuery, nc, pcs, err
}
for _, idx := range s.database.getForms()[s.formName].getIndexes() { // 如果存在排序查询,则优先排序查询
if s.Sort != nil && s.Sort.Param == idx.getKeyStructure() {
return idx, s.Sort.ASC, nc, pcs, nil
}
}
// 取值默认索引来进行查询操作
for _, idx := range s.database.getForms()[s.formName].getIndexes() {
log.Debug("getIndex", log.Field("index", index))
return idx, true, nc, pcs, nil
}
return nil, false, nc, pcs, errors.New("index not found")
}
// getIndexCondition 优先尝试采用条件作为索引,缩小索引范围以提高检索效率
//
// 优先匹配有多个相同Param参数的条件,如果相同数量一样,则按照先后顺序选择最先匹配的
func (s *Selector) getIndexCondition() (index Index, leftQuery bool, nc *nodeCondition, pcs map[string]*paramCondition, err error) {
pcs = make(map[string]*paramCondition)
ncs := make(map[string]*nodeCondition)
leftQuery = true
for _, condition := range s.Conditions { // 遍历检索条件
for _, idx := range s.database.getForms()[s.formName].getIndexes() {
if condition.Param == idx.getKeyStructure() { // 匹配条件是否存在已有索引
if nil != s.Sort && s.Sort.Param == idx.getKeyStructure() { // 如果有,则继续判断该索引是否存在排序需求
index = idx
leftQuery = s.Sort.ASC
if ncs[condition.Param] == nil {
ncs[condition.Param] = &nodeCondition{nss: []*nodeSelector{}}
}
s.getConditionNode(ncs[condition.Param], condition)
break
}
if index != nil {
continue
}
index = idx
if ncs[condition.Param] == nil {
ncs[condition.Param] = &nodeCondition{nss: []*nodeSelector{}}
}
s.getConditionNode(ncs[condition.Param], condition)
}
}
paramType, paramValue, support := s.formatParam(condition.Value)
if support {
pcs[s.pcMapName(condition)] = ¶mCondition{paramType: paramType, paramValue: paramValue}
}
}
var nodeCount = 0
for _, ncNow := range ncs {
ncNowCount := len(ncNow.nss)
if ncNowCount > nodeCount {
nodeCount = ncNowCount
nc = ncNow
}
}
return
}
// leftQueryIndex 索引顺序检索
//
// index 已获取索引对象
func (s *Selector) leftQueryIndex(index Index, ns *nodeCondition, pcs map[string]*paramCondition) (int32, []interface{}) {
var (
count, nc int32
nis []interface{}
is = make([]interface{}, 0)
skipIn = s.Skip
limitIn uint32
)
for _, node := range index.getNode().getNodes() {
if nil == ns {
skipIn, limitIn, nc, nis = s.leftQueryNode(skipIn, limitIn, node, nil, pcs)
} else {
skipIn, limitIn, nc, nis = s.leftQueryNode(skipIn, limitIn, node, ns.nextNode, pcs)
}
count += nc
is = append(is, nis...)
if limitIn >= s.Limit {
break
}
}
if s.Sort == nil {
return count, is
}
return count, s.shellSort(is)
}
// leftQueryNode 节点顺序检索
//
// skipIn 传入skip表示需要跳过的数量,返回skip表示剩余待跳过的数量
//
// limitIn 传入limit表示已经命中的数量,返回limit表示已经命中的数量
func (s *Selector) leftQueryNode(skipIn, limitIn uint32, node Nodal, ns *nodeCondition, pcs map[string]*paramCondition) (uint32, uint32, int32, []interface{}) {
var (
skip = skipIn
limit = limitIn
count int32
is = make([]interface{}, 0)
)
if nodes := node.getNodes(); nil != nodes {
for _, nd := range nodes {
var (
nc int32
nis []interface{}
)
if ns == nil {
skip, limit, nc, nis = s.leftQueryNode(skip, limit, nd, nil, pcs)
} else if s.nodeConditions(nd, ns.nextNode.nss) { // 判断当前条件是否满足,如果满足则继续下一步
skip, limit, nc, nis = s.leftQueryNode(skip, limit, nd, ns.nextNode, pcs)
}
count += nc
is = append(is, nis...)
if limit >= s.Limit {
break
}
}
} else {
if ns == nil {
return s.leftQueryLeaf(skip, limit, node.(Leaf), nil, pcs)
}
return s.leftQueryLeaf(skip, limit, node.(Leaf), ns, pcs)
}
return skip, limit, count, is
}
// leftQueryLeaf 叶子节点顺序检索
//
// skip 传入skip表示需要跳过的数量,返回skip表示剩余待跳过的数量
//
// limit 传入limit表示已经命中的数量,返回limit表示已经命中的数量
func (s *Selector) leftQueryLeaf(skip, limit uint32, leaf Leaf, ns *nodeCondition, pcs map[string]*paramCondition) (uint32, uint32, int32, []interface{}) {
var (
count int32
is = make([]interface{}, 0)
)
if (nil != ns && s.leafConditions(leaf, ns.nss)) || nil == ns { // 满足等于与不等于条件
if limit >= s.Limit {
return skip, limit, 0, is
}
for _, link := range leaf.getLinks() {
if nil == pcs || len(pcs) == 0 {
if skip > 0 {
skip--
continue
}
}
rs := link.get()
if nil == rs.err && s.conditionNoIndexLeaf(ns, pcs, rs.value) {
count++
if skip > 0 {
skip--
continue
}
limit++
if s.delete {
form := leaf.getIndex().getForm()
indexes := form.getIndexes() // 获取表索引ID集合
_, _ = s.database.insertDataWithIndexInfo(form, rs.key, indexes, rs.value, true, false)
}
is = append(is, rs.value)
}
}
}
return skip, limit, count, is
}
// rightQueryIndex 索引倒序检索
//
// index 已获取索引对象
func (s *Selector) rightQueryIndex(index Index, ns *nodeCondition, pcs map[string]*paramCondition) (int32, []interface{}) {
var (
count, nc int32
nis []interface{}
is = make([]interface{}, 0)
skipIn = s.Skip
limitIn uint32
)
lenNode := len(index.getNode().getNodes())
for i := lenNode - 1; i >= 0; i-- {
if nil == ns {
skipIn, limitIn, nc, nis = s.rightQueryNode(skipIn, limitIn, index.getNode().getNodes()[i], nil, pcs)
} else {
skipIn, limitIn, nc, nis = s.rightQueryNode(skipIn, limitIn, index.getNode().getNodes()[i], ns.nextNode, pcs)
}
count += nc
is = append(is, nis...)
if limitIn >= s.Limit {
break
}
}
return count, is
}
// rightQueryNode 节点倒序检索
//
// skip 传入skip表示需要跳过的数量,返回skip表示剩余待跳过的数量
//
// limit 传入limit表示已经命中的数量,返回limit表示已经命中的数量
func (s *Selector) rightQueryNode(skipIn, limitIn uint32, node Nodal, ns *nodeCondition, pcs map[string]*paramCondition) (uint32, uint32, int32, []interface{}) {
var (
skip = skipIn
limit = limitIn
count int32
is = make([]interface{}, 0)
)
if nodes := node.getNodes(); nil != nodes {
lenNode := len(nodes)
for i := lenNode - 1; i >= 0; i-- {
var (
nc int32
nis []interface{}
)
if ns == nil {
skip, limit, nc, nis = s.rightQueryNode(skip, limit, nodes[i], nil, pcs)
} else if s.nodeConditions(nodes[i], ns.nextNode.nss) { // 判断当前条件是否满足,如果满足则继续下一步
skip, limit, nc, nis = s.rightQueryNode(skip, limit, nodes[i], ns.nextNode, pcs)
}
count += nc
is = append(is, nis...)
}
} else {
if ns == nil {
return s.rightQueryLeaf(skip, limit, node.(Leaf), nil, pcs)
}
return s.rightQueryLeaf(skip, limit, node.(Leaf), ns, pcs)
}
return skip, limit, count, is
}
// rightQueryLeaf 叶子节点倒序检索
//
// skip 传入skip表示需要跳过的数量,返回skip表示剩余待跳过的数量
//
// limit 传入limit表示已经命中的数量,返回limit表示已经命中的数量
func (s *Selector) rightQueryLeaf(skip, limit uint32, leaf Leaf, ns *nodeCondition, pcs map[string]*paramCondition) (uint32, uint32, int32, []interface{}) {
var (
count int32
is = make([]interface{}, 0)
)
links := leaf.getLinks()
lenLink := len(links)
if (nil != ns && s.leafConditions(leaf, ns.nss)) || nil == ns { // 满足等于与不等于条件
if limit >= s.Limit {
return skip, limit, 0, is
}
for i := lenLink - 1; i >= 0; i-- {
if nil == pcs || len(pcs) == 0 {
if skip > 0 {
skip--
continue
}
}
rs := links[i].get()
if nil == rs.err && s.conditionNoIndexLeaf(ns, pcs, rs.value) {
count++
if skip > 0 {
skip--
continue
}
limit++
if s.delete {
form := leaf.getIndex().getForm()
indexes := form.getIndexes() // 获取表索引ID集合
_, _ = s.database.insertDataWithIndexInfo(form, rs.key, indexes, rs.value, true, false)
}
is = append(is, rs.value)
}
}
}
return skip, limit, count, is
}
// nodeConditions 判断当前条件集合是否满足
func (s *Selector) nodeConditions(node Nodal, nss []*nodeSelector) bool {
for _, ns := range nss {
if !s.conditionNode(node, ns) {
return false
}
}
return true
}
// leafConditions 判断当前条件集合是否满足
func (s *Selector) leafConditions(node Nodal, nss []*nodeSelector) bool {
for _, ns := range nss {
if !s.conditionLeaf(node, ns) {
return false
}
}
return true
}
// conditionNode 判断当前条件是否满足
func (s *Selector) conditionNode(node Nodal, ns *nodeSelector) bool {
if ns != nil {
for _, cond := range s.Conditions {
if cond != ns.cond {
continue
}
switch cond.Cond {
case "gt":
return s.conditionGT(node, ns)
case "lt":
return s.conditionLT(node, ns)
}
}
}
return true
}
// conditionLeaf 判断当前条件是否满足
func (s *Selector) conditionLeaf(node Nodal, ns *nodeSelector) bool {
if ns != nil {
for _, cond := range s.Conditions {
if cond != ns.cond {
continue
}
switch cond.Cond {
case "eq":
return ns.level == 5 && ns.degreeIndex == node.getDegreeIndex()
case "dif":
return ns.level == 5 && ns.degreeIndex != node.getDegreeIndex()
}
}
}
return true
}
// paramCondition 参数条件结构
type paramCondition struct {
paramType int // paramType 参数类型
paramValue interface{} // paramValue 参数对应指定类型的值
}
// pcMapName map[string]*paramCondition string
func (s *Selector) pcMapName(cond *condition) string {
return strings.Join([]string{cond.Param, cond.Cond}, "")
}
// conditionNoIndexLeaf 判断当前条件是否满足
func (s *Selector) conditionNoIndexLeaf(ns *nodeCondition, pcs map[string]*paramCondition, value interface{}) bool {
for _, cond := range s.Conditions {
if nil != ns && cond.Param == ns.nss[0].cond.Param {
continue
}
pc := pcs[s.pcMapName(cond)]
if nil == pc {
continue
}
if !s.conditionValue(cond.Cond, strings.Split(cond.Param, "."), pc.paramType, pc.paramValue, value) {
return false
}
}
return true
}
// conditionGT 条件大于判断
func (s *Selector) conditionGT(node Nodal, ns *nodeSelector) bool {
switch ns.level {
default:
return ns.degreeIndex < node.getDegreeIndex()
case 1, 2, 3, 4:
return ns.degreeIndex <= node.getDegreeIndex()
}
}
// conditionLT 条件小于判断
func (s *Selector) conditionLT(node Nodal, ns *nodeSelector) bool {
switch ns.level {
default:
return ns.degreeIndex > node.getDegreeIndex()
case 1, 2, 3, 4:
return ns.degreeIndex >= node.getDegreeIndex()
}
}
const (
paramInt64 = iota
paramUint64
paramFloat64
paramString
paramBool
)
// formatParam 梳理param的类型及值
//
// 类型:int=0;int64=1;uint64=2;string=3;bool=4
func (s *Selector) formatParam(paramValue interface{}) (paramType int, value interface{}, support bool) {
switch paramValue := paramValue.(type) {
default:
return -1, nil, false
case int:
return paramInt64, int64(paramValue), true
case int8, int16, int32, int64:
return paramInt64, paramValue.(int64), true
case uint8, uint16, uint32, uint, uint64, uintptr:
return paramUint64, paramValue.(uint64), true
case float32, float64:
return paramFloat64, paramValue.(float64), true
case string:
return paramString, paramValue, true
case bool:
return paramBool, paramValue, true
}
}
// conditionValue 判断当前条件是否满足
func (s *Selector) conditionValue(cond string, params []string, paramType int, paramValue, objValue interface{}) bool {
var value interface{}
if value = s.getValueFromParams(params, objValue); nil == value {
return false
}
switch value := value.(type) {
default:
return false
case int, int8, int16, int32, int64:
if paramType != paramInt64 {
return false
}
return s.conditionValueInt64(cond, paramValue.(int64), value.(int64))
case uint8, uint16, uint32, uint, uint64, uintptr:
if paramType != paramUint64 {
return false
}
return s.conditionValueUint64(cond, paramValue.(uint64), value.(uint64))
case float32, float64:
if paramType != paramFloat64 {
return false
}
return s.conditionValueFloat64(cond, paramValue.(float64), value.(float64))
case string:
if paramType != paramString {
return false
}
return s.conditionValueString(cond, paramValue.(string), value)
case bool:
if paramType != paramBool {
return false
}
return s.conditionValueBool(cond, paramValue.(bool), value)
}
}
// conditionValueInt64 判断当前条件是否满足
func (s *Selector) conditionValueInt64(cond string, param, value int64) bool {
switch cond {
default:
return false
case "gt":
return value > param
case "lt":
return value < param
case "eq":
return value == param
case "dif":
return value != param
}
}
// conditionValueUint64 判断当前条件是否满足
func (s *Selector) conditionValueUint64(cond string, param, value uint64) bool {
switch cond {
default:
return false
case "gt":
return value > param
case "lt":
return value < param
case "eq":
return value == param
case "dif":
return value != param
}
}
// conditionValueFloat64 判断当前条件是否满足
func (s *Selector) conditionValueFloat64(cond string, param, value float64) bool {
switch cond {
default:
return false
case "gt":
return value > param
case "lt":
return value < param
case "eq":
return value == param
case "dif":
return value != param
}
}
// conditionValueString 判断当前条件是否满足
func (s *Selector) conditionValueString(cond string, param, value string) bool {
switch cond {
default:
return false
case "gt":
return value > param
case "lt":
return value < param
case "eq":
return value == param
case "dif":
return value != param
}
}
// conditionValueBool 判断当前条件是否满足
func (s *Selector) conditionValueBool(cond string, param, value bool) bool {
switch cond {
default:
return false
case "eq":
return value == param
case "dif":
return value != param
}
}
// shellSort 希尔排序
func (s *Selector) shellSort(is []interface{}) []interface{} {
log.Debug("shellSort 希尔排序", log.Field("s.Sort", s.Sort))
if s.Sort.ASC {
log.Debug("shellAsc 希尔顺序排序")
return s.shellAsc(is)
}
log.Debug("shellDesc 希尔倒序排序")
return s.shellDesc(is)
}
// shellAsc 希尔顺序排序
func (s *Selector) shellAsc(is []interface{}) []interface{} {
length := len(is)
gap := length / 2
params := strings.Split(s.Sort.Param, ".")
for gap > 0 {
for i := gap; i < length; i++ {
tempI := is[i]
temp := s.hashKeyFromValue(params, is[i])
preIndex := i - gap
for preIndex >= 0 && s.hashKeyFromValue(params, is[preIndex]) > temp {
is[preIndex+gap] = is[preIndex]
preIndex -= gap
}
is[preIndex+gap] = tempI
}
gap /= 2
}
return is
}
// shellDesc 希尔倒序排序
func (s *Selector) shellDesc(is []interface{}) []interface{} {
length := len(is)
gap := length / 2
params := strings.Split(s.Sort.Param, ".")
for gap > 0 {
for i := gap; i < length; i++ {
tempI := is[i]
temp := s.hashKeyFromValue(params, is[i])
preIndex := i - gap
for preIndex >= 0 && s.hashKeyFromValue(params, is[preIndex]) < temp {
is[preIndex+gap] = is[preIndex]
preIndex -= gap
}
is[preIndex+gap] = tempI
}
gap /= 2
}
return is
}
// hashKeyFromValue 通过Param获取该参数所属hashKey
func (s *Selector) hashKeyFromValue(params []string, value interface{}) uint64 {
hashKey, support := s.getInterValue(params, value)
if !support {
return 0
}
return hashKey
}
// getInterValue 根据索引描述和当前检索到的value对象获取当前value对象所在索引的hashKey
func (s *Selector) getInterValue(params []string, value interface{}) (hashKey uint64, support bool) {
reflectObj := reflect.ValueOf(value) // 反射对象,通过reflectObj获取存储在里面的值,还可以去改变值
if reflectObj.Kind() == reflect.Map {
interMap := value.(map[string]interface{})
lenParams := len(params)
var valueResult interface{}
for i, param := range params {
if i == lenParams-1 {
valueResult = interMap[param]
break
}
interMap = interMap[param].(map[string]interface{})
}
//log.Debug("getInterValue", log.Field("valueResult", valueResult))
checkValue := reflect.ValueOf(valueResult)
return value2hashKey(&checkValue)
}
log.Debug("getInterValue", log.Field("kind", reflectObj.Kind()), log.Field("support", false))
return 0, false
}
// getValueFromParams 根据索引描述获取当前value
func (s *Selector) getValueFromParams(params []string, value interface{}) interface{} {
reflectObj := reflect.ValueOf(value) // 反射对象,通过reflectObj获取存储在里面的值,还可以去改变值
if reflectObj.Kind() == reflect.Map {
interMap := value.(map[string]interface{})
lenParams := len(params)
var valueResult interface{}
for i, param := range params {
if i == lenParams-1 {
valueResult = interMap[param]
break
}
interMap = interMap[param].(map[string]interface{})
}
return valueResult
}
log.Debug("getValueFromParams", log.Field("kind", reflectObj.Kind()), log.Field("support", false))
return nil
}
// getConditionNode 根据条件匹配节点单元
//
// 该方法可以用更优雅或正确的方式实现,但烧脑,性能无影响,就这样吧
func (s *Selector) getConditionNode(nc *nodeCondition, cond *condition) {
var (
hashKey, flexibleKey, nextFlexibleKey, distance uint64
nextDegree uint16
ok bool
)
if _, hashKey, ok = type2index(cond.Value); !ok {
return
}
nodeLevel1 := &nodeSelector{level: 1, degreeIndex: 0, cond: cond}
nc.nss = append(nc.nss, nodeLevel1)
flexibleKey = hashKey
distance = levelDistance(nodeLevel1.level)
nextDegree = uint16(flexibleKey / distance)
nextFlexibleKey = flexibleKey - uint64(nextDegree)*distance
nodeLevel2 := &nodeSelector{level: 2, degreeIndex: nextDegree, cond: cond}
nodeLevel1.nextNode = nodeLevel2
if nil == nc.nextNode {
nc.nextNode = &nodeCondition{nss: []*nodeSelector{}}
}
nc.nextNode.nss = append(nc.nextNode.nss, nodeLevel2)
flexibleKey = nextFlexibleKey
distance = levelDistance(nodeLevel2.level)
nextDegree = uint16(flexibleKey / distance)
nextFlexibleKey = flexibleKey - uint64(nextDegree)*distance
nodeLevel3 := &nodeSelector{level: 3, degreeIndex: nextDegree, cond: cond}
nodeLevel2.nextNode = nodeLevel3
if nil == nc.nextNode.nextNode {
nc.nextNode.nextNode = &nodeCondition{nss: []*nodeSelector{}}
}
nc.nextNode.nextNode.nss = append(nc.nextNode.nextNode.nss, nodeLevel3)
flexibleKey = nextFlexibleKey
distance = levelDistance(nodeLevel3.level)
nextDegree = uint16(flexibleKey / distance)
nextFlexibleKey = flexibleKey - uint64(nextDegree)*distance
nodeLevel4 := &nodeSelector{level: 4, degreeIndex: nextDegree, cond: cond}
nodeLevel3.nextNode = nodeLevel4
if nil == nc.nextNode.nextNode.nextNode {
nc.nextNode.nextNode.nextNode = &nodeCondition{nss: []*nodeSelector{}}
}
nc.nextNode.nextNode.nextNode.nss = append(nc.nextNode.nextNode.nextNode.nss, nodeLevel4)
flexibleKey = nextFlexibleKey
distance = levelDistance(nodeLevel4.level)
nextDegree = uint16(flexibleKey / distance)
nodeLevel5 := &nodeSelector{level: 5, degreeIndex: nextDegree, cond: cond}
nodeLevel4.nextNode = nodeLevel5
nodeLevel3.nextNode = nodeLevel4
if nil == nc.nextNode.nextNode.nextNode.nextNode {
nc.nextNode.nextNode.nextNode.nextNode = &nodeCondition{nss: []*nodeSelector{}}
}
nc.nextNode.nextNode.nextNode.nextNode.nss = append(nc.nextNode.nextNode.nextNode.nextNode.nss, nodeLevel5)
}
// nodeCondition 多个相同Param条件检索预匹配的节点单元
type nodeCondition struct {
nextNode *nodeCondition
nss []*nodeSelector
}
// nodeSelector 条件检索预匹配的节点单元
type nodeSelector struct {
level uint8 // 当前节点所在树层级
degreeIndex uint16 // 当前节点所在集合中的索引下标,该坐标不一定在数组中的正确位置,但一定是逻辑正确的
nextNode *nodeSelector
cond *condition
}
func (s *Selector) formatAPI(apiSelector *api.Selector) error {
if nil == apiSelector {
return errors.New("selector is nil")
}
s.Skip = apiSelector.Skip
s.Limit = apiSelector.Limit
if nil != apiSelector.Sort {
s.Sort = &sort{
Param: apiSelector.Sort.Param,
ASC: apiSelector.Sort.ASC,
}
}
if len(apiSelector.Conditions) > 0 {
s.Conditions = []*condition{}
for _, cond := range apiSelector.Conditions {
s.Conditions = append(s.Conditions, &condition{
Param: cond.Param,
Cond: cond.Cond,
Value: cond.Value,
})
}
}
return nil
}