|
|
@@ -460,97 +460,41 @@ public class IndexServiceImpl implements IndexService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Long downloadGoods(Integer startFileIndex) {
|
|
|
- int fileIndex = 1;
|
|
|
- if (startFileIndex != null) {
|
|
|
- if (startFileIndex < 1) {
|
|
|
- throw new SearchException("startFileIndex不小于1");
|
|
|
- } else {
|
|
|
- fileIndex = startFileIndex;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- Long startTime = new Date().getTime();
|
|
|
- logger.info("下载批次... ");
|
|
|
-
|
|
|
- Sort sort = new Sort(Sort.Direction.ASC, "id");
|
|
|
- // 分页获取数据
|
|
|
- PageParams pageParams = new PageParams();
|
|
|
- pageParams.setPage((fileIndex - 1) * SINGLE_FILE_MAX_SIZE / PAGE_SIZE + 1);
|
|
|
- pageParams.setSize(PAGE_SIZE);
|
|
|
- PageInfo pageInfo = new PageInfo(pageParams, sort);
|
|
|
- Page<Component> pageResult = componentDao.findAll(pageInfo);
|
|
|
-
|
|
|
- // 数据库中数据的总数目
|
|
|
- if (pageResult.getTotalElements() < (fileIndex - 1) * SINGLE_FILE_MAX_SIZE) {
|
|
|
- throw new SearchException("startFileIndex不超过"
|
|
|
- + ((int) Math.ceil(pageResult.getTotalElements() / (1.0 * SINGLE_FILE_MAX_SIZE))));
|
|
|
- }
|
|
|
- long totalElements = pageResult.getTotalElements() - (fileIndex - 1) * SINGLE_FILE_MAX_SIZE;
|
|
|
- logger.info("发现数据:" + totalElements + "条");
|
|
|
- // 已翻页的数据数目
|
|
|
- Long size = 0L;
|
|
|
- PrintWriter printWriter = null;
|
|
|
- int count = 0;
|
|
|
- try {
|
|
|
- String goodsDataPath = SearchUtils.getDataPath(SearchConstants.GOODS_TABLE_NAME);
|
|
|
- File file = new File(goodsDataPath);
|
|
|
- if (!file.exists()) {
|
|
|
- file.mkdirs();
|
|
|
- }
|
|
|
- printWriter = new PrintWriter(goodsDataPath + "/" + fileIndex + ".txt");
|
|
|
- while (totalElements > size) {
|
|
|
- // 一个文件存放100000条数据,一旦超过,写入新的文件
|
|
|
- if (count == SINGLE_FILE_MAX_SIZE) {
|
|
|
- count = 0;
|
|
|
- printWriter.flush();
|
|
|
- printWriter.close();
|
|
|
- fileIndex++;
|
|
|
- printWriter = new PrintWriter(goodsDataPath + "/" + fileIndex + ".txt");
|
|
|
- }
|
|
|
- List<Component> content = pageResult.getContent();
|
|
|
- for (Component element : content) {
|
|
|
- List<Goods> goodses = goodsDao.findByComponent(element);
|
|
|
- for (Goods goods : goodses) {
|
|
|
- printWriter.println(JSONObject.toJSONString(goods));
|
|
|
- }
|
|
|
- count++;
|
|
|
- }
|
|
|
- size += content.size();
|
|
|
- logger.info(String.format("Downloaded...................%.2f%%", size * 100.0 / totalElements));
|
|
|
-
|
|
|
- pageParams.setPage(pageParams.getPage() + 1);
|
|
|
- pageInfo = new PageInfo(pageParams, sort);
|
|
|
- pageResult = componentDao.findAll(pageInfo);
|
|
|
- }
|
|
|
- printWriter.flush();
|
|
|
- printWriter.close();
|
|
|
- } catch (FileNotFoundException e) {
|
|
|
- logger.error("", e);
|
|
|
- }
|
|
|
-
|
|
|
- Long endTime = new Date().getTime();
|
|
|
- logger.info(String.format("下载数据%s条,耗时%.2fs\n ", totalElements, (endTime - startTime) / 1000.0));
|
|
|
- return endTime - startTime;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void multiDownloadGoods(Integer number, Integer startFileIndex, Integer endFileIndex) {
|
|
|
- number = number == null || number < 1 ? 1 : number;
|
|
|
+ public void multiDownloadGoods(Integer threads, Integer startFileIndex, Integer endFileIndex) {
|
|
|
+ threads = threads == null || threads < 1 ? 1 : threads;
|
|
|
startFileIndex = startFileIndex == null || startFileIndex < 1 ? 1 : startFileIndex;
|
|
|
endFileIndex = endFileIndex == null || endFileIndex < 1 ? 1024 * 1024 * 1024 : endFileIndex;
|
|
|
- for (int i = 1; i <= number; i++) {
|
|
|
- new Thread(new DownloadTread("Thread-" + i, number, startFileIndex + i - 1, endFileIndex)).start();
|
|
|
+ for (int i = 1; i <= threads; i++) {
|
|
|
+ new Thread(new DownloadGoodsTread("Thread-" + i, threads, startFileIndex + i - 1, endFileIndex)).start();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- class DownloadTread implements Runnable {
|
|
|
+ /**
|
|
|
+ * 下载批次的线程
|
|
|
+ */
|
|
|
+ private class DownloadGoodsTread implements Runnable {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 线程名称
|
|
|
+ */
|
|
|
private String name;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增文件时,文件 id 的自增步长,(即线程数量)
|
|
|
+ */
|
|
|
private int step;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 开始的文件
|
|
|
+ */
|
|
|
private int startFileIndex;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 开始的文件
|
|
|
+ */
|
|
|
private int endFileIndex;
|
|
|
|
|
|
- public DownloadTread(String name, int step, int startFileIndex, int endFileIndex) {
|
|
|
+ public DownloadGoodsTread(String name, int step, int startFileIndex, int endFileIndex) {
|
|
|
this.name = name;
|
|
|
this.step = step;
|
|
|
this.startFileIndex = startFileIndex;
|
|
|
@@ -591,6 +535,7 @@ public class IndexServiceImpl implements IndexService {
|
|
|
PrintWriter printWriter = new PrintWriter(goodsDataPath + "/" + String.format("%05d", startFileIndex) + ".txt");
|
|
|
List<Component> content = pageResult.getContent();
|
|
|
for (Component element : content) {
|
|
|
+ // 器件作为主体,再下载批次
|
|
|
List<Goods> goodses = goodsDao.findByComponent(element);
|
|
|
for (Goods goods : goodses) {
|
|
|
printWriter.println(JSONObject.toJSONString(goods));
|