|
|
@@ -14,6 +14,8 @@ import com.uas.search.jms.JmsListener;
|
|
|
import com.uas.search.jms.QueueMessageParser;
|
|
|
import com.uas.search.model.*;
|
|
|
import com.uas.search.service.IndexService;
|
|
|
+import com.uas.search.support.DownloadHelper;
|
|
|
+import com.uas.search.support.DownloadService;
|
|
|
import com.uas.search.support.IndexSearcherManager;
|
|
|
import com.uas.search.support.IndexWriterManager;
|
|
|
import com.uas.search.util.FileUtils;
|
|
|
@@ -95,7 +97,7 @@ public class IndexServiceImpl implements IndexService {
|
|
|
/**
|
|
|
* 从数据库获取数据时的分页大小
|
|
|
*/
|
|
|
- private static final int PAGE_SIZE = 1000;
|
|
|
+ public static final int PAGE_SIZE = 1000;
|
|
|
|
|
|
/**
|
|
|
* 单个文件存储的最大数据数目,需是PAGE_SIZE的整数倍
|
|
|
@@ -490,111 +492,23 @@ public class IndexServiceImpl implements IndexService {
|
|
|
if (threads > druidDBConfiguration.getMaxActive()) {
|
|
|
throw new IllegalArgumentException("线程数量不可超过 " + druidDBConfiguration.getMaxActive());
|
|
|
}
|
|
|
- startFileIndex = startFileIndex == null || startFileIndex < 1 ? 1 : startFileIndex;
|
|
|
- if (startFileIndex == 1 && endFileIndex == null) {
|
|
|
- // 删除旧的文件
|
|
|
- FileUtils.deleteSubFiles(new File(SearchUtils.getDataPath(tableName)));
|
|
|
- }
|
|
|
- endFileIndex = endFileIndex == null || endFileIndex < 1 ? 1024 * 1024 * 1024 : endFileIndex;
|
|
|
- for (int i = 1; i <= threads; i++) {
|
|
|
- if (tableName.equals(COMPONENT_TABLE_NAME)) {
|
|
|
- new Thread(new DownloadComponentTread(i, threads, startFileIndex + i - 1, endFileIndex)).start();
|
|
|
- } else if (tableName.equals(GOODS_TABLE_NAME)) {
|
|
|
- new Thread(new DownloadGoodsTread(i, threads, startFileIndex + i - 1, endFileIndex)).start();
|
|
|
- } else {
|
|
|
- throw new IllegalArgumentException("多线程下载不支持该表:" + tableName);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 下载器件的线程
|
|
|
- */
|
|
|
- private class DownloadComponentTread implements Runnable {
|
|
|
-
|
|
|
- /**
|
|
|
- * 线程名称
|
|
|
- */
|
|
|
- private int id;
|
|
|
-
|
|
|
- /**
|
|
|
- * 新增文件时,文件 id 的自增步长,(即线程数量)
|
|
|
- */
|
|
|
- private int step;
|
|
|
-
|
|
|
- /**
|
|
|
- * 开始的文件
|
|
|
- */
|
|
|
- private int startFileIndex;
|
|
|
-
|
|
|
- /**
|
|
|
- * 开始的文件
|
|
|
- */
|
|
|
- private int endFileIndex;
|
|
|
-
|
|
|
- public DownloadComponentTread(int id, int step, int startFileIndex, int endFileIndex) {
|
|
|
- this.id = id;
|
|
|
- this.step = step;
|
|
|
- this.startFileIndex = startFileIndex;
|
|
|
- this.endFileIndex = endFileIndex;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- String name = "Thread-" + id;
|
|
|
- try {
|
|
|
- if (endFileIndex < startFileIndex) {
|
|
|
- logger.error(name + " fileIndex 不可超过 : endFileIndex=" + endFileIndex);
|
|
|
- return;
|
|
|
- }
|
|
|
- Long startTime = new Date().getTime();
|
|
|
- logger.info(name + " 下载器件... ");
|
|
|
-
|
|
|
- Sort sort = new Sort(Sort.Direction.ASC, "id");
|
|
|
- // 分页获取数据
|
|
|
- PageParams pageParams = new PageParams();
|
|
|
- pageParams.setPage(startFileIndex);
|
|
|
- pageParams.setSize(PAGE_SIZE);
|
|
|
- PageInfo pageInfo = new PageInfo(pageParams, sort);
|
|
|
- Page<Component> pageResult = componentDao.findAll(pageInfo);
|
|
|
- if(id == 1){
|
|
|
- logger.info(name + " 发现数据 " + pageResult.getTotalElements() + " 条");
|
|
|
- }
|
|
|
-
|
|
|
- int totalPages = pageResult.getTotalPages();
|
|
|
- if (totalPages < startFileIndex) {
|
|
|
- logger.error(name + " fileIndex 不可超过 : totalPages=" + totalPages);
|
|
|
- return;
|
|
|
- }
|
|
|
- // 已翻页的数据数目
|
|
|
- Long size = 0L;
|
|
|
- String goodsDataPath = SearchUtils.getDataPath(COMPONENT_TABLE_NAME);
|
|
|
- File file = new File(goodsDataPath);
|
|
|
- if (!file.exists()) {
|
|
|
- file.mkdirs();
|
|
|
- }
|
|
|
- while (totalPages >= startFileIndex && endFileIndex >= startFileIndex) {
|
|
|
- String componentFileName = String.format("%010d", startFileIndex) + ".txt";
|
|
|
- PrintWriter printWriter = new PrintWriter(goodsDataPath + "/" + componentFileName);
|
|
|
- List<Component> content = pageResult.getContent();
|
|
|
- for (Component element : content) {
|
|
|
- printWriter.println(JSONObject.toJSONString(element));
|
|
|
- }
|
|
|
- size += content.size();
|
|
|
- logger.info(name + " " + componentFileName + " - Downloaded..................." + size);
|
|
|
|
|
|
- printWriter.flush();
|
|
|
- printWriter.close();
|
|
|
- startFileIndex += step;
|
|
|
- pageParams.setPage(startFileIndex);
|
|
|
- pageInfo = new PageInfo(pageParams, sort);
|
|
|
- pageResult = componentDao.findAll(pageInfo);
|
|
|
- }
|
|
|
-
|
|
|
- logger.info(String.format("%s 下载完成,耗时%.2fs\n ", name, (new Date().getTime() - startTime) / 1000.0));
|
|
|
- } catch (Throwable e) {
|
|
|
- logger.error(name + " 器件下载失败", e);
|
|
|
+ if (tableName.equals(COMPONENT_TABLE_NAME)) {
|
|
|
+ DownloadHelper<Component> downloadHelper = new DownloadHelper<>(threads, startFileIndex, endFileIndex, tableName, "id",componentDao, new DownloadService<Component>());
|
|
|
+ long result = downloadHelper.getResult();
|
|
|
+ logger.info("totalSize = " + result);
|
|
|
+ } else if (tableName.equals(GOODS_TABLE_NAME)) {
|
|
|
+ startFileIndex = startFileIndex == null || startFileIndex < 1 ? 1 : startFileIndex;
|
|
|
+ if (startFileIndex == 1 && endFileIndex == null) {
|
|
|
+ // 删除旧的文件
|
|
|
+ FileUtils.deleteSubFiles(new File(SearchUtils.getDataPath(tableName)));
|
|
|
}
|
|
|
+ endFileIndex = endFileIndex == null || endFileIndex < 1 ? 1024 * 1024 * 1024 : endFileIndex;
|
|
|
+ for (int i = 1; i <= threads; i++) {
|
|
|
+ new Thread(new DownloadGoodsTread(i, threads, startFileIndex + i - 1, endFileIndex)).start();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw new IllegalArgumentException("多线程下载不支持该表:" + tableName);
|
|
|
}
|
|
|
}
|
|
|
|