|
|
@@ -17,6 +17,7 @@ import java.util.concurrent.*;
|
|
|
|
|
|
import static com.uas.search.service.impl.IndexServiceImpl.PAGE_SIZE;
|
|
|
import static com.uas.search.service.impl.IndexServiceImpl.SINGLE_FILE_MAX_SIZE;
|
|
|
+import static com.uas.search.support.DownloadHelper.ValidateResult.*;
|
|
|
|
|
|
|
|
|
/**
|
|
|
@@ -85,7 +86,7 @@ public class DownloadHelper<T> {
|
|
|
/**
|
|
|
* 下载完成后,是否对结果进行校验
|
|
|
*/
|
|
|
- private boolean validateResult;
|
|
|
+ private ValidateResult validateResult;
|
|
|
|
|
|
/**
|
|
|
* 线程管理
|
|
|
@@ -119,7 +120,7 @@ public class DownloadHelper<T> {
|
|
|
* @param downloadService 下载的实现
|
|
|
*/
|
|
|
public DownloadHelper(Integer threadSize, Integer startFileIndex, Integer endFileIndex, String tableName, String sortField, JpaRepository<T, Long> dao, DownloadService<T> downloadService) {
|
|
|
- this(threadSize, startFileIndex, endFileIndex, tableName, sortField, dao, downloadService, true);
|
|
|
+ this(threadSize, startFileIndex, endFileIndex, tableName, sortField, dao, downloadService, CURRENT);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -132,7 +133,7 @@ public class DownloadHelper<T> {
|
|
|
* @param downloadService 下载的实现
|
|
|
* @param validateResult 下载完成后,是否对结果进行校验
|
|
|
*/
|
|
|
- public DownloadHelper(Integer threadSize, Integer startFileIndex, Integer endFileIndex, String tableName, String sortField, JpaRepository<T, Long> dao, DownloadService<T> downloadService, boolean validateResult) {
|
|
|
+ public DownloadHelper(Integer threadSize, Integer startFileIndex, Integer endFileIndex, String tableName, String sortField, JpaRepository<T, Long> dao, DownloadService<T> downloadService, ValidateResult validateResult) {
|
|
|
if (threadSize == null || threadSize < MIN_THREAD_SIZE || threadSize > MAX_THREAD_SIZE) {
|
|
|
throw new IllegalArgumentException("threadSize is between " + MIN_THREAD_SIZE + " and " + MAX_THREAD_SIZE);
|
|
|
}
|
|
|
@@ -168,7 +169,7 @@ public class DownloadHelper<T> {
|
|
|
completionService.submit(getTask(i, threadSize, startFileIndex + i, endFileIndex, tableName, sortField, dao));
|
|
|
}
|
|
|
waitResult();
|
|
|
- if (validateResult) {
|
|
|
+ if (validateResult != null && validateResult != NONE) {
|
|
|
// 对结果进行校验,只校验一定的次数,防止因特殊原因,某些数据始终无法成功下载,陷入死循环
|
|
|
int retry = 1;
|
|
|
List<Integer> missingFiles;
|
|
|
@@ -177,7 +178,7 @@ public class DownloadHelper<T> {
|
|
|
logger.error("第 " + retry + " 次校验,下载遗失的文件:" + missingFiles);
|
|
|
List<DownloadHelper<T>> downloadHelpers = new ArrayList<>();
|
|
|
for (Integer missingFile : missingFiles) {
|
|
|
- downloadHelpers.add(new DownloadHelper<T>(1, missingFile, missingFile, tableName, sortField, dao, downloadService, false));
|
|
|
+ downloadHelpers.add(new DownloadHelper<T>(1, missingFile, missingFile, tableName, sortField, dao, downloadService, NONE));
|
|
|
}
|
|
|
for (DownloadHelper downloadHelper : downloadHelpers) {
|
|
|
downloadHelper.getResult();
|
|
|
@@ -260,9 +261,21 @@ public class DownloadHelper<T> {
|
|
|
* @return 未下载成功的文件
|
|
|
*/
|
|
|
private List<Integer> validateResult() {
|
|
|
- // 期待的文件数目
|
|
|
int totalFiles = (int) Math.ceil(totalElements / (1.0 * SINGLE_FILE_MAX_SIZE));
|
|
|
- final int expectedEndFileIndex = endFileIndex < totalFiles ? endFileIndex : totalFiles;
|
|
|
+ // 期待的起始、结束文件
|
|
|
+ final int expectStartFileIndex;
|
|
|
+ final int expectEndFileIndex;
|
|
|
+ if (validateResult == CURRENT) {
|
|
|
+ // 校验本次下载的文件
|
|
|
+ expectStartFileIndex = startFileIndex;
|
|
|
+ expectEndFileIndex = endFileIndex < totalFiles ? endFileIndex : totalFiles;
|
|
|
+ } else if (validateResult == ALL) {
|
|
|
+ // 校验所有文件
|
|
|
+ expectStartFileIndex = DEFAULT_START_FILE_INDEX;
|
|
|
+ expectEndFileIndex = totalFiles;
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
List<Integer> missingFiles = new ArrayList<>();
|
|
|
|
|
|
File[] files = new File(SearchUtils.getDataPath(tableName)).listFiles(new FileFilter() {
|
|
|
@@ -273,7 +286,7 @@ public class DownloadHelper<T> {
|
|
|
return false;
|
|
|
}
|
|
|
int id = parseName(pathname);
|
|
|
- return id >= startFileIndex && id <= expectedEndFileIndex;
|
|
|
+ return id >= expectStartFileIndex && id <= expectEndFileIndex;
|
|
|
}
|
|
|
});
|
|
|
if (ArrayUtils.isEmpty(files)) {
|
|
|
@@ -282,7 +295,7 @@ public class DownloadHelper<T> {
|
|
|
// 将文件按名称排序
|
|
|
sort(files);
|
|
|
// 默认把所有文件视为未下载成功
|
|
|
- for (int i = startFileIndex; i <= expectedEndFileIndex; i++) {
|
|
|
+ for (int i = expectStartFileIndex; i <= expectEndFileIndex; i++) {
|
|
|
missingFiles.add(i);
|
|
|
}
|
|
|
result = 0L;
|
|
|
@@ -357,4 +370,20 @@ public class DownloadHelper<T> {
|
|
|
files[j] = temp;
|
|
|
}
|
|
|
|
|
|
+ public enum ValidateResult {
|
|
|
+ /**
|
|
|
+ * 不校验
|
|
|
+ */
|
|
|
+ NONE,
|
|
|
+ /**
|
|
|
+ * 校验所有文件
|
|
|
+ */
|
|
|
+ ALL,
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 只校验本次下载的文件
|
|
|
+ */
|
|
|
+ CURRENT
|
|
|
+ }
|
|
|
+
|
|
|
}
|