removed checked exceptions from interfaces

This commit is contained in:
Timo Westkämper 2010-12-23 09:15:37 +00:00
parent af6f9df171
commit 1fbc56be30
5 changed files with 86 additions and 85 deletions

View File

@ -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> T query(QueryCallback<T> callback) throws CorruptIndexException, IOException;
<T> T query(QueryCallback<T> 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);
}

View File

@ -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<IndexSearcher> searcher = new AtomicReference<IndexSearcher>();
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> T query(QueryCallback<T> 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> T query(QueryCallback<T> 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);
}
}
}

View File

@ -8,5 +8,7 @@ import com.mysema.query.lucene.LuceneQuery;
*
*/
public interface QueryCallback<T> {
T query(LuceneQuery query);
}

View File

@ -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);
}

View File

@ -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<Long>() {
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();