提交 3c5a9b62 authored 作者: Thomas Mueller's avatar Thomas Mueller

Lucene fulltext search: creating an index is now faster if the table already…

Lucene fulltext search: creating an index is now faster if the table already contains data. Thanks a lot to Angel Leon from the FrostWire Team for the patch!
上级 403d624a
...@@ -325,8 +325,9 @@ public class FullTextLucene extends FullText { ...@@ -325,8 +325,9 @@ public class FullTextLucene extends FullText {
for (int i = 0; i < columnCount; i++) { for (int i = 0; i < columnCount; i++) {
row[i] = rs.getObject(i + 1); row[i] = rs.getObject(i + 1);
} }
existing.fire(conn, null, row); existing.insert(row, false);
} }
existing.commitIndex();
} }
private static void removeIndexFiles(Connection conn) throws SQLException { private static void removeIndexFiles(Connection conn) throws SQLException {
...@@ -547,7 +548,7 @@ public class FullTextLucene extends FullText { ...@@ -547,7 +548,7 @@ public class FullTextLucene extends FullText {
// update // update
if (hasChanged(oldRow, newRow, indexColumns)) { if (hasChanged(oldRow, newRow, indexColumns)) {
delete(oldRow); delete(oldRow);
insert(newRow); insert(newRow, true);
} }
} else { } else {
// delete // delete
...@@ -555,7 +556,7 @@ public class FullTextLucene extends FullText { ...@@ -555,7 +556,7 @@ public class FullTextLucene extends FullText {
} }
} else if (newRow != null) { } else if (newRow != null) {
// insert // insert
insert(newRow); insert(newRow, true);
} }
} }
...@@ -576,12 +577,26 @@ public class FullTextLucene extends FullText { ...@@ -576,12 +577,26 @@ public class FullTextLucene extends FullText {
// ignore // ignore
} }
public void commitIndex() throws SQLException {
try {
indexAccess.writer.commit();
// recreate Searcher with the IndexWriter's reader.
indexAccess.searcher.close();
indexAccess.reader.close();
IndexReader reader = indexAccess.writer.getReader();
indexAccess.reader = reader;
indexAccess.searcher = new IndexSearcher(reader);
} catch (IOException e) {
throw convertException(e);
}
}
/** /**
* Add a row to the index. * Add a row to the index.
* *
* @param row the row * @param row the row
*/ */
protected void insert(Object[] row) throws SQLException { protected void insert(Object[] row, boolean commitIndex) throws SQLException {
/*## LUCENE2 ## /*## LUCENE2 ##
String query = getQuery(row); String query = getQuery(row);
Document doc = new Document(); Document doc = new Document();
...@@ -645,13 +660,15 @@ public class FullTextLucene extends FullText { ...@@ -645,13 +660,15 @@ public class FullTextLucene extends FullText {
Field.Index.ANALYZED)); Field.Index.ANALYZED));
try { try {
indexAccess.writer.addDocument(doc); indexAccess.writer.addDocument(doc);
indexAccess.writer.commit(); if (commitIndex) {
// recreate Searcher with the IndexWriter's reader. indexAccess.writer.commit();
indexAccess.searcher.close(); // recreate Searcher with the IndexWriter's reader.
indexAccess.reader.close(); indexAccess.searcher.close();
IndexReader reader = indexAccess.writer.getReader(); indexAccess.reader.close();
indexAccess.reader = reader; IndexReader reader = indexAccess.writer.getReader();
indexAccess.searcher = new IndexSearcher(reader); indexAccess.reader = reader;
indexAccess.searcher = new IndexSearcher(reader);
}
} catch (IOException e) { } catch (IOException e) {
throw convertException(e); throw convertException(e);
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论