]> SALOME platform Git repositories - tools/siman.git/blobdiff - Workspace/Siman-Common/src/org/splat/service/technical/IndexServiceImpl.java
Salome HOME
Search knowledge is implemented. Unit tests are improved. Lucene index is not used...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / technical / IndexServiceImpl.java
index 7e7602086572b28f2c68b4d5adfd9a28f9bc692f..c3b11cc96f3e4dc2f182938275689d00cd913888 100644 (file)
@@ -35,22 +35,21 @@ import org.splat.service.dto.Proxy;
 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;
@@ -58,6 +57,7 @@ public class IndexServiceImpl implements IndexService {
 
        protected static StandardAnalyzer analyzer = new StandardAnalyzer(
                        Version.LUCENE_29);
+
        private class Entry extends IndexWriter {
                private transient final org.apache.lucene.document.Document _entry;
 
@@ -101,8 +101,9 @@ public class IndexServiceImpl implements IndexService {
                        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
@@ -145,19 +146,35 @@ public class IndexServiceImpl implements IndexService {
                }
 
                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) {
@@ -243,6 +260,17 @@ public class IndexServiceImpl implements IndexService {
 
        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,
@@ -251,32 +279,50 @@ public class IndexServiceImpl implements IndexService {
                                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);
+                                       }
+                               }
+                       }
+               }
        }
 
        // ==============================================================================================================================