提交 5841c4df authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #575 from andreitokar/full_text_mt

Support for full text search in multithreaded mode
...@@ -1320,13 +1320,6 @@ SELECT * FROM FTL_SEARCH_DATA('John', 0, 0); ...@@ -1320,13 +1320,6 @@ SELECT * FROM FTL_SEARCH_DATA('John', 0, 0);
SELECT * FROM FTL_SEARCH_DATA('LAST_NAME:John', 0, 0); SELECT * FROM FTL_SEARCH_DATA('LAST_NAME:John', 0, 0);
CALL FTL_DROP_ALL(); CALL FTL_DROP_ALL();
</pre> </pre>
<p>
The Lucene fulltext search implementation is not synchronized internally.
If you update the database and query the fulltext search concurrently
(directly using the Java API of H2 or Lucene itself), you need to ensure
operations are properly synchronized. If this is not the case, you may get
exceptions such as <code>org.apache.lucene.store.AlreadyClosedException: this IndexReader is closed</code>.
</p>
<h2 id="user_defined_variables">User-Defined Variables</h2> <h2 id="user_defined_variables">User-Defined Variables</h2>
<p> <p>
......
...@@ -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 = New.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 = New.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 = New.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,26 +62,63 @@ class FullTextSettings { ...@@ -60,26 +62,63 @@ class FullTextSettings {
/** /**
* Create a new instance. * Create a new instance.
*/ */
protected FullTextSettings() { private FullTextSettings() {
// don't allow construction // don't allow construction
} }
/** /**
* Get the ignore list. * Clear set of ignored words
*
* @return the ignore list
*/ */
protected HashSet<String> getIgnoreList() { public void clearInored() {
return ignoreList; synchronized (ignoreList) {
ignoreList.clear();
}
} }
/** /**
* Get the word list. * Amend set of ignored words
* * @param words to add
* @return the word list */
public void addIgnored(Iterable<String> words) {
synchronized (ignoreList) {
for (String word : words) {
word = normalizeWord(word);
ignoreList.add(word);
}
}
}
/**
* Clear set of searchable words
*/
public void clearWordList() {
synchronized (words) {
words.clear();
}
}
/**
* Get id for a searchable word
* @param word to find id for
* @return Integer id or null if word is not found
*/
public Integer getWordId(String word) {
synchronized (words) {
return words.get(word);
}
}
/**
* Register searchable word
* @param word to register
* @param id to register with
*/ */
protected HashMap<String, Integer> getWordList() { public void addWord(String word, Integer id) {
return words; synchronized (words) {
if(!words.containsKey(word)) {
words.put(word, id);
}
}
} }
/** /**
...@@ -109,10 +148,11 @@ class FullTextSettings { ...@@ -109,10 +148,11 @@ class FullTextSettings {
* @return the uppercase version of the word or null * @return the uppercase version of the word or null
*/ */
protected String convertWord(String word) { protected String convertWord(String word) {
// TODO this is locale specific, document word = normalizeWord(word);
word = word.toUpperCase(); synchronized (ignoreList) {
if (ignoreList.contains(word)) { if (ignoreList.contains(word)) {
return null; return null;
}
} }
return word; return word;
} }
...@@ -126,10 +166,13 @@ class FullTextSettings { ...@@ -126,10 +166,13 @@ class FullTextSettings {
protected static FullTextSettings getInstance(Connection conn) protected static FullTextSettings getInstance(Connection conn)
throws SQLException { throws SQLException {
String path = getIndexPath(conn); String path = getIndexPath(conn);
FullTextSettings setting = SETTINGS.get(path); FullTextSettings setting;
if (setting == null) { synchronized (SETTINGS) {
setting = new FullTextSettings(); setting = SETTINGS.get(path);
SETTINGS.put(path, setting); if (setting == null) {
setting = new FullTextSettings();
SETTINGS.put(path, setting);
}
} }
return setting; return setting;
} }
...@@ -140,7 +183,7 @@ class FullTextSettings { ...@@ -140,7 +183,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())");
...@@ -218,7 +261,9 @@ class FullTextSettings { ...@@ -218,7 +261,9 @@ class FullTextSettings {
* Close all fulltext settings, freeing up memory. * Close all fulltext settings, freeing up memory.
*/ */
protected static void closeAll() { protected static void closeAll() {
SETTINGS.clear(); synchronized (SETTINGS) {
SETTINGS.clear();
}
} }
protected void setWhitespaceChars(String whitespaceChars) { protected void setWhitespaceChars(String whitespaceChars) {
...@@ -229,4 +274,8 @@ class FullTextSettings { ...@@ -229,4 +274,8 @@ class FullTextSettings {
return whitespaceChars; return whitespaceChars;
} }
private String normalizeWord(String word) {
// TODO this is locale specific, document
return word.toUpperCase();
}
} }
...@@ -22,6 +22,7 @@ import java.util.concurrent.TimeUnit; ...@@ -22,6 +22,7 @@ import java.util.concurrent.TimeUnit;
import org.h2.fulltext.FullText; import org.h2.fulltext.FullText;
import org.h2.store.fs.FileUtils; import org.h2.store.fs.FileUtils;
import org.h2.test.TestAll;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.util.IOUtils; import org.h2.util.IOUtils;
import org.h2.util.Task; import org.h2.util.Task;
...@@ -50,10 +51,6 @@ public class TestFullText extends TestBase { ...@@ -50,10 +51,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 +68,9 @@ public class TestFullText extends TestBase { ...@@ -71,7 +68,9 @@ public class TestFullText extends TestBase {
testCreateDropLucene(); testCreateDropLucene();
testUuidPrimaryKey(true); testUuidPrimaryKey(true);
testMultiThreaded(true); testMultiThreaded(true);
testMultiThreaded(false); if(config.mvStore || !config.multiThreaded) {
testMultiThreaded(false);
}
testTransaction(true); testTransaction(true);
test(true, "VARCHAR"); test(true, "VARCHAR");
test(true, "CLOB"); test(true, "CLOB");
...@@ -256,7 +255,7 @@ public class TestFullText extends TestBase { ...@@ -256,7 +255,7 @@ public class TestFullText extends TestBase {
int len = 2; int len = 2;
Task[] task = new Task[len]; Task[] task = new Task[len];
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
final Connection conn = getConnection("fullText", connList); final Connection conn = getConnection("fullText;LOCK_TIMEOUT=60000", connList);
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
initFullText(stat, lucene); initFullText(stat, lucene);
initFullText(stat, lucene); initFullText(stat, lucene);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论