|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| LucenePrayerRequestSearcher.java | 70% | 73.7% | 85.7% | 74.5% |
|
||||||||||||||
| 1 |
package org.marketchangers.prayer.lucene;
|
|
| 2 |
|
|
| 3 |
import java.io.IOException;
|
|
| 4 |
import java.util.Collection;
|
|
| 5 |
import java.util.Collections;
|
|
| 6 |
import java.util.Iterator;
|
|
| 7 |
import java.util.LinkedList;
|
|
| 8 |
import java.util.List;
|
|
| 9 |
|
|
| 10 |
import org.apache.lucene.document.Document;
|
|
| 11 |
import org.apache.lucene.index.IndexReader;
|
|
| 12 |
import org.apache.lucene.index.IndexWriter;
|
|
| 13 |
import org.apache.lucene.search.Hits;
|
|
| 14 |
import org.apache.lucene.search.Query;
|
|
| 15 |
import org.marketchangers.prayer.PrayerRequest;
|
|
| 16 |
import org.marketchangers.prayer.PrayerRequestQuery;
|
|
| 17 |
import org.marketchangers.prayer.PrayerRequestSearcher;
|
|
| 18 |
|
|
| 19 |
/**
|
|
| 20 |
* @author <a href="mailto:jniu@wc-group.com">Jianshuo Niu</a>
|
|
| 21 |
* @author <a href="mailto:mtodd@wc-group.com">Matthew Todd</a>
|
|
| 22 |
*/
|
|
| 23 |
public class LucenePrayerRequestSearcher implements PrayerRequestSearcher { |
|
| 24 |
private IndexProvider provider;
|
|
| 25 |
|
|
| 26 | 18 |
public LucenePrayerRequestSearcher(IndexProvider provider) {
|
| 27 | 18 |
this.provider = provider;
|
| 28 |
} |
|
| 29 |
|
|
| 30 | 18 |
public synchronized void index(PrayerRequest request) { |
| 31 | 18 |
Document document = provider.translateToDocument(request); |
| 32 |
|
|
| 33 | 18 |
try {
|
| 34 | 18 |
IndexWriter writer = provider.getWriter(); |
| 35 | 18 |
writer.addDocument(document); |
| 36 | 18 |
writer.close(); |
| 37 |
} catch (IOException e) {
|
|
| 38 |
// TODO How should we handle this?
|
|
| 39 | 0 |
e.printStackTrace(); |
| 40 |
} |
|
| 41 |
} |
|
| 42 |
|
|
| 43 | 0 |
public synchronized void index(Collection prayerRequests) { |
| 44 | 0 |
Iterator requests = prayerRequests.iterator(); |
| 45 |
|
|
| 46 | 0 |
try {
|
| 47 | 0 |
IndexWriter writer = provider.getWriter(); |
| 48 |
|
|
| 49 | 0 |
while (requests.hasNext()) {
|
| 50 | 0 |
PrayerRequest request = (PrayerRequest) requests.next(); |
| 51 | 0 |
writer.addDocument(provider.translateToDocument(request)); |
| 52 |
} |
|
| 53 |
|
|
| 54 | 0 |
writer.close(); |
| 55 |
} catch (IOException e) {
|
|
| 56 |
// TODO How should we handle this?
|
|
| 57 | 0 |
e.printStackTrace(); |
| 58 |
} |
|
| 59 |
} |
|
| 60 |
|
|
| 61 | 20 |
public List search(PrayerRequestQuery q) {
|
| 62 | 20 |
if (q.getRequestorUserIds().isEmpty()) {
|
| 63 | 2 |
return Collections.EMPTY_LIST;
|
| 64 |
} |
|
| 65 |
|
|
| 66 | 18 |
Hits hits; |
| 67 |
|
|
| 68 | 18 |
try {
|
| 69 | 18 |
hits = provider.getSearcher().search(provider.translateToQuery(q)); |
| 70 |
} catch (IOException ioe) {
|
|
| 71 | 0 |
return Collections.EMPTY_LIST;
|
| 72 |
} |
|
| 73 |
|
|
| 74 | 18 |
List prayerRequestIds = new LinkedList();
|
| 75 |
|
|
| 76 | 18 |
for (int i = 0; i < hits.length(); i++) { |
| 77 | 8 |
try {
|
| 78 | 8 |
prayerRequestIds.add(id(hits, i)); |
| 79 |
} catch (IOException ioe) {
|
|
| 80 |
// what can we do?
|
|
| 81 |
} |
|
| 82 |
} |
|
| 83 |
|
|
| 84 | 18 |
return prayerRequestIds;
|
| 85 |
} |
|
| 86 |
|
|
| 87 | 2 |
public void updateStatus(PrayerRequest request) { |
| 88 | 2 |
Query query = provider.translateToQuery(request); |
| 89 | 2 |
List prayerRequestIds = new LinkedList();
|
| 90 |
|
|
| 91 |
//hits should contain only one documents id, otherwise,
|
|
| 92 |
// it is a bug in the search.
|
|
| 93 | 2 |
try {
|
| 94 | 2 |
Hits hits = provider.getSearcher().search(query); |
| 95 | 2 |
for (int i = 0; i < hits.length(); i++) { |
| 96 | 2 |
delete(hits.id(i)); |
| 97 |
} |
|
| 98 |
} catch (IOException ioe) {
|
|
| 99 |
// can't think of much to do here
|
|
| 100 |
} |
|
| 101 | 2 |
index(request); |
| 102 |
} |
|
| 103 |
|
|
| 104 | 8 |
private Integer id(Hits hits, int i) throws IOException { |
| 105 | 8 |
return Integer.valueOf(hits.doc(i).get("id")); |
| 106 |
} |
|
| 107 |
|
|
| 108 | 4 |
public synchronized void delete(int docId) throws IOException { |
| 109 |
|
|
| 110 | 4 |
IndexReader reader = provider.getReader(); |
| 111 | 4 |
if (reader != null) { |
| 112 | 4 |
reader.delete(docId); |
| 113 |
} |
|
| 114 | 4 |
reader.close(); |
| 115 |
} |
|
| 116 |
} |
|
| 117 |
|
|
||||||||||