import org.splat.som.Step;
/**
- * Implementation of the service for work with Lucen index.
+ * Implementation of the service for work with Lucen index.
*/
public class IndexServiceImpl implements IndexService {
/**
* The logger for this service.
*/
- private static final Logger LOG = Logger
- .getLogger(IndexServiceImpl.class);
+ private static final Logger LOG = Logger.getLogger(IndexServiceImpl.class);
- private final static String PROP_INDEX = "index";
- private final static String PROP_REF = "ref";
- private final static String PROP_STATE = "state";
+ private final static String PROP_INDEX = "index";
+ private final static String PROP_REF = "ref";
+ private final static String PROP_STATE = "state";
private final static String PROP_AUTHOR = "author";
- private final static String PROP_TITLE = "title";
-
+ private final static String PROP_TITLE = "title";
+
private transient Directory _index;
private transient org.apache.lucene.document.Document _body;
private ProjectElementService _projectElementService;
protected static StandardAnalyzer analyzer = new StandardAnalyzer(
Version.LUCENE_29);
+
private class Entry extends IndexWriter {
private transient final org.apache.lucene.document.Document _entry;
setContextAt(getProjectElementService().getSteps(study));
}
- private Entry(final KnowledgeElement kelm) throws CorruptIndexException,
- LockObtainFailedException, IOException {
+ private Entry(final KnowledgeElement kelm)
+ throws CorruptIndexException, LockObtainFailedException,
+ IOException {
super(_index, analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED);
// Addition of mandatory fields
}
private void add() throws CorruptIndexException, IOException {
- addDocument(_entry);
- // Save the new entry
- optimize(); // Should be called before committing the index
- close(); // Commits the index
+ try {
+ addDocument(_entry);
+ // Save the new entry
+ optimize(); // Should be called before committing the index
+ close(); // Commits the index
+ } finally {
+ if (IndexWriter.isLocked(FSDirectory.open(getRepositoryService()
+ .getRepositoryIndexDirectory()))) {
+ IndexWriter.unlock(FSDirectory.open(getRepositoryService()
+ .getRepositoryIndexDirectory()));
+ }
+ }
}
private void update() throws CorruptIndexException, IOException {
- String value = _entry.getField(PROP_REF).stringValue(); // Only field with unique value
- Term term = new Term(PROP_REF).createTerm(value);
- updateDocument(term, _entry);
- // Save the updated entry
- optimize(); // Should be called before committing the index
- close(); // Commits the index
+ try {
+ String value = _entry.getField(PROP_REF).stringValue(); // Only field with unique value
+ Term term = new Term(PROP_REF).createTerm(value);
+ updateDocument(term, _entry);
+ // Save the updated entry
+ optimize(); // Should be called before committing the index
+ close(); // Commits the index
+ } finally {
+ if (IndexWriter.isLocked(FSDirectory.open(getRepositoryService()
+ .getRepositoryIndexDirectory()))) {
+ IndexWriter.unlock(FSDirectory.open(getRepositoryService()
+ .getRepositoryIndexDirectory()));
+ }
+ }
}
private void setContextAt(final Step[] step) {
public void configure() throws IOException {
File indir = getRepositoryService().getRepositoryIndexDirectory();
+ if (_index != null) {
+ try {
+ _index.close();
+ } finally {
+ if (IndexWriter.isLocked(FSDirectory.open(getRepositoryService()
+ .getRepositoryIndexDirectory()))) {
+ IndexWriter.unlock(FSDirectory.open(getRepositoryService()
+ .getRepositoryIndexDirectory()));
+ }
+ }
+ }
_index = FSDirectory.open(indir);
_body = new org.apache.lucene.document.Document();
_body.add(new Field(PROP_INDEX, "", Field.Store.YES,
Field.Index.NOT_ANALYZED));
_body.add(new Field("type", "", Field.Store.YES,
Field.Index.NOT_ANALYZED));
- _body
- .add(new Field(PROP_REF, "", Field.Store.YES,
- Field.Index.NOT_ANALYZED));
- _body
- .add(new Field("area", "", Field.Store.NO,
- Field.Index.NOT_ANALYZED));
+ _body.add(new Field(PROP_REF, "", Field.Store.YES,
+ Field.Index.NOT_ANALYZED));
+ _body.add(new Field("area", "", Field.Store.NO,
+ Field.Index.NOT_ANALYZED));
_body.add(new Field(PROP_STATE, "", Field.Store.YES,
Field.Index.NOT_ANALYZED));
_body.add(new Field(PROP_AUTHOR, "", Field.Store.YES,
Field.Index.NOT_ANALYZED));
_body.add(new Field(PROP_TITLE, "", Field.Store.YES,
Field.Index.NOT_ANALYZED));
- _body
- .add(new Field("contents", "", Field.Store.NO,
- Field.Index.ANALYZED));
+ _body.add(new Field("contents", "", Field.Store.NO,
+ Field.Index.ANALYZED));
if (!this.exists()) {
this.create(); // Happens when re-indexing all studies
}
}
public void create() throws IOException {
+ if (IndexWriter.isLocked(FSDirectory.open(getRepositoryService()
+ .getRepositoryIndexDirectory()))) {
+ IndexWriter.unlock(FSDirectory.open(getRepositoryService()
+ .getRepositoryIndexDirectory()));
+ }
Directory index = FSDirectory.open(getRepositoryService()
.getRepositoryIndexDirectory());
- IndexWriter writer = new IndexWriter(index, analyzer, true,
- IndexWriter.MaxFieldLength.UNLIMITED);
- writer.close(); // ==== Creates an empty index
+ if (IndexWriter.isLocked(index)) {
+ IndexWriter.unlock(index);
+ }
+ IndexWriter writer = null;
+ try {
+ writer = new IndexWriter(index, analyzer, true,
+ IndexWriter.MaxFieldLength.UNLIMITED);
+ } finally {
+ if (writer != null) {
+ try {
+ writer.optimize();
+ writer.close(); // ==== Creates an empty index
+ } finally {
+ if (IndexWriter.isLocked(index)) {
+ IndexWriter.unlock(index);
+ }
+ }
+ }
+ }
}
// ==============================================================================================================================