/*
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
*/
|
package com.megatim.fdxcommons.tools.utils;
|
|
import com.megatim.fdxcommons.model.integration.json.JsonStructure;
|
import com.megatim.fdxcommons.tools.database.exceptions.BadDataValueException;
|
import com.megatim.fdxcommons.tools.database.exceptions.ColumnNotFoundException;
|
import com.megatim.fdxcommons.tools.database.exceptions.LocalDateTimeValueParseError;
|
import com.megatim.fdxcommons.tools.database.queries.metadata.JsonFdxParsedDataRow;
|
import java.util.ArrayList;
|
import java.util.LinkedHashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.concurrent.ForkJoinTask;
|
import java.util.concurrent.RecursiveTask;
|
import java.util.logging.Level;
|
import java.util.logging.Logger;
|
|
/**
|
*
|
* @author ASUS
|
*/
|
public class JsonFdxParsedDataAction extends RecursiveTask<List<Map<String, Object>>> {
|
|
private final List<LinkedHashMap<String, Object>> sourceData;
|
private final JsonStructure jsonStructure;
|
private final List<Map<String, Object>> cachedData = new ArrayList<>();
|
private final int THRESHOLD = 5000;
|
|
public JsonFdxParsedDataAction(List<LinkedHashMap<String, Object>> sourceData, JsonStructure jsonStructure) {
|
this.sourceData = sourceData;
|
this.jsonStructure = jsonStructure;
|
}
|
|
@Override
|
protected List<Map<String, Object>> compute() {
|
if (sourceData.size() > THRESHOLD) {
|
ForkJoinTask
|
.invokeAll(createSubtasks())
|
.stream()
|
.map(JsonFdxParsedDataAction::join)
|
.forEach(l -> cachedData.addAll(l));
|
return cachedData;
|
} else {
|
if (cachedData.isEmpty()) {
|
for (Map<String, Object> data : sourceData) {
|
try {
|
cachedData.add(new JsonFdxParsedDataRow(data, jsonStructure).dataRow());
|
} catch (ColumnNotFoundException | BadDataValueException | LocalDateTimeValueParseError ex) {
|
Logger.getLogger(JsonFdxParsedDataAction.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
|
throw new RuntimeException(ex);
|
}
|
}
|
}
|
return cachedData;
|
}
|
}
|
|
private List<JsonFdxParsedDataAction> createSubtasks() {
|
List<JsonFdxParsedDataAction> subtasks = new ArrayList<>();
|
int size = sourceData.size() / 4;
|
int remainder = sourceData.size() % 4;
|
|
subtasks.add(new JsonFdxParsedDataAction(sourceData.subList(0, size), jsonStructure));
|
subtasks.add(new JsonFdxParsedDataAction(sourceData.subList(size, size * 2), jsonStructure));
|
subtasks.add(new JsonFdxParsedDataAction(sourceData.subList(size * 2, size * 3), jsonStructure));
|
subtasks.add(new JsonFdxParsedDataAction(sourceData.subList(size * 3, size * 4 + remainder), jsonStructure));
|
|
return subtasks;
|
}
|
}
|