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.Arrays; |
23 |
|
|
24 |
|
import javax.inject.Inject; |
25 |
|
import javax.inject.Named; |
26 |
|
|
27 |
|
import org.apache.commons.lang3.tuple.Pair; |
28 |
|
import org.xwiki.component.annotation.Component; |
29 |
|
import org.xwiki.component.annotation.InstantiationStrategy; |
30 |
|
import org.xwiki.component.descriptor.ComponentInstantiationStrategy; |
31 |
|
import org.xwiki.job.AbstractJob; |
32 |
|
import org.xwiki.job.DefaultJobStatus; |
33 |
|
import org.xwiki.job.GroupedJob; |
34 |
|
import org.xwiki.job.JobGroupPath; |
35 |
|
import org.xwiki.job.Request; |
36 |
|
import org.xwiki.model.reference.DocumentReference; |
37 |
|
import org.xwiki.model.reference.EntityReference; |
38 |
|
import org.xwiki.search.solr.internal.api.SolrIndexer; |
39 |
|
import org.xwiki.search.solr.internal.job.DiffDocumentIterator.Action; |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
@version |
45 |
|
@since |
46 |
|
|
47 |
|
@Component |
48 |
|
@InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP) |
49 |
|
@Named(IndexerJob.JOBTYPE) |
|
|
| 73.8% |
Uncovered Elements: 11 (42) |
Complexity: 12 |
Complexity Density: 0.44 |
|
50 |
|
public class IndexerJob extends AbstractJob<IndexerRequest, DefaultJobStatus<IndexerRequest>> implements GroupedJob |
51 |
|
{ |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
public static final String JOBTYPE = "solr.indexer"; |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
private static final JobGroupPath GROUP = new JobGroupPath(Arrays.asList("solr", "indexer")); |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
@Inject |
67 |
|
private transient SolrIndexer indexer; |
68 |
|
|
69 |
|
@Inject |
70 |
|
@Named("database") |
71 |
|
private transient DocumentIterator<String> databaseIterator; |
72 |
|
|
73 |
|
@Inject |
74 |
|
@Named("solr") |
75 |
|
private transient DocumentIterator<String> solrIterator; |
76 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
77 |
15 |
@Override... |
78 |
|
public String getType() |
79 |
|
{ |
80 |
15 |
return JOBTYPE; |
81 |
|
} |
82 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
83 |
3 |
@Override... |
84 |
|
public JobGroupPath getGroupPath() |
85 |
|
{ |
86 |
3 |
return GROUP; |
87 |
|
} |
88 |
|
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
89 |
3 |
@Override... |
90 |
|
protected IndexerRequest castRequest(Request request) |
91 |
|
{ |
92 |
3 |
IndexerRequest indexerRequest; |
93 |
3 |
if (request instanceof IndexerRequest) { |
94 |
3 |
indexerRequest = (IndexerRequest) request; |
95 |
|
} else { |
96 |
0 |
indexerRequest = new IndexerRequest(request); |
97 |
|
} |
98 |
|
|
99 |
3 |
return indexerRequest; |
100 |
|
} |
101 |
|
|
|
|
| 42.9% |
Uncovered Elements: 4 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
102 |
3 |
@Override... |
103 |
|
protected void runInternal() throws Exception |
104 |
|
{ |
105 |
3 |
if (getRequest().isOverwrite()) { |
106 |
0 |
EntityReference rootReference = getRequest().getRootReference(); |
107 |
0 |
this.logger.info("Index documents in [{}].", rootReference); |
108 |
0 |
this.indexer.index(rootReference, true); |
109 |
|
} else { |
110 |
3 |
updateSolrIndex(); |
111 |
|
} |
112 |
|
} |
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
|
|
| 76.2% |
Uncovered Elements: 5 (21) |
Complexity: 6 |
Complexity Density: 0.4 |
|
117 |
3 |
private void updateSolrIndex()... |
118 |
|
{ |
119 |
3 |
DiffDocumentIterator<String> iterator = new DiffDocumentIterator<>(this.solrIterator, this.databaseIterator); |
120 |
3 |
iterator.setRootReference(getRequest().getRootReference()); |
121 |
|
|
122 |
3 |
this.progressManager.pushLevelProgress((int) iterator.size(), this); |
123 |
|
|
124 |
3 |
try { |
125 |
3 |
long[] counter = new long[4]; |
126 |
483 |
while (iterator.hasNext()) { |
127 |
480 |
this.progressManager.startStep(this); |
128 |
|
|
129 |
480 |
Pair<DocumentReference, Action> entry = iterator.next(); |
130 |
480 |
if (entry.getValue() == Action.ADD || entry.getValue() == Action.UPDATE) { |
131 |
|
|
132 |
|
|
133 |
480 |
this.indexer.index(entry.getKey(), true); |
134 |
0 |
} else if (entry.getValue() == Action.DELETE && getRequest().isRemoveMissing()) { |
135 |
|
|
136 |
0 |
this.indexer.delete(entry.getKey(), true); |
137 |
|
} |
138 |
|
|
139 |
480 |
counter[entry.getValue().ordinal()]++; |
140 |
|
} |
141 |
|
|
142 |
3 |
this.logger.info( |
143 |
|
"{} documents added, {} deleted and {} updated during the synchronization of the Solr index.", |
144 |
|
counter[Action.ADD.ordinal()], counter[Action.DELETE.ordinal()], counter[Action.UPDATE.ordinal()]); |
145 |
|
} finally { |
146 |
3 |
this.progressManager.popLevelProgress(this); |
147 |
|
} |
148 |
|
} |
149 |
|
} |