Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-2585 NPE in TrieStringMatcher - move code preparing TrieNode for matching into synchronized method - add note about thread-safety

Sebastian Nagelb1c46c48451b
diff --git a/src/java/org/apache/nutch/util/TrieStringMatcher.java b/src/java/org/apache/nutch/util/TrieStringMatcher.java
index fddecf3bc..d974ecbc1 100644
--- a/src/java/org/apache/nutch/util/TrieStringMatcher.java
+++ b/src/java/org/apache/nutch/util/TrieStringMatcher.java
@@ -23,6 +23,8 @@ import java.util.ListIterator;
/**
* TrieStringMatcher is a base class for simple tree-based string matching.
*
+ * This class is thread-safe during string matching but not when adding strings
+ * to the trie.
*/
public abstract class TrieStringMatcher {
protected TrieNode root;
@@ -103,9 +105,7 @@ public abstract class TrieStringMatcher {
*/
TrieNode getChild(char nextChar) {
if (children == null) {
- children = childrenList.toArray(new TrieNode[childrenList.size()]);
- childrenList = null;
- Arrays.sort(children);
+ compile();
}
int min = 0;
@@ -137,6 +137,18 @@ public abstract class TrieStringMatcher {
// if (this.nodeChar > other.nodeChar)
return 1;
}
+
+ /**
+ * Prepare node for matching. Note: this method is synchronized because it
+ * may be called concurrently when the trie is used for matching.
+ */
+ synchronized void compile() {
+ if (childrenList != null) {
+ children = childrenList.toArray(new TrieNode[childrenList.size()]);
+ childrenList = null;
+ Arrays.sort(children);
+ }
+ }
}
/**