| Eliza Margaretha | c8d0a7a | 2014-09-16 13:08:30 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.query.spans; |
| 2 | |
| 3 | import java.io.IOException; |
| Eliza Margaretha | 57c9fc7 | 2014-09-16 16:44:00 +0000 | [diff] [blame] | 4 | import java.nio.ByteBuffer; |
| Eliza Margaretha | c8d0a7a | 2014-09-16 13:08:30 +0000 | [diff] [blame] | 5 | import java.util.ArrayList; |
| 6 | import java.util.List; |
| 7 | import java.util.Map; |
| 8 | |
| Akron | 700c1eb | 2015-09-25 16:57:30 +0200 | [diff] [blame] | 9 | import org.apache.lucene.index.LeafReaderContext; |
| Eliza Margaretha | c8d0a7a | 2014-09-16 13:08:30 +0000 | [diff] [blame] | 10 | import org.apache.lucene.index.Term; |
| 11 | import org.apache.lucene.index.TermContext; |
| 12 | import org.apache.lucene.search.spans.Spans; |
| 13 | import org.apache.lucene.util.Bits; |
| 14 | |
| 15 | import de.ids_mannheim.korap.query.SpanExpansionQuery; |
| 16 | |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 17 | /** |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 18 | * Enumeration of Spans expanded with minimum <code>m</code> and |
| 19 | * maximum |
| 20 | * <code>n</code> tokens, and throughout all the expansions do |
| 21 | * <em>not</em> |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 22 | * contain a specific Spans (notClause). See examples in |
| 23 | * {@link SpanExpansionQuery}. |
| Eliza Margaretha | 7dd8712 | 2014-09-16 15:34:46 +0000 | [diff] [blame] | 24 | * |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 25 | * The expansion direction is designated with the sign of a number, |
| 26 | * namely a |
| 27 | * negative number signifies the expansion to the <em>left</em> of the |
| 28 | * original |
| 29 | * span, and a positive number (including 0) signifies the expansion |
| 30 | * to the |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 31 | * <em>right</em> of the original span. |
| 32 | * |
| margaretha | 6cbe371 | 2018-10-23 13:22:49 +0200 | [diff] [blame^] | 33 | * The expansion offsets, namely the start and end positions of an |
| 34 | * expansion part, are stored in payloads. A class number is assigned |
| 35 | * to the offsets grouping them altogether. |
| Eliza Margaretha | afe9812 | 2015-01-23 17:37:57 +0000 | [diff] [blame] | 36 | * |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 37 | * @author margaretha |
| Eliza Margaretha | 6f98920 | 2016-10-14 21:48:29 +0200 | [diff] [blame] | 38 | */ |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 39 | public class ExpandedExclusionSpans extends SimpleSpans { |
| Eliza Margaretha | c8d0a7a | 2014-09-16 13:08:30 +0000 | [diff] [blame] | 40 | |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 41 | private int min, max; |
| 42 | private int direction; |
| 43 | private byte classNumber; |
| 44 | private List<CandidateSpan> candidateSpans; |
| 45 | private boolean hasMoreNotClause; |
| 46 | private Spans notClause; |
| Eliza Margaretha | c8d0a7a | 2014-09-16 13:08:30 +0000 | [diff] [blame] | 47 | |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 48 | private long matchCost; |
| Eliza Margaretha | c8d0a7a | 2014-09-16 13:08:30 +0000 | [diff] [blame] | 49 | |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 50 | /** |
| 51 | * Constructs ExpandedExclusionSpans from the given |
| 52 | * {@link SpanExpansionQuery}. |
| 53 | * |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 54 | * @param spanExpansionQuery |
| 55 | * a SpanExpansionQuery |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 56 | * @param context |
| 57 | * @param acceptDocs |
| 58 | * @param termContexts |
| 59 | * @throws IOException |
| 60 | */ |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 61 | public ExpandedExclusionSpans (SpanExpansionQuery spanExpansionQuery, |
| Akron | 4299355 | 2016-02-04 13:24:24 +0100 | [diff] [blame] | 62 | LeafReaderContext context, Bits acceptDocs, |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 63 | Map<Term, TermContext> termContexts) |
| 64 | throws IOException { |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 65 | super(spanExpansionQuery, context, acceptDocs, termContexts); |
| 66 | |
| 67 | if (spanExpansionQuery.getSecondClause() == null) { |
| Eliza Margaretha | 6f98920 | 2016-10-14 21:48:29 +0200 | [diff] [blame] | 68 | throw new IllegalArgumentException("The SpanExpansionQuery " |
| 69 | + "is not valid. The spanquery to exclude (notClause) cannot " |
| 70 | + "be null."); |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /* |
| 74 | * if (spanExpansionQuery.getMin() < 1){ throw new |
| 75 | * IllegalArgumentException("Min occurrence for notClause " + |
| 76 | * "must be at least 1."); } |
| 77 | */ |
| 78 | |
| 79 | this.min = spanExpansionQuery.getMin(); |
| 80 | this.max = spanExpansionQuery.getMax(); |
| 81 | this.direction = spanExpansionQuery.getDirection(); |
| 82 | this.classNumber = spanExpansionQuery.getClassNumber(); |
| 83 | |
| 84 | this.notClause = secondSpans; |
| 85 | this.hasMoreNotClause = notClause.next(); |
| 86 | |
| 87 | candidateSpans = new ArrayList<CandidateSpan>(); |
| 88 | hasMoreSpans = firstSpans.next(); |
| 89 | } |
| 90 | |
| 91 | @Override |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 92 | public boolean next () throws IOException { |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 93 | matchPayload.clear(); |
| 94 | isStartEnumeration = false; |
| 95 | return advance(); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Advances the ExpandedExclusionSpans to the next match. |
| 100 | * |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 101 | * @return <code>true</code> if a match is found, |
| 102 | * <code>false</code> |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 103 | * otherwise. |
| 104 | * @throws IOException |
| 105 | */ |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 106 | private boolean advance () throws IOException { |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 107 | while (hasMoreSpans || candidateSpans.size() > 0) { |
| 108 | if (candidateSpans.size() > 0) { |
| 109 | // set a candidate span as a match |
| 110 | CandidateSpan cs = candidateSpans.get(0); |
| 111 | matchDocNumber = cs.getDoc(); |
| 112 | matchStartPosition = cs.getStart(); |
| 113 | matchEndPosition = cs.getEnd(); |
| 114 | matchPayload = cs.getPayloads(); |
| 115 | matchCost = cs.getCost() + notClause.cost(); |
| 116 | candidateSpans.remove(0); |
| 117 | return true; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 118 | } |
| 119 | else if (!hasMoreNotClause || notClause.doc() > firstSpans.doc()) { |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 120 | generateCandidates(min, max, direction); |
| 121 | hasMoreSpans = firstSpans.next(); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 122 | } |
| 123 | else |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 124 | findMatches(); |
| 125 | } |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | /** |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 130 | * Finds matches by expanding the firstspans either to the left or |
| 131 | * to the |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 132 | * right. |
| 133 | * |
| 134 | * @throws IOException |
| 135 | */ |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 136 | private void findMatches () throws IOException { |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 137 | while (hasMoreNotClause && notClause.doc() <= firstSpans.doc()) { |
| 138 | if (notClause.doc() == firstSpans.doc()) { |
| 139 | if (direction < 0) { // left |
| 140 | expandLeft(); |
| 141 | } // right |
| 142 | else { |
| 143 | expandRight(); |
| 144 | } |
| 145 | break; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 146 | } |
| margaretha | e43c5e5 | 2018-03-20 15:24:53 +0100 | [diff] [blame] | 147 | else if (!notClause.next()) hasMoreNotClause = false; |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Expands the firstspans to the left. |
| 153 | * |
| 154 | * @throws IOException |
| 155 | */ |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 156 | private void expandLeft () throws IOException { |
| margaretha | 6cbe371 | 2018-10-23 13:22:49 +0200 | [diff] [blame^] | 157 | // int counter = max; |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 158 | int maxPos = max; |
| 159 | CandidateSpan lastNotClause = null; |
| margaretha | 6cbe371 | 2018-10-23 13:22:49 +0200 | [diff] [blame^] | 160 | while (hasMoreNotClause && |
| 161 | notClause.doc() == firstSpans.doc() && |
| 162 | notClause.start() < firstSpans.start()) { |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 163 | |
| 164 | // between max and firstspan.start() |
| 165 | if (notClause.start() >= firstSpans.start() - maxPos) { |
| 166 | maxPos = firstSpans.start() - notClause.start() - 1; |
| 167 | lastNotClause = new CandidateSpan(notClause); |
| margaretha | 6cbe371 | 2018-10-23 13:22:49 +0200 | [diff] [blame^] | 168 | // counter--; |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 169 | } |
| margaretha | e43c5e5 | 2018-03-20 15:24:53 +0100 | [diff] [blame] | 170 | if (!notClause.next()) { |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 171 | hasMoreNotClause = false; |
| margaretha | e43c5e5 | 2018-03-20 15:24:53 +0100 | [diff] [blame] | 172 | } |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| margaretha | 6cbe371 | 2018-10-23 13:22:49 +0200 | [diff] [blame^] | 175 | // if a notClause is between max and firstspan.start, |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 176 | // then maxPos = last NotClause pos -1 |
| 177 | generateCandidates(min, maxPos, direction); |
| 178 | |
| margaretha | e43c5e5 | 2018-03-20 15:24:53 +0100 | [diff] [blame] | 179 | if (lastNotClause != null && hasMoreNotClause) |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 180 | while ((hasMoreSpans = firstSpans.next()) |
| margaretha | 6cbe371 | 2018-10-23 13:22:49 +0200 | [diff] [blame^] | 181 | // the next notClause is not in between max and |
| 182 | // firstspan.start() |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 183 | && notClause.start() > firstSpans.start() |
| margaretha | 6cbe371 | 2018-10-23 13:22:49 +0200 | [diff] [blame^] | 184 | // the last notClause is in between max and |
| 185 | // firstspan.start() |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 186 | && lastNotClause.getStart() < firstSpans.start() |
| 187 | && lastNotClause.getStart() >= firstSpans.start() - max) { |
| 188 | |
| 189 | maxPos = firstSpans.start() - lastNotClause.getStart() - 1; |
| 190 | generateCandidates(min, maxPos, direction); |
| 191 | } |
| margaretha | e43c5e5 | 2018-03-20 15:24:53 +0100 | [diff] [blame] | 192 | else { |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 193 | hasMoreSpans = firstSpans.next(); |
| margaretha | e43c5e5 | 2018-03-20 15:24:53 +0100 | [diff] [blame] | 194 | } |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Expands the firstspans to the right. |
| 199 | * |
| 200 | * @throws IOException |
| 201 | */ |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 202 | private void expandRight () throws IOException { |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 203 | int expansionEnd = firstSpans.end() + max; |
| 204 | int maxPos = max; |
| 205 | boolean isFound = false; |
| 206 | |
| 207 | CandidateSpan firstNotClause = null; |
| margaretha | 6cbe371 | 2018-10-23 13:22:49 +0200 | [diff] [blame^] | 208 | // System.out.println("main start:"+firstSpans.start()); |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 209 | while (hasMoreNotClause && notClause.start() < expansionEnd) { |
| 210 | // between firstspan.end() and expansionEnd |
| 211 | if (!isFound && notClause.start() >= firstSpans.end()) { |
| 212 | maxPos = notClause.start() - firstSpans.end() - 1; |
| 213 | firstNotClause = new CandidateSpan(notClause); |
| 214 | isFound = true; |
| 215 | } |
| margaretha | e43c5e5 | 2018-03-20 15:24:53 +0100 | [diff] [blame] | 216 | if (!notClause.next()) { |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 217 | hasMoreNotClause = false; |
| margaretha | e43c5e5 | 2018-03-20 15:24:53 +0100 | [diff] [blame] | 218 | } |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 219 | } |
| 220 | // if a notClause is between firstSpan.end and max |
| margaretha | 6cbe371 | 2018-10-23 13:22:49 +0200 | [diff] [blame^] | 221 | // then maxPos = the first notClause pos -1 |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 222 | generateCandidates(min, maxPos, direction); |
| 223 | |
| 224 | if (firstNotClause != null) { |
| 225 | while ((hasMoreSpans = firstSpans.next()) |
| 226 | // in between |
| 227 | && firstNotClause.getStart() < firstSpans.end() + max |
| 228 | && firstNotClause.getStart() >= firstSpans.end()) { |
| margaretha | 6cbe371 | 2018-10-23 13:22:49 +0200 | [diff] [blame^] | 229 | // System.out.println("first |
| 230 | // start:"+firstNotClause.getStart()+", main |
| 231 | // start:"+firstSpans.start()); |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 232 | maxPos = firstNotClause.getStart() - firstSpans.end() - 1; |
| 233 | generateCandidates(min, maxPos, direction); |
| 234 | } |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 235 | } |
| margaretha | e43c5e5 | 2018-03-20 15:24:53 +0100 | [diff] [blame] | 236 | else { |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 237 | hasMoreSpans = firstSpans.next(); |
| margaretha | e43c5e5 | 2018-03-20 15:24:53 +0100 | [diff] [blame] | 238 | } |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /** |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 242 | * Creates new candidate matches for the given direction, minimum |
| 243 | * and |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 244 | * maximum positions. |
| 245 | * |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 246 | * @param minPos |
| 247 | * minimum position |
| 248 | * @param maxPos |
| 249 | * maximum position |
| 250 | * @param direction |
| 251 | * the expansion direction |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 252 | * @throws IOException |
| 253 | */ |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 254 | private void generateCandidates (int minPos, int maxPos, int direction) |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 255 | throws IOException { |
| 256 | int counter; |
| 257 | int start, end; |
| 258 | CandidateSpan cs; |
| 259 | if (direction < 0) { // left |
| 260 | counter = maxPos; |
| 261 | while (counter >= min) { |
| 262 | start = Math.max(0, firstSpans.start() - counter); |
| 263 | if (start > -1) { |
| 264 | end = firstSpans.end(); |
| margaretha | 6cbe371 | 2018-10-23 13:22:49 +0200 | [diff] [blame^] | 265 | // System.out.println(start+","+end); |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 266 | cs = new CandidateSpan(start, end, firstSpans.doc(), |
| Eliza Margaretha | 6f98920 | 2016-10-14 21:48:29 +0200 | [diff] [blame] | 267 | firstSpans.cost(), |
| 268 | createPayloads(start, firstSpans.start())); |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 269 | candidateSpans.add(cs); |
| 270 | } |
| 271 | counter--; |
| 272 | } |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 273 | } |
| 274 | else { // right |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 275 | counter = minPos; |
| 276 | while (counter <= maxPos) { |
| 277 | start = firstSpans.start(); |
| 278 | end = firstSpans.end() + counter; |
| margaretha | 6cbe371 | 2018-10-23 13:22:49 +0200 | [diff] [blame^] | 279 | // System.out.println(start+","+end); |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 280 | |
| 281 | cs = new CandidateSpan(start, end, firstSpans.doc(), |
| Eliza Margaretha | afe9812 | 2015-01-23 17:37:57 +0000 | [diff] [blame] | 282 | firstSpans.cost(), |
| 283 | createPayloads(firstSpans.end(), end)); |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 284 | candidateSpans.add(cs); |
| 285 | counter++; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /** |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 291 | * Creates payloads for a candiate match by copying the payloads |
| 292 | * of the |
| 293 | * firstspans, and adds expansion offsets with the given start and |
| 294 | * end |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 295 | * positions to the payloads, if the class number is set. |
| 296 | * |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 297 | * @param start |
| 298 | * the start offset |
| 299 | * @param end |
| 300 | * the end offset |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 301 | * @return payloads |
| 302 | * @throws IOException |
| 303 | */ |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 304 | private ArrayList<byte[]> createPayloads (int start, int end) |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 305 | throws IOException { |
| 306 | |
| 307 | ArrayList<byte[]> payload = new ArrayList<byte[]>(); |
| 308 | |
| 309 | if (firstSpans.isPayloadAvailable()) { |
| 310 | payload.addAll(firstSpans.getPayload()); |
| 311 | } |
| 312 | if (classNumber > 0) { |
| margaretha | 6cbe371 | 2018-10-23 13:22:49 +0200 | [diff] [blame^] | 313 | // System.out.println("Extension offsets "+start+","+end); |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 314 | payload.add(createExtensionPayloads(start, end)); |
| 315 | } |
| 316 | return payload; |
| 317 | } |
| 318 | |
| 319 | /** |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 320 | * Generates a byte array of extension offsets and class number to |
| 321 | * be added |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 322 | * into the payloads. |
| 323 | * |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 324 | * @param start |
| 325 | * the start offset |
| 326 | * @param end |
| 327 | * the end offset |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 328 | * @return a byte array of extension offsets and class number |
| 329 | */ |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 330 | private byte[] createExtensionPayloads (int start, int end) { |
| Akron | 4299355 | 2016-02-04 13:24:24 +0100 | [diff] [blame] | 331 | ByteBuffer buffer = ByteBuffer.allocate(10); |
| 332 | Byte classPTI = 0; |
| 333 | buffer.put(classPTI); |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 334 | buffer.putInt(start); |
| 335 | buffer.putInt(end); |
| 336 | buffer.put(classNumber); |
| 337 | return buffer.array(); |
| 338 | } |
| 339 | |
| 340 | @Override |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 341 | public boolean skipTo (int target) throws IOException { |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 342 | if (hasMoreSpans && (firstSpans.doc() < target)) { |
| 343 | if (!firstSpans.skipTo(target)) { |
| 344 | hasMoreSpans = false; |
| 345 | return false; |
| 346 | } |
| 347 | } |
| 348 | matchPayload.clear(); |
| 349 | return advance(); |
| 350 | } |
| 351 | |
| 352 | @Override |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 353 | public long cost () { |
| Eliza Margaretha | dffd059 | 2015-01-15 18:24:39 +0000 | [diff] [blame] | 354 | return matchCost; |
| 355 | } |
| Eliza Margaretha | c8d0a7a | 2014-09-16 13:08:30 +0000 | [diff] [blame] | 356 | } |