Index: src/main/java/org/crosswire/jsword/index/IndexManager.java =================================================================== --- src/main/java/org/crosswire/jsword/index/IndexManager.java (revision 1937) +++ src/main/java/org/crosswire/jsword/index/IndexManager.java (working copy) @@ -47,6 +47,22 @@ Index getIndex(Book book) throws BookException; /** + * Detect or checking whether it is need reindex. + * It is safe methods, you can always call it whether the book + * is already indexed or not. + * This check for
+ *
+     * - isIndexed(Book book)
+     * - Is index valid, eg index change due to internal structure change or Lucene update
+     * - etc
+     * 
+ * + * @param book the Book + * @return true if need reindex + */ + boolean isNeedReindex(Book book); + + /** * Read from the given source version to generate ourselves. On completion * of this method the index should be usable. */ Index: src/main/java/org/crosswire/jsword/index/lucene/LuceneIndex.java =================================================================== --- src/main/java/org/crosswire/jsword/index/lucene/LuceneIndex.java (revision 1937) +++ src/main/java/org/crosswire/jsword/index/lucene/LuceneIndex.java (working copy) @@ -21,7 +21,10 @@ */ package org.crosswire.jsword.index.lucene; +import java.io.BufferedReader; import java.io.File; +import java.io.FileWriter; +import java.io.FileReader; import java.io.IOException; import java.net.URI; import java.util.ArrayList; @@ -109,6 +112,11 @@ public static final String FIELD_NOTE = "note"; //$NON-NLS-1$ /** + * File name to store various index info eg: index version + */ + public static final String INDEX_INFO = "index-info.txt"; //$NON-NLS-1$ + + /** * Read an existing index and use it. * @throws BookException If we fail to read the index files */ @@ -119,6 +127,7 @@ try { this.path = NetUtil.getAsFile(storage).getCanonicalPath(); + readIndexInfo(); } catch (IOException ex) { @@ -212,6 +221,22 @@ Reporter.informUser(this, UserMsg.BAD_VERSE, buf); } + // try to write index info + File infoFile = new File(finalPath, INDEX_INFO); + FileWriter infoWriter = new FileWriter(infoFile); + try { + // What version numbering scheme that I need to use ??? + // is it ok to use IndexMetadata ? + String strVersion = Float.toString(IndexMetadata.instance().getLatestIndexVersion()); + infoWriter.write(strVersion); + } finally { + // make sure it doesn't lock the file, in case something happen + if (infoWriter != null) { + infoWriter.flush(); + infoWriter.close(); + } + } + } } catch (IOException ex) @@ -226,6 +251,42 @@ } } + /** Return the index version + * @return index version + */ + public float getVersion() { + return version; + } + + /** + * Currently it designed to swallow all exception to keep API compabilities (aka does not throw weird Exception) + * Is it better to make it throw IOException ??? + * + */ + protected void readIndexInfo() { + try + { + File infoFile = new File(this.path, INDEX_INFO); + BufferedReader reader = new BufferedReader(new FileReader(infoFile)); + String str; + try { + if ((str = reader.readLine()) != null) { + version = Float.parseFloat(str); + } + } finally { + // make sure it doesn't lock the file, in case something happen + if (reader != null) { + reader.close(); + } + } + } + catch (Exception ex) + { + // hard coded "1.2" or need to get the value from IndexMetadata.getInstalledIndexVersion() + version = 1.2f; + } + } + /* (non-Javadoc) * @see org.crosswire.jsword.index.search.Index#findWord(java.lang.String) */ @@ -525,4 +586,9 @@ * The Lucene search engine */ protected Searcher searcher; + + /** + * The index version + */ + protected float version = 1.2f; } Index: src/main/java/org/crosswire/jsword/index/lucene/LuceneIndexManager.java =================================================================== --- src/main/java/org/crosswire/jsword/index/lucene/LuceneIndexManager.java (revision 1937) +++ src/main/java/org/crosswire/jsword/index/lucene/LuceneIndexManager.java (working copy) @@ -90,6 +90,34 @@ } /* (non-Javadoc) + * @see org.crosswire.jsword.index.search.IndexManager#isNeedReindex(org.crosswire.jsword.book.Book) + */ + public boolean isNeedReindex(Book book) { + // step 1: check for existing index + if (!isIndexed(book)) { + return true; + } + + boolean reindex = false; + + // step 2: check for index version + try { + // need hard casting because it is only implemented on LuceneIndex, not the Index interface + LuceneIndex index = (LuceneIndex)getIndex(book); + + // need to compare with what ??? indexMetadata ? + if (index.getVersion() < IndexMetadata.instance().getInstalledIndexVersion()) { + reindex = true; + } + + } catch (Exception ex) { + reindex = true; + } + + return reindex; + } + + /* (non-Javadoc) * @see org.crosswire.jsword.index.search.AbstractIndex#generateSearchIndex(org.crosswire.common.progress.Job) */ public void scheduleIndexCreation(final Book book)