|
|
@@ -10,6 +10,7 @@ import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Set;
|
|
|
|
|
|
+import org.apache.log4j.Logger;
|
|
|
import org.apache.lucene.document.Document;
|
|
|
import org.apache.lucene.document.Field.Store;
|
|
|
import org.apache.lucene.document.StringField;
|
|
|
@@ -64,6 +65,8 @@ public class IndexServiceImpl implements IndexService {
|
|
|
|
|
|
private static final int PAGE_SIZE = 1000;
|
|
|
|
|
|
+ private Logger logger = Logger.getLogger(IndexServiceImpl.class);
|
|
|
+
|
|
|
public IndexServiceImpl() {
|
|
|
try {
|
|
|
directory = FSDirectory.open(Paths.get(SearchConstants.INDEX_DIR));
|
|
|
@@ -77,7 +80,7 @@ public class IndexServiceImpl implements IndexService {
|
|
|
public Long createIndexs() {
|
|
|
try {
|
|
|
if (IndexWriter.isLocked(directory)) {
|
|
|
- System.out.println("已有线程正在创建索引!");
|
|
|
+ logger.warn("已有线程正在创建索引!");
|
|
|
return 0L;
|
|
|
}
|
|
|
} catch (IOException e1) {
|
|
|
@@ -96,18 +99,17 @@ public class IndexServiceImpl implements IndexService {
|
|
|
|
|
|
Long kindSize = createKindIndexs();
|
|
|
Long kindTime = new Date().getTime();
|
|
|
- System.out.println("创建类目索引: " + kindSize + "条,耗时 " + (kindTime - startTime) + " ms\n");
|
|
|
+ logger.info("创建类目索引: " + kindSize + "条,耗时 " + (kindTime - startTime) + " ms\n");
|
|
|
|
|
|
Long brandSize = createBrandIndexs();
|
|
|
Long brandTime = new Date().getTime();
|
|
|
- System.out.println("创建品牌索引: " + brandSize + "条,耗时 " + (brandTime - kindTime) + " ms\n");
|
|
|
+ logger.info("创建品牌索引: " + brandSize + "条,耗时 " + (brandTime - kindTime) + " ms\n");
|
|
|
|
|
|
- // Long componentSize = createComponentIndexs();
|
|
|
Long componentSize = createComponentIndexesWithFiles();
|
|
|
Long componentTime = new Date().getTime();
|
|
|
- System.out.println("创建器件索引: " + componentSize + "条,耗时 " + (componentTime - brandTime) + " ms\n");
|
|
|
+ logger.info("创建器件索引: " + componentSize + "条,耗时 " + (componentTime - brandTime) + " ms\n");
|
|
|
|
|
|
- System.out.println("索引创建成功, 共用时间 " + (componentTime - startTime) + " ms");
|
|
|
+ logger.info("索引创建成功, 共用时间 " + (componentTime - startTime) + " ms");
|
|
|
|
|
|
return componentTime - startTime;
|
|
|
} catch (IOException e) {
|
|
|
@@ -128,7 +130,7 @@ public class IndexServiceImpl implements IndexService {
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
private Long createKindIndexs() throws IOException {
|
|
|
- System.out.println("正在创建类目索引...");
|
|
|
+ logger.info("正在创建类目索引...");
|
|
|
List<KindSimpleInfo> kinds = kindDao.findAll();
|
|
|
|
|
|
if (CollectionUtils.isEmpty(kinds))
|
|
|
@@ -150,7 +152,7 @@ public class IndexServiceImpl implements IndexService {
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
private Long createBrandIndexs() throws IOException {
|
|
|
- System.out.println("正在创建品牌索引...");
|
|
|
+ logger.info("正在创建品牌索引...");
|
|
|
List<BrandSimpleInfo> brands = brandDao.findAll();
|
|
|
if (CollectionUtils.isEmpty(brands))
|
|
|
return 0L;
|
|
|
@@ -166,19 +168,19 @@ public class IndexServiceImpl implements IndexService {
|
|
|
}
|
|
|
|
|
|
private Long createComponentIndexesWithFiles() {
|
|
|
- System.out.println("正在创建器件索引...");
|
|
|
+ logger.info("正在创建器件索引...");
|
|
|
Long size = 0L;
|
|
|
try {
|
|
|
// 从本地路径读取器件数据
|
|
|
File[] files = new File(SearchConstants.COMPONENT_WITH_PROPERTY_DIR).listFiles();
|
|
|
if (files == null || files.length == 0) {
|
|
|
- System.out.println("创建器件索引失败,原因:器件数据文件不存在!");
|
|
|
+ logger.info("创建器件索引失败,原因:器件数据文件不存在!");
|
|
|
return 0L;
|
|
|
}
|
|
|
// 将要创建的索引总数目约为:文件数目*单个文件的行数
|
|
|
long totalSize = files.length * MergeComponentData.SINGLE_FILE_MAX_SIZE;
|
|
|
for (File file : files) {
|
|
|
- System.out.println("读取文件: " + file.getName());
|
|
|
+ logger.info("读取文件: " + file.getName());
|
|
|
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
|
|
|
String line = null;
|
|
|
while (!StringUtils.isEmpty(line = bufferedReader.readLine())) {
|
|
|
@@ -197,7 +199,7 @@ public class IndexServiceImpl implements IndexService {
|
|
|
bufferedReader.close();
|
|
|
}
|
|
|
} catch (FileNotFoundException e) {
|
|
|
- System.out.println("创建器件索引失败,原因:器件数据文件不存在!");
|
|
|
+ logger.error("创建器件索引失败,原因:器件数据文件不存在!");
|
|
|
return 0L;
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
@@ -212,7 +214,7 @@ public class IndexServiceImpl implements IndexService {
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
public Long createComponentIndexs() throws IOException {
|
|
|
- System.out.println("正在创建器件索引...");
|
|
|
+ logger.info("正在创建器件索引...");
|
|
|
Long size = 0L;
|
|
|
PageParams params = new PageParams();
|
|
|
int page = 1;
|
|
|
@@ -222,7 +224,7 @@ public class IndexServiceImpl implements IndexService {
|
|
|
PageInfo info = new PageInfo(params);
|
|
|
Page<ComponentSimpleInfo> pageResult = componentDao.findAll(info);
|
|
|
long totalElements = pageResult.getTotalElements();
|
|
|
- System.out.println("Number of components: " + totalElements);
|
|
|
+ logger.info("Number of components: " + totalElements);
|
|
|
// 用于记录上次提交索引时的创建进度
|
|
|
double recordProgress = 0;
|
|
|
while (totalElements > size) {
|
|
|
@@ -348,7 +350,7 @@ public class IndexServiceImpl implements IndexService {
|
|
|
|
|
|
file.delete();
|
|
|
try {
|
|
|
- System.out.println("deleted " + file.getCanonicalPath());
|
|
|
+ logger.info("deleted " + file.getCanonicalPath());
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
@@ -378,7 +380,7 @@ public class IndexServiceImpl implements IndexService {
|
|
|
indexWriter.addDocument(document);
|
|
|
}
|
|
|
} else {
|
|
|
- SearchConstants.logger.error("message parsing failed!");
|
|
|
+ logger.error("message parsing failed!");
|
|
|
}
|
|
|
indexWriter.commit();
|
|
|
} catch (IOException e) {
|
|
|
@@ -388,7 +390,7 @@ public class IndexServiceImpl implements IndexService {
|
|
|
} finally {
|
|
|
indexWriterManager.release();
|
|
|
}
|
|
|
- System.out.println("saved object... " + obj + "\n");
|
|
|
+ logger.info("saved object... " + obj + "\n");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -420,7 +422,7 @@ public class IndexServiceImpl implements IndexService {
|
|
|
new Term(SearchConstants.COMPONENT_ID_FIELD, String.valueOf(component.getId())), document);
|
|
|
}
|
|
|
} else {
|
|
|
- SearchConstants.logger.error("message parsing failed!");
|
|
|
+ logger.error("message parsing failed!");
|
|
|
}
|
|
|
indexWriter.commit();
|
|
|
} catch (IOException e) {
|
|
|
@@ -430,7 +432,7 @@ public class IndexServiceImpl implements IndexService {
|
|
|
} finally {
|
|
|
indexWriterManager.release();
|
|
|
}
|
|
|
- System.out.println("updated object... " + obj + "\n");
|
|
|
+ logger.info("updated object... " + obj + "\n");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -450,7 +452,7 @@ public class IndexServiceImpl implements IndexService {
|
|
|
indexWriter.deleteDocuments(
|
|
|
new Term(SearchConstants.COMPONENT_ID_FIELD, ((ComponentSimpleInfo) obj).getId().toString()));
|
|
|
} else {
|
|
|
- SearchConstants.logger.error("message parsing failed!");
|
|
|
+ logger.error("message parsing failed!");
|
|
|
}
|
|
|
indexWriter.commit();
|
|
|
} catch (IOException e) {
|
|
|
@@ -460,6 +462,6 @@ public class IndexServiceImpl implements IndexService {
|
|
|
} finally {
|
|
|
indexWriterManager.release();
|
|
|
}
|
|
|
- System.out.println("deleted object... " + obj + "\n");
|
|
|
+ logger.info("deleted object... " + obj + "\n");
|
|
|
}
|
|
|
}
|