|
|
@@ -66,9 +66,14 @@ public class IndexServiceImpl implements IndexService {
|
|
|
|
|
|
private Logger logger = LoggerFactory.getLogger(IndexServiceImpl.class);
|
|
|
|
|
|
+ /**
|
|
|
+ * 从数据库获取数据时的分页大小
|
|
|
+ */
|
|
|
private static final int PAGE_SIZE = 1000;
|
|
|
|
|
|
- // 单个文件存储的最大数据数目
|
|
|
+ /**
|
|
|
+ * 单个文件存储的最大数据数目,需是PAGE_SIZE的整数倍
|
|
|
+ */
|
|
|
public static final int SINGLE_FILE_MAX_SIZE = 100000;
|
|
|
|
|
|
/**
|
|
|
@@ -257,7 +262,7 @@ public class IndexServiceImpl implements IndexService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Long downloadDataFromDatabase(List<Table_name> tableNames) {
|
|
|
+ public Long downloadDataFromDatabase(List<Table_name> tableNames, Integer startFileIndex) {
|
|
|
Long startTime = new Date().getTime();
|
|
|
// 参数为空,则下载所有需要建立索引的表的数据
|
|
|
if (CollectionUtils.isEmpty(tableNames)) {
|
|
|
@@ -266,7 +271,9 @@ public class IndexServiceImpl implements IndexService {
|
|
|
}
|
|
|
Long size = 0L;
|
|
|
for (Table_name tableName : tableNames) {
|
|
|
- size += downloadDataFromDatabase(ClassAndTableNameUtils.toClass(tableName));
|
|
|
+ // 指定的表数目为1时才起作用
|
|
|
+ size += downloadDataFromDatabase(ClassAndTableNameUtils.toClass(tableName),
|
|
|
+ tableNames.size() == 1 ? startFileIndex : null);
|
|
|
}
|
|
|
Long endTime = new Date().getTime();
|
|
|
logger.info(size + "条,耗时 " + (endTime - startTime) + " ms\n");
|
|
|
@@ -279,7 +286,15 @@ public class IndexServiceImpl implements IndexService {
|
|
|
* @param clazz
|
|
|
* @return 下载的数据的数目
|
|
|
*/
|
|
|
- private <T> Long downloadDataFromDatabase(Class<T> clazz) {
|
|
|
+ private <T> Long downloadDataFromDatabase(Class<T> clazz, Integer startFileIndex) {
|
|
|
+ int fileIndex = 1;
|
|
|
+ if (startFileIndex != null) {
|
|
|
+ if (startFileIndex < 1) {
|
|
|
+ throw new SearchException("startFileIndex需大于1");
|
|
|
+ } else {
|
|
|
+ fileIndex = startFileIndex;
|
|
|
+ }
|
|
|
+ }
|
|
|
Long startTime = new Date().getTime();
|
|
|
Table_name tableName = ClassAndTableNameUtils.toTableName(clazz);
|
|
|
logger.info("Download... " + tableName.value());
|
|
|
@@ -287,17 +302,16 @@ public class IndexServiceImpl implements IndexService {
|
|
|
|
|
|
// 分页获取数据
|
|
|
PageParams pageParams = new PageParams();
|
|
|
- pageParams.setPage(1);
|
|
|
+ pageParams.setPage((fileIndex - 1) * SINGLE_FILE_MAX_SIZE / PAGE_SIZE + 1);
|
|
|
pageParams.setCount(PAGE_SIZE);
|
|
|
PageInfo pageInfo = new PageInfo(pageParams);
|
|
|
Page<T> pageResult = dao.findAll(pageInfo);
|
|
|
|
|
|
// 数据库中数据的总数目
|
|
|
- long totalElements = pageResult.getTotalElements();
|
|
|
+ long totalElements = pageResult.getTotalElements() - (fileIndex - 1) * SINGLE_FILE_MAX_SIZE;
|
|
|
logger.info("发现数据:" + totalElements + "条");
|
|
|
// 已翻页的数据数目
|
|
|
Long size = 0L;
|
|
|
- int fileIndex = 1;
|
|
|
PrintWriter printWriter = null;
|
|
|
int count = 0;
|
|
|
try {
|
|
|
@@ -308,8 +322,8 @@ public class IndexServiceImpl implements IndexService {
|
|
|
printWriter = new PrintWriter(SearchUtils.getDataPath(tableName) + "/" + fileIndex + ".txt");
|
|
|
while (totalElements > size) {
|
|
|
// 一个文件存放SINGLE_FILE_MAX_SIZE条数据,一旦超过,写入新的文件
|
|
|
- if (count > SINGLE_FILE_MAX_SIZE) {
|
|
|
- count = 1;
|
|
|
+ if (count == SINGLE_FILE_MAX_SIZE) {
|
|
|
+ count = 0;
|
|
|
printWriter.flush();
|
|
|
printWriter.close();
|
|
|
fileIndex++;
|