| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.index; |
| 2 | |
| 3 | import java.util.*; |
| 4 | import java.io.*; |
| 5 | |
| 6 | import java.nio.ByteBuffer; |
| 7 | |
| 8 | import org.apache.lucene.index.DocsAndPositionsEnum; |
| 9 | import org.apache.lucene.index.AtomicReaderContext; |
| 10 | import org.apache.lucene.index.Terms; |
| 11 | import org.apache.lucene.index.Term; |
| 12 | import org.apache.lucene.index.TermsEnum; |
| 13 | import org.apache.lucene.util.BytesRef; |
| 14 | |
| 15 | import org.slf4j.Logger; |
| 16 | import org.slf4j.LoggerFactory; |
| 17 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 18 | public class PositionsToOffset { |
| 19 | private String field; |
| 20 | private AtomicReaderContext atomic; |
| 21 | private boolean processed = false; |
| 22 | private Integer[] pair; |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 23 | private static ByteBuffer bbOffset = |
| 24 | ByteBuffer.allocate(8); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 25 | |
| 26 | HashSet<PositionsToOffsetArray> positions; |
| 27 | HashMap<PositionsToOffsetArray, Integer[]> offsets; |
| 28 | |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 29 | private final static Logger log = |
| 30 | LoggerFactory.getLogger(PositionsToOffset.class); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 31 | |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 32 | // This advices the java compiler to ignore all loggings |
| 33 | public static final boolean DEBUG = false; |
| 34 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 35 | private class PositionsToOffsetArray { |
| 36 | public int docID; |
| 37 | public int pos; |
| 38 | |
| 39 | public PositionsToOffsetArray (int docID, int pos) { |
| 40 | this.docID = docID; |
| 41 | this.pos = pos; |
| 42 | }; |
| 43 | |
| 44 | public int hashCode(){ |
| 45 | long hashCode; |
| 46 | hashCode = (docID * Integer.MAX_VALUE) - Integer.MAX_VALUE + pos; |
| 47 | return new Long(hashCode).hashCode(); |
| 48 | }; |
| 49 | |
| 50 | public boolean equals(Object obj){ |
| 51 | if (obj instanceof PositionsToOffsetArray) { |
| 52 | PositionsToOffsetArray ptoa = (PositionsToOffsetArray) obj; |
| 53 | return (ptoa.docID == this.docID && ptoa.pos == this.pos); |
| 54 | }; |
| 55 | return false; |
| 56 | }; |
| 57 | }; |
| 58 | |
| 59 | public PositionsToOffset (AtomicReaderContext atomic, String field) { |
| 60 | this.field = field; |
| 61 | this.atomic = atomic; |
| 62 | this.positions = new HashSet<>(64); |
| 63 | this.offsets = new HashMap<>(64); |
| 64 | }; |
| 65 | |
| 66 | public void clear () { |
| 67 | this.positions.clear(); |
| 68 | this.offsets.clear(); |
| 69 | this.bbOffset.clear(); |
| 70 | this.processed = false; |
| 71 | }; |
| 72 | |
| 73 | public void add (int docID, int pos) { |
| 74 | this.add(new PositionsToOffsetArray(docID, pos)); |
| 75 | }; |
| 76 | |
| 77 | public void add (PositionsToOffsetArray ptoa) { |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 78 | if (DEBUG) |
| 79 | log.trace("Add positionsToOffsetArray {}/{}", ptoa.docID, ptoa.pos); |
| Nils Diewald | 20607ab | 2014-03-20 23:28:36 +0000 | [diff] [blame] | 80 | if (ptoa.pos < 0) |
| 81 | return; |
| 82 | |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 83 | if (this.processed && this.exists(ptoa)) |
| 84 | return; |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 85 | |
| 86 | if (DEBUG) |
| 87 | log.trace("Reopen processing"); |
| 88 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 89 | this.positions.add(ptoa); |
| 90 | this.processed = false; |
| 91 | }; |
| 92 | |
| 93 | public boolean exists (int docID, int pos) { |
| 94 | return this.offsets.containsKey(new PositionsToOffsetArray(docID, pos)); |
| 95 | }; |
| 96 | |
| 97 | public boolean exists (PositionsToOffsetArray ptoa) { |
| 98 | return this.offsets.containsKey(ptoa); |
| 99 | }; |
| 100 | |
| 101 | public int start (int docID, int pos) { |
| 102 | return this.start(new PositionsToOffsetArray(docID, pos)); |
| 103 | }; |
| 104 | |
| 105 | public int start (PositionsToOffsetArray ptoa) { |
| 106 | if (ptoa.pos < 0) |
| 107 | return 0; |
| 108 | |
| 109 | if (!processed) |
| 110 | this.offsets(); |
| 111 | |
| 112 | Integer[] pair = this.offsets.get(ptoa); |
| 113 | |
| 114 | if (pair == null) |
| 115 | return 0; |
| 116 | |
| 117 | return pair[0]; |
| 118 | }; |
| 119 | |
| 120 | public int end (int docID, int pos) { |
| 121 | return this.end(new PositionsToOffsetArray(docID, pos)); |
| 122 | }; |
| 123 | |
| 124 | public int end (PositionsToOffsetArray ptoa) { |
| 125 | if (ptoa.pos < 0) |
| 126 | return -1; |
| 127 | |
| 128 | if (!processed) |
| 129 | this.offsets(); |
| 130 | |
| 131 | Integer[] pair = this.offsets.get(ptoa); |
| 132 | if (pair == null) |
| 133 | return -1; |
| Nils Diewald | 3ef9a47 | 2013-12-02 16:06:09 +0000 | [diff] [blame] | 134 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 135 | return pair[1]; |
| 136 | }; |
| 137 | |
| 138 | public Integer[] span (int docID, int pos) { |
| 139 | return this.span(new PositionsToOffsetArray(docID, pos)); |
| 140 | }; |
| 141 | |
| 142 | public Integer[] span (PositionsToOffsetArray ptoa) { |
| 143 | if (!processed) |
| 144 | this.offsets(); |
| 145 | return this.offsets.get(ptoa); |
| 146 | }; |
| 147 | |
| Nils Diewald | cde6908 | 2014-01-16 15:46:48 +0000 | [diff] [blame] | 148 | public void addOffset (int docID, |
| 149 | int pos, |
| 150 | int startOffset, |
| 151 | int endOffset) { |
| 152 | offsets.put( |
| 153 | new PositionsToOffsetArray(docID, pos), |
| 154 | new Integer[]{startOffset, endOffset} |
| 155 | ); |
| 156 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 157 | |
| 158 | public HashMap<PositionsToOffsetArray, Integer[]> offsets () { |
| 159 | if (processed) |
| 160 | return offsets; |
| 161 | |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 162 | if (DEBUG) |
| 163 | log.trace("Process offsets"); |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 164 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 165 | StringBuilder sb = new StringBuilder().append('_'); |
| 166 | |
| 167 | try { |
| 168 | Terms terms = atomic.reader().fields().terms(field); |
| 169 | |
| 170 | if (terms != null) { |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 171 | // TODO: Maybe reuse a termsEnum! |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 172 | |
| 173 | final TermsEnum termsEnum = terms.iterator(null); |
| 174 | |
| 175 | for (PositionsToOffsetArray posDoc : positions) { |
| 176 | if (this.exists(posDoc)) |
| 177 | continue; |
| 178 | |
| 179 | int docID = posDoc.docID; |
| 180 | |
| 181 | /* |
| 182 | int pos = posDoc[1]; |
| 183 | Integer[] posDoc2 = new Integer[2]; |
| 184 | posDoc2[0] = docID; |
| 185 | posDoc2[1] = pos; |
| 186 | */ |
| 187 | |
| 188 | sb.append(posDoc.pos); |
| 189 | |
| 190 | Term term = new Term(field, sb.toString()); |
| 191 | sb.setLength(1); |
| Nils Diewald | 67f5404 | 2014-09-27 14:53:38 +0000 | [diff] [blame] | 192 | |
| 193 | // Set the position in the iterator to the term that is seeked |
| 194 | if (termsEnum.seekExact(term.bytes())) { |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 195 | |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 196 | if (DEBUG) |
| 197 | log.trace("Search for {} in doc {} with pos {}", |
| 198 | term.toString(), |
| 199 | posDoc.docID, |
| 200 | posDoc.pos); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 201 | |
| 202 | // Start an iterator to fetch all payloads of the term |
| 203 | DocsAndPositionsEnum docs = termsEnum.docsAndPositions( |
| 204 | null, |
| 205 | null, |
| 206 | DocsAndPositionsEnum.FLAG_PAYLOADS |
| 207 | ); |
| 208 | |
| 209 | if (docs.advance(docID) == docID) { |
| 210 | docs.nextPosition(); |
| 211 | |
| 212 | BytesRef payload = docs.getPayload(); |
| 213 | |
| 214 | if (payload.length == 8) { |
| 215 | bbOffset.clear(); |
| 216 | bbOffset.put(payload.bytes, payload.offset, 8); |
| 217 | bbOffset.rewind(); |
| 218 | Integer[] offsetArray = new Integer[2]; |
| 219 | offsetArray[0] = bbOffset.getInt(); |
| 220 | offsetArray[1] = bbOffset.getInt(); |
| 221 | offsets.put(posDoc, offsetArray); |
| 222 | |
| Nils Diewald | 82a4b86 | 2014-02-20 21:17:41 +0000 | [diff] [blame] | 223 | if (DEBUG) |
| 224 | log.trace("Found {}-{} for {}", |
| 225 | offsetArray[0], |
| 226 | offsetArray[1], |
| 227 | term.toString()); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | else { |
| 231 | log.error( |
| 232 | "Doc {} has no offsets stored for {}", |
| 233 | docID, |
| 234 | term.toString() |
| 235 | ); |
| 236 | }; |
| 237 | }; |
| 238 | }; |
| 239 | }; |
| 240 | }; |
| 241 | } |
| 242 | catch (IOException e) { |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 243 | log.warn(e.getLocalizedMessage()); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 244 | }; |
| 245 | |
| 246 | processed = true; |
| 247 | positions.clear(); |
| 248 | return offsets; |
| 249 | }; |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 250 | |
| 251 | public AtomicReaderContext getAtomicReader () { |
| 252 | return this.atomic; |
| 253 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 254 | }; |