/* * 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>> { private final List> sourceData; private final JsonStructure jsonStructure; private final List> cachedData = new ArrayList<>(); private final int THRESHOLD = 5000; public JsonFdxParsedDataAction(List> sourceData, JsonStructure jsonStructure) { this.sourceData = sourceData; this.jsonStructure = jsonStructure; } @Override protected List> 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 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 createSubtasks() { List 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; } }