| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
package org.crosswire.jsword.index.lucene; |
| 21 | |
|
| 22 | |
import java.io.IOException; |
| 23 | |
|
| 24 | |
import org.crosswire.common.util.PropertyMap; |
| 25 | |
import org.crosswire.common.util.ResourceUtil; |
| 26 | |
import org.crosswire.jsword.book.Book; |
| 27 | |
import org.crosswire.jsword.book.BookFilter; |
| 28 | |
import org.crosswire.jsword.book.BookMetaData; |
| 29 | |
import org.crosswire.jsword.book.Books; |
| 30 | |
import org.crosswire.jsword.index.IndexManagerFactory; |
| 31 | |
import org.slf4j.Logger; |
| 32 | |
import org.slf4j.LoggerFactory; |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
public final class IndexMetadata { |
| 42 | |
|
| 43 | |
|
| 44 | |
public static final float INDEX_VERSION_1_2 = 1.2f; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
@Deprecated |
| 51 | |
public static final float INDEX_VERSION_1_1 = 1.1f; |
| 52 | |
|
| 53 | |
public static final String LATEST_INDEX_VERSION = "Latest.Index.Version"; |
| 54 | |
public static final String LUCENE_VERSION = "Lucene.Version"; |
| 55 | |
|
| 56 | |
public static final String PREFIX_LATEST_INDEX_VERSION_BOOK_OVERRIDE = "Latest.Index.Version.Book."; |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
@Deprecated |
| 61 | |
public static final String INDEX_VERSION = "Installed.Index.Version"; |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
public static IndexMetadata instance() { |
| 69 | 0 | return myInstance; |
| 70 | |
} |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
@Deprecated |
| 79 | |
public float getInstalledIndexVersion() { |
| 80 | 0 | String value = props.get(INDEX_VERSION, "1.1"); |
| 81 | |
|
| 82 | 0 | return Float.parseFloat(value); |
| 83 | |
} |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
public float getLatestIndexVersion() { |
| 88 | 0 | String value = props.get(LATEST_INDEX_VERSION, "1.2"); |
| 89 | 0 | return Float.parseFloat(value); |
| 90 | |
} |
| 91 | |
|
| 92 | |
public float getLatestIndexVersion(Book b) { |
| 93 | 0 | if (b == null) { |
| 94 | 0 | return getLatestIndexVersion(); |
| 95 | |
} |
| 96 | |
|
| 97 | 0 | String value = props.get(PREFIX_LATEST_INDEX_VERSION_BOOK_OVERRIDE + IndexMetadata.getBookIdentifierPropSuffix(b.getBookMetaData()), |
| 98 | |
props.get(LATEST_INDEX_VERSION)); |
| 99 | 0 | return Float.parseFloat(value); |
| 100 | |
} |
| 101 | |
|
| 102 | |
public String getLatestIndexVersionStr() { |
| 103 | 0 | String value = props.get(LATEST_INDEX_VERSION, "1.2"); |
| 104 | 0 | return value; |
| 105 | |
} |
| 106 | |
|
| 107 | |
|
| 108 | |
public static String getBookIdentifierPropSuffix(BookMetaData meta) { |
| 109 | 0 | String moduleVer = null; |
| 110 | 0 | if (meta.getProperty("Version") != null) { |
| 111 | 0 | moduleVer = '[' + meta.getProperty("Version") + ']'; |
| 112 | |
} |
| 113 | |
|
| 114 | 0 | return meta.getInitials() + moduleVer; |
| 115 | |
|
| 116 | |
} |
| 117 | |
|
| 118 | |
public float getLuceneVersion() { |
| 119 | 0 | return Float.parseFloat(props.get(LUCENE_VERSION)); |
| 120 | |
} |
| 121 | |
|
| 122 | 0 | private IndexMetadata() { |
| 123 | |
try { |
| 124 | 0 | props = ResourceUtil.getProperties(getClass()); |
| 125 | 0 | } catch (IOException e) { |
| 126 | 0 | log.error("Property file read error", e); |
| 127 | 0 | } |
| 128 | 0 | } |
| 129 | |
|
| 130 | |
|
| 131 | |
public static String generateInstalledBooksIndexVersionReport(BookFilter filter) { |
| 132 | 0 | StringBuilder toReturn = new StringBuilder(); |
| 133 | 0 | int installedBookCount = 0; |
| 134 | 0 | int searchEnabledBookCount = 0; |
| 135 | 0 | int reindexMandatoryBookCount = 0; |
| 136 | 0 | LuceneIndexManager indexManager = (LuceneIndexManager) IndexManagerFactory.getIndexManager(); |
| 137 | 0 | Books myBooks = Books.installed(); |
| 138 | 0 | toReturn.append("InstalledBooks:"); |
| 139 | 0 | for (Book insBook : myBooks.getBooks(filter)) { |
| 140 | 0 | installedBookCount++; |
| 141 | 0 | toReturn.append("\n\t").append(insBook.getBookMetaData().getInitials()).append(": "); |
| 142 | 0 | if (indexManager.isIndexed(insBook)) { |
| 143 | 0 | searchEnabledBookCount++; |
| 144 | 0 | toReturn.append("search enabled, "); |
| 145 | 0 | if (indexManager.needsReindexing(insBook)) { |
| 146 | 0 | reindexMandatoryBookCount++; |
| 147 | 0 | toReturn.append("index outdated, "); |
| 148 | |
} |
| 149 | |
} |
| 150 | |
|
| 151 | |
} |
| 152 | 0 | toReturn.append("\nSummary: installedBooks ").append(installedBookCount).append(", searchEnabledBooks ").append(searchEnabledBookCount) |
| 153 | |
.append(", booksWithOutdatedIndex ").append(reindexMandatoryBookCount).append("\n"); |
| 154 | 0 | return toReturn.toString(); |
| 155 | |
} |
| 156 | |
|
| 157 | 0 | private static final Logger log = LoggerFactory.getLogger(IndexMetadata.class); |
| 158 | 0 | private static IndexMetadata myInstance = new IndexMetadata(); |
| 159 | |
private PropertyMap props; |
| 160 | |
} |