1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.search.solr.internal.job

File IndexerJob.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

10
27
5
1
149
89
12
0.44
5.4
5
2.4

Classes

Class Line # Actions
IndexerJob 50 27 0% 12 11
0.738095273.8%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Provide progress information and store logging of an advanced indexing.
43    *
44    * @version $Id: f07fcb8c97d1fbb1cbb7ce35d31f223a8893c360 $
45    * @since 5.1RC1
46    */
47    @Component
48    @InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
49    @Named(IndexerJob.JOBTYPE)
 
50    public class IndexerJob extends AbstractJob<IndexerRequest, DefaultJobStatus<IndexerRequest>> implements GroupedJob
51    {
52    /**
53    * The id of the job.
54    */
55    public static final String JOBTYPE = "solr.indexer";
56   
57    /**
58    * All indexers run in the same thread.
59    */
60    // TODO: group indexers based on the IndexerRequest root entity
61    private static final JobGroupPath GROUP = new JobGroupPath(Arrays.asList("solr", "indexer"));
62   
63    /**
64    * Used to send documents to index or delete to/from Solr index.
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   
 
77  15 toggle @Override
78    public String getType()
79    {
80  15 return JOBTYPE;
81    }
82   
 
83  3 toggle @Override
84    public JobGroupPath getGroupPath()
85    {
86  3 return GROUP;
87    }
88   
 
89  3 toggle @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   
 
102  3 toggle @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    * Update the Solr index to match the current state of the database.
116    */
 
117  3 toggle 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    // The database entry has not been indexed or the indexed version doesn't match the latest version
132    // from the database.
133  480 this.indexer.index(entry.getKey(), true);
134  0 } else if (entry.getValue() == Action.DELETE && getRequest().isRemoveMissing()) {
135    // The index entry doesn't exist anymore in the database.
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    }