| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.query.spans; |
| 2 | |
| Eliza Margaretha | 2289898 | 2014-11-04 17:10:21 +0000 | [diff] [blame] | 3 | import java.io.IOException; |
| 4 | import java.util.ArrayList; |
| 5 | import java.util.Collection; |
| 6 | import java.util.LinkedList; |
| 7 | import java.util.Map; |
| 8 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 9 | import org.apache.lucene.index.AtomicReaderContext; |
| 10 | import org.apache.lucene.index.Term; |
| 11 | import org.apache.lucene.index.TermContext; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 12 | import org.apache.lucene.search.DocIdSetIterator; |
| Eliza Margaretha | 2289898 | 2014-11-04 17:10:21 +0000 | [diff] [blame] | 13 | import org.apache.lucene.search.spans.Spans; |
| 14 | import org.apache.lucene.util.Bits; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 15 | import org.slf4j.Logger; |
| 16 | import org.slf4j.LoggerFactory; |
| 17 | |
| Eliza Margaretha | 2289898 | 2014-11-04 17:10:21 +0000 | [diff] [blame] | 18 | import de.ids_mannheim.korap.query.SpanWithinQuery; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 19 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * SpanWithinQuery is DEPRECATED and will |
| 23 | * be replaced by SpanPositionQuery in the near future |
| Nils Diewald | cb8afb3 | 2015-02-04 21:12:37 +0000 | [diff] [blame] | 24 | * |
| 25 | * TODO: Support exclusivity |
| 26 | * TODO: Use the term "queue" and implement it similar to SpanOrQuery |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 27 | */ |
| 28 | |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 29 | /** |
| Nils Diewald | 1455e1e | 2014-08-01 16:12:43 +0000 | [diff] [blame] | 30 | * Compare two spans and check how they relate positionally. |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 31 | * |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 32 | * @author diewald |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 33 | */ |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 34 | public class WithinSpans extends Spans { |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 35 | |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 36 | // Logger |
| 37 | private final Logger log = LoggerFactory.getLogger(WithinSpans.class); |
| Nils Diewald | 1455e1e | 2014-08-01 16:12:43 +0000 | [diff] [blame] | 38 | |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 39 | // This advices the java compiler to ignore all loggings |
| 40 | public static final boolean DEBUG = false; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 41 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 42 | private boolean more = false; |
| 43 | |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 44 | // Boolean value indicating if span B |
| 45 | // should be forwarded next (true) |
| 46 | // or span A (false); |
| 47 | boolean nextSpanB = true; |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 48 | |
| 49 | private int |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 50 | wrapStart = -1, |
| 51 | wrapEnd = -1, |
| 52 | embeddedStart = -1, |
| 53 | embeddedEnd = -1, |
| 54 | wrapDoc = -1, |
| 55 | embeddedDoc = -1, |
| 56 | matchDoc = -1, |
| 57 | matchStart = -1, |
| 58 | matchEnd = -1; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 59 | |
| 60 | private Collection<byte[]> matchPayload; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 61 | private Collection<byte[]> embeddedPayload; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 62 | |
| 63 | // Indicates that the wrap and the embedded spans are in the same doc |
| 64 | private boolean inSameDoc = false; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 65 | |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 66 | /* |
| 67 | Supported flags are currently: |
| Nils Diewald | cb8afb3 | 2015-02-04 21:12:37 +0000 | [diff] [blame] | 68 | ov -> 0 | overlap: A & B != empty |
| 69 | rov -> 2 | real overlap: A & B != empty and |
| 70 | ((A | B) != A or |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 71 | (A | B) != B) |
| Nils Diewald | cb8afb3 | 2015-02-04 21:12:37 +0000 | [diff] [blame] | 72 | in -> 4 | within: A | B = A |
| 73 | rin -> 6 | real within: A | B = A and |
| 74 | A & B != A |
| 75 | ew -> 8 | endswith: A | B = A and |
| 76 | A.start = B.start |
| 77 | sw -> 10 | startswith: A | B = A and |
| 78 | A.end = B.end |
| 79 | m -> 12 | A = B |
| 80 | */ |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 81 | public static final byte |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 82 | OVERLAP = (byte) 0, |
| 83 | REAL_OVERLAP = (byte) 2, |
| 84 | WITHIN = (byte) 4, |
| 85 | REAL_WITHIN = (byte) 6, |
| 86 | ENDSWITH = (byte) 8, |
| 87 | STARTSWITH = (byte) 10, |
| 88 | MATCH = (byte) 12; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 89 | |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 90 | private byte flag; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 91 | |
| Nils Diewald | 1455e1e | 2014-08-01 16:12:43 +0000 | [diff] [blame] | 92 | // Contains the query |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 93 | private SpanWithinQuery query; |
| 94 | |
| Nils Diewald | cb8afb3 | 2015-02-04 21:12:37 +0000 | [diff] [blame] | 95 | // Representing the first operand |
| 96 | private final Spans wrapSpans; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 97 | |
| Nils Diewald | cb8afb3 | 2015-02-04 21:12:37 +0000 | [diff] [blame] | 98 | // Representing the second operand |
| 99 | private final Spans embeddedSpans; |
| 100 | |
| 101 | // Check flag if the current constellation |
| 102 | // was checked yet |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 103 | private boolean tryMatch = true; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 104 | |
| Nils Diewald | 01ff7af | 2015-02-04 22:54:26 +0000 | [diff] [blame] | 105 | // Two buffers for storing candidates |
| Nils Diewald | 41750bf | 2015-02-06 17:45:20 +0000 | [diff] [blame] | 106 | private LinkedList<WithinSpan> |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 107 | spanStore1, |
| 108 | spanStore2; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 109 | |
| Nils Diewald | 01ff7af | 2015-02-04 22:54:26 +0000 | [diff] [blame] | 110 | /** |
| 111 | * Construct a new WithinSpans object. |
| 112 | * |
| 113 | * @param spanWithinQuery The parental {@link SpanWithinQuery}. |
| 114 | * @param context The {@link AtomicReaderContext}. |
| 115 | * @param acceptDocs Bit vector representing the documents |
| 116 | * to be searched in. |
| Nils Diewald | 41750bf | 2015-02-06 17:45:20 +0000 | [diff] [blame] | 117 | * @param termContexts A map managing {@link TermState TermStates}. |
| 118 | * @param flag A byte flag indicating the positional condition of the sub spans. |
| Nils Diewald | 01ff7af | 2015-02-04 22:54:26 +0000 | [diff] [blame] | 119 | */ |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 120 | public WithinSpans (SpanWithinQuery spanWithinQuery, |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 121 | AtomicReaderContext context, |
| 122 | Bits acceptDocs, |
| 123 | Map<Term,TermContext> termContexts, |
| 124 | byte flag) throws IOException { |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 125 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 126 | if (DEBUG) |
| 127 | log.trace("Construct WithinSpans"); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 128 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 129 | // Init copies |
| 130 | this.matchPayload = new LinkedList<byte[]>(); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 131 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 132 | // Get spans |
| 133 | this.wrapSpans = spanWithinQuery.wrap().getSpans( |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 134 | context, |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 135 | acceptDocs, |
| 136 | termContexts |
| 137 | ); |
| 138 | this.embeddedSpans = spanWithinQuery.embedded().getSpans( |
| 139 | context, |
| 140 | acceptDocs, |
| 141 | termContexts |
| 142 | ); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 143 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 144 | this.flag = flag; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 145 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 146 | // SpanStores for backtracking |
| Nils Diewald | 41750bf | 2015-02-06 17:45:20 +0000 | [diff] [blame] | 147 | this.spanStore1 = new LinkedList<WithinSpan>(); |
| 148 | this.spanStore2 = new LinkedList<WithinSpan>(); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 149 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 150 | // kept for toString() only. |
| 151 | this.query = spanWithinQuery; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 152 | }; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 153 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 154 | |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 155 | // Move to next match, returning true iff any such exists. |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 156 | @Override |
| 157 | public boolean next () throws IOException { |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 158 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 159 | if (DEBUG) |
| 160 | log.trace("Next with docs {}, {}", wrapDoc, embeddedDoc); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 161 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 162 | // Initialize spans |
| 163 | if (!this.init()) { |
| 164 | this.more = false; |
| 165 | this.inSameDoc = false; |
| 166 | this.wrapDoc = DocIdSetIterator.NO_MORE_DOCS; |
| 167 | this.embeddedDoc = DocIdSetIterator.NO_MORE_DOCS; |
| 168 | this.matchDoc = DocIdSetIterator.NO_MORE_DOCS; |
| 169 | return false; |
| 170 | }; |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 171 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 172 | // There are more spans and they are in the same document |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 173 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 174 | while (this.more && (wrapDoc == embeddedDoc || |
| 175 | // this.inSameDoc || |
| 176 | this.toSameDoc())) { |
| 177 | if (DEBUG) |
| 178 | log.trace("We are in the same doc: {}, {}", wrapDoc, embeddedDoc); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 179 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 180 | // Both spans match according to the flag |
| 181 | // Silently the next operations are prepared |
| 182 | if (this.tryMatch && this.doesMatch()) { |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 183 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 184 | if (this.wrapEnd == -1) |
| 185 | this.wrapEnd = this.wrapSpans.end(); |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 186 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 187 | this.matchStart = embeddedStart < wrapStart ? embeddedStart : wrapStart; |
| 188 | this.matchEnd = embeddedEnd > wrapEnd ? embeddedEnd : wrapEnd; |
| 189 | this.matchDoc = embeddedDoc; |
| 190 | this.matchPayload.clear(); |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 191 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 192 | if (this.embeddedPayload != null) |
| 193 | matchPayload.addAll(embeddedPayload); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 194 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 195 | if (this.wrapSpans.isPayloadAvailable()) |
| 196 | this.matchPayload.addAll(wrapSpans.getPayload()); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 197 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 198 | if (DEBUG) |
| 199 | log.trace( |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 200 | " ---- MATCH ---- {}-{} ({})", |
| 201 | matchStart, |
| 202 | matchEnd, |
| 203 | matchDoc |
| 204 | ); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 205 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 206 | this.tryMatch = false; |
| 207 | return true; |
| 208 | } |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 209 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 210 | // Get next embedded |
| 211 | else if (this.nextSpanB) { |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 212 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 213 | // Next time try the match |
| 214 | this.tryMatch = true; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 215 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 216 | if (DEBUG) |
| 217 | log.trace("In the next embedded branch"); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 218 | |
| Nils Diewald | 41750bf | 2015-02-06 17:45:20 +0000 | [diff] [blame] | 219 | WithinSpan current = null; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 220 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 221 | // New - fetch until theres a span in the correct doc or bigger |
| 222 | while (!this.spanStore2.isEmpty()) { |
| 223 | current = spanStore2.removeFirst(); |
| 224 | if (current.doc >= this.wrapDoc) |
| 225 | break; |
| 226 | }; |
| 227 | |
| 228 | |
| 229 | // There is nothing in the second store |
| 230 | if (current == null) { |
| 231 | if (DEBUG) |
| 232 | log.trace("SpanStore 2 is empty"); |
| 233 | |
| 234 | // Forward with embedding |
| 235 | if (!this.embeddedSpans.next()) { |
| 236 | this.nextSpanA(); |
| 237 | continue; |
| 238 | } |
| 239 | |
| 240 | else if (DEBUG) { |
| 241 | log.trace("Fetch next embedded span"); |
| 242 | }; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 243 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 244 | this.embeddedStart = this.embeddedSpans.start(); |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 245 | this.embeddedEnd = -1; |
| 246 | this.embeddedPayload = null; |
| 247 | this.embeddedDoc = this.embeddedSpans.doc(); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 248 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 249 | if (this.embeddedDoc != this.wrapDoc) { |
| 250 | |
| 251 | if (DEBUG) { |
| 252 | log.trace("Embedded span is in a new document {}", |
| 253 | _currentEmbedded().toString()); |
| 254 | log.trace("Reset current embedded doc"); |
| 255 | }; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 256 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 257 | /* |
| 258 | if (DEBUG) |
| 259 | log.trace("Clear all span stores"); |
| 260 | this.spanStore1.clear(); |
| 261 | this.spanStore2.clear(); |
| 262 | */ |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 263 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 264 | this.storeEmbedded(); |
| 265 | |
| 266 | // That is necessary to backtrack to the last document! |
| 267 | this.inSameDoc = true; |
| 268 | this.embeddedDoc = wrapDoc; |
| 269 | // this.tryMatch = false; // already covered in nextSpanA |
| 270 | |
| 271 | this.nextSpanA(); |
| 272 | continue; |
| 273 | }; |
| 274 | |
| 275 | if (DEBUG) |
| 276 | log.trace( |
| 277 | " Forward embedded span to {}", |
| 278 | _currentEmbedded().toString() |
| 279 | ); |
| 280 | |
| 281 | if (this.embeddedDoc != this.wrapDoc) { |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 282 | |
| 283 | // Is this always a good idea? |
| 284 | /* |
| 285 | this.spanStore1.clear(); |
| 286 | this.spanStore2.clear(); |
| 287 | */ |
| 288 | |
| 289 | this.embeddedStart = -1; |
| 290 | this.embeddedEnd = -1; |
| 291 | this.embeddedPayload = null; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 292 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 293 | if (!this.toSameDoc()) { |
| 294 | this.more = false; |
| 295 | this.inSameDoc = false; |
| 296 | return false; |
| 297 | }; |
| 298 | }; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 299 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 300 | this.more = true; |
| 301 | this.inSameDoc = true; |
| 302 | this.tryMatch = true; |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 303 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 304 | this.nextSpanB(); |
| 305 | continue; |
| 306 | } |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 307 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 308 | // Fetch from second store? |
| 309 | else { |
| 310 | /** TODO: Change this to a single embedded object! */ |
| 311 | this.embeddedStart = current.start; |
| 312 | this.embeddedEnd = current.end; |
| 313 | this.embeddedDoc = current.doc; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 314 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 315 | if (current.payload != null) { |
| 316 | this.embeddedPayload = new ArrayList<byte[]>(current.payload.size()); |
| 317 | this.embeddedPayload.addAll(current.payload); |
| 318 | } |
| 319 | else { |
| 320 | this.embeddedPayload = null; |
| 321 | }; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 322 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 323 | if (DEBUG) |
| 324 | log.trace("Fetch current from SpanStore 2: {}", current.toString()); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 325 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 326 | this.tryMatch = true; |
| 327 | }; |
| 328 | continue; |
| 329 | }; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 330 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 331 | // get next wrap |
| 332 | if (DEBUG) |
| 333 | log.trace("In the next wrap branch"); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 334 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 335 | this.tryMatch = true; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 336 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 337 | if (DEBUG) |
| 338 | log.trace("Try next wrap"); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 339 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 340 | // shift the stored spans |
| 341 | if (!this.spanStore1.isEmpty()) { |
| 342 | if (DEBUG) { |
| 343 | log.trace("Move everything from SpanStore 1 to SpanStore 2:"); |
| Nils Diewald | 41750bf | 2015-02-06 17:45:20 +0000 | [diff] [blame] | 344 | for (WithinSpan i : this.spanStore1) { |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 345 | log.trace(" | {}", i.toString()); |
| 346 | }; |
| 347 | }; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 348 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 349 | // Move everything to spanStore2 |
| 350 | this.spanStore2.addAll( |
| 351 | 0, |
| Nils Diewald | 41750bf | 2015-02-06 17:45:20 +0000 | [diff] [blame] | 352 | (LinkedList<WithinSpan>) this.spanStore1.clone() |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 353 | ); |
| 354 | this.spanStore1.clear(); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 355 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 356 | if (DEBUG) { |
| 357 | log.trace("SpanStore 2 now is:"); |
| Nils Diewald | 41750bf | 2015-02-06 17:45:20 +0000 | [diff] [blame] | 358 | for (WithinSpan i : this.spanStore2) { |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 359 | log.trace(" | {}", i.toString()); |
| 360 | }; |
| 361 | }; |
| 362 | |
| 363 | } |
| 364 | else if (DEBUG) { |
| 365 | log.trace("spanStore 1 is empty"); |
| 366 | }; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 367 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 368 | // Get next wrap |
| 369 | if (this.wrapSpans.next()) { |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 370 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 371 | // Reset wrapping information |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 372 | this.wrapStart = this.wrapSpans.start(); |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 373 | this.wrapEnd = -1; |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 374 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 375 | // Retrieve doc information |
| 376 | this.wrapDoc = this.wrapSpans.doc(); |
| Nils Diewald | 20607ab | 2014-03-20 23:28:36 +0000 | [diff] [blame] | 377 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 378 | if (DEBUG) |
| 379 | log.trace( |
| 380 | " Forward wrap span to {}", |
| 381 | _currentWrap().toString() |
| 382 | ); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 383 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 384 | if (this.embeddedDoc != this.wrapDoc) { |
| 385 | if (DEBUG) |
| 386 | log.trace("Delete all span stores"); |
| 387 | this.spanStore1.clear(); |
| 388 | this.spanStore2.clear(); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 389 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 390 | // Reset embedded: |
| 391 | this.embeddedStart = -1; |
| 392 | this.embeddedEnd = -1; |
| 393 | this.embeddedPayload = null; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 394 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 395 | if (!this.toSameDoc()) { |
| 396 | this.inSameDoc = false; |
| 397 | this.more = false; |
| 398 | return false; |
| 399 | }; |
| 400 | } |
| 401 | else { |
| 402 | this.inSameDoc = true; |
| 403 | // Do not match with the current state |
| 404 | this.tryMatch = false; |
| 405 | }; |
| 406 | |
| 407 | this.nextSpanB(); |
| 408 | continue; |
| 409 | } |
| 410 | this.more = false; |
| 411 | this.inSameDoc = false; |
| 412 | this.spanStore1.clear(); |
| 413 | this.spanStore2.clear(); |
| 414 | return false; |
| 415 | }; |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 416 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 417 | // No more matches |
| 418 | return false; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 419 | }; |
| 420 | |
| 421 | |
| 422 | /** |
| 423 | * Skip to the next document |
| 424 | */ |
| 425 | private boolean toSameDoc () throws IOException { |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 426 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 427 | if (DEBUG) |
| 428 | log.trace("Forward to find same docs"); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 429 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 430 | /* |
| 431 | if (this.embeddedSpans == null) { |
| 432 | this.more = false; |
| 433 | this.matchDoc = DocIdSetIterator.NO_MORE_DOCS; |
| 434 | this.inSameDoc = false; |
| 435 | return false; |
| 436 | }; |
| 437 | */ |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 438 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 439 | this.more = true; |
| 440 | this.inSameDoc = true; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 441 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 442 | this.wrapDoc = this.wrapSpans.doc(); |
| 443 | this.embeddedDoc = this.embeddedSpans.doc(); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 444 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 445 | // Clear all spanStores |
| 446 | if (this.wrapDoc != this.embeddedDoc) { |
| 447 | /* |
| 448 | if (DEBUG) |
| 449 | log.trace("Clear all spanStores when moving forward"); |
| 450 | // Why?? |
| 451 | this.spanStore1.clear(); |
| 452 | this.spanStore2.clear(); |
| 453 | */ |
| 454 | } |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 455 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 456 | // Last doc was reached |
| 457 | else if (this.wrapDoc == DocIdSetIterator.NO_MORE_DOCS) { |
| 458 | this.more = false; |
| 459 | this.matchDoc = DocIdSetIterator.NO_MORE_DOCS; |
| 460 | this.inSameDoc = false; |
| 461 | return false; |
| 462 | } |
| 463 | else { |
| 464 | if (DEBUG) { |
| 465 | log.trace("Current position already is in the same doc"); |
| 466 | log.trace("Embedded: {}", _currentEmbedded().toString()); |
| 467 | }; |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 468 | this.matchDoc = this.embeddedDoc; |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 469 | return true; |
| 470 | }; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 471 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 472 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 473 | // Forward till match |
| 474 | while (this.wrapDoc != this.embeddedDoc) { |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 475 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 476 | // Forward wrapInfo |
| 477 | if (this.wrapDoc < this.embeddedDoc) { |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 478 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 479 | // Set document information |
| 480 | if (!wrapSpans.skipTo(this.embeddedDoc)) { |
| 481 | this.more = false; |
| 482 | this.inSameDoc = false; |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 483 | this.matchDoc = DocIdSetIterator.NO_MORE_DOCS; |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 484 | return false; |
| 485 | }; |
| 486 | |
| 487 | if (DEBUG) |
| 488 | log.trace("Skip wrap to doc {}", this.embeddedDoc); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 489 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 490 | this.wrapDoc = this.wrapSpans.doc(); |
| 491 | |
| 492 | if (wrapDoc == DocIdSetIterator.NO_MORE_DOCS) { |
| 493 | this.more = false; |
| 494 | this.inSameDoc = false; |
| 495 | this.embeddedDoc = DocIdSetIterator.NO_MORE_DOCS; |
| 496 | this.matchDoc = DocIdSetIterator.NO_MORE_DOCS; |
| 497 | return false; |
| 498 | }; |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 499 | |
| 500 | /* |
| 501 | Remove stored information |
| 502 | */ |
| 503 | if (DEBUG) |
| 504 | log.trace("Delete all span stores"); |
| 505 | |
| 506 | this.spanStore1.clear(); |
| 507 | this.spanStore2.clear(); |
| 508 | |
| 509 | if (wrapDoc == embeddedDoc) { |
| 510 | this.wrapStart = this.wrapSpans.start(); |
| 511 | this.embeddedStart = this.embeddedSpans.start(); |
| 512 | this.matchDoc = this.embeddedDoc; |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 513 | return true; |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 514 | }; |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 515 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 516 | this.wrapStart = -1; |
| 517 | this.embeddedStart = -1; |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 518 | } |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 519 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 520 | // Forward embedInfo |
| 521 | else if (this.wrapDoc > this.embeddedDoc) { |
| 522 | |
| 523 | // Set document information |
| 524 | if (!this.embeddedSpans.skipTo(this.wrapDoc)) { |
| 525 | this.more = false; |
| 526 | this.inSameDoc = false; |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 527 | this.matchDoc = DocIdSetIterator.NO_MORE_DOCS; |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 528 | return false; |
| 529 | }; |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 530 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 531 | this.embeddedDoc = this.embeddedSpans.doc(); |
| 532 | |
| 533 | if (this.embeddedDoc == DocIdSetIterator.NO_MORE_DOCS) { |
| 534 | this.more = false; |
| 535 | this.inSameDoc = false; |
| 536 | this.wrapDoc = DocIdSetIterator.NO_MORE_DOCS; |
| 537 | this.matchDoc = DocIdSetIterator.NO_MORE_DOCS; |
| 538 | return false; |
| 539 | }; |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 540 | |
| 541 | if (DEBUG) |
| 542 | log.trace("Skip embedded to doc {}", this.embeddedDoc); |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 543 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 544 | this.embeddedStart = this.embeddedSpans.start(); |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 545 | this.embeddedEnd = -1; |
| 546 | this.embeddedPayload = null; |
| 547 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 548 | if (this.wrapDoc == this.embeddedDoc) { |
| 549 | this.matchDoc = this.embeddedDoc; |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 550 | return true; |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 551 | }; |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 552 | } |
| 553 | else { |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 554 | this.matchDoc = DocIdSetIterator.NO_MORE_DOCS; |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 555 | return false; |
| 556 | }; |
| 557 | }; |
| 558 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 559 | this.matchDoc = this.wrapDoc; |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 560 | return true; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 561 | }; |
| 562 | |
| 563 | |
| 564 | // Initialize spans |
| 565 | private boolean init () throws IOException { |
| 566 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 567 | // There is a missing span |
| 568 | if (this.embeddedDoc >= 0) |
| 569 | return true; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 570 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 571 | if (DEBUG) |
| 572 | log.trace("Initialize spans"); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 573 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 574 | // First tick for both spans |
| 575 | if (!(this.embeddedSpans.next() && this.wrapSpans.next())) { |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 576 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 577 | if (DEBUG) |
| 578 | log.trace("No spans initialized"); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 579 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 580 | this.embeddedDoc = -1; |
| 581 | this.more = false; |
| 582 | return false; |
| 583 | }; |
| 584 | this.more = true; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 585 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 586 | // Store current positions for wrapping and embedded spans |
| 587 | this.wrapDoc = this.wrapSpans.doc(); |
| 588 | this.embeddedDoc = this.embeddedSpans.doc(); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 589 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 590 | // Set inSameDoc to true, if it is true |
| 591 | if (this.embeddedDoc == this.wrapDoc) |
| 592 | this.inSameDoc = true; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 593 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 594 | return true; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 595 | }; |
| 596 | |
| 597 | |
| 598 | /** Skips to the first match beyond the current, whose document number is |
| 599 | * greater than or equal to <i>target</i>. <p>Returns true iff there is such |
| 600 | * a match. <p>Behaves as if written: <pre class="prettyprint"> |
| 601 | * boolean skipTo(int target) { |
| 602 | * do { |
| 603 | * if (!next()) |
| 604 | * return false; |
| 605 | * } while (target > doc()); |
| 606 | * return true; |
| 607 | * } |
| 608 | * </pre> |
| 609 | * Most implementations are considerably more efficient than that. |
| 610 | */ |
| 611 | public boolean skipTo (int target) throws IOException { |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 612 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 613 | if (DEBUG) |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 614 | log.trace( |
| 615 | "skipTo document {}/{} -> {}", |
| 616 | this.embeddedDoc, |
| 617 | this.wrapDoc, |
| 618 | target |
| 619 | ); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 620 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 621 | // Initialize spans |
| 622 | if (!this.init()) |
| 623 | return false; |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 624 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 625 | assert target > this.embeddedDoc; |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 626 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 627 | // Only forward embedded spans |
| 628 | if (this.more && (this.embeddedDoc < target)) { |
| 629 | if (this.embeddedSpans.skipTo(target)) { |
| 630 | this.inSameDoc = false; |
| 631 | this.embeddedStart = -1; |
| 632 | this.embeddedEnd = -1; |
| 633 | this.embeddedPayload = null; |
| 634 | this.embeddedDoc = this.embeddedSpans.doc(); |
| 635 | } |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 636 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 637 | // Can't be skipped to target |
| 638 | else { |
| 639 | this.inSameDoc = false; |
| 640 | this.more = false; |
| 641 | return false; |
| 642 | }; |
| 643 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 644 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 645 | // Move to same doc |
| 646 | return this.toSameDoc(); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 647 | }; |
| 648 | |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 649 | private void nextSpanA () { |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 650 | if (DEBUG) |
| 651 | log.trace("Try wrap next time"); |
| 652 | this.tryMatch = false; |
| 653 | this.nextSpanB = false; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 654 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 655 | |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 656 | private void nextSpanB () { |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 657 | if (DEBUG) |
| 658 | log.trace("Try embedded next time"); |
| 659 | this.nextSpanB = true; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 660 | }; |
| 661 | |
| 662 | |
| 663 | // Check if the current span constellation does match |
| 664 | // Store backtracking relevant data and say, how to proceed |
| 665 | private boolean doesMatch () { |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 666 | if (DEBUG) |
| 667 | log.trace("In the match test branch"); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 668 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 669 | if (this.wrapStart == -1) |
| 670 | this.wrapStart = this.wrapSpans.start(); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 671 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 672 | if (this.embeddedStart == -1) { |
| 673 | this.embeddedStart = this.embeddedSpans.start(); |
| 674 | this.embeddedEnd = this.embeddedSpans.end(); |
| 675 | }; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 676 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 677 | this.wrapEnd = -1; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 678 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 679 | // Shortcut to prevent lazyloading of .end() |
| 680 | if (this.wrapStart > this.embeddedStart) { |
| 681 | // Can't match for in, rin, ew, sw, and m |
| 682 | // and will always lead to next_b |
| 683 | if (flag >= WITHIN) { |
| 684 | this.nextSpanB(); |
| 685 | if (DEBUG) |
| 686 | _logCurrentCase((byte) 16); |
| 687 | return false; |
| 688 | }; |
| 689 | } |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 690 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 691 | else if (this.wrapStart < this.embeddedStart) { |
| 692 | // Can't match for sw and m and will always |
| 693 | // lead to next_a |
| 694 | if (flag >= STARTSWITH) { |
| 695 | this.nextSpanA(); |
| 696 | if (DEBUG) |
| 697 | _logCurrentCase((byte) 15); |
| 698 | return false; |
| 699 | }; |
| 700 | }; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 701 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 702 | // Now check correctly |
| 703 | byte currentCase = this.withinCase(); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 704 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 705 | if (DEBUG) |
| 706 | _logCurrentCase(currentCase); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 707 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 708 | boolean match = false; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 709 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 710 | // Test case |
| 711 | if (currentCase >= (byte) 3 && currentCase <= (byte) 11) { |
| 712 | switch (flag) { |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 713 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 714 | case WITHIN: |
| 715 | if (currentCase >= 6 && currentCase <= 10 && currentCase != 8) |
| 716 | match = true; |
| 717 | break; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 718 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 719 | case REAL_WITHIN: |
| 720 | if (currentCase == 6 || |
| 721 | currentCase == 9 || |
| 722 | currentCase == 10) |
| 723 | match = true; |
| 724 | break; |
| 725 | |
| 726 | case MATCH: |
| 727 | if (currentCase == 7) |
| 728 | match = true; |
| 729 | break; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 730 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 731 | case STARTSWITH: |
| 732 | if (currentCase == 7 || |
| 733 | currentCase == 6) |
| 734 | match = true; |
| 735 | break; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 736 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 737 | case ENDSWITH: |
| 738 | if (currentCase == 7 || |
| 739 | currentCase == 10) |
| 740 | match = true; |
| 741 | break; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 742 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 743 | case OVERLAP: |
| 744 | match = true; |
| 745 | break; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 746 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 747 | case REAL_OVERLAP: |
| 748 | if (currentCase == 3 || |
| 749 | currentCase == 11) |
| 750 | match = true; |
| 751 | break; |
| 752 | }; |
| 753 | }; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 754 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 755 | try { |
| 756 | this.todo(currentCase); |
| 757 | } |
| 758 | catch (IOException e) { |
| 759 | return false; |
| 760 | } |
| 761 | return match; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 762 | }; |
| 763 | |
| 764 | |
| 765 | private void _logCurrentCase (byte currentCase) { |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 766 | log.trace("Current Case is {}", currentCase); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 767 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 768 | String _e = _currentEmbedded().toString(); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 769 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 770 | log.trace(" |---| {}", _currentWrap().toString()); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 771 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 772 | switch (currentCase) { |
| 773 | case 1: |
| 774 | log.trace("|-| {}", _e); |
| 775 | break; |
| 776 | case 2: |
| 777 | log.trace("|---| {}", _e); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 778 | break; |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 779 | case 3: |
| 780 | log.trace(" |---| {}", _e); |
| 781 | break; |
| 782 | case 4: |
| 783 | log.trace(" |-----| {}", _e); |
| 784 | break; |
| 785 | case 5: |
| 786 | log.trace(" |-------| {}", _e); |
| 787 | break; |
| 788 | case 6: |
| 789 | log.trace(" |-| {}", _e); |
| 790 | break; |
| 791 | case 7: |
| 792 | log.trace(" |---| {}", _e); |
| 793 | break; |
| 794 | case 8: |
| 795 | log.trace(" |-----| {}", _e); |
| 796 | break; |
| 797 | case 9: |
| 798 | log.trace(" |-| {}", _e); |
| 799 | break; |
| 800 | case 10: |
| 801 | log.trace(" |-| {}", _e); |
| 802 | break; |
| 803 | case 11: |
| 804 | log.trace(" |---| {}", _e); |
| 805 | break; |
| 806 | case 12: |
| 807 | log.trace(" |-| {}", _e); |
| 808 | break; |
| 809 | case 13: |
| 810 | log.trace(" |-| {}", _e); |
| 811 | break; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 812 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 813 | case 15: |
| 814 | // Fake case |
| 815 | log.trace(" |---? {}", _e); |
| 816 | break; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 817 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 818 | case 16: |
| 819 | // Fake case |
| 820 | log.trace(" |---? {}", _e); |
| 821 | break; |
| 822 | }; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 823 | }; |
| 824 | |
| 825 | |
| Nils Diewald | 41750bf | 2015-02-06 17:45:20 +0000 | [diff] [blame] | 826 | private WithinSpan _currentWrap () { |
| 827 | WithinSpan _wrap = new WithinSpan(); |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 828 | _wrap.start = this.wrapStart != -1 ? this.wrapStart : this.wrapSpans.start(); |
| 829 | _wrap.end = this.wrapEnd != -1 ? this.wrapEnd : this.wrapSpans.end(); |
| 830 | _wrap.doc = this.wrapDoc != -1 ? this.wrapDoc : this.wrapSpans.doc(); |
| 831 | return _wrap; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 832 | }; |
| 833 | |
| Nils Diewald | 41750bf | 2015-02-06 17:45:20 +0000 | [diff] [blame] | 834 | private WithinSpan _currentEmbedded () { |
| 835 | WithinSpan _embedded = new WithinSpan(); |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 836 | _embedded.start = this.embeddedStart != -1 ? |
| 837 | this.embeddedStart : this.embeddedSpans.start(); |
| 838 | _embedded.end = this.embeddedEnd != -1 ? |
| 839 | this.embeddedEnd : this.embeddedSpans.end(); |
| 840 | _embedded.doc = this.embeddedDoc != -1 ? |
| 841 | this.embeddedDoc : this.embeddedSpans.doc(); |
| 842 | return _embedded; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 843 | }; |
| 844 | |
| 845 | |
| 846 | private void todo (byte currentCase) throws IOException { |
| 847 | /* |
| 848 | Check what to do next with the spans. |
| 849 | |
| 850 | The different follow up steps are: |
| 851 | - storeEmbedded -> store span B for later checks |
| 852 | - nextSpanA -> forward a |
| 853 | - nextSpanB -> forward b |
| 854 | |
| 855 | These rules were automatically generated |
| 856 | */ |
| 857 | |
| 858 | // Case 1, 2 |
| 859 | if (currentCase <= (byte) 2) { |
| 860 | this.nextSpanB(); |
| 861 | } |
| 862 | |
| 863 | // Case 12, 13 |
| 864 | else if (currentCase >= (byte) 12) { |
| 865 | this.storeEmbedded(); |
| 866 | this.nextSpanA(); |
| 867 | } |
| 868 | |
| 869 | // Case 3, 4, 5, 8 |
| 870 | else if (currentCase <= (byte) 5 || |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 871 | currentCase == (byte) 8) { |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 872 | if (flag <= 2) |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 873 | this.storeEmbedded(); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 874 | this.nextSpanB(); |
| 875 | } |
| 876 | |
| 877 | // Case 11 |
| 878 | else if (currentCase == (byte) 11) { |
| 879 | if (this.flag == REAL_WITHIN) { |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 880 | this.nextSpanB(); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 881 | } |
| 882 | else if (this.flag >= STARTSWITH) { |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 883 | this.nextSpanA(); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 884 | } |
| 885 | else { |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 886 | this.storeEmbedded(); |
| 887 | this.nextSpanB(); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 888 | }; |
| 889 | } |
| 890 | |
| 891 | |
| 892 | // Case 6, 7, 9, 10 |
| 893 | else { |
| 894 | |
| 895 | if ( |
| 896 | // Case 6 |
| 897 | (currentCase == (byte) 6 && this.flag == MATCH) || |
| 898 | |
| 899 | // Case 7 |
| 900 | (currentCase == (byte) 7 && this.flag == REAL_WITHIN) || |
| 901 | |
| 902 | // Case 9, 10 |
| 903 | (currentCase >= (byte) 9 && this.flag >= STARTSWITH)) { |
| 904 | |
| 905 | this.nextSpanA(); |
| 906 | } |
| 907 | else { |
| 908 | this.storeEmbedded(); |
| 909 | this.nextSpanB(); |
| 910 | }; |
| 911 | }; |
| 912 | }; |
| 913 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 914 | // Store the current embedded span in the first spanStore |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 915 | private void storeEmbedded () throws IOException { |
| 916 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 917 | // Create a current copy |
| Nils Diewald | 41750bf | 2015-02-06 17:45:20 +0000 | [diff] [blame] | 918 | WithinSpan embedded = new WithinSpan(); |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 919 | embedded.start = this.embeddedStart != -1 ? |
| 920 | this.embeddedStart : this.embeddedSpans.start(); |
| 921 | embedded.end = this.embeddedEnd != -1 ? |
| 922 | this.embeddedEnd : this.embeddedSpans.end(); |
| 923 | embedded.doc = this.embeddedDoc; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 924 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 925 | // Copy payloads |
| 926 | if (this.embeddedPayload != null) { |
| 927 | embedded.payload = new ArrayList<byte[]>(this.embeddedPayload.size()); |
| 928 | embedded.payload.addAll(this.embeddedPayload); |
| 929 | } |
| 930 | else if (this.embeddedSpans.isPayloadAvailable()) { |
| 931 | embedded.payload = new ArrayList<byte[]>(3); |
| 932 | Collection<byte[]> payload = this.embeddedSpans.getPayload(); |
| 933 | |
| 934 | this.embeddedPayload = new ArrayList<byte[]>(payload.size()); |
| 935 | this.embeddedPayload.addAll(payload); |
| 936 | embedded.payload.addAll(payload); |
| 937 | }; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 938 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 939 | this.spanStore1.add(embedded); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 940 | |
| Nils Diewald | 83c9b16 | 2015-02-03 21:05:07 +0000 | [diff] [blame] | 941 | if (DEBUG) |
| 942 | log.trace("Pushed to spanStore 1 {} (in storeEmbedded)", embedded.toString()); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 943 | }; |
| 944 | |
| 945 | |
| 946 | // Return case number |
| 947 | private byte withinCase () { |
| 948 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 949 | // case 1-5 |
| 950 | if (this.wrapStart > this.embeddedStart) { |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 951 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 952 | // Case 1 |
| 953 | // |-| |
| 954 | // |-| |
| 955 | if (this.wrapStart > this.embeddedEnd) { |
| 956 | return (byte) 1; |
| 957 | } |
| 958 | |
| 959 | // Case 2 |
| 960 | // |-| |
| 961 | // |-| |
| 962 | else if (this.wrapStart == this.embeddedEnd) { |
| 963 | return (byte) 2; |
| 964 | }; |
| 965 | |
| 966 | // Load wrapEnd |
| 967 | this.wrapEnd = this.wrapSpans.end(); |
| 968 | |
| 969 | // Case 3 |
| 970 | // |---| |
| 971 | // |---| |
| 972 | if (this.wrapEnd > this.embeddedEnd) { |
| 973 | return (byte) 3; |
| 974 | } |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 975 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 976 | // Case 4 |
| 977 | // |-| |
| 978 | // |---| |
| 979 | else if (this.wrapEnd == this.embeddedEnd) { |
| 980 | return (byte) 4; |
| 981 | }; |
| 982 | |
| 983 | // Case 5 |
| 984 | // |-| |
| 985 | // |---| |
| 986 | return (byte) 5; |
| 987 | } |
| 988 | |
| 989 | // case 6-8 |
| 990 | else if (this.wrapStart == this.embeddedStart) { |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 991 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 992 | // Load wrapEnd |
| 993 | this.wrapEnd = this.wrapSpans.end(); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 994 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 995 | // Case 6 |
| 996 | // |---| |
| 997 | // |-| |
| 998 | if (this.wrapEnd > this.embeddedEnd) { |
| 999 | return (byte) 6; |
| 1000 | } |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 1001 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 1002 | // Case 7 |
| 1003 | // |---| |
| 1004 | // |---| |
| 1005 | else if (this.wrapEnd == this.embeddedEnd) { |
| 1006 | return (byte) 7; |
| 1007 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1008 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 1009 | // Case 8 |
| 1010 | // |-| |
| 1011 | // |---| |
| 1012 | return (byte) 8; |
| 1013 | } |
| 1014 | |
| 1015 | // wrapStart < embeddedStart |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1016 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 1017 | // Load wrapEnd |
| 1018 | this.wrapEnd = this.wrapSpans.end(); |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 1019 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 1020 | // Case 13 |
| 1021 | // |-| |
| 1022 | // |-| |
| 1023 | if (this.wrapEnd < this.embeddedStart) { |
| 1024 | return (byte) 13; |
| 1025 | } |
| 1026 | |
| 1027 | // Case 9 |
| 1028 | // |---| |
| 1029 | // |-| |
| 1030 | else if (this.wrapEnd > this.embeddedEnd) { |
| 1031 | return (byte) 9; |
| 1032 | } |
| 1033 | |
| 1034 | // Case 10 |
| 1035 | // |---| |
| 1036 | // |-| |
| 1037 | else if (this.wrapEnd == this.embeddedEnd) { |
| 1038 | return (byte) 10; |
| 1039 | } |
| 1040 | |
| 1041 | // Case 11 |
| 1042 | // |---| |
| 1043 | // |---| |
| 1044 | else if (this.wrapEnd > this.embeddedStart) { |
| 1045 | return (byte) 11; |
| 1046 | } |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 1047 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 1048 | // case 12 |
| 1049 | // |-| |
| 1050 | // |-| |
| 1051 | return (byte) 12; |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 1052 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1053 | |
| 1054 | |
| 1055 | /** Returns the document number of the current match. Initially invalid. */ |
| 1056 | @Override |
| 1057 | public int doc () { |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 1058 | return matchDoc; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1059 | }; |
| 1060 | |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 1061 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1062 | /** Returns the start position of the embedding wrap. Initially invalid. */ |
| 1063 | @Override |
| 1064 | public int start () { |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 1065 | return matchStart; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1066 | }; |
| 1067 | |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 1068 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1069 | /** Returns the end position of the embedding wrap. Initially invalid. */ |
| 1070 | @Override |
| 1071 | public int end () { |
| 1072 | return matchEnd; |
| 1073 | }; |
| 1074 | |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 1075 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1076 | /** |
| 1077 | * Returns the payload data for the current span. |
| 1078 | * This is invalid until {@link #next()} is called for |
| 1079 | * the first time. |
| 1080 | * This method must not be called more than once after each call |
| 1081 | * of {@link #next()}. However, most payloads are loaded lazily, |
| 1082 | * so if the payload data for the current position is not needed, |
| 1083 | * this method may not be called at all for performance reasons. An ordered |
| 1084 | * SpanQuery does not lazy load, so if you have payloads in your index and |
| 1085 | * you do not want ordered SpanNearQuerys to collect payloads, you can |
| Nils Diewald | e072501 | 2014-09-25 19:32:52 +0000 | [diff] [blame] | 1086 | * disable collection with a constructor option.<br> |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1087 | * <br> |
| Nils Diewald | e072501 | 2014-09-25 19:32:52 +0000 | [diff] [blame] | 1088 | * Note that the return type is a collection, thus the ordering should not be relied upon. |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1089 | * <br/> |
| 1090 | * @lucene.experimental |
| 1091 | * |
| 1092 | * @return a List of byte arrays containing the data of this payload, otherwise null if isPayloadAvailable is false |
| 1093 | * @throws IOException if there is a low-level I/O error |
| 1094 | */ |
| 1095 | // public abstract Collection<byte[]> getPayload() throws IOException; |
| 1096 | @Override |
| 1097 | public Collection<byte[]> getPayload() throws IOException { |
| 1098 | return matchPayload; |
| 1099 | }; |
| 1100 | |
| 1101 | |
| 1102 | /** |
| 1103 | * Checks if a payload can be loaded at this position. |
| 1104 | * <p/> |
| 1105 | * Payloads can only be loaded once per call to |
| 1106 | * {@link #next()}. |
| 1107 | * |
| 1108 | * @return true if there is a payload available at this position that can be loaded |
| 1109 | */ |
| 1110 | @Override |
| 1111 | public boolean isPayloadAvailable() { |
| 1112 | return matchPayload.isEmpty() == false; |
| 1113 | }; |
| 1114 | |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 1115 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1116 | // Todo: This may be in the wrong version |
| 1117 | @Override |
| 1118 | public long cost() { |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 1119 | return wrapSpans.cost() + embeddedSpans.cost(); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1120 | }; |
| 1121 | |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 1122 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1123 | @Override |
| 1124 | public String toString() { |
| 1125 | return getClass().getName() + "("+query.toString()+")@"+ |
| Nils Diewald | 6802acd | 2014-03-18 18:29:30 +0000 | [diff] [blame] | 1126 | (embeddedDoc <= 0?"START":(more?(doc()+":"+start()+"-"+end()):"END")); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1127 | }; |
| Nils Diewald | 41750bf | 2015-02-06 17:45:20 +0000 | [diff] [blame] | 1128 | |
| 1129 | |
| 1130 | // This was formerly the default candidate span class, |
| 1131 | // before it was refactored out |
| 1132 | private class WithinSpan implements Comparable<WithinSpan>, Cloneable { |
| 1133 | public int |
| 1134 | start = -1, |
| 1135 | end = -1, |
| 1136 | doc = -1; |
| 1137 | |
| 1138 | public Collection<byte[]> payload; |
| 1139 | |
| 1140 | public short elementRef = -1; |
| 1141 | |
| 1142 | public void clear () { |
| 1143 | this.start = -1; |
| 1144 | this.end = -1; |
| 1145 | this.doc = -1; |
| 1146 | clearPayload(); |
| 1147 | }; |
| 1148 | |
| 1149 | @Override |
| 1150 | public int compareTo (WithinSpan o) { |
| 1151 | /* optimizable for short numbers to return o.end - this.end */ |
| 1152 | if (this.doc < o.doc) { |
| 1153 | return -1; |
| 1154 | } |
| 1155 | else if (this.doc == o.doc) { |
| 1156 | if (this.start < o.start) { |
| 1157 | return -1; |
| 1158 | } |
| 1159 | else if (this.start == o.start) { |
| 1160 | if (this.end < o.end) |
| 1161 | return -1; |
| 1162 | }; |
| 1163 | }; |
| 1164 | return 1; |
| 1165 | }; |
| 1166 | |
| 1167 | public short getElementRef() { |
| 1168 | return elementRef; |
| 1169 | } |
| 1170 | |
| 1171 | public void setElementRef(short elementRef) { |
| 1172 | this.elementRef = elementRef; |
| 1173 | }; |
| 1174 | |
| 1175 | @Override |
| 1176 | public Object clone() { |
| 1177 | WithinSpan span = new WithinSpan(); |
| 1178 | span.start = this.start; |
| 1179 | span.end = this.end; |
| 1180 | span.doc = this.doc; |
| 1181 | span.payload.addAll(this.payload); |
| 1182 | return span; |
| 1183 | }; |
| 1184 | |
| 1185 | public WithinSpan copyFrom (WithinSpan o) { |
| 1186 | this.start = o.start; |
| 1187 | this.end = o.end; |
| 1188 | this.doc = o.doc; |
| 1189 | // this.clearPayload(); |
| 1190 | this.payload.addAll(o.payload); |
| 1191 | return this; |
| 1192 | }; |
| 1193 | |
| 1194 | public void clearPayload () { |
| 1195 | if (this.payload != null) |
| 1196 | this.payload.clear(); |
| 1197 | }; |
| 1198 | |
| 1199 | public String toString () { |
| 1200 | StringBuilder sb = new StringBuilder("["); |
| 1201 | return sb.append(this.start).append('-') |
| 1202 | .append(this.end) |
| 1203 | .append('(').append(this.doc).append(')') |
| 1204 | .append(']') |
| 1205 | .toString(); |
| 1206 | }; |
| 1207 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1208 | }; |