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

File DiffDocumentIterator.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

14
30
5
2
154
78
13
0.43
6
2.5
2.6

Classes

Class Line # Actions
DiffDocumentIterator 34 30 0% 13 0
1.0100%
DiffDocumentIterator.Action 39 0 - 0 0
-1.0 -
 

Contributing tests

This file is covered by 4 tests. .

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 org.apache.commons.lang3.tuple.ImmutablePair;
23    import org.apache.commons.lang3.tuple.Pair;
24    import org.xwiki.model.reference.DocumentReference;
25    import org.xwiki.model.reference.EntityReference;
26   
27    /**
28    * Compares the list of document references from two document iterators.
29    *
30    * @param <T> the type of data used to determine if a document is up to date
31    * @version $Id: d8f95bc9fe7c7de4eb6a10a1c343f88a383f59f7 $
32    * @since 5.4.5
33    */
 
34    public class DiffDocumentIterator<T> extends AbstractDocumentIterator<DiffDocumentIterator.Action>
35    {
36    /**
37    * The action that must be taken in order to move from the previous version to the next version.
38    */
 
39    public enum Action
40    {
41    /** This document hasn't changed so you can skip it. */
42    SKIP,
43   
44    /** This document is new so you must add it. */
45    ADD,
46   
47    /** This document doesn't exist anymore so you must delete it. */
48    DELETE,
49   
50    /** This document has been modified so you must update it. */
51    UPDATE
52    }
53   
54    /**
55    * The document iterator that corresponds to the previous state (the store that needs to be updated).
56    */
57    private final DocumentIterator<T> previous;
58   
59    /**
60    * The document iterator that corresponds to the next state (the store that is used as a reference point).
61    */
62    private final DocumentIterator<T> next;
63   
64    /**
65    * Used to compare document references.
66    */
67    private final DocumentReferenceComparator documentReferenceComparator = new DocumentReferenceComparator();
68   
69    /**
70    * The last entry taken from the {@link #previous} iterator.
71    */
72    private Pair<DocumentReference, T> previousEntry;
73   
74    /**
75    * The last entry taken from the {@link #next} iterator.
76    */
77    private Pair<DocumentReference, T> nextEntry;
78   
79    /**
80    * The last compare result between {@link #previousEntry} and {@link #nextEntry}.
81    */
82    private int diff;
83   
84    /**
85    * Initializes this iterator with the two iterators to compare.
86    *
87    * @param previous the document iterator that corresponds to the previous state (the store that needs to be updated)
88    * @param next the document iterator that corresponds to the next state (the store that is used as a reference
89    * point)
90    */
 
91  7 toggle public DiffDocumentIterator(DocumentIterator<T> previous, DocumentIterator<T> next)
92    {
93  7 this.previous = previous;
94  7 this.next = next;
95    }
96   
 
97  4 toggle @Override
98    public void setRootReference(EntityReference rootReference)
99    {
100  4 previous.setRootReference(rootReference);
101  4 next.setRootReference(rootReference);
102    }
103   
 
104  492 toggle @Override
105    public boolean hasNext()
106    {
107  492 return previous.hasNext() || next.hasNext();
108    }
109   
 
110  487 toggle @Override
111    public Pair<DocumentReference, Action> next()
112    {
113  487 DocumentReference documentReference;
114  487 Action action;
115  487 if (next.hasNext() && previous.hasNext()) {
116  4 if (diff >= 0) {
117  3 nextEntry = next.next();
118    }
119  4 if (diff <= 0) {
120  3 previousEntry = previous.next();
121    }
122  4 diff = documentReferenceComparator.compare(previousEntry.getKey(), nextEntry.getKey());
123  4 if (diff == 0) {
124  2 documentReference = nextEntry.getKey();
125    // Compare the document version.
126  2 if (nextEntry.getValue().equals(previousEntry.getValue())) {
127  1 action = Action.SKIP;
128    } else {
129  1 action = Action.UPDATE;
130    }
131  2 } else if (diff > 0) {
132  1 documentReference = nextEntry.getKey();
133  1 action = Action.ADD;
134    } else {
135  1 documentReference = previousEntry.getKey();
136  1 action = Action.DELETE;
137    }
138  483 } else if (next.hasNext()) {
139  481 documentReference = next.next().getKey();
140  481 action = Action.ADD;
141    } else {
142  2 documentReference = previous.next().getKey();
143  2 action = Action.DELETE;
144    }
145   
146  487 return new ImmutablePair<DocumentReference, Action>(documentReference, action);
147    }
148   
 
149  4 toggle @Override
150    public long size()
151    {
152  4 return Math.max(previous.size(), next.size());
153    }
154    }