diff --git a/modules/suggest/src/java/org/apache/lucene/search/suggest/Lookup.java b/modules/suggest/src/java/org/apache/lucene/search/suggest/Lookup.java index f6abab6..e200225 100644 --- a/modules/suggest/src/java/org/apache/lucene/search/suggest/Lookup.java +++ b/modules/suggest/src/java/org/apache/lucene/search/suggest/Lookup.java @@ -150,21 +150,4 @@ public abstract class Lookup { */ public abstract boolean load(InputStream input) throws IOException; - /** - * Persist the constructed lookup data to a directory. Optional operation. - * @param storeDir directory where data can be stored. - * @return true if successful, false if unsuccessful or not supported. - * @throws IOException when fatal IO error occurs. - */ - public abstract boolean store(File storeDir) throws IOException; - - /** - * Discard current lookup data and load it from a previously saved copy. - * Optional operation. - * @param storeDir directory where lookup data was stored. - * @return true if completed successfully, false if unsuccessful or not supported. - * @throws IOException when fatal IO error occurs. - */ - public abstract boolean load(File storeDir) throws IOException; - } diff --git a/modules/suggest/src/java/org/apache/lucene/search/suggest/fst/FSTCompletionLookup.java b/modules/suggest/src/java/org/apache/lucene/search/suggest/fst/FSTCompletionLookup.java index 9bd0ce7..ca3913a 100644 --- a/modules/suggest/src/java/org/apache/lucene/search/suggest/fst/FSTCompletionLookup.java +++ b/modules/suggest/src/java/org/apache/lucene/search/suggest/fst/FSTCompletionLookup.java @@ -264,40 +264,6 @@ public class FSTCompletionLookup extends Lookup { return bucket == -1 ? null : Long.valueOf(bucket); } - /** - * Deserialization from disk. - */ - @Override - public synchronized boolean load(File storeDir) throws IOException { - File data = new File(storeDir, FILENAME); - if (!data.exists() || !data.canRead()) { - return false; - } - - this.higherWeightsCompletion = new FSTCompletion( - FST.read(data, NoOutputs.getSingleton())); - this.normalCompletion = new FSTCompletion( - higherWeightsCompletion.getFST(), false, exactMatchFirst); - - return true; - } - - /** - * Serialization to disk. - */ - @Override - public synchronized boolean store(File storeDir) throws IOException { - if (!storeDir.exists() || !storeDir.isDirectory() || !storeDir.canWrite()) { - return false; - } - - if (this.normalCompletion == null) - return false; - - normalCompletion.getFST().save(new File(storeDir, FILENAME)); - return true; - } - @Override public synchronized boolean store(OutputStream output) throws IOException { diff --git a/modules/suggest/src/java/org/apache/lucene/search/suggest/fst/WFSTCompletionLookup.java b/modules/suggest/src/java/org/apache/lucene/search/suggest/fst/WFSTCompletionLookup.java index 330cf3c..7dbf557 100644 --- a/modules/suggest/src/java/org/apache/lucene/search/suggest/fst/WFSTCompletionLookup.java +++ b/modules/suggest/src/java/org/apache/lucene/search/suggest/fst/WFSTCompletionLookup.java @@ -64,14 +64,6 @@ import org.apache.lucene.util.fst.Util.MinResult; public class WFSTCompletionLookup extends Lookup { /** - * File name for the automaton. - * - * @see #store(File) - * @see #load(File) - */ - private static final String FILENAME = "wfst.bin"; - - /** * FST, weights are encoded as costs: (Integer.MAX_VALUE-weight) */ // NOTE: like FSTSuggester, this is really a WFSA, if you want to @@ -128,18 +120,6 @@ public class WFSTCompletionLookup extends Lookup { } @Override - public boolean store(File storeDir) throws IOException { - fst.save(new File(storeDir, FILENAME)); - return true; - } - - @Override - public boolean load(File storeDir) throws IOException { - this.fst = FST.read(new File(storeDir, FILENAME), PositiveIntOutputs.getSingleton(true)); - return true; - } - - @Override public boolean store(OutputStream output) throws IOException { try { fst.save(new OutputStreamDataOutput(output)); diff --git a/modules/suggest/src/java/org/apache/lucene/search/suggest/jaspell/JaspellLookup.java b/modules/suggest/src/java/org/apache/lucene/search/suggest/jaspell/JaspellLookup.java index b7bb15e..1d9b047 100644 --- a/modules/suggest/src/java/org/apache/lucene/search/suggest/jaspell/JaspellLookup.java +++ b/modules/suggest/src/java/org/apache/lucene/search/suggest/jaspell/JaspellLookup.java @@ -19,9 +19,6 @@ package org.apache.lucene.search.suggest.jaspell; import java.io.DataInputStream; import java.io.DataOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -109,22 +106,12 @@ public class JaspellLookup extends Lookup { return res; } - public static final String FILENAME = "jaspell.dat"; private static final byte LO_KID = 0x01; private static final byte EQ_KID = 0x02; private static final byte HI_KID = 0x04; private static final byte HAS_VALUE = 0x08; - @Override - public boolean load(File storeDir) throws IOException { - File data = new File(storeDir, FILENAME); - if (!data.exists() || !data.canRead()) { - return false; - } - return load(new FileInputStream(data)); - } - private void readRecursively(DataInputStream in, TSTNode node) throws IOException { node.splitchar = in.readChar(); byte mask = in.readByte(); @@ -148,15 +135,6 @@ public class JaspellLookup extends Lookup { } } - @Override - public boolean store(File storeDir) throws IOException { - if (!storeDir.exists() || !storeDir.isDirectory() || !storeDir.canWrite()) { - return false; - } - File data = new File(storeDir, FILENAME); - return store(new FileOutputStream(data)); - } - private void writeRecursively(DataOutputStream out, TSTNode node) throws IOException { if (node == null) { return; diff --git a/modules/suggest/src/java/org/apache/lucene/search/suggest/tst/TSTLookup.java b/modules/suggest/src/java/org/apache/lucene/search/suggest/tst/TSTLookup.java index 99e4e6a..86a10cd 100644 --- a/modules/suggest/src/java/org/apache/lucene/search/suggest/tst/TSTLookup.java +++ b/modules/suggest/src/java/org/apache/lucene/search/suggest/tst/TSTLookup.java @@ -119,23 +119,12 @@ public class TSTLookup extends Lookup { return res; } - public static final String FILENAME = "tst.dat"; - private static final byte LO_KID = 0x01; private static final byte EQ_KID = 0x02; private static final byte HI_KID = 0x04; private static final byte HAS_TOKEN = 0x08; private static final byte HAS_VALUE = 0x10; - @Override - public synchronized boolean load(File storeDir) throws IOException { - File data = new File(storeDir, FILENAME); - if (!data.exists() || !data.canRead()) { - return false; - } - return load(new FileInputStream(data)); - } - // pre-order traversal private void readRecursively(DataInputStream in, TernaryTreeNode node) throws IOException { node.splitchar = in.readChar(); @@ -160,15 +149,6 @@ public class TSTLookup extends Lookup { } } - @Override - public synchronized boolean store(File storeDir) throws IOException { - if (!storeDir.exists() || !storeDir.isDirectory() || !storeDir.canWrite()) { - return false; - } - File data = new File(storeDir, FILENAME); - return store(new FileOutputStream(data)); - } - // pre-order traversal private void writeRecursively(DataOutputStream out, TernaryTreeNode node) throws IOException { // write out the current node diff --git a/modules/suggest/src/test/org/apache/lucene/search/suggest/PersistenceTest.java b/modules/suggest/src/test/org/apache/lucene/search/suggest/PersistenceTest.java index 73f5ae8..c8f4a78 100644 --- a/modules/suggest/src/test/org/apache/lucene/search/suggest/PersistenceTest.java +++ b/modules/suggest/src/test/org/apache/lucene/search/suggest/PersistenceTest.java @@ -17,6 +17,8 @@ package org.apache.lucene.search.suggest; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.util.List; import org.apache.lucene.search.suggest.Lookup; @@ -68,12 +70,12 @@ public class PersistenceTest extends LuceneTestCase { lookup.build(new TermFreqArrayIterator(keys)); // Store the suggester. - File storeDir = TEMP_DIR; - lookup.store(storeDir); + File storeFile = new File(TEMP_DIR, "lookup.dat"); + lookup.store(new FileOutputStream(storeFile )); // Re-read it from disk. lookup = lookupClass.newInstance(); - lookup.load(storeDir); + lookup.load(new FileInputStream(storeFile)); // Assert validity. long previous = Long.MIN_VALUE; diff --git a/solr/core/src/java/org/apache/solr/spelling/suggest/Suggester.java b/solr/core/src/java/org/apache/solr/spelling/suggest/Suggester.java index 525ce3b..f856232 100644 --- a/solr/core/src/java/org/apache/solr/spelling/suggest/Suggester.java +++ b/solr/core/src/java/org/apache/solr/spelling/suggest/Suggester.java @@ -18,6 +18,8 @@ package org.apache.solr.spelling.suggest; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; @@ -40,6 +42,7 @@ import org.apache.solr.spelling.SolrSpellChecker; import org.apache.solr.spelling.SpellingOptions; import org.apache.solr.spelling.SpellingResult; import org.apache.solr.spelling.suggest.fst.FSTLookupFactory; +import org.apache.solr.spelling.suggest.fst.WFSTLookupFactory; import org.apache.solr.spelling.suggest.jaspell.JaspellLookupFactory; import org.apache.solr.spelling.suggest.tst.TSTLookupFactory; import org.slf4j.Logger; @@ -65,6 +68,8 @@ public class Suggester extends SolrSpellChecker { */ public static final String STORE_DIR = "storeDir"; + public static final String STORE_FILE_NAME = "suggest.dat"; + protected String sourceLocation; protected File storeDir; protected float threshold; @@ -82,7 +87,6 @@ public class Suggester extends SolrSpellChecker { : (Float)config.get(THRESHOLD_TOKEN_FREQUENCY); sourceLocation = (String) config.get(LOCATION); lookupImpl = (String)config.get(LOOKUP_IMPL); - // support the old classnames without -Factory for config file backwards compatibility. if (lookupImpl == null || "org.apache.solr.spelling.suggest.jaspell.JaspellLookup".equals(lookupImpl)) { lookupImpl = JaspellLookupFactory.class.getName(); @@ -90,6 +94,8 @@ public class Suggester extends SolrSpellChecker { lookupImpl = TSTLookupFactory.class.getName(); } else if ("org.apache.solr.spelling.suggest.fst.FSTLookup".equals(lookupImpl)) { lookupImpl = FSTLookupFactory.class.getName(); + } else if ("org.apache.solr.spelling.suggest.fst.WFSTLookup".equals(lookupImpl)) { + lookupImpl = WFSTLookupFactory.class.getName(); } LookupFactory factory = (LookupFactory) core.getResourceLoader().newInstance(lookupImpl); @@ -105,7 +111,7 @@ public class Suggester extends SolrSpellChecker { } else { // attempt reload of the stored lookup try { - lookup.load(storeDir); + lookup.load(new FileInputStream(new File(storeDir, STORE_FILE_NAME))); } catch (IOException e) { LOG.warn("Loading stored lookup data failed", e); } @@ -132,7 +138,7 @@ public class Suggester extends SolrSpellChecker { try { lookup.build(dictionary); if (storeDir != null) { - lookup.store(storeDir); + lookup.store(new FileOutputStream(new File(storeDir, STORE_FILE_NAME))); } } catch (Exception e) { LOG.error("Error while building or storing Suggester data", e); @@ -144,7 +150,7 @@ public class Suggester extends SolrSpellChecker { LOG.info("reload()"); if (dictionary == null && storeDir != null) { // this may be a firstSearcher event, try loading it - if (lookup.load(storeDir)) { + if (lookup.load(new FileInputStream(new File(storeDir, STORE_FILE_NAME)))) { return; // loaded ok } LOG.debug("load failed, need to build Lookup again");