-
Notifications
You must be signed in to change notification settings - Fork 1
/
StoreServiceImpl.java
936 lines (746 loc) · 32.3 KB
/
StoreServiceImpl.java
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
package org.fbase.service.impl;
import static org.fbase.service.mapping.Mapper.INT_NULL;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.IntStream;
import lombok.extern.log4j.Log4j2;
import org.fbase.core.metamodel.MetaModelApi;
import org.fbase.exception.EnumByteExceedException;
import org.fbase.model.profile.CProfile;
import org.fbase.model.profile.cstype.CSType;
import org.fbase.model.profile.cstype.CType;
import org.fbase.model.profile.cstype.SType;
import org.fbase.model.profile.table.IType;
import org.fbase.service.CommonServiceApi;
import org.fbase.service.StoreService;
import org.fbase.service.mapping.Mapper;
import org.fbase.service.store.EStore;
import org.fbase.service.store.HEntry;
import org.fbase.service.store.RStore;
import org.fbase.service.store.TStore;
import org.fbase.storage.Converter;
import org.fbase.storage.EnumDAO;
import org.fbase.storage.HistogramDAO;
import org.fbase.storage.RawDAO;
@Log4j2
public class StoreServiceImpl extends CommonServiceApi implements StoreService {
private final MetaModelApi metaModelApi;
private final Converter converter;
private final EnumDAO enumDAO;
private final RawDAO rawDAO;
private final HistogramDAO histogramDAO;
public StoreServiceImpl(MetaModelApi metaModelApi,
Converter converter,
RawDAO rawDAO,
EnumDAO enumDAO,
HistogramDAO histogramDAO) {
this.metaModelApi = metaModelApi;
this.converter = converter;
this.rawDAO = rawDAO;
this.enumDAO = enumDAO;
this.histogramDAO = histogramDAO;
}
@Override
public void putDataDirect(String tableName,
List<List<Object>> data) {
int rowCount = data.get(0).size();
byte tableId = metaModelApi.getTableId(tableName);
boolean compression = metaModelApi.getTableCompression(tableName);
List<CProfile> cProfiles = metaModelApi.getCProfiles(tableName);
int colCount = cProfiles.size();
Map<Integer, SType> colIdSTypeMap = new HashMap<>();
cProfiles.stream().filter(isNotTimestamp)
.forEach(e -> colIdSTypeMap.put(e.getColId(), e.getCsType().getSType()));
/* Timestamp */
TStore tStore = new TStore(1, cProfiles);
/* Raw */
RStore rStore = new RStore(converter, cProfiles, colIdSTypeMap);
/* Enum */
EStore eStore = new EStore(cProfiles, colIdSTypeMap);
/* Histogram */
Map<Integer, HEntry> histograms = new HashMap<>();
cProfiles.stream()
.filter(isNotTimestamp)
.forEach(e -> histograms.put(e.getColId(), new HEntry(new ArrayList<>(), new ArrayList<>())));
List<Integer> iColumn = IntStream.range(0, colCount).boxed().toList();
IntStream iRow = IntStream.range(0, rowCount);
iRow.forEach(iR -> iColumn.forEach(iC -> {
CProfile cProfile = cProfiles.get(iC);
CSType csType = cProfile.getCsType();
int colId = cProfile.getColId();
Object currObject = data.get(iC).get(iR);
// Fill timestamps
if (csType.isTimeStamp()) {
if (csType.getSType() == SType.RAW) {
tStore.add(iC, iR, this.converter.getKeyValue(currObject, cProfile));
}
}
// Fill raw data
if (csType.getSType() == SType.RAW) {
if (!csType.isTimeStamp()) {
rStore.add(cProfile, iR, currObject);
}
}
// Fill enum data
if (csType.getSType() == SType.ENUM) {
int curValue = this.converter.convertRawToInt(currObject, cProfile);
int iMapping = eStore.getMapping().get(colId);
try {
eStore.add(iMapping, iR, curValue);
} catch (EnumByteExceedException e) {
throw new RuntimeException(e);
}
}
// Fill histogram data
if (csType.getSType() == SType.HISTOGRAM) {
int prevInt = getPrevValue(histograms, colId);
int currInt = this.converter.convertRawToInt(currObject, cProfile);
if (prevInt != currInt) {
histograms.get(colId).getIndex().add(iR);
histograms.get(colId).getValue().add(currInt);
} else if (iR == 0) {
histograms.get(colId).getIndex().add(iR);
histograms.get(colId).getValue().add(currInt);
}
}
}));
long blockId = tStore.getBlockId();
this.storeDataGlobal(tableId, compression, blockId, cProfiles, colIdSTypeMap, tStore, rStore, eStore, histograms);
}
@Override
public long putDataJdbc(String tableName,
ResultSet resultSet) {
IType indexType = metaModelApi.getIndexType(tableName);
if (IType.LOCAL.equals(indexType)) {
log.info(IType.LOCAL);
return putDataJdbcLocal(tableName, resultSet);
}
byte tableId = metaModelApi.getTableId(tableName);
boolean compression = metaModelApi.getTableCompression(tableName);
List<CProfile> cProfiles = metaModelApi.getCProfiles(tableName);
int colCount = cProfiles.size();
Map<Integer, SType> colIdSTypeMap = new HashMap<>();
cProfiles.stream().filter(isNotTimestamp)
.forEach(e -> colIdSTypeMap.put(e.getColId(), e.getCsType().getSType()));
/* Timestamp */
TStore tStore = new TStore(1, cProfiles);
/* Raw */
RStore rStore = new RStore(converter, cProfiles, colIdSTypeMap);
/* Enum */
EStore eStore = new EStore(cProfiles, colIdSTypeMap);
/* Histogram */
Map<Integer, HEntry> histograms = new HashMap<>();
cProfiles.stream()
.filter(isNotTimestamp)
.forEach(e -> histograms.put(e.getColId(), new HEntry(new ArrayList<>(), new ArrayList<>())));
List<Integer> iColumn = IntStream.range(0, colCount).boxed().toList();
try {
final AtomicInteger iRow = new AtomicInteger(0);
while (resultSet.next()) {
int iR = iRow.getAndAdd(1);
iColumn.forEach(iC -> {
try {
CProfile cProfile = cProfiles.get(iC);
CSType csType = cProfile.getCsType();
int colId = cProfile.getColId();
Object currObject = resultSet.getObject(cProfile.getColIdSql());
// Fill timestamps
if (csType.isTimeStamp()) {
if (csType.getSType() == SType.RAW) {
tStore.add(iC, iR, this.converter.getKeyValue(currObject, cProfile));
}
}
// Fill raw data
if (csType.getSType() == SType.RAW) {
if (!csType.isTimeStamp()) {
rStore.add(cProfile, iR, currObject);
}
}
// Fill enum data
if (csType.getSType() == SType.ENUM) {
int curValue = this.converter.convertRawToInt(currObject, cProfile);
int iMapping = eStore.getMapping().get(colId);
try {
eStore.add(iMapping, iR, curValue);
} catch (EnumByteExceedException e) {
throw new RuntimeException(e);
}
}
// Fill histogram data
if (csType.getSType() == SType.HISTOGRAM) {
int prevInt = getPrevValue(histograms, colId);
int currInt = this.converter.convertRawToInt(currObject, cProfile);
if (prevInt != currInt) {
histograms.get(colId).getIndex().add(iR);
histograms.get(colId).getValue().add(currInt);
} else if (iR == 0) {
histograms.get(colId).getIndex().add(iR);
histograms.get(colId).getValue().add(currInt);
}
}
} catch (SQLException e) {
log.catching(e);
throw new RuntimeException(e);
}
});
}
if (tStore.size() == 0) {
return -1;
}
long blockId = tStore.getBlockId();
this.storeDataGlobal(tableId, compression, blockId, cProfiles, colIdSTypeMap, tStore, rStore, eStore, histograms);
return tStore.getTail();
} catch (Exception e) {
log.catching(e);
throw new RuntimeException(e);
}
}
private long putDataJdbcLocal(String tableName,
ResultSet resultSet) {
byte tableId = metaModelApi.getTableId(tableName);
boolean compression = metaModelApi.getTableCompression(tableName);
List<CProfile> cProfiles = metaModelApi.getCProfiles(tableName);
int colCount = cProfiles.size();
/* Timestamp */
TStore tStore = new TStore(1, cProfiles);
// Fill histogram data
Map<Integer, HEntry> histograms = new HashMap<>();
cProfiles.stream()
.filter(isNotTimestamp)
.forEach(e -> histograms.put(e.getColId(), new HEntry(new ArrayList<>(), new ArrayList<>())));
List<Integer> iColumn = IntStream.range(0, colCount).boxed().toList();
try {
final AtomicInteger iRow = new AtomicInteger(0);
while (resultSet.next()) {
int iR = iRow.getAndAdd(1);
iColumn.forEach(iC -> {
try {
CProfile cProfile = cProfiles.get(iC);
CSType csType = cProfile.getCsType();
Object currObject = resultSet.getObject(cProfile.getColIdSql());
// Fill timestamps
if (csType.isTimeStamp()) {
if (csType.getSType() == SType.RAW) {
tStore.add(iC, iR, this.converter.getKeyValue(currObject, cProfile));
}
}
if (!csType.isTimeStamp()) {
// Fill histogram data
int colId = cProfile.getColId();
int prevInt = getPrevValue(histograms, colId);
int currInt = this.converter.convertRawToInt(currObject, cProfile);
if (prevInt != currInt) {
histograms.get(colId).getIndex().add(iR);
histograms.get(colId).getValue().add(currInt);
} else if (iR == 0) {
histograms.get(colId).getIndex().add(iR);
histograms.get(colId).getValue().add(currInt);
}
}
} catch (SQLException e) {
log.catching(e);
throw new RuntimeException(e);
}
});
}
log.info("Rows: " + iRow.get());
if (tStore.size() == 0) {
return -1;
}
long blockId = tStore.getBlockId();
/* Store data and metadata */
this.storeDataLocal(tableId, compression, blockId, cProfiles, tStore, histograms);
return tStore.getTail();
} catch (Exception e) {
log.catching(e);
throw new RuntimeException(e);
}
}
private int getPrevValue(Map<Integer, HEntry> histograms,
int colId) {
int size = histograms.get(colId).getValue().size();
if (size == 0) {
return INT_NULL;
}
return histograms.get(colId).getValue().get(size - 1);
}
private void storeDataGlobal(byte tableId,
boolean compression,
long blockId,
List<CProfile> cProfiles,
Map<Integer, SType> colIdSTypeMap,
TStore tStore,
RStore rStore,
EStore eStore,
Map<Integer, HEntry> histograms) {
/* Store metadata */
this.storeMetadataLocal(tableId, blockId, cProfiles, colIdSTypeMap);
/* Store raw data */
this.storeRaw(tableId, compression, blockId, tStore, rStore);
/* Store enum data */
this.storeEnum(tableId, compression, blockId, eStore);
/* Store histogram data */
this.storeHistogramsEntry(tableId, compression, blockId, histograms);
}
private void storeRaw(byte tableId,
boolean compression,
long blockId,
TStore tStore,
RStore rStore) {
if (compression) {
rawDAO.putCompressed(tableId, blockId,
tStore.getMapping(), tStore.getRawData(),
rStore.getMappingInt(), rStore.getRawDataInt(),
rStore.getMappingLong(), rStore.getRawDataLong(),
rStore.getMappingFloat(), rStore.getRawDataFloat(),
rStore.getMappingDouble(), rStore.getRawDataDouble(),
rStore.getMappingString(), rStore.getRawDataString());
return;
}
this.rawDAO.putLong(tableId, blockId, tStore.mappingToArray(), tStore.dataToArray());
if (rStore.getInitialCapacityInt() > 0) {
this.rawDAO.putInt(tableId, blockId,
rStore.getMappingInt().keySet().stream().mapToInt(i -> i)
.toArray(), getArrayInt(rStore.getRawDataInt()));
}
if (rStore.getInitialCapacityLong() > 0) {
this.rawDAO.putLong(tableId, blockId,
rStore.getMappingLong().keySet().stream().mapToInt(i -> i)
.toArray(), getArrayLong(rStore.getRawDataLong()));
}
if (rStore.getInitialCapacityFloat() > 0) {
this.rawDAO.putFloat(tableId, blockId,
rStore.getMappingFloat().keySet().stream().mapToInt(i -> i)
.toArray(), getArrayFloat(rStore.getRawDataFloat()));
}
if (rStore.getInitialCapacityDouble() > 0) {
this.rawDAO.putDouble(tableId, blockId,
rStore.getMappingDouble().keySet().stream().mapToInt(i -> i)
.toArray(), getArrayDouble(rStore.getRawDataDouble()));
}
if (rStore.getInitialCapacityString() > 0) {
this.rawDAO.putString(tableId, blockId,
rStore.getMappingString().keySet().stream().mapToInt(i -> i)
.toArray(), getArrayString(rStore.getRawDataString()));
}
}
private void storeDataLocal(byte tableId,
boolean compression,
long blockId,
List<CProfile> cProfiles,
TStore tStore,
Map<Integer, HEntry> histograms) {
Map<Integer, SType> colIdSTypeMap = new HashMap<>();
histograms.forEach((colId, value) -> {
int sizeOfRaw = tStore.size();
int sizeOfHist = value.getValue().size();
if (sizeOfHist < (sizeOfRaw / 2)) {
colIdSTypeMap.put(colId, SType.HISTOGRAM);
} else {
if (value.getValue().stream().distinct().count() > 255) {
colIdSTypeMap.put(colId, SType.RAW);
} else {
colIdSTypeMap.put(colId, SType.ENUM);
}
}
});
/* Raw */
RStore rStore = new RStore(converter, cProfiles, colIdSTypeMap);
/* Enums */
EStore eStore = new EStore(cProfiles, colIdSTypeMap);
cProfiles.forEach(cProfile -> {
int colId = cProfile.getColId();
if (SType.ENUM.equals(colIdSTypeMap.get(colId))) {
int iMapping = eStore.getMapping().get(colId);
List<Integer> indexList = histograms.get(colId).getIndex();
List<Integer> valueList = histograms.get(colId).getValue();
for (int i = 0; i < indexList.size(); i++) {
int curIndex = indexList.get(i);
int nextIndex;
int curValue = valueList.get(i);
if (indexList.size() != i + 1) {
nextIndex = indexList.get(i + 1);
for (int j = curIndex; j < nextIndex; j++) {
try {
eStore.add(iMapping, j, curValue);
} catch (EnumByteExceedException e) {
throw new RuntimeException(e);
}
}
} else {
nextIndex = tStore.size() - 1;
for (int j = curIndex; j <= nextIndex; j++) {
try {
eStore.add(iMapping, j, curValue);
} catch (EnumByteExceedException e) {
throw new RuntimeException(e);
}
}
}
}
} else if (SType.RAW.equals(colIdSTypeMap.get(cProfile.getColId()))) {
List<Integer> indexList = histograms.get(colId).getIndex();
List<Integer> valueList = histograms.get(colId).getValue();
for (int i = 0; i < indexList.size(); i++) {
int curIndex = indexList.get(i);
int nextIndex;
int curValue = valueList.get(i);
if (indexList.size() != i + 1) {
nextIndex = indexList.get(i + 1);
for (int j = curIndex; j < nextIndex; j++) {
rStore.add(Mapper.isCType(cProfile), cProfile, j, curValue);
}
} else {
nextIndex = tStore.size() - 1;
for (int j = curIndex; j <= nextIndex; j++) {
rStore.add(Mapper.isCType(cProfile), cProfile, j, curValue);
}
}
}
}
});
/* Store metadata */
this.storeMetadataLocal(tableId, blockId, cProfiles, colIdSTypeMap);
/* Store raw data */
this.storeRaw(tableId, compression, blockId, tStore, rStore);
/* Store enum data */
this.storeEnum(tableId, compression, blockId, eStore);
/* Store histogram data */
colIdSTypeMap.entrySet().stream()
.filter(f -> !SType.HISTOGRAM.equals(f.getValue()))
.forEach(obj -> histograms.remove(obj.getKey()));
this.storeHistogramsEntry(tableId, compression, blockId, histograms);
}
@Override
public void putDataJdbcBatch(String tableName,
ResultSet resultSet,
Integer fBaseBatchSize) {
byte tableId = metaModelApi.getTableId(tableName);
boolean compression = metaModelApi.getTableCompression(tableName);
List<CProfile> cProfiles = metaModelApi.getCProfiles(tableName);
int colCount = cProfiles.size();
Map<Integer, SType> colIdSTypeMap = new HashMap<>();
cProfiles.stream().filter(isNotTimestamp)
.forEach(e -> colIdSTypeMap.put(e.getColId(), e.getCsType().getSType()));
/* Timestamp */
TStore tStore = new TStore(1, cProfiles);
/* Raw */
RStore rStore = new RStore(converter, cProfiles, colIdSTypeMap);
/* Enum */
EStore eStore = new EStore(cProfiles, colIdSTypeMap);
/* Histogram */
Map<Integer, HEntry> histograms = new HashMap<>();
cProfiles.stream()
.filter(isNotTimestamp)
.forEach(e -> histograms.put(e.getColId(), new HEntry(new ArrayList<>(), new ArrayList<>())));
try {
final AtomicInteger iRow = new AtomicInteger(0);
while (resultSet.next()) {
int iR = iRow.getAndAdd(1);
// Reinitialize
if (iR == fBaseBatchSize) {
long blockId = tStore.getBlockId();
this.storeDataGlobal(tableId, compression, blockId, cProfiles, colIdSTypeMap, tStore, rStore, eStore, histograms);
// Reset
iRow.set(0);
iR = iRow.getAndAdd(1);
/* Timestamp */
tStore = new TStore(1, cProfiles);
/* Raw */
rStore = new RStore(converter, cProfiles, colIdSTypeMap);
/* Enum */
eStore = new EStore(cProfiles, colIdSTypeMap);
/* Histogram */
histograms.clear();
cProfiles.stream()
.filter(isNotTimestamp)
.forEach(e -> histograms.put(e.getColId(), new HEntry(new ArrayList<>(), new ArrayList<>())));
}
for (int iC = 0; iC < colCount; iC++) {
try {
CProfile cProfile = cProfiles.get(iC);
int colId = cProfile.getColId();
CSType csType = cProfile.getCsType();
Object currObject = resultSet.getObject(cProfile.getColIdSql());
// Fill timestamps
if (csType.isTimeStamp()) {
if (csType.getSType() == SType.RAW) {
tStore.add(iC, iR, this.converter.getKeyValue(currObject, cProfile));
}
}
// Fill raw data
if (csType.getSType() == SType.RAW) {
if (!csType.isTimeStamp()) {
rStore.add(cProfile, iR, currObject);
}
}
// Fill enum data
if (csType.getSType() == SType.ENUM) {
int curValue = this.converter.convertRawToInt(currObject, cProfile);
int iMapping = eStore.getMapping().get(colId);
try {
eStore.add(iMapping, iR, curValue);
} catch (EnumByteExceedException e) {
throw new RuntimeException(e);
}
}
// Fill histogram data
if (csType.getSType() == SType.HISTOGRAM) {
int prevInt = getPrevValue(histograms, colId);
int currInt = this.converter.convertRawToInt(currObject, cProfile);
if (prevInt != currInt) {
histograms.get(colId).getIndex().add(iR);
histograms.get(colId).getValue().add(currInt);
} else if (iR == 0) {
histograms.get(colId).getIndex().add(iR);
histograms.get(colId).getValue().add(currInt);
}
}
} catch (SQLException e) {
log.catching(e);
throw new RuntimeException(e);
}
}
}
if (iRow.get() <= fBaseBatchSize) {
long blockId = tStore.getBlockId();
this.storeDataGlobal(tableId, compression, blockId, cProfiles, colIdSTypeMap, tStore, rStore, eStore, histograms);
log.info("Final flush for iRow: " + iRow.get());
}
} catch (Exception e) {
log.catching(e);
throw new RuntimeException(e);
}
}
@Override
public void putDataCsvBatch(String tableName,
String fileName,
String csvSplitBy,
Integer fBaseBatchSize) {
byte tableId = metaModelApi.getTableId(tableName);
boolean compression = metaModelApi.getTableCompression(tableName);
List<CProfile> cProfiles = metaModelApi.getCProfiles(tableName);
final AtomicLong counter = new AtomicLong(rawDAO.getLastBlockId(tableId));
/* Long */
int colRawDataLongCount = Mapper.getColumnCount(cProfiles, isRaw, isLong);
List<List<Long>> rawDataLong = new ArrayList<>(colRawDataLongCount);
fillArrayList(rawDataLong, colRawDataLongCount);
List<Integer> rawDataLongMapping = new ArrayList<>(colRawDataLongCount);
fillMappingRaw(cProfiles, rawDataLongMapping, isRaw, isLong);
/* Double */
int colRawDataDoubleCount = Mapper.getColumnCount(cProfiles, isRaw, isDouble);
List<List<Double>> rawDataDouble = new ArrayList<>(colRawDataDoubleCount);
fillArrayList(rawDataDouble, colRawDataDoubleCount);
List<Integer> rawDataDoubleMapping = new ArrayList<>(colRawDataDoubleCount);
fillMappingRaw(cProfiles, rawDataDoubleMapping, isRaw, isDouble);
/* String */
int colRawDataStringCount = Mapper.getColumnCount(cProfiles, isRaw, isString);
List<List<String>> rawDataString = new ArrayList<>(colRawDataStringCount);
fillArrayList(rawDataString, colRawDataStringCount);
List<Integer> rawDataStringMapping = new ArrayList<>(colRawDataStringCount);
fillMappingRaw(cProfiles, rawDataStringMapping, isRaw, isString);
String line = "";
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
line = br.readLine();
String[] headers = line.split(csvSplitBy);
log.info("Header = " + Arrays.toString(headers));
final AtomicInteger iRow = new AtomicInteger(0);
while ((line = br.readLine()) != null) {
int iR = iRow.getAndAdd(1);
// Reinitialize
if (iR == fBaseBatchSize) {
long blockId = counter.getAndAdd(1);
/* Store metadata */
this.storeMetadata(tableId, blockId, cProfiles);
this.storeData(tableId, compression, blockId,
colRawDataLongCount, rawDataLongMapping, rawDataLong,
colRawDataDoubleCount, rawDataDoubleMapping, rawDataDouble,
colRawDataStringCount, rawDataStringMapping, rawDataString);
iRow.set(0);
iRow.getAndAdd(1);
/* Long */
rawDataLong = new ArrayList<>(colRawDataLongCount);
fillArrayList(rawDataLong, colRawDataLongCount);
rawDataLongMapping = new ArrayList<>(colRawDataLongCount);
fillMappingRaw(cProfiles, rawDataLongMapping, isRaw, isLong);
/* Double */
rawDataDouble = new ArrayList<>(colRawDataDoubleCount);
fillArrayList(rawDataDouble, colRawDataDoubleCount);
rawDataDoubleMapping = new ArrayList<>(colRawDataDoubleCount);
fillMappingRaw(cProfiles, rawDataDoubleMapping, isRaw, isDouble);
/* String */
rawDataString = new ArrayList<>(colRawDataStringCount);
fillArrayList(rawDataString, colRawDataStringCount);
rawDataStringMapping = new ArrayList<>(colRawDataStringCount);
fillMappingRaw(cProfiles, rawDataStringMapping, isRaw, isString);
}
String[] data = line.split(csvSplitBy);
for (int iC = 0; iC < headers.length; iC++) {
String header = headers[iC];
String colData = data[iC];
Optional<CProfile> optionalCProfile = cProfiles.stream()
.filter(f -> f.getColName().equals(header))
.findAny();
// Fill raw data
if (optionalCProfile.isPresent()) {
CProfile cProfile = optionalCProfile.get();
if (cProfile.getCsType().getSType() == SType.RAW) {
if (CType.LONG == Mapper.isCType(cProfile)) {
rawDataLong.get(rawDataLongMapping.indexOf(iC)).add(Long.valueOf(colData));
} else if (CType.DOUBLE == Mapper.isCType(cProfile)) {
rawDataDouble.get(rawDataDoubleMapping.indexOf(iC)).add(Double.valueOf(colData));
} else if (CType.STRING == Mapper.isCType(cProfile)) {
rawDataString.get(rawDataStringMapping.indexOf(iC)).add(colData);
}
}
}
}
}
if (iRow.get() <= fBaseBatchSize) {
long blockId = counter.getAndAdd(1);
/* Store metadata */
this.storeMetadata(tableId, blockId, cProfiles);
this.storeData(tableId, compression, blockId,
colRawDataLongCount, rawDataLongMapping, rawDataLong,
colRawDataDoubleCount, rawDataDoubleMapping, rawDataDouble,
colRawDataStringCount, rawDataStringMapping, rawDataString);
log.info("Final flush for iRow: " + iRow.get());
}
} catch (IOException e) {
log.catching(e);
}
}
private void storeMetadata(byte tableId,
long blockId,
List<CProfile> cProfiles) {
List<Byte> rawCTypeKeys = new ArrayList<>();
List<Integer> rawColIds = new ArrayList<>();
List<Integer> enumColIds = new ArrayList<>();
List<Integer> histogramColIds = new ArrayList<>();
cProfiles.forEach(cProfile -> {
if (SType.RAW.equals(cProfile.getCsType().getSType())) {
rawCTypeKeys.add(cProfile.getCsType().getCType().getKey());
rawColIds.add(cProfile.getColId());
} else if (SType.ENUM.equals(cProfile.getCsType().getSType())) {
enumColIds.add(cProfile.getColId());
} else if (SType.HISTOGRAM.equals(cProfile.getCsType().getSType())) {
histogramColIds.add(cProfile.getColId());
}
});
this.rawDAO.putMetadata(tableId, blockId, getByteFromList(rawCTypeKeys),
rawColIds.stream().mapToInt(j -> j).toArray(),
enumColIds.stream().mapToInt(j -> j).toArray(),
histogramColIds.stream().mapToInt(j -> j).toArray());
}
private void storeMetadataLocal(byte tableId,
long blockId,
List<CProfile> cProfiles,
Map<Integer, SType> colIdSTypeMap) {
List<Byte> rawCTypeKeys = new ArrayList<>();
List<Integer> rawColIds = new ArrayList<>();
List<Integer> enumColIds = new ArrayList<>();
List<Integer> histogramColIds = new ArrayList<>();
cProfiles.forEach(cProfile -> {
if (SType.RAW.equals(colIdSTypeMap.get(cProfile.getColId()))) {
rawCTypeKeys.add(cProfile.getCsType().getCType().getKey());
rawColIds.add(cProfile.getColId());
} else if (SType.ENUM.equals(colIdSTypeMap.get(cProfile.getColId()))) {
enumColIds.add(cProfile.getColId());
} else if (SType.HISTOGRAM.equals(colIdSTypeMap.get(cProfile.getColId()))) {
histogramColIds.add(cProfile.getColId());
}
});
this.rawDAO.putMetadata(tableId, blockId, getByteFromList(rawCTypeKeys),
rawColIds.stream().mapToInt(j -> j).toArray(),
enumColIds.stream().mapToInt(j -> j).toArray(),
histogramColIds.stream().mapToInt(j -> j).toArray());
}
private void storeHistogramsEntry(byte tableID,
boolean compression,
long blockId,
Map<Integer, HEntry> histograms) {
histograms.forEach((colId, hEntry) -> {
if (!hEntry.getIndex().isEmpty()) {
if (compression) {
this.histogramDAO.putCompressedKeysValues(tableID, blockId, colId,
hEntry.getIndex().stream().mapToInt(Integer::intValue).toArray(),
hEntry.getValue().stream().mapToInt(Integer::intValue).toArray());
} else {
this.histogramDAO.put(tableID, blockId, colId, getArrayFromMapHEntry(hEntry));
}
}
});
}
private void storeEnum(byte tableId,
boolean compression,
long blockId,
EStore eStore) {
if (eStore.getInitialCapacity() > 0) {
eStore.getMapping().forEach((colId, iMapping) -> {
int[] values = new int[eStore.getRawDataEColumn().get(iMapping).size()];
AtomicInteger counter = new AtomicInteger(0);
eStore.getRawDataEColumn().get(iMapping)
.forEach((enumKey, enumValue) -> values[counter.getAndAdd(1)] = enumKey);
try {
this.enumDAO.putEColumn(tableId, blockId, colId, values,
getByteFromList(eStore.getRawData().get(iMapping)), compression);
} catch (IOException e) {
log.catching(e);
throw new RuntimeException(e);
}
});
}
}
private void storeData(byte tableId,
boolean compression,
long blockId,
int colRawDataLongCount,
List<Integer> rawDataLongMapping,
List<List<Long>> rawDataLong,
int colRawDataDoubleCount,
List<Integer> rawDataDoubleMapping,
List<List<Double>> rawDataDouble,
int colRawDataStringCount,
List<Integer> rawDataStringMapping,
List<List<String>> rawDataString) {
/* Store raw data entity */
if (compression) {
try {
rawDAO.putCompressed(tableId, blockId,
Collections.emptyList(), Collections.emptyList(),
Collections.emptyList(), Collections.emptyList(),
rawDataLongMapping, rawDataLong,
Collections.emptyList(), Collections.emptyList(),
rawDataDoubleMapping, rawDataDouble,
rawDataStringMapping, rawDataString);
} catch (IOException e) {
throw new RuntimeException(e);
}
return;
}
if (colRawDataLongCount > 0) {
this.rawDAO.putLong(tableId, blockId, rawDataLongMapping.stream().mapToInt(i -> i)
.toArray(), getArrayLong(rawDataLong));
}
if (colRawDataDoubleCount > 0) {
this.rawDAO.putDouble(tableId, blockId, rawDataDoubleMapping.stream().mapToInt(i -> i)
.toArray(), getArrayDouble(rawDataDouble));
}
if (colRawDataStringCount > 0) {
this.rawDAO.putString(tableId, blockId, rawDataStringMapping.stream().mapToInt(i -> i)
.toArray(), getArrayString(rawDataString));
}
}
}