提交 e1a91d29 authored 作者: andrei's avatar andrei

full_text_mt

上级 a24af894
...@@ -18,11 +18,13 @@ import java.sql.Statement; ...@@ -18,11 +18,13 @@ import java.sql.Statement;
import java.sql.Types; import java.sql.Types;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.UUID; import java.util.UUID;
import org.h2.api.Trigger; import org.h2.api.Trigger;
import org.h2.command.Parser; import org.h2.command.Parser;
import org.h2.engine.Session; import org.h2.engine.Session;
...@@ -151,7 +153,7 @@ public class FullText { ...@@ -151,7 +153,7 @@ public class FullText {
} }
} }
rs = stat.executeQuery("SELECT * FROM " + SCHEMA + ".WORDS"); rs = stat.executeQuery("SELECT * FROM " + SCHEMA + ".WORDS");
HashMap<String, Integer> map = setting.getWordList(); Map<String, Integer> map = setting.getWordList();
while (rs.next()) { while (rs.next()) {
String word = rs.getString("NAME"); String word = rs.getString("NAME");
int id = rs.getInt("ID"); int id = rs.getInt("ID");
...@@ -243,9 +245,9 @@ public class FullText { ...@@ -243,9 +245,9 @@ public class FullText {
break; break;
} }
} }
prep = conn.prepareStatement("DELETE FROM " + SCHEMA + ".MAP M " + prep = conn.prepareStatement("DELETE FROM " + SCHEMA + ".MAP " +
"WHERE NOT EXISTS (SELECT * FROM " + SCHEMA + "WHERE NOT EXISTS (SELECT * FROM " + SCHEMA +
".ROWS R WHERE R.ID=M.ROWID) AND ROWID<10000"); ".ROWS R WHERE R.ID=ROWID) AND ROWID<10000");
while (true) { while (true) {
int deleted = prep.executeUpdate(); int deleted = prep.executeUpdate();
if (deleted == 0) { if (deleted == 0) {
...@@ -605,10 +607,10 @@ public class FullText { ...@@ -605,10 +607,10 @@ public class FullText {
if (!setting.isInitialized()) { if (!setting.isInitialized()) {
init(conn); init(conn);
} }
HashSet<String> words = New.hashSet(); Set<String> words = New.hashSet();
addWords(setting, words, text); addWords(setting, words, text);
HashSet<Integer> rIds = null, lastRowIds = null; Set<Integer> rIds = null, lastRowIds;
HashMap<String, Integer> allWords = setting.getWordList(); Map<String, Integer> allWords = setting.getWordList();
PreparedStatement prepSelectMapByWordId = setting.prepare(conn, PreparedStatement prepSelectMapByWordId = setting.prepare(conn,
SELECT_MAP_BY_WORD_ID); SELECT_MAP_BY_WORD_ID);
...@@ -698,7 +700,7 @@ public class FullText { ...@@ -698,7 +700,7 @@ public class FullText {
* @param reader the reader * @param reader the reader
*/ */
protected static void addWords(FullTextSettings setting, protected static void addWords(FullTextSettings setting,
HashSet<String> set, Reader reader) { Set<String> set, Reader reader) {
StreamTokenizer tokenizer = new StreamTokenizer(reader); StreamTokenizer tokenizer = new StreamTokenizer(reader);
tokenizer.resetSyntax(); tokenizer.resetSyntax();
tokenizer.wordChars(' ' + 1, 255); tokenizer.wordChars(' ' + 1, 255);
...@@ -732,7 +734,7 @@ public class FullText { ...@@ -732,7 +734,7 @@ public class FullText {
* @param text the text * @param text the text
*/ */
protected static void addWords(FullTextSettings setting, protected static void addWords(FullTextSettings setting,
HashSet<String> set, String text) { Set<String> set, String text) {
String whitespaceChars = setting.getWhitespaceChars(); String whitespaceChars = setting.getWhitespaceChars();
StringTokenizer tokenizer = new StringTokenizer(text, whitespaceChars); StringTokenizer tokenizer = new StringTokenizer(text, whitespaceChars);
while (tokenizer.hasMoreTokens()) { while (tokenizer.hasMoreTokens()) {
...@@ -751,23 +753,30 @@ public class FullText { ...@@ -751,23 +753,30 @@ public class FullText {
* @param schema the schema name * @param schema the schema name
* @param table the table name * @param table the table name
*/ */
protected static void createTrigger(Connection conn, String schema, private static void createTrigger(Connection conn, String schema,
String table) throws SQLException { String table) throws SQLException {
createOrDropTrigger(conn, schema, table, true); createOrDropTrigger(conn, schema, table, true);
} }
private static void createOrDropTrigger(Connection conn, private static void createOrDropTrigger(Connection conn,
String schema, String table, boolean create) throws SQLException { String schema, String table, boolean create) throws SQLException {
Statement stat = conn.createStatement(); try (Statement stat = conn.createStatement()) {
String trigger = StringUtils.quoteIdentifier(schema) + "." String trigger = StringUtils.quoteIdentifier(schema) + "."
+ StringUtils.quoteIdentifier(TRIGGER_PREFIX + table); + StringUtils.quoteIdentifier(TRIGGER_PREFIX + table);
stat.execute("DROP TRIGGER IF EXISTS " + trigger); stat.execute("DROP TRIGGER IF EXISTS " + trigger);
if (create) { if (create) {
ResultSet rs = stat.executeQuery("SELECT value FROM information_schema.settings WHERE name = 'MV_STORE'");
boolean multiThread = FullTextTrigger.isMultiThread(conn);
StringBuilder buff = new StringBuilder("CREATE TRIGGER IF NOT EXISTS "); StringBuilder buff = new StringBuilder("CREATE TRIGGER IF NOT EXISTS ");
// needs to be called on rollback as well, because we use the init // unless multithread, trigger needs to be called on rollback as well,
// connection do to changes in the index (not the user connection) // because we use the init connection do to changes in the index
// (not the user connection)
buff.append(trigger). buff.append(trigger).
append(" AFTER INSERT, UPDATE, DELETE, ROLLBACK ON "). append(" AFTER INSERT, UPDATE, DELETE");
if(!multiThread) {
buff.append(", ROLLBACK");
}
buff.append(" ON ").
append(StringUtils.quoteIdentifier(schema)). append(StringUtils.quoteIdentifier(schema)).
append('.'). append('.').
append(StringUtils.quoteIdentifier(table)). append(StringUtils.quoteIdentifier(table)).
...@@ -777,6 +786,7 @@ public class FullText { ...@@ -777,6 +786,7 @@ public class FullText {
stat.execute(buff.toString()); stat.execute(buff.toString());
} }
} }
}
/** /**
* Add the existing data to the index. * Add the existing data to the index.
...@@ -785,7 +795,7 @@ public class FullText { ...@@ -785,7 +795,7 @@ public class FullText {
* @param schema the schema name * @param schema the schema name
* @param table the table name * @param table the table name
*/ */
protected static void indexExistingRows(Connection conn, String schema, private static void indexExistingRows(Connection conn, String schema,
String table) throws SQLException { String table) throws SQLException {
FullText.FullTextTrigger existing = new FullText.FullTextTrigger(); FullText.FullTextTrigger existing = new FullText.FullTextTrigger();
existing.init(conn, schema, null, table, false, Trigger.INSERT); existing.init(conn, schema, null, table, false, Trigger.INSERT);
...@@ -823,7 +833,7 @@ public class FullText { ...@@ -823,7 +833,7 @@ public class FullText {
private static void setIgnoreList(FullTextSettings setting, private static void setIgnoreList(FullTextSettings setting,
String commaSeparatedList) { String commaSeparatedList) {
String[] list = StringUtils.arraySplit(commaSeparatedList, ',', true); String[] list = StringUtils.arraySplit(commaSeparatedList, ',', true);
HashSet<String> set = setting.getIgnoreList(); Set<String> set = setting.getIgnoreList();
for (String word : list) { for (String word : list) {
String converted = setting.convertWord(word); String converted = setting.convertWord(word);
if (converted != null) { if (converted != null) {
...@@ -860,14 +870,28 @@ public class FullText { ...@@ -860,14 +870,28 @@ public class FullText {
/** /**
* Trigger updates the index when a inserting, updating, or deleting a row. * Trigger updates the index when a inserting, updating, or deleting a row.
*/ */
public static class FullTextTrigger implements Trigger { public static final class FullTextTrigger implements Trigger {
private FullTextSettings setting;
private IndexInfo index;
private int[] columnTypes;
private final PreparedStatement[] prepStatements = new PreparedStatement[SQL.length];
private boolean useOwnConnection;
private static final int INSERT_WORD = 0;
private static final int INSERT_ROW = 1;
private static final int INSERT_MAP = 2;
private static final int DELETE_ROW = 3;
private static final int DELETE_MAP = 4;
private static final int SELECT_ROW = 5;
protected FullTextSettings setting; private static final String SQL[] = {
protected IndexInfo index; "INSERT INTO " + SCHEMA + ".WORDS(NAME) VALUES(?)",
protected int[] columnTypes; "INSERT INTO " + SCHEMA + ".ROWS(HASH, INDEXID, KEY) VALUES(?, ?, ?)",
protected PreparedStatement prepInsertWord, prepInsertRow, prepInsertMap; "INSERT INTO " + SCHEMA + ".MAP(ROWID, WORDID) VALUES(?, ?)",
protected PreparedStatement prepDeleteRow, prepDeleteMap; "DELETE FROM " + SCHEMA + ".ROWS WHERE HASH=? AND INDEXID=? AND KEY=?",
protected PreparedStatement prepSelectRow; "DELETE FROM " + SCHEMA + ".MAP WHERE ROWID=? AND WORDID=?",
"SELECT ID FROM " + SCHEMA + ".ROWS WHERE HASH=? AND INDEXID=? AND KEY=?"
};
/** /**
* INTERNAL * INTERNAL
...@@ -923,9 +947,7 @@ public class FullText { ...@@ -923,9 +947,7 @@ public class FullText {
index.id = rs.getInt(1); index.id = rs.getInt(1);
String columns = rs.getString(2); String columns = rs.getString(2);
if (columns != null) { if (columns != null) {
for (String s : StringUtils.arraySplit(columns, ',', true)) { Collections.addAll(indexList, StringUtils.arraySplit(columns, ',', true));
indexList.add(s);
}
} }
} }
if (indexList.size() == 0) { if (indexList.size() == 0) {
...@@ -936,18 +958,23 @@ public class FullText { ...@@ -936,18 +958,23 @@ public class FullText {
index.indexColumns = new int[indexList.size()]; index.indexColumns = new int[indexList.size()];
setColumns(index.indexColumns, indexList, columnList); setColumns(index.indexColumns, indexList, columnList);
setting.addIndexInfo(index); setting.addIndexInfo(index);
prepInsertWord = conn.prepareStatement(
"INSERT INTO " + SCHEMA + ".WORDS(NAME) VALUES(?)"); useOwnConnection = isMultiThread(conn);
prepInsertRow = conn.prepareStatement( if(!useOwnConnection) {
"INSERT INTO " + SCHEMA + ".ROWS(HASH, INDEXID, KEY) VALUES(?, ?, ?)"); for (int i = 0; i < SQL.length; i++) {
prepInsertMap = conn.prepareStatement( prepStatements[i] = conn.prepareStatement(SQL[i]);
"INSERT INTO " + SCHEMA + ".MAP(ROWID, WORDID) VALUES(?, ?)"); }
prepDeleteRow = conn.prepareStatement( }
"DELETE FROM " + SCHEMA + ".ROWS WHERE HASH=? AND INDEXID=? AND KEY=?"); }
prepDeleteMap = conn.prepareStatement(
"DELETE FROM " + SCHEMA + ".MAP WHERE ROWID=? AND WORDID=?"); private static boolean isMultiThread(Connection conn)
prepSelectRow = conn.prepareStatement( throws SQLException {
"SELECT ID FROM " + SCHEMA + ".ROWS WHERE HASH=? AND INDEXID=? AND KEY=?"); try (Statement stat = conn.createStatement()) {
ResultSet rs = stat.executeQuery(
"SELECT value FROM information_schema.settings" +
" WHERE name = 'MULTI_THREADED'");
return rs.next() && !"0".equals(rs.getString(1));
}
} }
/** /**
...@@ -960,16 +987,16 @@ public class FullText { ...@@ -960,16 +987,16 @@ public class FullText {
if (newRow != null) { if (newRow != null) {
// update // update
if (hasChanged(oldRow, newRow, index.indexColumns)) { if (hasChanged(oldRow, newRow, index.indexColumns)) {
delete(oldRow); delete(conn, oldRow);
insert(newRow); insert(conn, newRow);
} }
} else { } else {
// delete // delete
delete(oldRow); delete(conn, oldRow);
} }
} else if (newRow != null) { } else if (newRow != null) {
// insert // insert
insert(newRow); insert(conn, newRow);
} }
} }
...@@ -992,11 +1019,16 @@ public class FullText { ...@@ -992,11 +1019,16 @@ public class FullText {
/** /**
* Add a row to the index. * Add a row to the index.
* *
* @param conn to use
* @param row the row * @param row the row
*/ */
protected void insert(Object[] row) throws SQLException { protected void insert(Connection conn, Object[] row) throws SQLException {
PreparedStatement prepInsertRow = null;
PreparedStatement prepInsertMap = null;
try {
String key = getKey(row); String key = getKey(row);
int hash = key.hashCode(); int hash = key.hashCode();
prepInsertRow = getStatement(conn, INSERT_ROW);
prepInsertRow.setInt(1, hash); prepInsertRow.setInt(1, hash);
prepInsertRow.setInt(2, index.id); prepInsertRow.setInt(2, index.id);
prepInsertRow.setString(3, key); prepInsertRow.setString(3, key);
...@@ -1004,30 +1036,46 @@ public class FullText { ...@@ -1004,30 +1036,46 @@ public class FullText {
ResultSet rs = prepInsertRow.getGeneratedKeys(); ResultSet rs = prepInsertRow.getGeneratedKeys();
rs.next(); rs.next();
int rowId = rs.getInt(1); int rowId = rs.getInt(1);
prepInsertMap = getStatement(conn, INSERT_MAP);
prepInsertMap.setInt(1, rowId); prepInsertMap.setInt(1, rowId);
int[] wordIds = getWordIds(row); int[] wordIds = getWordIds(conn, row);
for (int id : wordIds) { for (int id : wordIds) {
prepInsertMap.setInt(2, id); prepInsertMap.setInt(2, id);
prepInsertMap.execute(); prepInsertMap.execute();
} }
} finally {
if (useOwnConnection) {
IOUtils.closeSilently(prepInsertRow);
IOUtils.closeSilently(prepInsertMap);
}
}
} }
/** /**
* Delete a row from the index. * Delete a row from the index.
* *
* @param conn to use
* @param row the row * @param row the row
*/ */
protected void delete(Object[] row) throws SQLException { protected void delete(Connection conn, Object[] row) throws SQLException {
PreparedStatement prepSelectRow = null;
PreparedStatement prepDeleteMap = null;
PreparedStatement prepDeleteRow = null;
try {
String key = getKey(row); String key = getKey(row);
int hash = key.hashCode(); int hash = key.hashCode();
prepSelectRow = getStatement(conn, SELECT_ROW);
prepSelectRow.setInt(1, hash); prepSelectRow.setInt(1, hash);
prepSelectRow.setInt(2, index.id); prepSelectRow.setInt(2, index.id);
prepSelectRow.setString(3, key); prepSelectRow.setString(3, key);
ResultSet rs = prepSelectRow.executeQuery(); ResultSet rs = prepSelectRow.executeQuery();
prepDeleteMap = getStatement(conn, DELETE_MAP);
prepDeleteRow = getStatement(conn, DELETE_ROW);
if (rs.next()) { if (rs.next()) {
int rowId = rs.getInt(1); int rowId = rs.getInt(1);
prepDeleteMap.setInt(1, rowId); prepDeleteMap.setInt(1, rowId);
int[] wordIds = getWordIds(row); int[] wordIds = getWordIds(conn, row);
for (int id : wordIds) { for (int id : wordIds) {
prepDeleteMap.setInt(2, id); prepDeleteMap.setInt(2, id);
prepDeleteMap.executeUpdate(); prepDeleteMap.executeUpdate();
...@@ -1037,9 +1085,16 @@ public class FullText { ...@@ -1037,9 +1085,16 @@ public class FullText {
prepDeleteRow.setString(3, key); prepDeleteRow.setString(3, key);
prepDeleteRow.executeUpdate(); prepDeleteRow.executeUpdate();
} }
} finally {
if (useOwnConnection) {
IOUtils.closeSilently(prepSelectRow);
IOUtils.closeSilently(prepDeleteMap);
IOUtils.closeSilently(prepDeleteRow);
}
}
} }
private int[] getWordIds(Object[] row) throws SQLException { private int[] getWordIds(Connection conn, Object[] row) throws SQLException {
HashSet<String> words = New.hashSet(); HashSet<String> words = New.hashSet();
for (int idx : index.indexColumns) { for (int idx : index.indexColumns) {
int type = columnTypes[idx]; int type = columnTypes[idx];
...@@ -1057,11 +1112,14 @@ public class FullText { ...@@ -1057,11 +1112,14 @@ public class FullText {
addWords(setting, words, string); addWords(setting, words, string);
} }
} }
HashMap<String, Integer> allWords = setting.getWordList(); PreparedStatement prepInsertWord = null;
try {
prepInsertWord = getStatement(conn, INSERT_WORD);
Map<String, Integer> allWords = setting.getWordList();
int[] wordIds = new int[words.size()]; int[] wordIds = new int[words.size()];
Iterator<String> it = words.iterator(); synchronized (allWords) {
for (int i = 0; it.hasNext(); i++) { int i = 0;
String word = it.next(); for (String word : words) {
Integer wId = allWords.get(word); Integer wId = allWords.get(word);
int wordId; int wordId;
if (wId == null) { if (wId == null) {
...@@ -1072,12 +1130,18 @@ public class FullText { ...@@ -1072,12 +1130,18 @@ public class FullText {
wordId = rs.getInt(1); wordId = rs.getInt(1);
allWords.put(word, wordId); allWords.put(word, wordId);
} else { } else {
wordId = wId.intValue(); wordId = wId;
}
wordIds[i++] = wordId;
} }
wordIds[i] = wordId;
} }
Arrays.sort(wordIds); Arrays.sort(wordIds);
return wordIds; return wordIds;
} finally {
if (useOwnConnection) {
IOUtils.closeSilently(prepInsertWord);
}
}
} }
private String getKey(Object[] row) throws SQLException { private String getKey(Object[] row) throws SQLException {
...@@ -1095,13 +1159,17 @@ public class FullText { ...@@ -1095,13 +1159,17 @@ public class FullText {
return buff.toString(); return buff.toString();
} }
private PreparedStatement getStatement(Connection conn, int indx) throws SQLException {
return useOwnConnection ? conn.prepareStatement(SQL[indx]) : prepStatements[indx];
}
} }
/** /**
* INTERNAL * INTERNAL
* Close all fulltext settings, freeing up memory. * Close all fulltext settings, freeing up memory.
*/ */
public static void closeAll() { public static synchronized void closeAll() {
FullTextSettings.closeAll(); FullTextSettings.closeAll();
} }
...@@ -1116,5 +1184,4 @@ public class FullText { ...@@ -1116,5 +1184,4 @@ public class FullText {
throws SQLException { throws SQLException {
throw new SQLException(message, "FULLTEXT"); throw new SQLException(message, "FULLTEXT");
} }
} }
...@@ -13,6 +13,7 @@ import java.sql.ResultSet; ...@@ -13,6 +13,7 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer;
...@@ -37,6 +38,8 @@ import org.h2.util.StatementBuilder; ...@@ -37,6 +38,8 @@ import org.h2.util.StatementBuilder;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
import org.h2.util.Utils; import org.h2.util.Utils;
import java.io.File; import java.io.File;
import java.util.Map;
import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs; import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.FSDirectory;
...@@ -94,7 +97,7 @@ public class FullTextLucene extends FullText { ...@@ -94,7 +97,7 @@ public class FullTextLucene extends FullText {
* @param conn the connection * @param conn the connection
*/ */
public static void init(Connection conn) throws SQLException { public static void init(Connection conn) throws SQLException {
Statement stat = conn.createStatement(); try (Statement stat = conn.createStatement()) {
stat.execute("CREATE SCHEMA IF NOT EXISTS " + SCHEMA); stat.execute("CREATE SCHEMA IF NOT EXISTS " + SCHEMA);
stat.execute("CREATE TABLE IF NOT EXISTS " + SCHEMA + stat.execute("CREATE TABLE IF NOT EXISTS " + SCHEMA +
".INDEXES(SCHEMA VARCHAR, TABLE VARCHAR, " + ".INDEXES(SCHEMA VARCHAR, TABLE VARCHAR, " +
...@@ -111,10 +114,6 @@ public class FullTextLucene extends FullText { ...@@ -111,10 +114,6 @@ public class FullTextLucene extends FullText {
FullTextLucene.class.getName() + ".reindex\""); FullTextLucene.class.getName() + ".reindex\"");
stat.execute("CREATE ALIAS IF NOT EXISTS FTL_DROP_ALL FOR \"" + stat.execute("CREATE ALIAS IF NOT EXISTS FTL_DROP_ALL FOR \"" +
FullTextLucene.class.getName() + ".dropAll\""); FullTextLucene.class.getName() + ".dropAll\"");
try {
getIndexAccess(conn);
} catch (SQLException e) {
throw convertException(e);
} }
} }
...@@ -157,12 +156,10 @@ public class FullTextLucene extends FullText { ...@@ -157,12 +156,10 @@ public class FullTextLucene extends FullText {
prep.setString(1, schema); prep.setString(1, schema);
prep.setString(2, table); prep.setString(2, table);
int rowCount = prep.executeUpdate(); int rowCount = prep.executeUpdate();
if (rowCount == 0) { if (rowCount != 0) {
return;
}
reindex(conn); reindex(conn);
} }
}
/** /**
* Re-creates the full text index for this database. Calling this method is * Re-creates the full text index for this database. Calling this method is
...@@ -248,10 +245,7 @@ public class FullTextLucene extends FullText { ...@@ -248,10 +245,7 @@ public class FullTextLucene extends FullText {
* @return the converted SQL exception * @return the converted SQL exception
*/ */
protected static SQLException convertException(Exception e) { protected static SQLException convertException(Exception e) {
SQLException e2 = new SQLException( return new SQLException("Error while indexing document", "FULLTEXT", e);
"Error while indexing document", "FULLTEXT");
e2.initCause(e);
return e2;
} }
/** /**
...@@ -261,7 +255,7 @@ public class FullTextLucene extends FullText { ...@@ -261,7 +255,7 @@ public class FullTextLucene extends FullText {
* @param schema the schema name * @param schema the schema name
* @param table the table name * @param table the table name
*/ */
protected static void createTrigger(Connection conn, String schema, private static void createTrigger(Connection conn, String schema,
String table) throws SQLException { String table) throws SQLException {
createOrDropTrigger(conn, schema, table, true); createOrDropTrigger(conn, schema, table, true);
} }
...@@ -309,11 +303,7 @@ public class FullTextLucene extends FullText { ...@@ -309,11 +303,7 @@ public class FullTextLucene extends FullText {
conf.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND); conf.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
IndexWriter writer = new IndexWriter(indexDir, conf); IndexWriter writer = new IndexWriter(indexDir, conf);
//see http://wiki.apache.org/lucene-java/NearRealtimeSearch //see http://wiki.apache.org/lucene-java/NearRealtimeSearch
IndexReader reader = IndexReader.open(writer, true); access = new IndexAccess(writer);
access = new IndexAccess();
access.writer = writer;
access.reader = reader;
access.searcher = new IndexSearcher(reader);
} catch (IOException e) { } catch (IOException e) {
throw convertException(e); throw convertException(e);
} }
...@@ -353,7 +343,7 @@ public class FullTextLucene extends FullText { ...@@ -353,7 +343,7 @@ public class FullTextLucene extends FullText {
* @param schema the schema name * @param schema the schema name
* @param table the table name * @param table the table name
*/ */
protected static void indexExistingRows(Connection conn, String schema, private static void indexExistingRows(Connection conn, String schema,
String table) throws SQLException { String table) throws SQLException {
FullTextLucene.FullTextTrigger existing = new FullTextLucene.FullTextTrigger(); FullTextLucene.FullTextTrigger existing = new FullTextLucene.FullTextTrigger();
existing.init(conn, schema, null, table, false, Trigger.INSERT); existing.init(conn, schema, null, table, false, Trigger.INSERT);
...@@ -373,10 +363,7 @@ public class FullTextLucene extends FullText { ...@@ -373,10 +363,7 @@ public class FullTextLucene extends FullText {
private static void removeIndexFiles(Connection conn) throws SQLException { private static void removeIndexFiles(Connection conn) throws SQLException {
String path = getIndexPath(conn); String path = getIndexPath(conn);
IndexAccess access = INDEX_ACCESS.get(path); removeIndexAccess(path);
if (access != null) {
removeIndexAccess(access, path);
}
if (!path.startsWith(IN_MEMORY_PREFIX)) { if (!path.startsWith(IN_MEMORY_PREFIX)) {
FileUtils.deleteRecursive(path, false); FileUtils.deleteRecursive(path, false);
} }
...@@ -386,17 +373,16 @@ public class FullTextLucene extends FullText { ...@@ -386,17 +373,16 @@ public class FullTextLucene extends FullText {
* Close the index writer and searcher and remove them from the index access * Close the index writer and searcher and remove them from the index access
* set. * set.
* *
* @param access the index writer/searcher wrapper
* @param indexPath the index path * @param indexPath the index path
*/ */
protected static void removeIndexAccess(IndexAccess access, String indexPath) protected static void removeIndexAccess(String indexPath)
throws SQLException { throws SQLException {
synchronized (INDEX_ACCESS) { synchronized (INDEX_ACCESS) {
try { try {
INDEX_ACCESS.remove(indexPath); IndexAccess access = INDEX_ACCESS.remove(indexPath);
access.searcher.close(); if(access != null) {
access.reader.close(); access.close();
access.writer.close(); }
} catch (Exception e) { } catch (Exception e) {
throw convertException(e); throw convertException(e);
} }
...@@ -426,7 +412,8 @@ public class FullTextLucene extends FullText { ...@@ -426,7 +412,8 @@ public class FullTextLucene extends FullText {
try { try {
IndexAccess access = getIndexAccess(conn); IndexAccess access = getIndexAccess(conn);
// take a reference as the searcher may change // take a reference as the searcher may change
IndexSearcher searcher = access.searcher; IndexSearcher searcher = access.getSearcher();
try {
// reuse the same analyzer; it's thread-safe; // reuse the same analyzer; it's thread-safe;
// also allows subclasses to control the analyzer used. // also allows subclasses to control the analyzer used.
Analyzer analyzer = access.writer.getAnalyzer(); Analyzer analyzer = access.writer.getAnalyzer();
...@@ -470,6 +457,9 @@ public class FullTextLucene extends FullText { ...@@ -470,6 +457,9 @@ public class FullTextLucene extends FullText {
result.addRow(q, score); result.addRow(q, score);
} }
} }
} finally {
access.returnSearcher(searcher);
}
} catch (Exception e) { } catch (Exception e) {
throw convertException(e); throw convertException(e);
} }
...@@ -479,16 +469,16 @@ public class FullTextLucene extends FullText { ...@@ -479,16 +469,16 @@ public class FullTextLucene extends FullText {
/** /**
* Trigger updates the index when a inserting, updating, or deleting a row. * Trigger updates the index when a inserting, updating, or deleting a row.
*/ */
public static class FullTextTrigger implements Trigger { public static final class FullTextTrigger implements Trigger {
protected String schema; private String schema;
protected String table; private String table;
protected int[] keys; private int[] keys;
protected int[] indexColumns; private int[] indexColumns;
protected String[] columns; private String[] columns;
protected int[] columnTypes; private int[] columnTypes;
protected String indexPath; private String indexPath;
protected IndexAccess indexAccess; private IndexAccess indexAccess;
/** /**
* INTERNAL * INTERNAL
...@@ -541,9 +531,8 @@ public class FullTextLucene extends FullText { ...@@ -541,9 +531,8 @@ public class FullTextLucene extends FullText {
if (rs.next()) { if (rs.next()) {
String cols = rs.getString(1); String cols = rs.getString(1);
if (cols != null) { if (cols != null) {
for (String s : StringUtils.arraySplit(cols, ',', true)) { Collections.addAll(indexList,
indexList.add(s); StringUtils.arraySplit(cols, ',', true));
}
} }
} }
if (indexList.size() == 0) { if (indexList.size() == 0) {
...@@ -583,10 +572,7 @@ public class FullTextLucene extends FullText { ...@@ -583,10 +572,7 @@ public class FullTextLucene extends FullText {
*/ */
@Override @Override
public void close() throws SQLException { public void close() throws SQLException {
if (indexAccess != null) { removeIndexAccess(indexPath);
removeIndexAccess(indexAccess, indexPath);
indexAccess = null;
}
} }
/** /**
...@@ -600,14 +586,9 @@ public class FullTextLucene extends FullText { ...@@ -600,14 +586,9 @@ public class FullTextLucene extends FullText {
/** /**
* Commit all changes to the Lucene index. * Commit all changes to the Lucene index.
*/ */
void commitIndex() throws SQLException { private void commitIndex() throws SQLException {
try { try {
indexAccess.writer.commit(); indexAccess.commit();
// recreate Searcher with the IndexWriter's reader.
indexAccess.searcher.close();
indexAccess.reader.close();
indexAccess.reader = IndexReader.open(indexAccess.writer, true);
indexAccess.searcher = new IndexSearcher(indexAccess.reader);
} catch (IOException e) { } catch (IOException e) {
throw convertException(e); throw convertException(e);
} }
...@@ -699,22 +680,80 @@ public class FullTextLucene extends FullText { ...@@ -699,22 +680,80 @@ public class FullTextLucene extends FullText {
/** /**
* A wrapper for the Lucene writer and searcher. * A wrapper for the Lucene writer and searcher.
*/ */
static class IndexAccess { static final class IndexAccess {
/** /**
* The index writer. * The index writer.
*/ */
IndexWriter writer; final IndexWriter writer;
/** /**
* The index reader. * Map of usage counters for outstanding searchers.
*/ */
IndexReader reader; private final Map<IndexSearcher,Integer> counters = New.hashMap();
/**
* Usage counter for current searcher.
*/
private int counter;
/** /**
* The index searcher. * The index searcher.
*/ */
IndexSearcher searcher; private IndexSearcher searcher;
private IndexAccess(IndexWriter writer) throws IOException {
this.writer = writer;
IndexReader reader = IndexReader.open(writer, true);
searcher = new IndexSearcher(reader);
}
private synchronized IndexSearcher getSearcher() {
++counter;
return searcher;
}
private synchronized void returnSearcher(IndexSearcher searcher) {
if (this.searcher == searcher) {
--counter;
assert counter >= 0;
} else {
Integer cnt = counters.remove(searcher);
assert cnt != null;
if(--cnt == 0) {
closeSearcher(searcher);
} else {
counters.put(searcher, cnt);
}
}
} }
public synchronized void commit() throws IOException {
writer.commit();
if (counter != 0) {
counters.put(searcher, counter);
counter = 0;
} else {
closeSearcher(searcher);
}
// recreate Searcher with the IndexWriter's reader.
searcher = new IndexSearcher(IndexReader.open(writer, true));
}
public synchronized void close() throws IOException {
for (IndexSearcher searcher : counters.keySet()) {
closeSearcher(searcher);
}
counters.clear();
closeSearcher(searcher);
searcher = null;
writer.close();
}
private static void closeSearcher(IndexSearcher searcher) {
IndexReader indexReader = searcher.getIndexReader();
try { searcher.close(); } catch(IOException ignore) {/**/}
try { indexReader.close(); } catch(IOException ignore) {/**/}
}
}
} }
...@@ -10,20 +10,22 @@ import java.sql.PreparedStatement; ...@@ -10,20 +10,22 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.HashMap; import java.util.Collections;
import java.util.HashSet; import java.util.Map;
import java.util.Set;
import org.h2.util.New; import org.h2.util.New;
import org.h2.util.SoftHashMap; import org.h2.util.SoftHashMap;
/** /**
* The global settings of a full text search. * The global settings of a full text search.
*/ */
class FullTextSettings { final class FullTextSettings {
/** /**
* The settings of open indexes. * The settings of open indexes.
*/ */
private static final HashMap<String, FullTextSettings> SETTINGS = New.hashMap(); private static final Map<String, FullTextSettings> SETTINGS = Collections.synchronizedMap(New.<String, FullTextSettings>hashMap());
/** /**
* Whether this instance has been initialized. * Whether this instance has been initialized.
...@@ -33,17 +35,17 @@ class FullTextSettings { ...@@ -33,17 +35,17 @@ class FullTextSettings {
/** /**
* The set of words not to index (stop words). * The set of words not to index (stop words).
*/ */
private final HashSet<String> ignoreList = New.hashSet(); private final Set<String> ignoreList = Collections.synchronizedSet(New.<String>hashSet());
/** /**
* The set of words / terms. * The set of words / terms.
*/ */
private final HashMap<String, Integer> words = New.hashMap(); private final Map<String, Integer> words = Collections.synchronizedMap(New.<String, Integer>hashMap());
/** /**
* The set of indexes in this database. * The set of indexes in this database.
*/ */
private final HashMap<Integer, IndexInfo> indexes = New.hashMap(); private final Map<Integer, IndexInfo> indexes = Collections.synchronizedMap(New.<Integer, IndexInfo>hashMap());
/** /**
* The prepared statement cache. * The prepared statement cache.
...@@ -60,7 +62,7 @@ class FullTextSettings { ...@@ -60,7 +62,7 @@ class FullTextSettings {
/** /**
* Create a new instance. * Create a new instance.
*/ */
protected FullTextSettings() { private FullTextSettings() {
// don't allow construction // don't allow construction
} }
...@@ -69,7 +71,7 @@ class FullTextSettings { ...@@ -69,7 +71,7 @@ class FullTextSettings {
* *
* @return the ignore list * @return the ignore list
*/ */
protected HashSet<String> getIgnoreList() { protected Set<String> getIgnoreList() {
return ignoreList; return ignoreList;
} }
...@@ -78,7 +80,7 @@ class FullTextSettings { ...@@ -78,7 +80,7 @@ class FullTextSettings {
* *
* @return the word list * @return the word list
*/ */
protected HashMap<String, Integer> getWordList() { protected Map<String, Integer> getWordList() {
return words; return words;
} }
...@@ -140,7 +142,7 @@ class FullTextSettings { ...@@ -140,7 +142,7 @@ class FullTextSettings {
* @param conn the connection * @param conn the connection
* @return the file system path * @return the file system path
*/ */
protected static String getIndexPath(Connection conn) throws SQLException { private static String getIndexPath(Connection conn) throws SQLException {
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery( ResultSet rs = stat.executeQuery(
"CALL IFNULL(DATABASE_PATH(), 'MEM:' || DATABASE())"); "CALL IFNULL(DATABASE_PATH(), 'MEM:' || DATABASE())");
......
...@@ -50,10 +50,6 @@ public class TestFullText extends TestBase { ...@@ -50,10 +50,6 @@ public class TestFullText extends TestBase {
@Override @Override
public void test() throws Exception { public void test() throws Exception {
if (config.multiThreaded) {
// It is even mentioned in the docs that this is not supported
return;
}
testUuidPrimaryKey(false); testUuidPrimaryKey(false);
testAutoAnalyze(); testAutoAnalyze();
testNativeFeatures(); testNativeFeatures();
...@@ -71,7 +67,9 @@ public class TestFullText extends TestBase { ...@@ -71,7 +67,9 @@ public class TestFullText extends TestBase {
testCreateDropLucene(); testCreateDropLucene();
testUuidPrimaryKey(true); testUuidPrimaryKey(true);
testMultiThreaded(true); testMultiThreaded(true);
if(config.mvStore || !config.multiThreaded) {
testMultiThreaded(false); testMultiThreaded(false);
}
testTransaction(true); testTransaction(true);
test(true, "VARCHAR"); test(true, "VARCHAR");
test(true, "CLOB"); test(true, "CLOB");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论