Kenmegne
7 days ago 23a46b4be35277e06ec89f48730eeb694e686be8
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
/*
 * 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;
    }
}