Просмотр исходного кода

支持从指定的第startFileIndex个文件开始下载器件数据

sunyj 9 лет назад
Родитель
Сommit
ab3a458542

+ 2 - 2
search-console/src/main/java/com/uas/search/console/controller/IndexController.java

@@ -87,8 +87,8 @@ public class IndexController {
 
 	@RequestMapping("/downloadComponentData")
 	@ResponseBody
-	public String downloadComponentData() {
-		Long size = indexService.downloadComponentData();
+	public String downloadComponentData(Integer startFileIndex) {
+		Long size = indexService.downloadComponentData(startFileIndex);
 		logger.info("Downloading finished, components' size " + size);
 		return "Components' size: " + size;
 	}

+ 3 - 1
search-console/src/main/java/com/uas/search/console/service/IndexService.java

@@ -20,9 +20,11 @@ public interface IndexService {
 	/**
 	 * 下载component数据至本地
 	 * 
+	 * @param startFileIndex
+	 *            从第startFileIndex(从1开始)个文件继续下载
 	 * @return component记录的数目
 	 */
-	public Long downloadComponentData();
+	public Long downloadComponentData(Integer startFileIndex);
 
 	/**
 	 * 将新对象添加在lucene索引中

+ 21 - 7
search-console/src/main/java/com/uas/search/console/service/impl/IndexServiceImpl.java

@@ -224,13 +224,16 @@ public class IndexServiceImpl implements IndexService {
 	}
 
 	@Override
-	public Long downloadComponentData() {
+	public Long downloadComponentData(Integer startFileIndex) {
 		logger.info("正在下载器件数据...");
 
 		// 清空data文件夹
 		File dataDir = new File(SearchConstants.COMPONENT_DATA_DIR);
-		if (dataDir.isDirectory()) {
-			clearDir(dataDir);
+		// 若startFileIndex无意义,全部重新下载
+		if (startFileIndex == null || startFileIndex < 1) {
+			if (dataDir.isDirectory()) {
+				clearDir(dataDir);
+			}
 		}
 		if (!dataDir.exists()) {
 			dataDir.mkdir();
@@ -239,6 +242,10 @@ public class IndexServiceImpl implements IndexService {
 		Long size = 0L;
 		PageParams params = new PageParams();
 		int page = 1;
+		// 重新计算开始的页码
+		if (startFileIndex != null && startFileIndex >= 1) {
+			page = SINGLE_FILE_MAX_SIZE / PAGE_SIZE * (startFileIndex - 1) + 1;
+		}
 		params.setCount(PAGE_SIZE);
 		params.setPage(page);
 
@@ -246,14 +253,21 @@ public class IndexServiceImpl implements IndexService {
 		PageInfo info = new PageInfo(params);
 		Page<ComponentSimpleInfo> pageResult = componentDao.findAll(info);
 		long totalElements = pageResult.getTotalElements();
+		int fileIndex = 1;
+		// 重新计算已下载的数据大小
+		if (startFileIndex != null && startFileIndex >= 1) {
+			size = Math.min(SINGLE_FILE_MAX_SIZE * (startFileIndex - 1), totalElements);
+			fileIndex = startFileIndex;
+		}
 		logger.info("Number of components: " + totalElements);
 
 		PrintWriter printWriter = null;
 		try {
-			String dataFilePath = SearchConstants.COMPONENT_DATA_DIR + "component_1.json";
-			printWriter = new PrintWriter(dataFilePath);
-			logger.info("Writing data into " + dataFilePath);
-			int fileIndex = 1;
+			String dataFilePath = SearchConstants.COMPONENT_DATA_DIR + "component_" + fileIndex + ".json";
+			if(totalElements > size){
+				printWriter = new PrintWriter(dataFilePath);
+				logger.info("Writing data into " + dataFilePath);
+			}
 			int dataCount = 0;
 			while (totalElements > size) {
 				// 一个文件存放SINGLE_FILE_MAX_SIZE条数据,一旦超过,写入新的文件