diff --git a/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/LuceneSession.java b/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/LuceneSession.java index 0720c55ae..34c402034 100644 --- a/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/LuceneSession.java +++ b/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/LuceneSession.java @@ -1,10 +1,5 @@ package com.mysema.query.lucene.session; -import java.io.IOException; -import java.util.List; - -import org.apache.lucene.document.Document; -import org.apache.lucene.index.CorruptIndexException; /** * General interface on using Lucene. @@ -16,30 +11,25 @@ public interface LuceneSession { /** * Lucene query callback for querying * - * @param clazz * @param callback * @return - * @throws IOException - * @throws CorruptIndexException */ - T query(QueryCallback callback) throws CorruptIndexException, IOException; + T query(QueryCallback callback); /** * Creates a new index, adds updates to it and publishes the new index to * all readers after callback finishes. * * @param callback - * @throws IOException */ - void updateNew(WriteCallback callback) throws IOException; + void updateNew(WriteCallback callback); /** * Updates the current index and publishes it to all readers after callback * finishes. * * @param callback - * @throws IOException */ - void update(WriteCallback callback) throws IOException; + void update(WriteCallback callback); } diff --git a/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/LuceneSessionImpl.java b/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/LuceneSessionImpl.java index bfc9665dd..6cc1a5324 100644 --- a/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/LuceneSessionImpl.java +++ b/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/LuceneSessionImpl.java @@ -2,12 +2,9 @@ package com.mysema.query.lucene.session; import java.io.File; import java.io.IOException; -import java.util.List; import java.util.concurrent.atomic.AtomicReference; import org.apache.lucene.analysis.standard.StandardAnalyzer; -import org.apache.lucene.document.Document; -import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriter.MaxFieldLength; import org.apache.lucene.search.IndexSearcher; @@ -17,6 +14,7 @@ import org.apache.lucene.util.Version; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.mysema.query.QueryException; import com.mysema.query.lucene.LuceneQuery; import com.mysema.query.lucene.LuceneSerializer; @@ -28,53 +26,48 @@ import com.mysema.query.lucene.LuceneSerializer; */ public class LuceneSessionImpl implements LuceneSession { - private final Logger logger = LoggerFactory.getLogger(LuceneSessionImpl.class); - private Directory directory; + private final Directory directory; private final AtomicReference searcher = new AtomicReference(); - - private LuceneSerializer serializer = new LuceneSerializer(true, true); + + private final LuceneSerializer serializer = new LuceneSerializer(true, true); + + public LuceneSessionImpl(Directory directory) { + this.directory = directory; + } public LuceneSessionImpl(String indexPath) throws IOException { File folder = new File(indexPath); if (!folder.exists() && !folder.mkdirs()) { - throw new IOException("Could not create directory: " + folder.getAbsolutePath()); + throw new IOException("Could not create directory: " + + folder.getAbsolutePath()); } try { directory = new SimpleFSDirectory(folder); } catch (IOException e) { - logger.error("Could not create lucene directory to " + folder.getAbsolutePath()); + logger.error("Could not create lucene directory to " + + folder.getAbsolutePath()); throw e; } } - public LuceneSessionImpl(Directory directory) { - this.directory = directory; - } - - @Override - public T query(QueryCallback callback) throws CorruptIndexException, IOException { - - IndexSearcher is = getSearcher(); - T results = null; - try { - // Incrementing the reference count + private IndexSearcher createNewSearcher(IndexSearcher expected) throws IOException { + IndexSearcher is = new IndexSearcher(directory); + if (!searcher.compareAndSet(expected, is)) { + // Some thread already created a new one so just close this + is.close(); + } else { + // Incrementing the reference count first time + // We want to keep using the same reader until the index is changed is.getIndexReader().incRef(); - results = callback.query(new LuceneQuery(serializer, is)); - - } finally { - // Releasing the reference count - // This can be the last to actually close the reader - is.getIndexReader().decRef(); } - - return results; + return searcher.get(); } - private IndexSearcher getSearcher() throws CorruptIndexException, IOException { + private IndexSearcher getSearcher() throws IOException { if (searcher.get() == null) { createNewSearcher(null); } @@ -95,34 +88,41 @@ public class LuceneSessionImpl implements LuceneSession { return searcher.get(); } - private IndexSearcher createNewSearcher(IndexSearcher expected) throws IOException { - IndexSearcher is = new IndexSearcher(directory); - if (!searcher.compareAndSet(expected, is)) { - // Some thread already created a new one so just close this - is.close(); - } else { - // Incrementing the reference count first time - // We want to keep using the same reader until the index is changed - is.getIndexReader().incRef(); + @Override + public T query(QueryCallback callback) { + try { + IndexSearcher is = getSearcher(); + T results = null; + try { + // Incrementing the reference count + is.getIndexReader().incRef(); + results = callback.query(new LuceneQuery(serializer, is)); + + } finally { + // Releasing the reference count + // This can be the last to actually close the reader + is.getIndexReader().decRef(); + } + + return results; + } catch (IOException e) { + throw new QueryException(e); } - return searcher.get(); - } - - @Override - public void updateNew(WriteCallback callback) throws IOException { - update(callback, true); } @Override - public void update(WriteCallback callback) throws IOException { - update(callback, false); + public void update(WriteCallback callback) { + try { + update(callback, false); + } catch (IOException e) { + throw new QueryException(e); + } } private void update(WriteCallback callback, boolean create) throws IOException { - IndexWriter writer = - new IndexWriter(directory, new StandardAnalyzer(Version.LUCENE_CURRENT), create, - MaxFieldLength.LIMITED); + IndexWriter writer = new IndexWriter(directory, new StandardAnalyzer( + Version.LUCENE_CURRENT), create, MaxFieldLength.LIMITED); try { callback.write(writer); @@ -143,4 +143,14 @@ public class LuceneSessionImpl implements LuceneSession { } + @Override + public void updateNew(WriteCallback callback) { + try { + update(callback, true); + } catch (IOException e) { + throw new QueryException(e); + } + + } + } diff --git a/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/QueryCallback.java b/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/QueryCallback.java index b229d9536..53fcd733f 100644 --- a/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/QueryCallback.java +++ b/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/QueryCallback.java @@ -8,5 +8,7 @@ import com.mysema.query.lucene.LuceneQuery; * */ public interface QueryCallback { + T query(LuceneQuery query); + } diff --git a/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/WriteCallback.java b/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/WriteCallback.java index 81f5e45f1..9d32a17bf 100644 --- a/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/WriteCallback.java +++ b/querydsl-lucene/src/main/java/com/mysema/query/lucene/session/WriteCallback.java @@ -1,8 +1,5 @@ package com.mysema.query.lucene.session; -import java.io.IOException; - -import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexWriter; /** @@ -13,6 +10,6 @@ import org.apache.lucene.index.IndexWriter; */ public interface WriteCallback { - void write(IndexWriter writer) throws CorruptIndexException, IOException; + void write(IndexWriter writer); } diff --git a/querydsl-lucene/src/test/java/com/mysema/query/lucene/session/LuceneSessionImplTest.java b/querydsl-lucene/src/test/java/com/mysema/query/lucene/session/LuceneSessionImplTest.java index c09dab183..40782fcd8 100644 --- a/querydsl-lucene/src/test/java/com/mysema/query/lucene/session/LuceneSessionImplTest.java +++ b/querydsl-lucene/src/test/java/com/mysema/query/lucene/session/LuceneSessionImplTest.java @@ -1,26 +1,23 @@ package com.mysema.query.lucene.session; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; import java.io.IOException; import java.util.List; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; +import org.apache.lucene.document.NumericField; import org.apache.lucene.document.Field.Index; import org.apache.lucene.document.Field.Store; -import org.apache.lucene.document.NumericField; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexWriter; -import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.store.Directory; import org.apache.lucene.store.RAMDirectory; import org.junit.Before; import org.junit.Test; import com.mysema.query.lucene.LuceneQuery; -import com.mysema.query.lucene.LuceneSerializer; import com.mysema.query.types.PathMetadataFactory; import com.mysema.query.types.path.EntityPathBase; import com.mysema.query.types.path.NumberPath; @@ -67,20 +64,27 @@ public class LuceneSessionImplTest { public void testCreate() throws IOException { session.updateNew(new WriteCallback() { - public void write(IndexWriter writer) throws CorruptIndexException, IOException { + public void write(IndexWriter writer){ - writer.addDocument(createDocument( - "Jurassic Park", - "Michael Crichton", - "It's a UNIX system! I know this!", - 1990, - 90.00)); - writer.addDocument(createDocument( - "Nummisuutarit", - "Aleksis Kivi", - "ESKO. Ja iloitset ja riemuitset?", - 1864, - 10.00)); + try { + writer.addDocument(createDocument( + "Jurassic Park", + "Michael Crichton", + "It's a UNIX system! I know this!", + 1990, + 90.00)); + writer.addDocument(createDocument( + "Nummisuutarit", + "Aleksis Kivi", + "ESKO. Ja iloitset ja riemuitset?", + 1864, + 10.00)); + } catch (CorruptIndexException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + } }); @@ -98,8 +102,6 @@ public class LuceneSessionImplTest { Long count = session.query(new QueryCallback() { public Long query(LuceneQuery query) { - - //TODO Tästä tulee 0 eikä 2!! //return query.where(title.ne("AA")).count(); return query.where(title.startsWith("Nummi")).count();