📄 abstractcollection.java
字号:
}
/**
* Return the version of the Index.
*
* @return version of last Index
*/
public final long getVersion() {
return version;
}
/**
* Index the given Collection.
*
* @param fullIndex
* indicated whether a full or incremental index should be
* created
* @throws IndexException
* if the Collections can not be indexed
*/
public abstract void index(final boolean fullIndex) throws IndexException;
/**
* Index the given Collection in a background thread. Stops the indexing if
* already running.
*
* @param fullIndex
* indicated whether a full or incremental index should be
* created
* @throws IndexException
* if the Collections can not be indexed
*/
public final void indexInThread(final boolean fullIndex)
throws IndexException {
if (isIndexingInProgress()) {
log.warn("Collection " + name
+ " is already being indexed, now stopping");
stopRequest();
return;
}
stopRequested = false;
Runnable r = new Runnable() {
public void run() {
try {
index(fullIndex);
} catch (Exception x) {
// in case ANY exception slips through
log
.error(
"Can't succesfully finish background indexing process",
x);
}
}
};
if (fullIndex) {
// update the info now so it is shown in user interface
numberOfDocs = 0;
lastIndexed = new Date();
}
indexingThread = new Thread(r);
indexingThread.setName(name + "IndexingThread");
if (manager.getPriority() != null) {
indexingThread.setPriority(manager.getPriority().intValue());
} else {
indexingThread.setPriority(Thread.NORM_PRIORITY);
}
indexingThread.start();
}
/**
* Initialize this collection by getting its index. It retrieves the number
* of documents and the MD5 hash of all documents in the collection.
*
* If the index does not exist (this is a new Collection) just return.
*
* @throws IndexException
* when existing index of Collection can not be succesfully
* opened.
*/
public final void init() throws IndexException {
log.debug("Initializing collection " + name);
IndexReader index = null;
// Determine whether the collection exists on disk
setExistsOnDisk();
// check whether this collection has a cache for the MD5 hashes of
// documents
if (md5DocumentCache == null) {
md5DocumentCache = new HashSet();
}
// check whether this collection has a cache for the MD5 hashes of
// indexed archives
if (archiveCache == null) {
archiveCache = new HashSet();
}
if (!isIndexValid()) {
log.info("Index does not exist (yet) for collection '" + name
+ "'. Possibly new collection.");
numberOfDocs = 0;
return;
}
// Get the index
File thisIndex = getIndexDirWithManagerDefaults();
try {
index = IndexReader.open(thisIndex);
if (index != null) {
numberOfDocs = index.numDocs();
// retrieve all hashes of Documents from the cache
md5DocumentCache.clear();
for (int i = 0; i < numberOfDocs; i++) {
Document d = index.document(i);
String hashValue = d.get("hash");
md5DocumentCache.add(hashValue);
}
// get some relevant information from the index
version = IndexReader.getCurrentVersion(thisIndex);
// deprecated, but needed
lastIndexed = new Date(IndexReader.lastModified(thisIndex));
log.debug("Collection " + name + " has " + numberOfDocs
+ " documents, index created at: " + lastIndexed);
} else {
log
.error("Index could not be retrieved for collection "
+ name);
}
} catch (IOException e) {
throw new IndexException("Error initializing collection '" + name
+ "'", e);
} finally {
if (index != null) {
try {
index.close();
} catch (IOException e1) {
log.error("Error closing index for collection " + name, e1);
}
} else {
numberOfDocs = 0;
version = 0;
lastIndexed = null;
}
}
}
/**
* Returns whether the collection exists on disk. It does not actually
* determine that.
*
* @return true if existsOnDisk
*/
public final boolean isExistsOnDisk() {
return existsOnDisk;
}
/**
* Indicates whether any indexing is going on.
*
* @return true if so.
*/
public final boolean isIndexingInProgress() {
if (indexingThread == null) {
return false;
}
return indexingThread.isAlive();
}
/**
* Check whether the index of this collection is valid. An index is valid
* when the directory exists and there is an index in it.
*
* @return true if the index is valid, otherwise false.
*
* @throws IndexException
* when existing index of Collection can not be succesfully
* opened.
*/
public final boolean isIndexValid() throws IndexException {
File file = this.getIndexDirWithManagerDefaults();
if ((file == null) || !IndexReader.indexExists(file)) {
log.warn("Index '" + file + "' not valid for collection '" + name
+ "'.");
return false;
}
return true;
}
/**
* Returns whether the cache containing archive's contents should be kept
* after being indexed.
*
* @return true if so.
*/
public final boolean isKeepCache() {
return keepCache;
}
/**
* Determines whether the cache containing archive's contents should be kept
* after being indexed. It does so by retrieving the defaults from the
* manager if needed.
*
* @return true if so.
*/
public final boolean isKeepCacheWithManagerDefaults() {
if (isKeepCacheSet) {
return keepCache;
} else {
if (manager != null) {
// the value was not set here, get it from manager.
return manager.isKeepCache();
}
}
return false;
}
/**
* Indicates whether collection is just instantiated and has no id yet.
*
* @return true if has no id yet
*/
public final boolean isNew() {
return id == null;
}
/**
* Sets analyzer and creates an Analyzer object as specified by the given
* String.
*
* @param analyzerClassName
* the name of the class. The actual class needs to be available
* on the classpath.
*/
public final void setAnalyzer(final String analyzerClassName) {
try {
if (StringUtils.hasText(analyzerClassName)) {
analyzer = analyzerClassName;
Class c = Class.forName(analyzerClassName);
if (c != null) {
log.debug("Returning Analyzer: '" + analyzerClassName + "'");
analyzerObject = (Analyzer) c.newInstance();
}
}
} catch (InstantiationException e1) {
log.warn("Can not initiate Analyzer '" + analyzerClassName, e1);
} catch (IllegalAccessException e1) {
log.warn("Can not access Analyzer " + analyzerClassName, e1);
} catch (ClassNotFoundException e) {
log.warn("Class not found: " + analyzerClassName, e);
}
}
/**
* Sets the cacheDir of the collection.
*
* The cache is used to (temporarily) store expanded content, such as zip
* files.
*
* @param thisCacheDir
* The directory where the cache of this collection is stored on
* disk.
*/
public final void setCacheDir(final File thisCacheDir) {
cacheDir = thisCacheDir;
}
/**
* Sete the cacheUrl of the collection.
*
* The URL is the prefix that corresponds to the cacheDir. e.g. A document
* 'ldap.pdf' in cacheDir 'd:\temp\books\cache\' can be returned in a search
* result as http://search.company.com/books/cache/ldap.pdf The URL in this
* case is the prefix 'http://search.company.com/books/cache/'
*
* @param theCacheURL
* the URL of this collection's cache.
*/
public final void setCacheUrl(final String theCacheURL) {
cacheUrl = theCacheURL;
}
/**
* Sets the content directory of the collection, and checks whether the
* contentDir actually exists by setting existsOnDisk.
*
* @param theContentDir
* directory where collection sits on disk.
*/
public final void setContentDir(final File theContentDir) {
contentDir = theContentDir;
// check whether contentDir is really a directory
setExistsOnDisk();
if (!isExistsOnDisk()) {
log.warn("Set contentDir for collection '" + name
+ "' at non-existing: " + contentDir);
} else {
log.info("Set contentDir for collection '" + name + "' at: "
+ contentDir);
}
}
/**
* Set the description of the collection.
*
* @param theDescription
* description for collection
*/
public final void setDescription(final String theDescription) {
description = theDescription;
}
/**
* Sets existsOnDisk based on whether the collection (contentDir) actually
* (now) sits on disk.
*
* @todo the whole existsOnDisk construction is a little funny, refactor
* some time
*/
protected abstract void setExistsOnDisk();
/**
* Set the id of the collection. The id is used by the collectionManager to
* add and retrieve collections.
*
* @param theId
* the Id
*/
public final void setId(final Long theId) {
id = theId;
}
/**
* Set the indexDir of this collection. The indexDir is the directory where
* the index is stored.
*
* <p>
* e.g. d:\temp\zilverline\index
* </p>
*
* @param theIndexDir
* path to the index
*/
public final void setIndexDir(final File theIndexDir) {
indexDir = theIndexDir;
}
/**
* Set whether the cache should be deleted after indexing this collection.
* In the meantime mark this attribute as being set externally, to
* distinguish from the default value of a boolean.
*
* @param b
* true or false
*/
public final void setKeepCache(final boolean b) {
keepCache = b;
isKeepCacheSet = true;
}
/**
* Set the collectionManager.
*
* @param thisManager
* The CollectionManager holding this collection.
*/
public final void setManager(final CollectionManager thisManager) {
this.manager = thisManager;
}
/**
* Set the name of the Collection.
*
* @param theName
* name for collection
*/
public final void setName(final String theName) {
name = theName;
}
/**
* Set the URL of this Collection. The URL is the prefix that corresponds to
* the contentDir. e.g. A document 'ldap.pdf' in contentDir
* 'e:\collection\books\' can be returned in a search result as
* http://search.company.com/books/ldap.pdf The URL in this case is the
* prefix 'http://search.company.com/books/'
*
* @param theURL
* the URL of this collection.
*/
public final void setUrl(final String theURL) {
url = theURL;
}
/**
* Stop the indexing thread.
*/
public final void stopRequest() {
stopRequested = true;
indexingThread.interrupt();
}
/**
* Resets the cache by deleting all keys, and removing documents form cache.
*
* @param fullIndex
* @throws IndexException
*/
protected void resetCache(final boolean fullIndex) throws IndexException {
// create a cache for MD5 hashes if there's not one yet (new collection,
// first indexed)
if (this.getMd5DocumentCache() == null) {
this.init();
}
if (fullIndex) {
this.getMd5DocumentCache().clear();
// make sure the cache is flushed
if ((this.getCacheDirWithManagerDefaults() != null)
&& this.getCacheDirWithManagerDefaults().exists()) {
log.debug("Removing cache of " + this.getName());
boolean success = FileUtils.removeDir(this
.getCacheDirWithManagerDefaults());
if (!success) {
log
.warn("Could not entirely delete cache prior to indexing '"
+ this.getContentDir()
+ "'. \n\tThis may result in an inconsistent index, as some cache may not be inaccessible, "
+ "\n\tor old parts of cache included in new index.");
}
}
}
}
/**
* Collections are equal if their id are.
*
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
if (obj instanceof AbstractCollection) {
AbstractCollection thatCollection = ((AbstractCollection) obj);
if (this.getId() == null) {
return thatCollection.getId() == null;
}
return thatCollection.getId().equals(this.getId());
}
return false;
}
public int hashCode() {
return this.getId().hashCode();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -