| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.search.solr.internal.job; |
| 21 |
|
|
| 22 |
|
import java.util.Collections; |
| 23 |
|
import java.util.List; |
| 24 |
|
|
| 25 |
|
import javax.inject.Inject; |
| 26 |
|
import javax.inject.Named; |
| 27 |
|
import javax.inject.Provider; |
| 28 |
|
|
| 29 |
|
import org.apache.commons.lang3.tuple.ImmutablePair; |
| 30 |
|
import org.apache.commons.lang3.tuple.Pair; |
| 31 |
|
import org.apache.solr.client.solrj.SolrQuery; |
| 32 |
|
import org.apache.solr.client.solrj.SolrQuery.ORDER; |
| 33 |
|
import org.apache.solr.client.solrj.response.QueryResponse; |
| 34 |
|
import org.apache.solr.common.SolrDocument; |
| 35 |
|
import org.apache.solr.common.SolrDocumentList; |
| 36 |
|
import org.apache.solr.common.params.CursorMarkParams; |
| 37 |
|
import org.xwiki.component.annotation.Component; |
| 38 |
|
import org.xwiki.component.annotation.InstantiationStrategy; |
| 39 |
|
import org.xwiki.component.descriptor.ComponentInstantiationStrategy; |
| 40 |
|
import org.xwiki.model.EntityType; |
| 41 |
|
import org.xwiki.model.reference.DocumentReference; |
| 42 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
| 43 |
|
import org.xwiki.search.solr.internal.api.FieldUtils; |
| 44 |
|
import org.xwiki.search.solr.internal.api.SolrIndexerException; |
| 45 |
|
import org.xwiki.search.solr.internal.api.SolrInstance; |
| 46 |
|
import org.xwiki.search.solr.internal.reference.SolrReferenceResolver; |
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
@version |
| 52 |
|
@since |
| 53 |
|
|
| 54 |
|
@Component |
| 55 |
|
@InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP) |
| 56 |
|
@Named("solr") |
| |
|
| 100% |
Uncovered Elements: 0 (43) |
Complexity: 10 |
Complexity Density: 0.33 |
|
| 57 |
|
public class SolrDocumentIterator extends AbstractDocumentIterator<String> |
| 58 |
|
{ |
| 59 |
|
|
| 60 |
|
@link |
| 61 |
|
|
| 62 |
|
private int index; |
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
private List<SolrDocument> results = Collections.emptyList(); |
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
private SolrQuery query; |
| 73 |
|
|
| 74 |
|
|
| 75 |
|
@link |
| 76 |
|
|
| 77 |
|
@Inject |
| 78 |
|
private Provider<SolrInstance> solrInstanceProvider; |
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
@Inject |
| 84 |
|
private SolrReferenceResolver solrReferenceResolver; |
| 85 |
|
|
| 86 |
|
@Inject |
| 87 |
|
private DocumentReferenceResolver<SolrDocument> solrDocumentReferenceResolver; |
| 88 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 89 |
967 |
@Override... |
| 90 |
|
public boolean hasNext() |
| 91 |
|
{ |
| 92 |
967 |
return getResults().size() > index; |
| 93 |
|
} |
| 94 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 95 |
3 |
@Override... |
| 96 |
|
public Pair<DocumentReference, String> next() |
| 97 |
|
{ |
| 98 |
3 |
SolrDocument result = getResults().get(index++); |
| 99 |
3 |
DocumentReference documentReference = this.solrDocumentReferenceResolver.resolve(result); |
| 100 |
3 |
String version = (String) result.get(FieldUtils.VERSION); |
| 101 |
3 |
return new ImmutablePair<DocumentReference, String>(documentReference, version); |
| 102 |
|
} |
| 103 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
| 104 |
5 |
@Override... |
| 105 |
|
public long size() |
| 106 |
|
{ |
| 107 |
5 |
return getResults() instanceof SolrDocumentList ? ((SolrDocumentList) results).getNumFound() : results.size(); |
| 108 |
|
} |
| 109 |
|
|
| 110 |
|
|
| 111 |
|
|
| 112 |
|
|
| 113 |
|
|
| 114 |
|
@return |
| 115 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 4 |
Complexity Density: 0.33 |
|
| 116 |
975 |
private List<SolrDocument> getResults()... |
| 117 |
|
{ |
| 118 |
975 |
if (index >= results.size()) { |
| 119 |
971 |
try { |
| 120 |
|
|
| 121 |
971 |
String cursorMark = getQuery().get(CursorMarkParams.CURSOR_MARK_PARAM); |
| 122 |
971 |
QueryResponse response = this.solrInstanceProvider.get().query(query); |
| 123 |
970 |
if (cursorMark.equals(response.getNextCursorMark())) { |
| 124 |
967 |
results = Collections.emptyList(); |
| 125 |
|
} else { |
| 126 |
3 |
results = response.getResults(); |
| 127 |
3 |
query.set(CursorMarkParams.CURSOR_MARK_PARAM, response.getNextCursorMark()); |
| 128 |
|
} |
| 129 |
|
} catch (Exception e) { |
| 130 |
1 |
results = Collections.emptyList(); |
| 131 |
1 |
logger.error("Failed to query the Solr index.", e); |
| 132 |
|
} |
| 133 |
971 |
index = 0; |
| 134 |
|
} |
| 135 |
975 |
return results; |
| 136 |
|
} |
| 137 |
|
|
| 138 |
|
|
| 139 |
|
@return |
| 140 |
|
@throws |
| 141 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 2 |
Complexity Density: 0.17 |
|
| 142 |
971 |
private SolrQuery getQuery() throws SolrIndexerException... |
| 143 |
|
{ |
| 144 |
971 |
if (query == null) { |
| 145 |
6 |
query = new SolrQuery(solrReferenceResolver.getQuery(rootReference)); |
| 146 |
6 |
query.setFields(FieldUtils.WIKI, FieldUtils.SPACES, FieldUtils.NAME, FieldUtils.DOCUMENT_LOCALE, |
| 147 |
|
FieldUtils.VERSION); |
| 148 |
6 |
query.addFilterQuery(FieldUtils.TYPE + ':' + EntityType.DOCUMENT.name()); |
| 149 |
|
|
| 150 |
|
|
| 151 |
|
|
| 152 |
|
|
| 153 |
|
|
| 154 |
|
|
| 155 |
|
|
| 156 |
|
|
| 157 |
|
|
| 158 |
6 |
query.addSort(FieldUtils.WIKI, ORDER.asc); |
| 159 |
6 |
query.addSort(FieldUtils.SPACE_EXACT, ORDER.asc); |
| 160 |
6 |
query.addSort(FieldUtils.NAME_EXACT, ORDER.asc); |
| 161 |
6 |
query.addSort(FieldUtils.DOCUMENT_LOCALE, ORDER.asc); |
| 162 |
|
|
| 163 |
|
|
| 164 |
6 |
query.addSort(FieldUtils.ID, ORDER.asc); |
| 165 |
|
|
| 166 |
|
|
| 167 |
|
|
| 168 |
|
|
| 169 |
6 |
query.set(CursorMarkParams.CURSOR_MARK_PARAM, CursorMarkParams.CURSOR_MARK_START); |
| 170 |
6 |
query.setRows(LIMIT); |
| 171 |
|
} |
| 172 |
971 |
return query; |
| 173 |
|
} |
| 174 |
|
} |