From 4e493831abf777587744d1639f9726ee90a82654 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E4=BC=9F=E6=9D=B0?= Date: Fri, 12 Aug 2022 14:52:01 +0800 Subject: [PATCH 1/3] Support loading data for specific range --- src/LoadData/LoadData.java | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/LoadData/LoadData.java b/src/LoadData/LoadData.java index 1cbf5c5..5f963bb 100644 --- a/src/LoadData/LoadData.java +++ b/src/LoadData/LoadData.java @@ -11,9 +11,14 @@ import java.sql.*; import java.util.*; import java.io.*; import java.lang.Integer; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.regex.Pattern; +import java.util.stream.IntStream; public class LoadData { + private static final Pattern RANGE_PATTERN = Pattern.compile("(\\d+|\\d+-\\d+)(,(\\d+|\\d+-\\d+))*"); + private static Properties ini = new Properties(); private static String db; private static Properties dbProps; @@ -23,7 +28,7 @@ public class LoadData private static int numWarehouses; private static int numWorkers; - private static int nextJob = 0; + private static Queue nextJob = new ConcurrentLinkedQueue<>(); private static Object nextJobLock = new Object(); private static LoadDataWorker[] workers; @@ -100,6 +105,25 @@ public class LoadData fileLocation = iniGetString("fileLocation"); csvNullValue = iniGetString("csvNullValue", "NULL"); + String loadDataRange = iniGetString("loadDataRange", "").trim(); + if (loadDataRange.isEmpty()) { + IntStream.rangeClosed(0, numWarehouses).forEach(nextJob::offer); + } else { + if (!RANGE_PATTERN.matcher(loadDataRange).matches()) { + throw new IllegalArgumentException("Invalid loadDataRange expression. Example: loadDataRange=0,3-6,9 equals to (0,3,4,5,6,9). 0 means load ITEM"); + } + Arrays.stream(loadDataRange.split(",")).flatMapToInt(each -> { + if (each.contains("-")) { + String[] split = each.split("-"); + return IntStream.rangeClosed(Integer.parseInt(split[0]), Integer.parseInt(split[1])); + } + return IntStream.of(Integer.parseInt(each)); + }).distinct().sorted().forEachOrdered(nextJob::offer); + if (nextJob.stream().anyMatch(each -> each < 0 || each > numWarehouses)) { + throw new IllegalArgumentException("loadDataRange must distribute in [0, totalWarehouses]. 0 means load ITEM"); + } + } + /* * If CSV files are requested, open them all. */ @@ -316,10 +340,10 @@ public class LoadData synchronized(nextJobLock) { - if (nextJob > numWarehouses) + if (nextJob.isEmpty()) job = -1; else - job = nextJob++; + job = nextJob.poll(); } return job; -- Gitee From 4e8ce1ff8a259192f67a8b3a9e25043604721ddc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E4=BC=9F=E6=9D=B0?= Date: Fri, 12 Aug 2022 15:06:38 +0800 Subject: [PATCH 2/3] Trim blank characters in loadDataRange --- src/LoadData/LoadData.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LoadData/LoadData.java b/src/LoadData/LoadData.java index 5f963bb..d1af4d9 100644 --- a/src/LoadData/LoadData.java +++ b/src/LoadData/LoadData.java @@ -105,7 +105,7 @@ public class LoadData fileLocation = iniGetString("fileLocation"); csvNullValue = iniGetString("csvNullValue", "NULL"); - String loadDataRange = iniGetString("loadDataRange", "").trim(); + String loadDataRange = iniGetString("loadDataRange", "").replaceAll("\\s", ""); if (loadDataRange.isEmpty()) { IntStream.rangeClosed(0, numWarehouses).forEach(nextJob::offer); } else { -- Gitee From aebf146715bd43380fd2fd325fc430a8d05387a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E4=BC=9F=E6=9D=B0?= Date: Tue, 16 Aug 2022 11:01:17 +0800 Subject: [PATCH 3/3] Change ConcurrentLinkedQueue to LinkedList --- src/LoadData/LoadData.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/LoadData/LoadData.java b/src/LoadData/LoadData.java index d1af4d9..01da0a2 100644 --- a/src/LoadData/LoadData.java +++ b/src/LoadData/LoadData.java @@ -11,7 +11,6 @@ import java.sql.*; import java.util.*; import java.io.*; import java.lang.Integer; -import java.util.concurrent.ConcurrentLinkedQueue; import java.util.regex.Pattern; import java.util.stream.IntStream; @@ -28,7 +27,7 @@ public class LoadData private static int numWarehouses; private static int numWorkers; - private static Queue nextJob = new ConcurrentLinkedQueue<>(); + private static final Queue nextJob = new LinkedList<>(); private static Object nextJobLock = new Object(); private static LoadDataWorker[] workers; -- Gitee