|
|
@@ -1,28 +1,14 @@
|
|
|
package com.uas.search.console.b2b.service.impl;
|
|
|
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileNotFoundException;
|
|
|
import java.io.IOException;
|
|
|
import java.io.NotSerializableException;
|
|
|
-import java.io.PrintWriter;
|
|
|
-import java.lang.reflect.InvocationTargetException;
|
|
|
-import java.lang.reflect.Method;
|
|
|
import java.util.Arrays;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
import java.util.Map;
|
|
|
-import java.util.Map.Entry;
|
|
|
-import java.util.Set;
|
|
|
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.uas.search.b2b.exception.SearchException;
|
|
|
-import com.uas.search.b2b.model.SPage;
|
|
|
import com.uas.search.b2b.service.SearchService.Table_name;
|
|
|
import com.uas.search.console.b2b.core.util.ContextUtils;
|
|
|
import com.uas.search.console.b2b.jms.AQListener;
|
|
|
@@ -33,9 +19,7 @@ import com.uas.search.console.b2b.service.IndexService;
|
|
|
import com.uas.search.console.b2b.service.InnerSearchService;
|
|
|
import com.uas.search.console.b2b.service.UpdateVirtualColumnService;
|
|
|
import com.uas.search.console.b2b.util.ClassAndTableNameUtils;
|
|
|
-import com.uas.search.console.b2b.util.FileUtils;
|
|
|
import com.uas.search.console.b2b.util.ObjectUtil;
|
|
|
-import com.uas.search.console.b2b.util.SearchUtils;
|
|
|
|
|
|
/**
|
|
|
* @author sunyj
|
|
|
@@ -44,16 +28,6 @@ import com.uas.search.console.b2b.util.SearchUtils;
|
|
|
@Service
|
|
|
public class UpdateVirtualColumnServiceImpl implements UpdateVirtualColumnService {
|
|
|
|
|
|
- /**
|
|
|
- * 默认的页码
|
|
|
- */
|
|
|
- private static final int PAGE_INDEX = 1;
|
|
|
-
|
|
|
- /**
|
|
|
- * 默认每页的大小
|
|
|
- */
|
|
|
- private static final int PAGE_SIZE = 1000;
|
|
|
-
|
|
|
@Autowired
|
|
|
private TaskService taskService;
|
|
|
|
|
|
@@ -66,8 +40,6 @@ public class UpdateVirtualColumnServiceImpl implements UpdateVirtualColumnServic
|
|
|
private InnerSearchService innerSearchService = ContextUtils.getApplicationContext().getBean("searchServiceImpl",
|
|
|
InnerSearchService.class);
|
|
|
|
|
|
- private Logger logger = LoggerFactory.getLogger(UpdateVirtualColumnServiceImpl.class);
|
|
|
-
|
|
|
/**
|
|
|
* 更新指定实体类的虚拟列的索引
|
|
|
*
|
|
|
@@ -79,68 +51,8 @@ public class UpdateVirtualColumnServiceImpl implements UpdateVirtualColumnServic
|
|
|
* 需更新的虚拟列,键值对(虚拟列名称:类型,实体中必须存在对应的get/set方法)
|
|
|
*/
|
|
|
private <T> void updateVirtualColumn(Class<T> clazz, Map<String, Class<?>> fields) {
|
|
|
- // 不能边更新索引边分页获取索引中的数据,因为索引更新后,分页顺序可能也会变化,
|
|
|
- // 所以要先把数据保存到本地,等待全部获取之后重建索引
|
|
|
- Long startTime = new Date().getTime();
|
|
|
Table_name tableName = ClassAndTableNameUtils.toTableName(clazz);
|
|
|
- // 先删除旧的文件
|
|
|
- FileUtils.deleteSubFiles(new File(SearchUtils.getDataPath(tableName)));
|
|
|
-
|
|
|
- int page = PAGE_INDEX;
|
|
|
- SPage<T> sPage = innerSearchService.getAllObjects(clazz, page, PAGE_SIZE);
|
|
|
-
|
|
|
- // 索引中数据的总数目
|
|
|
- long totalElements = sPage.getTotalElement();
|
|
|
- logger.info("发现数据:" + totalElements + "条");
|
|
|
- int fileIndex = 1;
|
|
|
- PrintWriter printWriter = null;
|
|
|
- int count = 0;
|
|
|
- try {
|
|
|
- File file = new File(SearchUtils.getDataPath(tableName));
|
|
|
- if (!file.exists()) {
|
|
|
- file.mkdirs();
|
|
|
- }
|
|
|
- printWriter = new PrintWriter(SearchUtils.getDataPath(tableName) + "/" + fileIndex + ".txt");
|
|
|
- while (true) {
|
|
|
- // 一个文件存放100000条数据,一旦超过,写入新的文件
|
|
|
- if (count > IndexServiceImpl.SINGLE_FILE_MAX_SIZE) {
|
|
|
- count = 1;
|
|
|
- printWriter.flush();
|
|
|
- printWriter.close();
|
|
|
- fileIndex++;
|
|
|
- printWriter = new PrintWriter(SearchUtils.getDataPath(tableName) + "/" + fileIndex + ".txt");
|
|
|
- }
|
|
|
- List<T> content = sPage.getContent();
|
|
|
- for (T element : content) {
|
|
|
- Set<Entry<String, Class<?>>> entrySet = fields.entrySet();
|
|
|
- for (Entry<String, Class<?>> entry : entrySet) {
|
|
|
- String key = entry.getKey();
|
|
|
- Class<?> type = entry.getValue();
|
|
|
- // 首字母大写
|
|
|
- key = key.substring(0, 1).toUpperCase() + key.substring(1);
|
|
|
- Method getMethod = clazz.getMethod("get" + key);
|
|
|
- Method setMethod = clazz.getMethod("set" + key, type);
|
|
|
- setMethod.invoke(element, getMethod.invoke(element));
|
|
|
- }
|
|
|
- printWriter.println(JSONObject.toJSONString(element));
|
|
|
- count++;
|
|
|
- }
|
|
|
- logger.info(String.format(tableName.value() + "...................%.2f%%",
|
|
|
- (page - 1) * PAGE_SIZE * 100.0 / totalElements));
|
|
|
-
|
|
|
- if (++page > sPage.getTotalPage()) {
|
|
|
- break;
|
|
|
- }
|
|
|
- sPage = innerSearchService.getAllObjects(clazz, page, PAGE_SIZE);
|
|
|
- }
|
|
|
- printWriter.flush();
|
|
|
- printWriter.close();
|
|
|
- } catch (FileNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException
|
|
|
- | IllegalArgumentException | InvocationTargetException e) {
|
|
|
- throw new SearchException(e).setDetailedMessage(e);
|
|
|
- }
|
|
|
- Long endTime = new Date().getTime();
|
|
|
- logger.info(String.format("修改数据%s条,耗时%.2fs\n ", totalElements, (endTime - startTime) / 1000.0));
|
|
|
+ innerSearchService.writeObjectsToFiles(clazz);
|
|
|
indexService.createIndexes(Arrays.asList(tableName), true, true);
|
|
|
aqListener.start(null, null);
|
|
|
}
|