-
Notifications
You must be signed in to change notification settings - Fork 1
/
CliffMemSQL.go
1658 lines (1591 loc) · 48.5 KB
/
CliffMemSQL.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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package CliffMemSQL
import (
"errors"
"reflect"
"strings"
"sort"
"strconv"
"sync"
)
//连表查询导致数据库资源被占用,其他服务可能变慢,需要将查询语句根据索引拆分,把数据计算放到本地,
//需求:一个查表数据内存映射
const printMaxLen = 100
type ST_MemTable struct {
memTable []st_MemTable_Row
colNameType map[string]string
colNameRemark map[string]string
colNameOrder string
m_RWLock *sync.RWMutex //支持多线程读写操作
}
type st_MemTable_Row map[string]interface{}
//对表格数据提取相应类型
func (this st_MemTable_Row) GetInt(inParam string) (int) {
if this != nil {
switch this[inParam].(type) {
case int:
return this[inParam].(int)
case float64:
return int(this[inParam].(float64))
default:
return 0
}
} else {
return 0
}
}
func (this st_MemTable_Row) GetInt64(inParam string) (int64) {
if this != nil {
switch this[inParam].(type) {
case int64:
return this[inParam].(int64)
case float64:
return int64(this[inParam].(float64))
default:
return 0
}
} else {
return 0
}
}
func (this st_MemTable_Row) GetString(inParam string) (string) {
if this != nil {
switch this[inParam].(type) {
case string:
return this[inParam].(string)
default:
return ""
}
} else {
return ""
}
}
func (this st_MemTable_Row) GetValToString(inParam string) (string) {
if this != nil {
switch this[inParam].(type) {
case string:
return this[inParam].(string)
case int:
return strconv.Itoa(this[inParam].(int))
case int64:
return strconv.FormatInt(this[inParam].(int64), 10)
case float64:
return strconv.FormatFloat(this[inParam].(float64), 'f', 0, 0)
default:
return ""
}
} else {
return ""
}
}
func (this st_MemTable_Row) GetVal(inParam string) (interface{}) {
if this != nil {
return this[inParam]
} else {
return nil
}
}
func (this st_MemTable_Row) GetStringToInt(inParam string) (int) {
if this != nil {
out, err := strconv.ParseInt(this.GetString(inParam), 10, 0)
if err != nil {
out = 0
}
return int(out)
} else {
return 0
}
}
func (this st_MemTable_Row) GetStringToInt64(inParam string) (int64) {
if this != nil {
out, err := strconv.ParseInt(this.GetString(inParam), 10, 64)
if err != nil {
out = 0
}
return out
} else {
return 0
}
}
func (this st_MemTable_Row) GetStringToFloat64(inParam string) (float64) {
if this != nil {
out, err := strconv.ParseFloat(this.GetString(inParam), 64)
if err != nil {
out = 0
}
return out
} else {
return 0
}
}
func (this st_MemTable_Row) GetStringToFloat32(inParam string) (float32) {
if this != nil {
out, err := strconv.ParseFloat(this.GetString(inParam), 32)
if err != nil {
out = 0
}
return float32(out)
} else {
return 0
}
}
func (this *st_MemTable_Row) SetVal(inKey string, inVal interface{}) {
if this != nil {
(*this)[inKey] = inVal
}
}
func (this *ST_MemTable) getColType(colName string) string {
for key, val := range this.colNameType {
if key == colName {
return val
}
}
return ""
}
func (this *ST_MemTable) CheckColNameExist(colName string) bool {
if str := this.getColType(colName); str == "" {
return false
} else {
return true
}
}
func NewMemTable(colNameType map[string]string) *ST_MemTable {
pMemTable := new(ST_MemTable)
pMemTable.memTable = make([]st_MemTable_Row, 0)
pMemTable.colNameType = make(map[string]string)
for k, v := range colNameType {
pMemTable.colNameType[k] = v
}
pMemTable.colNameType["m_ValidStatus"] = "int" //用于判断该行是否有效,内部维护
pMemTable.colNameRemark = make(map[string]string)
pMemTable.m_RWLock = &sync.RWMutex{}
return pMemTable
}
func (this *ST_MemTable) GetColType(colName string) (string, error) {
if this == nil {
return "", errors.New("pT is null")
}
return this.getColType(colName), nil
}
func (this *ST_MemTable) AddRemark(colName string, remark string) {
if this == nil {
return
}
if _, ok := this.colNameType[colName]; ok {
tmpRemark := strings.Join(strings.Split(remark, "\n"), " ")
tmpRemark2 := strings.Join(strings.Split(tmpRemark, "\t"), "")
this.colNameRemark[colName] = tmpRemark2
}
return
}
func (this *ST_MemTable) GetRemark(colName string) string {
if this == nil {
return ""
}
return this.colNameRemark[colName]
}
//获取表格有效行数
func (this *ST_MemTable) GetRowCount() (int) {
if this == nil {
return 0
}
cnt := 0
this.m_RWLock.RLock()
defer this.m_RWLock.RUnlock()
for _, val := range this.memTable {
if val.GetVal("m_ValidStatus") == 1 {
cnt ++
}
}
return cnt
}
//获取表格总共行数
//加锁
func (this *ST_MemTable) GetRowCount_Total() (int) {
if this == nil {
return 0
}
return len(this.memTable)
}
func (this *ST_MemTable) GetColCount() (int) {
if this == nil {
return 0
}
return len(this.colNameType)
}
func (this *ST_MemTable) GetColNames() ([]string) {
if this == nil {
return nil
}
retStr := make([]string, 0)
for key, _ := range this.colNameType {
retStr = append(retStr, key)
}
return retStr
}
func (this *ST_MemTable) InsertRow(mapRow map[string]interface{}) (bool, error) {
if this == nil {
return false, errors.New("pT is null")
}
this.m_RWLock.RLock()
for key, val := range mapRow {
//列名判断
if this.CheckColNameExist(key) == false {
return false, errors.New("colType not match:colName=" + key)
}
//类型判断
if val != nil {
if this.getColType(key) != reflect.TypeOf(val).String() {
return false, errors.New("Type:colName=" + key + " colType=" + this.getColType(key) + " NOT=" + reflect.TypeOf(val).String())
}
}
}
//更新
mapRowTmp := st_MemTable_Row{}
for key, val := range mapRow {
if this.colNameType[key] != "" {
mapRowTmp.SetVal(key, val)
}
}
mapRowTmp.SetVal("m_ValidStatus", 1)
this.m_RWLock.RUnlock()
this.m_RWLock.Lock()
defer this.m_RWLock.Unlock()
this.memTable = append(this.memTable, mapRowTmp)
return true, nil
}
//inCnt:-1 获取全部行数据
//inStart 为有效行目录的下标
func (this *ST_MemTable) GetRows_IndexOK(inStart int, inCnt int) (tf bool, effectRows int, outmap []st_MemTable_Row, err error) {
validStart := 0
this.m_RWLock.RLock()
defer this.m_RWLock.RUnlock()
if this == nil {
return false, 0, nil, errors.New("pT is null")
}
if inStart < 0 || (inStart >= this.GetRowCount() && this.GetRowCount() > 0) {
return false, 0, nil, errors.New("inStart out of range")
}
if inCnt < 0 && inCnt != -1 {
return false, 0, nil, errors.New("inCnt < 0 && inCnt != -1")
}
//找到有效行的开始
validCnt := 0
for i, val := range this.memTable {
if val.GetInt("m_ValidStatus") == 1 {
validCnt ++
if validCnt >= inStart+1 {
validStart = i
break
}
}
}
if inCnt == -1 {
retEffectCnt := 0
retList := make([]st_MemTable_Row, 0)
for i, val := range this.memTable {
if i >= validStart && val.GetInt("m_ValidStatus") == 1 {
retList = append(retList, val)
retEffectCnt++
}
}
return true, retEffectCnt, retList, nil
}
//获取
retEffectCnt := 0
retList := make([]st_MemTable_Row, 0)
for i, val := range this.memTable {
if i >= validStart && val.GetInt("m_ValidStatus") == 1 {
if retEffectCnt < inCnt {
retList = append(retList, val)
retEffectCnt ++
}
}
}
return true, retEffectCnt, retList, nil
}
//inStart 为总表的下标
func (this *ST_MemTable) GetRows(inStart int, inCnt int) (tf bool, effectRows int, outmap []st_MemTable_Row, err error) {
if this == nil {
return false, 0, nil, errors.New("pT is null")
}
if inStart < 0 || (inStart >= this.GetRowCount_Total() && this.GetRowCount_Total() > 0) {
return false, 0, nil, errors.New("inStart out of range")
}
if inCnt < 0 && inCnt != -1 {
return false, 0, nil, errors.New("inCnt < 0 && inCnt != -1")
}
this.m_RWLock.RLock()
defer this.m_RWLock.RUnlock()
if inCnt == -1 {
retEffectCnt := 0
retList := make([]st_MemTable_Row, 0)
for i, val := range this.memTable {
if i >= inStart && val.GetInt("m_ValidStatus") == 1 {
retList = append(retList, val)
retEffectCnt++
}
}
return true, retEffectCnt, retList, nil
}
//获取
retEffectCnt := 0
retList := make([]st_MemTable_Row, 0)
for i, val := range this.memTable {
if i >= inStart && val.GetInt("m_ValidStatus") == 1 {
if retEffectCnt < inCnt {
retList = append(retList, val)
retEffectCnt ++
}
}
}
return true, retEffectCnt, retList, nil
}
func (this *ST_MemTable) GetCols(inColName []string) (tf bool, outmap []map[string]interface{}, err error) {
if this == nil {
return false, nil, errors.New("pT is null")
}
this.m_RWLock.RLock()
defer this.m_RWLock.RUnlock()
for _, val := range inColName {
if !this.CheckColNameExist(val) {
return false, nil, errors.New("没有列名:" + val)
}
}
//获取
retList := make([]map[string]interface{}, 0)
for _, valRowMap := range this.memTable {
retListOne := make(map[string]interface{})
for _, inVal := range inColName {
if valRowMap.GetInt("m_ValidStatus") == 1 {
retListOne[inVal] = valRowMap[inVal]
}
}
retList = append(retList, retListOne)
}
return true, retList, nil
}
func (this *ST_MemTable) GetColsOne(inColName string) ([]map[string]interface{}, error) {
if this == nil {
return nil, errors.New("pT is null")
}
this.m_RWLock.RLock()
defer this.m_RWLock.RUnlock()
if !this.CheckColNameExist(inColName) {
return nil, errors.New("没有列名:" + inColName)
}
//获取
retList := make([]map[string]interface{}, 0)
retListOne := make(map[string]interface{})
for _, valRowMap := range this.memTable {
if valRowMap.GetInt("m_ValidStatus") == 1 {
retListOne[inColName] = valRowMap[inColName]
}
retList = append(retList, retListOne)
}
return retList, nil
}
func (this *ST_MemTable) Subset(inColName []string) (*ST_MemTable, error) {
if this == nil {
return nil, errors.New("pT is null")
}
this.m_RWLock.RLock()
defer this.m_RWLock.RUnlock()
for _, val := range inColName {
if !this.CheckColNameExist(val) {
return nil, errors.New("没有列名:" + val)
}
}
colNameType := make(map[string]string)
for _, val := range inColName {
colNameType[val] = this.getColType(val)
}
pTOut := NewMemTable(colNameType)
//获取
_, _, thisRows, err := this.GetRows(0, -1)
if err != nil {
return nil, err
}
for _, val := range thisRows {
rowOne := make(map[string]interface{})
for colName, colType := range colNameType {
switch colType {
case "string":
rowOne[colName] = val.GetString(colName)
break
case "int":
rowOne[colName] = val.GetInt(colName)
break
case "int64":
rowOne[colName] = val.GetInt64(colName)
break
default:
break
}
}
_, err := pTOut.InsertRow(rowOne)
if err != nil {
return pTOut, err
}
}
for key, val := range this.colNameRemark {
pTOut.AddRemark(key, val)
}
return pTOut, nil
}
func (this *ST_MemTable) QueryRows(whereMap map[string]interface{}) (posRow []int, total int, outMap []map[string]interface{}, err error) {
if this == nil {
return nil, 0, nil, errors.New("pT is null")
}
this.m_RWLock.RLock()
defer this.m_RWLock.RUnlock()
//获取
pos := make([]int, 0)
retList := make([]map[string]interface{}, 0)
cnt := 0
gotIt := 0
for i, valMapRow := range this.memTable {
gotIt = 0
for key, val := range whereMap { //要匹配的key和val
if valMapRow[key] == val && valMapRow.GetInt("m_ValidStatus") == 1 {
gotIt ++
}
}
if gotIt == len(whereMap) {
pos = append(pos, i)
retList = append(retList, this.memTable[i])
cnt++
}
}
return pos, cnt, retList, nil
}
func (this *ST_MemTable) QueryRowsLike(whereMap map[string]interface{}) (posRow []int, total int, outMap []map[string]interface{}, err error) {
if this == nil {
return nil, 0, nil, errors.New("pT is null")
}
this.m_RWLock.RLock()
defer this.m_RWLock.RUnlock()
//获取
pos := make([]int, 0)
retList := make([]map[string]interface{}, 0)
cnt := 0
gotIt := 0
for i, valMapRow := range this.memTable {
if valMapRow.GetInt("m_ValidStatus") == 1 {
gotIt = 0
for key, val := range whereMap { //要匹配的key和val
if this.colNameType[key] == "string" {
if strings.Contains(valMapRow[key].(string), val.(string)) {
gotIt ++
}
}
}
if gotIt == len(whereMap) {
pos = append(pos, i)
retList = append(retList, this.memTable[i])
cnt++
}
}
}
return pos, cnt, retList, nil
}
func (this *ST_MemTable) QueryTable(whereMap map[string]interface{}) (*ST_MemTable, error) {
if this == nil {
return nil, errors.New("pT is null")
}
this.m_RWLock.RLock()
defer this.m_RWLock.RUnlock()
//获取
gotIt := 0
pTOut := NewMemTable(this.colNameType)
for _, valMapRow := range this.memTable {
gotIt = 0
for key, val := range whereMap { //要匹配的key和val
if valMapRow[key] == val && valMapRow.GetInt("m_ValidStatus") == 1 {
gotIt ++
}
}
if gotIt == len(whereMap) {
pTOut.InsertRow(valMapRow)
}
}
return pTOut, nil
}
func (this *ST_MemTable) QueryTableInAnd(whereMapIn map[string][]interface{}) (*ST_MemTable, error) {
if this == nil {
return nil, errors.New("pT is null")
}
this.m_RWLock.RLock()
defer this.m_RWLock.RUnlock()
//获取
pTOut := NewMemTable(this.colNameType)
for _, valMapRow := range this.memTable {
gotIt := make(map[string]int)
for key, valList := range whereMapIn { //要匹配的key和val
for _, val := range valList {
if valMapRow[key] == val && valMapRow["m_ValidStatus"] == 1 {
gotIt[key] ++
}
}
}
gotItAnd := 0
for key, _ := range whereMapIn {
if gotIt[key] > 0 {
gotItAnd ++
}
}
if gotItAnd == len(whereMapIn) {
pTOut.InsertRow(valMapRow)
}
}
return pTOut, nil
}
func (this *ST_MemTable) AddColName(colNameType map[string]string) (bool, error) {
if this == nil {
return false, errors.New("pT is null")
}
this.m_RWLock.Lock()
defer this.m_RWLock.Unlock()
for key, val := range colNameType {
this.colNameType[key] = val
}
return true, nil
}
func (this *ST_MemTable) CloneTable() (*ST_MemTable, error) {
if this == nil {
return nil, errors.New("pT is null")
}
this.m_RWLock.RLock()
defer this.m_RWLock.RUnlock()
cloneTable := NewMemTable(this.colNameType)
_, _, inRows, _ := this.GetRows(0, -1)
for _, InsertRowOne := range inRows {
_, err := cloneTable.InsertRow(InsertRowOne)
if err != nil {
return nil, err
}
}
return cloneTable, nil
}
func (this *ST_MemTable) InserTable(inPT *ST_MemTable) (*ST_MemTable, error) {
if this == nil || inPT == nil {
return nil, errors.New("pT is null")
}
outTable, err := this.CloneTable()
if err != nil {
return nil, err
}
inPT.m_RWLock.RLock()
defer inPT.m_RWLock.RUnlock()
_, _, inRows, _ := inPT.GetRows(0, -1)
for _, valRowsOne := range inRows {
insertRowOne := make(map[string]interface{})
for _, val := range outTable.GetColNames() {
insertRowOne[val] = valRowsOne.GetVal(val)
}
_, err := outTable.InsertRow(insertRowOne)
if err != nil {
return nil, err
}
}
return outTable, nil
}
//pT1 join pT2
//Join--如果联合主键相同,则以pT2相同字段覆盖pT1相同字段内容
func (this *ST_MemTable) Join(pT2 *ST_MemTable, whereColNameEqual map[string]string) (outPT *ST_MemTable, effectRows int) {
if this == nil || pT2 == nil {
return nil, -1
}
joinMapNameType := make(map[string]string)
joinRemark := make(map[string]string)
for key, val := range this.colNameType {
joinMapNameType[key] = val
joinRemark[key] = this.colNameRemark[key]
}
for key, val := range pT2.colNameType {
joinMapNameType[key] = val
joinRemark[key] = pT2.colNameRemark[key]
}
retPT := NewMemTable(joinMapNameType)
//备注加入
for key, val := range joinRemark {
retPT.AddRemark(key, val)
}
//n^2匹配
this.m_RWLock.RLock()
defer this.m_RWLock.RUnlock()
for _, valMap1 := range this.memTable {
if valMap1.GetVal("m_ValidStatus") == 1 {
joinMapRow := make(map[string]interface{})
pT2.m_RWLock.RLock()
for _, valMap2 := range pT2.memTable {
if valMap2.GetVal("m_ValidStatus") == 1 {
mathCnt := 0
for WhereStr1, WhereStr2 := range whereColNameEqual {
if valMap1[WhereStr1] == valMap2[WhereStr2] {
mathCnt++
}
}
if mathCnt == len(whereColNameEqual) {
for key1, val1 := range valMap1 {
joinMapRow[key1] = val1
}
for key2, val2 := range valMap2 {
joinMapRow[key2] = val2
}
retPT.InsertRow(joinMapRow)
}
}
}
pT2.m_RWLock.RUnlock()
}
}
return retPT, retPT.GetRowCount()
}
func (this *ST_MemTable) LeftJoin(pT2 *ST_MemTable, whereColNameEqual map[string]string) (outPT *ST_MemTable, effectRows int) {
if this == nil || pT2 == nil {
return nil, -1
}
joinMapNameType := make(map[string]string)
joinRemark := make(map[string]string)
for key, val := range this.colNameType {
joinMapNameType[key] = val
joinRemark[key] = this.colNameRemark[key]
}
for key, val := range pT2.colNameType {
joinMapNameType[key] = val
joinRemark[key] = pT2.colNameRemark[key]
}
retPT := NewMemTable(joinMapNameType)
for key, val := range joinRemark {
retPT.AddRemark(key, val)
}
//n^2匹配
this.m_RWLock.RLock()
defer this.m_RWLock.RUnlock()
for _, valMap1 := range this.memTable {
if valMap1.GetVal("m_ValidStatus") == 1 {
joinMapRow := make(map[string]interface{})
for key1, val1 := range valMap1 {
joinMapRow[key1] = val1
}
rowMatchCnt := 0
pT2.m_RWLock.RLock()
for _, valMap2 := range pT2.memTable {
if valMap2.GetVal("m_ValidStatus") == 1 {
oneRowMathCnt := 0
for WhereStr1, WhereStr2 := range whereColNameEqual {
if valMap1[WhereStr1] == valMap2[WhereStr2] {
oneRowMathCnt++
}
}
if oneRowMathCnt == len(whereColNameEqual) {
for key2, val2 := range valMap2 {
joinMapRow[key2] = val2
}
rowMatchCnt++
retPT.InsertRow(joinMapRow)
}
}
}
pT2.m_RWLock.RUnlock()
if rowMatchCnt == 0 {
retPT.InsertRow(joinMapRow)
}
}
}
return retPT, retPT.GetRowCount()
}
//Join_1Cover2--如果联合主键相同,则以pT1相同字段覆盖pT2相同字段内容
//Join_2Cover1--如果联合主键相同,则以pT2相同字段覆盖pT1相同字段内容
func (this *ST_MemTable) Join_1Cover2(pT2 *ST_MemTable, whereColNameEqual map[string]string) (outPT *ST_MemTable, effectRows int) {
if this == nil {
return nil, -1
}
joinMapNameType := make(map[string]string)
joinRemark := make(map[string]string)
for key, val := range pT2.colNameType {
joinMapNameType[key] = val
joinRemark[key] = pT2.colNameRemark[key]
}
for key, val := range this.colNameType {
joinMapNameType[key] = val
joinRemark[key] = this.colNameRemark[key]
}
retPT := NewMemTable(joinMapNameType)
//备注加入
for key, val := range joinRemark {
retPT.AddRemark(key, val)
}
//n^2匹配
this.m_RWLock.RLock()
defer this.m_RWLock.RUnlock()
for _, valMap1 := range this.memTable {
if valMap1.GetVal("m_ValidStatus") == 1 {
joinMapRow := make(map[string]interface{})
pT2.m_RWLock.RLock()
for _, valMap2 := range pT2.memTable {
if valMap2.GetVal("m_ValidStatus") == 1 {
mathCnt := 0
for WhereStr1, WhereStr2 := range whereColNameEqual {
if valMap1[WhereStr1] == valMap2[WhereStr2] {
mathCnt++
}
}
if mathCnt == len(whereColNameEqual) {
for key2, val2 := range valMap2 {
joinMapRow[key2] = val2
}
for key1, val1 := range valMap1 {
joinMapRow[key1] = val1
}
retPT.InsertRow(joinMapRow)
}
}
}
pT2.m_RWLock.RUnlock()
}
}
return retPT, retPT.GetRowCount()
}
func (this *ST_MemTable) Join_2Cover1(pT2 *ST_MemTable, whereColNameEqual map[string]string) (outPT *ST_MemTable, effectRows int) {
if this == nil || pT2 == nil {
return nil, -1
}
joinMapNameType := make(map[string]string)
joinRemark := make(map[string]string)
for key, val := range this.colNameType {
joinMapNameType[key] = val
joinRemark[key] = this.colNameRemark[key]
}
for key, val := range pT2.colNameType {
joinMapNameType[key] = val
joinRemark[key] = pT2.colNameRemark[key]
}
retPT := NewMemTable(joinMapNameType)
//备注加入
for key, val := range joinRemark {
retPT.AddRemark(key, val)
}
//n^2匹配
this.m_RWLock.RLock()
defer this.m_RWLock.RUnlock()
for _, valMap1 := range this.memTable {
if valMap1.GetVal("m_ValidStatus") == 1 {
joinMapRow := make(map[string]interface{})
pT2.m_RWLock.RLock()
for _, valMap2 := range pT2.memTable {
if valMap2.GetVal("m_ValidStatus") == 1 {
mathCnt := 0
for WhereStr1, WhereStr2 := range whereColNameEqual {
if valMap1[WhereStr1] == valMap2[WhereStr2] {
mathCnt++
}
}
if mathCnt == len(whereColNameEqual) {
for key1, val1 := range valMap1 {
joinMapRow[key1] = val1
}
for key2, val2 := range valMap2 {
joinMapRow[key2] = val2
}
retPT.InsertRow(joinMapRow)
}
}
}
pT2.m_RWLock.RUnlock()
}
}
return retPT, retPT.GetRowCount()
}
func (this *ST_MemTable) LeftJoin_1Cover2(pT2 *ST_MemTable, whereColNameEqual map[string]string) (outPT *ST_MemTable, effectRows int) {
if this == nil || pT2 == nil {
return nil, -1
}
joinMapNameType := make(map[string]string)
joinRemark := make(map[string]string)
for key, val := range pT2.colNameType {
joinMapNameType[key] = val
joinRemark[key] = pT2.colNameRemark[key]
}
for key, val := range this.colNameType {
joinMapNameType[key] = val
joinRemark[key] = this.colNameRemark[key]
}
retPT := NewMemTable(joinMapNameType)
for key, val := range joinRemark {
retPT.AddRemark(key, val)
}
//n^2匹配
this.m_RWLock.RLock()
defer this.m_RWLock.RUnlock()
for _, valMap1 := range this.memTable {
if valMap1.GetVal("m_ValidStatus") == 1 {
joinMapRow := make(map[string]interface{})
for key1, val1 := range valMap1 {
joinMapRow[key1] = val1
}
rowMatchCnt := 0
pT2.m_RWLock.RLock()
for _, valMap2 := range pT2.memTable {
if valMap2.GetVal("m_ValidStatus") == 1 {
oneRowMathCnt := 0
for WhereStr1, WhereStr2 := range whereColNameEqual {
if valMap1[WhereStr1] == valMap2[WhereStr2] {
oneRowMathCnt++
}
}
if oneRowMathCnt == len(whereColNameEqual) {
for key2, val2 := range valMap2 {
if _, ok := joinMapRow[key2]; !ok {
joinMapRow[key2] = val2
}
}
rowMatchCnt++
retPT.InsertRow(joinMapRow)
}
}
}
pT2.m_RWLock.RUnlock()
if rowMatchCnt == 0 {
retPT.InsertRow(joinMapRow)
}
}
}
return retPT, retPT.GetRowCount()
}
func (this *ST_MemTable) LeftJoin_2Cover1(pT2 *ST_MemTable, whereColNameEqual map[string]string) (outPT *ST_MemTable, effectRows int) {
if this == nil || pT2 == nil {
return nil, -1
}
joinMapNameType := make(map[string]string)
joinRemark := make(map[string]string)
for key, val := range this.colNameType {
joinMapNameType[key] = val
joinRemark[key] = this.colNameRemark[key]
}
for key, val := range pT2.colNameType {
joinMapNameType[key] = val
joinRemark[key] = pT2.colNameRemark[key]
}
retPT := NewMemTable(joinMapNameType)
for key, val := range joinRemark {
retPT.AddRemark(key, val)
}
//n^2匹配
this.m_RWLock.RLock()
defer this.m_RWLock.RUnlock()
for _, valMap1 := range this.memTable {
if valMap1.GetVal("m_ValidStatus") == 1 {
joinMapRow := make(map[string]interface{})
for key1, val1 := range valMap1 {
joinMapRow[key1] = val1
}
rowMatchCnt := 0
pT2.m_RWLock.RLock()
for _, valMap2 := range pT2.memTable {
if valMap2.GetVal("m_ValidStatus") == 1 {
oneRowMathCnt := 0
for WhereStr1, WhereStr2 := range whereColNameEqual {
if valMap1[WhereStr1] == valMap2[WhereStr2] {
oneRowMathCnt++
}
}
if oneRowMathCnt == len(whereColNameEqual) {
for key2, val2 := range valMap2 {
joinMapRow[key2] = val2
}
rowMatchCnt++
retPT.InsertRow(joinMapRow)
}
}
}
pT2.m_RWLock.RUnlock()
if rowMatchCnt == 0 {
retPT.InsertRow(joinMapRow)
}
}
}
return retPT, retPT.GetRowCount()
}
//对表行 关键字去重
func (this *ST_MemTable) GroupBy_Limit1st(colName string) (error) {
if this == nil {
return errors.New("pT 空")
}
if !this.CheckColNameExist(colName) {
return errors.New("GroupBy_Limit1:" + "找不到对应列(" + colName + ")")
}
this.m_RWLock.Lock()
defer this.m_RWLock.Unlock()
cnt := this.GetRowCount_Total()
j := cnt - 1
//从后向前 删除重复数据
for j >= 0 {
for i, TableRow := range this.memTable {
if TableRow["m_ValidStatus"] == 1 {
if i < j {
if TableRow.GetVal(colName) == this.memTable[j].GetVal(colName) {
this.memTable[j].SetVal("m_ValidStatus", -1)
}
}
}
}
j--
}
return nil
}
//对表行 关键字去重
func (this *ST_MemTable) GroupBy_Limit1(colNameList []string) (error) {
if this == nil {
return errors.New("pT 空")
}
for _, val := range colNameList {
if !this.CheckColNameExist(val) {
return errors.New("GroupBy_Limit1:" + "找不到对应列(" + val + ")")
}
}
this.m_RWLock.Lock()
defer this.m_RWLock.Unlock()
cnt := this.GetRowCount_Total()
j := cnt - 1
//从后向前 删除重复数据
for j >= 0 {
for i, TableRow := range this.memTable {
if TableRow["m_ValidStatus"] == 1 {
if i < j {
isFoundCnt := 0
for _, colNameTmp := range colNameList {
if TableRow.GetVal(colNameTmp) == this.memTable[j].GetVal(colNameTmp) {
isFoundCnt++
}
}
if isFoundCnt == len(colNameList) && isFoundCnt != 0 {