| 1 | |
|
| 2 | |
|
| 3 | |
package org.galagosearch.core.types; |
| 4 | |
|
| 5 | |
import org.galagosearch.tupleflow.Utility; |
| 6 | |
import org.galagosearch.tupleflow.ArrayInput; |
| 7 | |
import org.galagosearch.tupleflow.ArrayOutput; |
| 8 | |
import org.galagosearch.tupleflow.Order; |
| 9 | |
import org.galagosearch.tupleflow.OrderedWriter; |
| 10 | |
import org.galagosearch.tupleflow.Type; |
| 11 | |
import org.galagosearch.tupleflow.TypeReader; |
| 12 | |
import org.galagosearch.tupleflow.Step; |
| 13 | |
import org.galagosearch.tupleflow.IncompatibleProcessorException; |
| 14 | |
import org.galagosearch.tupleflow.ReaderSource; |
| 15 | |
import java.io.IOException; |
| 16 | |
import java.io.EOFException; |
| 17 | |
import java.io.UnsupportedEncodingException; |
| 18 | |
import java.util.ArrayList; |
| 19 | |
import java.util.Arrays; |
| 20 | |
import java.util.Comparator; |
| 21 | |
import java.util.PriorityQueue; |
| 22 | |
import java.util.Collection; |
| 23 | |
|
| 24 | |
public class NumberedDocumentData implements Type<NumberedDocumentData> { |
| 25 | |
public String identifier; |
| 26 | |
public String url; |
| 27 | |
public int number; |
| 28 | |
public int textLength; |
| 29 | |
|
| 30 | 16 | public NumberedDocumentData() {} |
| 31 | 480 | public NumberedDocumentData(String identifier, String url, int number, int textLength) { |
| 32 | 480 | this.identifier = identifier; |
| 33 | 480 | this.url = url; |
| 34 | 480 | this.number = number; |
| 35 | 480 | this.textLength = textLength; |
| 36 | 480 | } |
| 37 | |
|
| 38 | |
public String toString() { |
| 39 | 0 | return String.format("%s,%s,%d,%d", |
| 40 | |
identifier, url, number, textLength); |
| 41 | |
} |
| 42 | |
|
| 43 | |
public Order<NumberedDocumentData> getOrder(String... spec) { |
| 44 | 16 | if (Arrays.equals(spec, new String[] { })) { |
| 45 | 0 | return new Unordered(); |
| 46 | |
} |
| 47 | 16 | if (Arrays.equals(spec, new String[] { "+number" })) { |
| 48 | 16 | return new NumberOrder(); |
| 49 | |
} |
| 50 | 0 | return null; |
| 51 | |
} |
| 52 | |
|
| 53 | |
public interface Processor extends Step, org.galagosearch.tupleflow.Processor<NumberedDocumentData> { |
| 54 | |
public void process(NumberedDocumentData object) throws IOException; |
| 55 | |
public void close() throws IOException; |
| 56 | |
} |
| 57 | |
public interface Source extends Step { |
| 58 | |
} |
| 59 | 0 | public static class Unordered implements Order<NumberedDocumentData> { |
| 60 | |
public int hash(NumberedDocumentData object) { |
| 61 | 0 | int h = 0; |
| 62 | 0 | return h; |
| 63 | |
} |
| 64 | |
public Comparator<NumberedDocumentData> greaterThan() { |
| 65 | 0 | return new Comparator<NumberedDocumentData>() { |
| 66 | 0 | public int compare(NumberedDocumentData one, NumberedDocumentData two) { |
| 67 | 0 | int result = 0; |
| 68 | |
do { |
| 69 | |
} while (false); |
| 70 | 0 | return -result; |
| 71 | |
} |
| 72 | |
}; |
| 73 | |
} |
| 74 | |
public Comparator<NumberedDocumentData> lessThan() { |
| 75 | 0 | return new Comparator<NumberedDocumentData>() { |
| 76 | 0 | public int compare(NumberedDocumentData one, NumberedDocumentData two) { |
| 77 | 0 | int result = 0; |
| 78 | |
do { |
| 79 | |
} while (false); |
| 80 | 0 | return result; |
| 81 | |
} |
| 82 | |
}; |
| 83 | |
} |
| 84 | |
public TypeReader<NumberedDocumentData> orderedReader(ArrayInput _input) { |
| 85 | 0 | return new ShreddedReader(_input); |
| 86 | |
} |
| 87 | |
|
| 88 | |
public TypeReader<NumberedDocumentData> orderedReader(ArrayInput _input, int bufferSize) { |
| 89 | 0 | return new ShreddedReader(_input, bufferSize); |
| 90 | |
} |
| 91 | |
public OrderedWriter<NumberedDocumentData> orderedWriter(ArrayOutput _output) { |
| 92 | 0 | ShreddedWriter w = new ShreddedWriter(_output); |
| 93 | 0 | return new OrderedWriterClass(w); |
| 94 | |
} |
| 95 | 0 | public static class OrderedWriterClass extends OrderedWriter< NumberedDocumentData > { |
| 96 | 0 | NumberedDocumentData last = null; |
| 97 | 0 | ShreddedWriter shreddedWriter = null; |
| 98 | |
|
| 99 | 0 | public OrderedWriterClass(ShreddedWriter s) { |
| 100 | 0 | this.shreddedWriter = s; |
| 101 | 0 | } |
| 102 | |
|
| 103 | |
public void process(NumberedDocumentData object) throws IOException { |
| 104 | 0 | boolean processAll = false; |
| 105 | 0 | shreddedWriter.processTuple(object.identifier, object.url, object.number, object.textLength); |
| 106 | 0 | last = object; |
| 107 | 0 | } |
| 108 | |
|
| 109 | |
public void close() throws IOException { |
| 110 | 0 | shreddedWriter.close(); |
| 111 | 0 | } |
| 112 | |
|
| 113 | |
public Class<NumberedDocumentData> getInputClass() { |
| 114 | 0 | return NumberedDocumentData.class; |
| 115 | |
} |
| 116 | |
} |
| 117 | |
public ReaderSource<NumberedDocumentData> orderedCombiner(Collection<TypeReader<NumberedDocumentData>> readers, boolean closeOnExit) { |
| 118 | 0 | ArrayList<ShreddedReader> shreddedReaders = new ArrayList(); |
| 119 | |
|
| 120 | 0 | for (TypeReader<NumberedDocumentData> reader : readers) { |
| 121 | 0 | shreddedReaders.add((ShreddedReader)reader); |
| 122 | |
} |
| 123 | |
|
| 124 | 0 | return new ShreddedCombiner(shreddedReaders, closeOnExit); |
| 125 | |
} |
| 126 | |
public NumberedDocumentData clone(NumberedDocumentData object) { |
| 127 | 0 | NumberedDocumentData result = new NumberedDocumentData(); |
| 128 | 0 | if (object == null) return result; |
| 129 | 0 | result.identifier = object.identifier; |
| 130 | 0 | result.url = object.url; |
| 131 | 0 | result.number = object.number; |
| 132 | 0 | result.textLength = object.textLength; |
| 133 | 0 | return result; |
| 134 | |
} |
| 135 | |
public Class<NumberedDocumentData> getOrderedClass() { |
| 136 | 0 | return NumberedDocumentData.class; |
| 137 | |
} |
| 138 | |
public String[] getOrderSpec() { |
| 139 | 0 | return new String[] {}; |
| 140 | |
} |
| 141 | |
|
| 142 | |
public static String getSpecString() { |
| 143 | 0 | return ""; |
| 144 | |
} |
| 145 | |
|
| 146 | |
public interface ShreddedProcessor extends Step { |
| 147 | |
public void processTuple(String identifier, String url, int number, int textLength) throws IOException; |
| 148 | |
public void close() throws IOException; |
| 149 | |
} |
| 150 | |
public interface ShreddedSource extends Step { |
| 151 | |
} |
| 152 | |
|
| 153 | |
public static class ShreddedWriter implements ShreddedProcessor { |
| 154 | |
ArrayOutput output; |
| 155 | 0 | ShreddedBuffer buffer = new ShreddedBuffer(); |
| 156 | 0 | boolean lastFlush = false; |
| 157 | |
|
| 158 | 0 | public ShreddedWriter(ArrayOutput output) { |
| 159 | 0 | this.output = output; |
| 160 | 0 | } |
| 161 | |
|
| 162 | |
public void close() throws IOException { |
| 163 | 0 | flush(); |
| 164 | 0 | } |
| 165 | |
|
| 166 | |
public final void processTuple(String identifier, String url, int number, int textLength) throws IOException { |
| 167 | 0 | if (lastFlush) { |
| 168 | 0 | lastFlush = false; |
| 169 | |
} |
| 170 | 0 | buffer.processTuple(identifier, url, number, textLength); |
| 171 | 0 | if (buffer.isFull()) |
| 172 | 0 | flush(); |
| 173 | 0 | } |
| 174 | |
public final void flushTuples(int pauseIndex) throws IOException { |
| 175 | |
|
| 176 | 0 | while (buffer.getReadIndex() < pauseIndex) { |
| 177 | |
|
| 178 | 0 | output.writeString(buffer.getIdentifier()); |
| 179 | 0 | output.writeString(buffer.getUrl()); |
| 180 | 0 | output.writeInt(buffer.getNumber()); |
| 181 | 0 | output.writeInt(buffer.getTextLength()); |
| 182 | 0 | buffer.incrementTuple(); |
| 183 | |
} |
| 184 | 0 | } |
| 185 | |
public void flush() throws IOException { |
| 186 | 0 | flushTuples(buffer.getWriteIndex()); |
| 187 | 0 | buffer.reset(); |
| 188 | 0 | lastFlush = true; |
| 189 | 0 | } |
| 190 | |
} |
| 191 | 0 | public static class ShreddedBuffer { |
| 192 | |
|
| 193 | |
String[] identifiers; |
| 194 | |
String[] urls; |
| 195 | |
int[] numbers; |
| 196 | |
int[] textLengths; |
| 197 | 0 | int writeTupleIndex = 0; |
| 198 | 0 | int readTupleIndex = 0; |
| 199 | |
int batchSize; |
| 200 | |
|
| 201 | 0 | public ShreddedBuffer(int batchSize) { |
| 202 | 0 | this.batchSize = batchSize; |
| 203 | |
|
| 204 | 0 | identifiers = new String[batchSize]; |
| 205 | 0 | urls = new String[batchSize]; |
| 206 | 0 | numbers = new int[batchSize]; |
| 207 | 0 | textLengths = new int[batchSize]; |
| 208 | 0 | } |
| 209 | |
|
| 210 | |
public ShreddedBuffer() { |
| 211 | 0 | this(10000); |
| 212 | 0 | } |
| 213 | |
|
| 214 | |
public void processTuple(String identifier, String url, int number, int textLength) { |
| 215 | 0 | identifiers[writeTupleIndex] = identifier; |
| 216 | 0 | urls[writeTupleIndex] = url; |
| 217 | 0 | numbers[writeTupleIndex] = number; |
| 218 | 0 | textLengths[writeTupleIndex] = textLength; |
| 219 | 0 | writeTupleIndex++; |
| 220 | 0 | } |
| 221 | |
public void resetData() { |
| 222 | 0 | writeTupleIndex = 0; |
| 223 | 0 | } |
| 224 | |
|
| 225 | |
public void resetRead() { |
| 226 | 0 | readTupleIndex = 0; |
| 227 | 0 | } |
| 228 | |
|
| 229 | |
public void reset() { |
| 230 | 0 | resetData(); |
| 231 | 0 | resetRead(); |
| 232 | 0 | } |
| 233 | |
public boolean isFull() { |
| 234 | 0 | return writeTupleIndex >= batchSize; |
| 235 | |
} |
| 236 | |
|
| 237 | |
public boolean isEmpty() { |
| 238 | 0 | return writeTupleIndex == 0; |
| 239 | |
} |
| 240 | |
|
| 241 | |
public boolean isAtEnd() { |
| 242 | 0 | return readTupleIndex >= writeTupleIndex; |
| 243 | |
} |
| 244 | |
public void incrementTuple() { |
| 245 | 0 | readTupleIndex++; |
| 246 | 0 | } |
| 247 | |
public int getReadIndex() { |
| 248 | 0 | return readTupleIndex; |
| 249 | |
} |
| 250 | |
|
| 251 | |
public int getWriteIndex() { |
| 252 | 0 | return writeTupleIndex; |
| 253 | |
} |
| 254 | |
public String getIdentifier() { |
| 255 | 0 | assert readTupleIndex < writeTupleIndex; |
| 256 | 0 | return identifiers[readTupleIndex]; |
| 257 | |
} |
| 258 | |
public String getUrl() { |
| 259 | 0 | assert readTupleIndex < writeTupleIndex; |
| 260 | 0 | return urls[readTupleIndex]; |
| 261 | |
} |
| 262 | |
public int getNumber() { |
| 263 | 0 | assert readTupleIndex < writeTupleIndex; |
| 264 | 0 | return numbers[readTupleIndex]; |
| 265 | |
} |
| 266 | |
public int getTextLength() { |
| 267 | 0 | assert readTupleIndex < writeTupleIndex; |
| 268 | 0 | return textLengths[readTupleIndex]; |
| 269 | |
} |
| 270 | |
public void copyTuples(int endIndex, ShreddedProcessor output) throws IOException { |
| 271 | 0 | while (getReadIndex() < endIndex) { |
| 272 | 0 | output.processTuple(getIdentifier(), getUrl(), getNumber(), getTextLength()); |
| 273 | 0 | incrementTuple(); |
| 274 | |
} |
| 275 | 0 | } |
| 276 | |
|
| 277 | |
public void copyUntil(ShreddedBuffer other, ShreddedProcessor output) throws IOException { |
| 278 | 0 | } |
| 279 | |
|
| 280 | |
} |
| 281 | 0 | public static class ShreddedCombiner implements ReaderSource<NumberedDocumentData>, ShreddedSource { |
| 282 | |
public ShreddedProcessor processor; |
| 283 | |
Collection<ShreddedReader> readers; |
| 284 | 0 | boolean closeOnExit = false; |
| 285 | 0 | boolean uninitialized = true; |
| 286 | 0 | PriorityQueue<ShreddedReader> queue = new PriorityQueue<ShreddedReader>(); |
| 287 | |
|
| 288 | 0 | public ShreddedCombiner(Collection<ShreddedReader> readers, boolean closeOnExit) { |
| 289 | 0 | this.readers = readers; |
| 290 | 0 | this.closeOnExit = closeOnExit; |
| 291 | 0 | } |
| 292 | |
|
| 293 | |
public void setProcessor(Step processor) throws IncompatibleProcessorException { |
| 294 | 0 | if (processor instanceof ShreddedProcessor) { |
| 295 | 0 | this.processor = new DuplicateEliminator((ShreddedProcessor) processor); |
| 296 | 0 | } else if (processor instanceof NumberedDocumentData.Processor) { |
| 297 | 0 | this.processor = new DuplicateEliminator(new TupleUnshredder((NumberedDocumentData.Processor) processor)); |
| 298 | 0 | } else if (processor instanceof org.galagosearch.tupleflow.Processor) { |
| 299 | 0 | this.processor = new DuplicateEliminator(new TupleUnshredder((org.galagosearch.tupleflow.Processor<NumberedDocumentData>) processor)); |
| 300 | |
} else { |
| 301 | 0 | throw new IncompatibleProcessorException(processor.getClass().getName() + " is not supported by " + this.getClass().getName()); |
| 302 | |
} |
| 303 | 0 | } |
| 304 | |
|
| 305 | |
public Class<NumberedDocumentData> getOutputClass() { |
| 306 | 0 | return NumberedDocumentData.class; |
| 307 | |
} |
| 308 | |
|
| 309 | |
public void initialize() throws IOException { |
| 310 | 0 | for (ShreddedReader reader : readers) { |
| 311 | 0 | reader.fill(); |
| 312 | |
|
| 313 | 0 | if (!reader.getBuffer().isAtEnd()) |
| 314 | 0 | queue.add(reader); |
| 315 | |
} |
| 316 | |
|
| 317 | 0 | uninitialized = false; |
| 318 | 0 | } |
| 319 | |
|
| 320 | |
public void run() throws IOException { |
| 321 | 0 | initialize(); |
| 322 | |
|
| 323 | 0 | while (queue.size() > 0) { |
| 324 | 0 | ShreddedReader top = queue.poll(); |
| 325 | 0 | ShreddedReader next = null; |
| 326 | 0 | ShreddedBuffer nextBuffer = null; |
| 327 | |
|
| 328 | 0 | assert !top.getBuffer().isAtEnd(); |
| 329 | |
|
| 330 | 0 | if (queue.size() > 0) { |
| 331 | 0 | next = queue.peek(); |
| 332 | 0 | nextBuffer = next.getBuffer(); |
| 333 | 0 | assert !nextBuffer.isAtEnd(); |
| 334 | |
} |
| 335 | |
|
| 336 | 0 | top.getBuffer().copyUntil(nextBuffer, processor); |
| 337 | 0 | if (top.getBuffer().isAtEnd()) |
| 338 | 0 | top.fill(); |
| 339 | |
|
| 340 | 0 | if (!top.getBuffer().isAtEnd()) |
| 341 | 0 | queue.add(top); |
| 342 | 0 | } |
| 343 | |
|
| 344 | 0 | if (closeOnExit) |
| 345 | 0 | processor.close(); |
| 346 | 0 | } |
| 347 | |
|
| 348 | |
public NumberedDocumentData read() throws IOException { |
| 349 | 0 | if (uninitialized) |
| 350 | 0 | initialize(); |
| 351 | |
|
| 352 | 0 | NumberedDocumentData result = null; |
| 353 | |
|
| 354 | 0 | while (queue.size() > 0) { |
| 355 | 0 | ShreddedReader top = queue.poll(); |
| 356 | 0 | result = top.read(); |
| 357 | |
|
| 358 | 0 | if (result != null) { |
| 359 | 0 | if (top.getBuffer().isAtEnd()) |
| 360 | 0 | top.fill(); |
| 361 | |
|
| 362 | 0 | queue.offer(top); |
| 363 | 0 | break; |
| 364 | |
} |
| 365 | 0 | } |
| 366 | |
|
| 367 | 0 | return result; |
| 368 | |
} |
| 369 | |
} |
| 370 | 0 | public static class ShreddedReader implements Step, Comparable<ShreddedReader>, TypeReader<NumberedDocumentData>, ShreddedSource { |
| 371 | |
public ShreddedProcessor processor; |
| 372 | |
ShreddedBuffer buffer; |
| 373 | 0 | NumberedDocumentData last = new NumberedDocumentData(); |
| 374 | 0 | long tupleCount = 0; |
| 375 | 0 | long bufferStartCount = 0; |
| 376 | |
ArrayInput input; |
| 377 | |
|
| 378 | 0 | public ShreddedReader(ArrayInput input) { |
| 379 | 0 | this.input = input; |
| 380 | 0 | this.buffer = new ShreddedBuffer(); |
| 381 | 0 | } |
| 382 | |
|
| 383 | 0 | public ShreddedReader(ArrayInput input, int bufferSize) { |
| 384 | 0 | this.input = input; |
| 385 | 0 | this.buffer = new ShreddedBuffer(bufferSize); |
| 386 | 0 | } |
| 387 | |
|
| 388 | |
public final int compareTo(ShreddedReader other) { |
| 389 | 0 | ShreddedBuffer otherBuffer = other.getBuffer(); |
| 390 | |
|
| 391 | 0 | if (buffer.isAtEnd() && otherBuffer.isAtEnd()) { |
| 392 | 0 | return 0; |
| 393 | 0 | } else if (buffer.isAtEnd()) { |
| 394 | 0 | return -1; |
| 395 | 0 | } else if (otherBuffer.isAtEnd()) { |
| 396 | 0 | return 1; |
| 397 | |
} |
| 398 | |
|
| 399 | 0 | int result = 0; |
| 400 | |
do { |
| 401 | |
} while (false); |
| 402 | |
|
| 403 | 0 | return result; |
| 404 | |
} |
| 405 | |
|
| 406 | |
public final ShreddedBuffer getBuffer() { |
| 407 | 0 | return buffer; |
| 408 | |
} |
| 409 | |
|
| 410 | |
public final NumberedDocumentData read() throws IOException { |
| 411 | 0 | if (buffer.isAtEnd()) { |
| 412 | 0 | fill(); |
| 413 | |
|
| 414 | 0 | if (buffer.isAtEnd()) { |
| 415 | 0 | return null; |
| 416 | |
} |
| 417 | |
} |
| 418 | |
|
| 419 | 0 | assert !buffer.isAtEnd(); |
| 420 | 0 | NumberedDocumentData result = new NumberedDocumentData(); |
| 421 | |
|
| 422 | 0 | result.identifier = buffer.getIdentifier(); |
| 423 | 0 | result.url = buffer.getUrl(); |
| 424 | 0 | result.number = buffer.getNumber(); |
| 425 | 0 | result.textLength = buffer.getTextLength(); |
| 426 | |
|
| 427 | 0 | buffer.incrementTuple(); |
| 428 | |
|
| 429 | 0 | return result; |
| 430 | |
} |
| 431 | |
|
| 432 | |
public final void fill() throws IOException { |
| 433 | |
try { |
| 434 | 0 | buffer.reset(); |
| 435 | |
|
| 436 | 0 | if (tupleCount != 0) { |
| 437 | 0 | bufferStartCount = tupleCount; |
| 438 | |
} |
| 439 | |
|
| 440 | 0 | while (!buffer.isFull()) { |
| 441 | 0 | buffer.processTuple(input.readString(), input.readString(), input.readInt(), input.readInt()); |
| 442 | 0 | tupleCount++; |
| 443 | |
} |
| 444 | 0 | } catch(EOFException e) {} |
| 445 | 0 | } |
| 446 | |
|
| 447 | |
|
| 448 | |
public void run() throws IOException { |
| 449 | |
while (true) { |
| 450 | 0 | fill(); |
| 451 | |
|
| 452 | 0 | if (buffer.isAtEnd()) |
| 453 | 0 | break; |
| 454 | |
|
| 455 | 0 | buffer.copyUntil(null, processor); |
| 456 | |
} |
| 457 | 0 | processor.close(); |
| 458 | 0 | } |
| 459 | |
|
| 460 | |
public void setProcessor(Step processor) throws IncompatibleProcessorException { |
| 461 | 0 | if (processor instanceof ShreddedProcessor) { |
| 462 | 0 | this.processor = new DuplicateEliminator((ShreddedProcessor) processor); |
| 463 | 0 | } else if (processor instanceof NumberedDocumentData.Processor) { |
| 464 | 0 | this.processor = new DuplicateEliminator(new TupleUnshredder((NumberedDocumentData.Processor) processor)); |
| 465 | 0 | } else if (processor instanceof org.galagosearch.tupleflow.Processor) { |
| 466 | 0 | this.processor = new DuplicateEliminator(new TupleUnshredder((org.galagosearch.tupleflow.Processor<NumberedDocumentData>) processor)); |
| 467 | |
} else { |
| 468 | 0 | throw new IncompatibleProcessorException(processor.getClass().getName() + " is not supported by " + this.getClass().getName()); |
| 469 | |
} |
| 470 | 0 | } |
| 471 | |
|
| 472 | |
public Class<NumberedDocumentData> getOutputClass() { |
| 473 | 0 | return NumberedDocumentData.class; |
| 474 | |
} |
| 475 | |
} |
| 476 | |
|
| 477 | |
public static class DuplicateEliminator implements ShreddedProcessor { |
| 478 | |
public ShreddedProcessor processor; |
| 479 | 0 | NumberedDocumentData last = new NumberedDocumentData(); |
| 480 | |
|
| 481 | 0 | public DuplicateEliminator() {} |
| 482 | 0 | public DuplicateEliminator(ShreddedProcessor processor) { |
| 483 | 0 | this.processor = processor; |
| 484 | 0 | } |
| 485 | |
|
| 486 | |
public void setShreddedProcessor(ShreddedProcessor processor) { |
| 487 | 0 | this.processor = processor; |
| 488 | 0 | } |
| 489 | |
|
| 490 | |
|
| 491 | |
|
| 492 | |
|
| 493 | |
public void processTuple(String identifier, String url, int number, int textLength) throws IOException { |
| 494 | 0 | processor.processTuple(identifier, url, number, textLength); |
| 495 | 0 | } |
| 496 | |
|
| 497 | |
public void close() throws IOException { |
| 498 | 0 | processor.close(); |
| 499 | 0 | } |
| 500 | |
} |
| 501 | |
public static class TupleUnshredder implements ShreddedProcessor { |
| 502 | 0 | NumberedDocumentData last = new NumberedDocumentData(); |
| 503 | |
public org.galagosearch.tupleflow.Processor<NumberedDocumentData> processor; |
| 504 | |
|
| 505 | 0 | public TupleUnshredder(NumberedDocumentData.Processor processor) { |
| 506 | 0 | this.processor = processor; |
| 507 | 0 | } |
| 508 | |
|
| 509 | 0 | public TupleUnshredder(org.galagosearch.tupleflow.Processor<NumberedDocumentData> processor) { |
| 510 | 0 | this.processor = processor; |
| 511 | 0 | } |
| 512 | |
|
| 513 | |
public NumberedDocumentData clone(NumberedDocumentData object) { |
| 514 | 0 | NumberedDocumentData result = new NumberedDocumentData(); |
| 515 | 0 | if (object == null) return result; |
| 516 | 0 | result.identifier = object.identifier; |
| 517 | 0 | result.url = object.url; |
| 518 | 0 | result.number = object.number; |
| 519 | 0 | result.textLength = object.textLength; |
| 520 | 0 | return result; |
| 521 | |
} |
| 522 | |
|
| 523 | |
|
| 524 | |
public void processTuple(String identifier, String url, int number, int textLength) throws IOException { |
| 525 | 0 | last.identifier = identifier; |
| 526 | 0 | last.url = url; |
| 527 | 0 | last.number = number; |
| 528 | 0 | last.textLength = textLength; |
| 529 | 0 | processor.process(clone(last)); |
| 530 | 0 | } |
| 531 | |
|
| 532 | |
public void close() throws IOException { |
| 533 | 0 | processor.close(); |
| 534 | 0 | } |
| 535 | |
} |
| 536 | 0 | public static class TupleShredder implements Processor { |
| 537 | 0 | NumberedDocumentData last = new NumberedDocumentData(); |
| 538 | |
public ShreddedProcessor processor; |
| 539 | |
|
| 540 | 0 | public TupleShredder(ShreddedProcessor processor) { |
| 541 | 0 | this.processor = processor; |
| 542 | 0 | } |
| 543 | |
|
| 544 | |
public NumberedDocumentData clone(NumberedDocumentData object) { |
| 545 | 0 | NumberedDocumentData result = new NumberedDocumentData(); |
| 546 | 0 | if (object == null) return result; |
| 547 | 0 | result.identifier = object.identifier; |
| 548 | 0 | result.url = object.url; |
| 549 | 0 | result.number = object.number; |
| 550 | 0 | result.textLength = object.textLength; |
| 551 | 0 | return result; |
| 552 | |
} |
| 553 | |
|
| 554 | |
public void process(NumberedDocumentData object) throws IOException { |
| 555 | 0 | boolean processAll = false; |
| 556 | 0 | processor.processTuple(object.identifier, object.url, object.number, object.textLength); |
| 557 | 0 | } |
| 558 | |
|
| 559 | |
public Class<NumberedDocumentData> getInputClass() { |
| 560 | 0 | return NumberedDocumentData.class; |
| 561 | |
} |
| 562 | |
|
| 563 | |
public void close() throws IOException { |
| 564 | 0 | processor.close(); |
| 565 | 0 | } |
| 566 | |
} |
| 567 | |
} |
| 568 | 124 | public static class NumberOrder implements Order<NumberedDocumentData> { |
| 569 | |
public int hash(NumberedDocumentData object) { |
| 570 | 0 | int h = 0; |
| 571 | 0 | h += Utility.hash(object.number); |
| 572 | 0 | return h; |
| 573 | |
} |
| 574 | |
public Comparator<NumberedDocumentData> greaterThan() { |
| 575 | 0 | return new Comparator<NumberedDocumentData>() { |
| 576 | 0 | public int compare(NumberedDocumentData one, NumberedDocumentData two) { |
| 577 | 0 | int result = 0; |
| 578 | |
do { |
| 579 | 0 | result = + Utility.compare(one.number, two.number); |
| 580 | 0 | if(result != 0) break; |
| 581 | |
} while (false); |
| 582 | 0 | return -result; |
| 583 | |
} |
| 584 | |
}; |
| 585 | |
} |
| 586 | |
public Comparator<NumberedDocumentData> lessThan() { |
| 587 | 0 | return new Comparator<NumberedDocumentData>() { |
| 588 | 0 | public int compare(NumberedDocumentData one, NumberedDocumentData two) { |
| 589 | 0 | int result = 0; |
| 590 | |
do { |
| 591 | 0 | result = + Utility.compare(one.number, two.number); |
| 592 | 0 | if(result != 0) break; |
| 593 | |
} while (false); |
| 594 | 0 | return result; |
| 595 | |
} |
| 596 | |
}; |
| 597 | |
} |
| 598 | |
public TypeReader<NumberedDocumentData> orderedReader(ArrayInput _input) { |
| 599 | 0 | return new ShreddedReader(_input); |
| 600 | |
} |
| 601 | |
|
| 602 | |
public TypeReader<NumberedDocumentData> orderedReader(ArrayInput _input, int bufferSize) { |
| 603 | 0 | return new ShreddedReader(_input, bufferSize); |
| 604 | |
} |
| 605 | |
public OrderedWriter<NumberedDocumentData> orderedWriter(ArrayOutput _output) { |
| 606 | 0 | ShreddedWriter w = new ShreddedWriter(_output); |
| 607 | 0 | return new OrderedWriterClass(w); |
| 608 | |
} |
| 609 | 0 | public static class OrderedWriterClass extends OrderedWriter< NumberedDocumentData > { |
| 610 | 0 | NumberedDocumentData last = null; |
| 611 | 0 | ShreddedWriter shreddedWriter = null; |
| 612 | |
|
| 613 | 0 | public OrderedWriterClass(ShreddedWriter s) { |
| 614 | 0 | this.shreddedWriter = s; |
| 615 | 0 | } |
| 616 | |
|
| 617 | |
public void process(NumberedDocumentData object) throws IOException { |
| 618 | 0 | boolean processAll = false; |
| 619 | 0 | if (processAll || last == null || 0 != Utility.compare(object.number, last.number)) { processAll = true; shreddedWriter.processNumber(object.number); } |
| 620 | 0 | shreddedWriter.processTuple(object.identifier, object.url, object.textLength); |
| 621 | 0 | last = object; |
| 622 | 0 | } |
| 623 | |
|
| 624 | |
public void close() throws IOException { |
| 625 | 0 | shreddedWriter.close(); |
| 626 | 0 | } |
| 627 | |
|
| 628 | |
public Class<NumberedDocumentData> getInputClass() { |
| 629 | 0 | return NumberedDocumentData.class; |
| 630 | |
} |
| 631 | |
} |
| 632 | |
public ReaderSource<NumberedDocumentData> orderedCombiner(Collection<TypeReader<NumberedDocumentData>> readers, boolean closeOnExit) { |
| 633 | 0 | ArrayList<ShreddedReader> shreddedReaders = new ArrayList(); |
| 634 | |
|
| 635 | 0 | for (TypeReader<NumberedDocumentData> reader : readers) { |
| 636 | 0 | shreddedReaders.add((ShreddedReader)reader); |
| 637 | |
} |
| 638 | |
|
| 639 | 0 | return new ShreddedCombiner(shreddedReaders, closeOnExit); |
| 640 | |
} |
| 641 | |
public NumberedDocumentData clone(NumberedDocumentData object) { |
| 642 | 0 | NumberedDocumentData result = new NumberedDocumentData(); |
| 643 | 0 | if (object == null) return result; |
| 644 | 0 | result.identifier = object.identifier; |
| 645 | 0 | result.url = object.url; |
| 646 | 0 | result.number = object.number; |
| 647 | 0 | result.textLength = object.textLength; |
| 648 | 0 | return result; |
| 649 | |
} |
| 650 | |
public Class<NumberedDocumentData> getOrderedClass() { |
| 651 | 108 | return NumberedDocumentData.class; |
| 652 | |
} |
| 653 | |
public String[] getOrderSpec() { |
| 654 | 108 | return new String[] {"+number"}; |
| 655 | |
} |
| 656 | |
|
| 657 | |
public static String getSpecString() { |
| 658 | 0 | return "+number"; |
| 659 | |
} |
| 660 | |
|
| 661 | |
public interface ShreddedProcessor extends Step { |
| 662 | |
public void processNumber(int number) throws IOException; |
| 663 | |
public void processTuple(String identifier, String url, int textLength) throws IOException; |
| 664 | |
public void close() throws IOException; |
| 665 | |
} |
| 666 | |
public interface ShreddedSource extends Step { |
| 667 | |
} |
| 668 | |
|
| 669 | 0 | public static class ShreddedWriter implements ShreddedProcessor { |
| 670 | |
ArrayOutput output; |
| 671 | 0 | ShreddedBuffer buffer = new ShreddedBuffer(); |
| 672 | |
int lastNumber; |
| 673 | 0 | boolean lastFlush = false; |
| 674 | |
|
| 675 | 0 | public ShreddedWriter(ArrayOutput output) { |
| 676 | 0 | this.output = output; |
| 677 | 0 | } |
| 678 | |
|
| 679 | |
public void close() throws IOException { |
| 680 | 0 | flush(); |
| 681 | 0 | } |
| 682 | |
|
| 683 | |
public void processNumber(int number) { |
| 684 | 0 | lastNumber = number; |
| 685 | 0 | buffer.processNumber(number); |
| 686 | 0 | } |
| 687 | |
public final void processTuple(String identifier, String url, int textLength) throws IOException { |
| 688 | 0 | if (lastFlush) { |
| 689 | 0 | if(buffer.numbers.size() == 0) buffer.processNumber(lastNumber); |
| 690 | 0 | lastFlush = false; |
| 691 | |
} |
| 692 | 0 | buffer.processTuple(identifier, url, textLength); |
| 693 | 0 | if (buffer.isFull()) |
| 694 | 0 | flush(); |
| 695 | 0 | } |
| 696 | |
public final void flushTuples(int pauseIndex) throws IOException { |
| 697 | |
|
| 698 | 0 | while (buffer.getReadIndex() < pauseIndex) { |
| 699 | |
|
| 700 | 0 | output.writeString(buffer.getIdentifier()); |
| 701 | 0 | output.writeString(buffer.getUrl()); |
| 702 | 0 | output.writeInt(buffer.getTextLength()); |
| 703 | 0 | buffer.incrementTuple(); |
| 704 | |
} |
| 705 | 0 | } |
| 706 | |
public final void flushNumber(int pauseIndex) throws IOException { |
| 707 | 0 | while (buffer.getReadIndex() < pauseIndex) { |
| 708 | 0 | int nextPause = buffer.getNumberEndIndex(); |
| 709 | 0 | int count = nextPause - buffer.getReadIndex(); |
| 710 | |
|
| 711 | 0 | output.writeInt(buffer.getNumber()); |
| 712 | 0 | output.writeInt(count); |
| 713 | 0 | buffer.incrementNumber(); |
| 714 | |
|
| 715 | 0 | flushTuples(nextPause); |
| 716 | 0 | assert nextPause == buffer.getReadIndex(); |
| 717 | 0 | } |
| 718 | 0 | } |
| 719 | |
public void flush() throws IOException { |
| 720 | 0 | flushNumber(buffer.getWriteIndex()); |
| 721 | 0 | buffer.reset(); |
| 722 | 0 | lastFlush = true; |
| 723 | 0 | } |
| 724 | |
} |
| 725 | 0 | public static class ShreddedBuffer { |
| 726 | 0 | ArrayList<Integer> numbers = new ArrayList(); |
| 727 | 0 | ArrayList<Integer> numberTupleIdx = new ArrayList(); |
| 728 | 0 | int numberReadIdx = 0; |
| 729 | |
|
| 730 | |
String[] identifiers; |
| 731 | |
String[] urls; |
| 732 | |
int[] textLengths; |
| 733 | 0 | int writeTupleIndex = 0; |
| 734 | 0 | int readTupleIndex = 0; |
| 735 | |
int batchSize; |
| 736 | |
|
| 737 | 0 | public ShreddedBuffer(int batchSize) { |
| 738 | 0 | this.batchSize = batchSize; |
| 739 | |
|
| 740 | 0 | identifiers = new String[batchSize]; |
| 741 | 0 | urls = new String[batchSize]; |
| 742 | 0 | textLengths = new int[batchSize]; |
| 743 | 0 | } |
| 744 | |
|
| 745 | |
public ShreddedBuffer() { |
| 746 | 0 | this(10000); |
| 747 | 0 | } |
| 748 | |
|
| 749 | |
public void processNumber(int number) { |
| 750 | 0 | numbers.add(number); |
| 751 | 0 | numberTupleIdx.add(writeTupleIndex); |
| 752 | 0 | } |
| 753 | |
public void processTuple(String identifier, String url, int textLength) { |
| 754 | 0 | assert numbers.size() > 0; |
| 755 | 0 | identifiers[writeTupleIndex] = identifier; |
| 756 | 0 | urls[writeTupleIndex] = url; |
| 757 | 0 | textLengths[writeTupleIndex] = textLength; |
| 758 | 0 | writeTupleIndex++; |
| 759 | 0 | } |
| 760 | |
public void resetData() { |
| 761 | 0 | numbers.clear(); |
| 762 | 0 | numberTupleIdx.clear(); |
| 763 | 0 | writeTupleIndex = 0; |
| 764 | 0 | } |
| 765 | |
|
| 766 | |
public void resetRead() { |
| 767 | 0 | readTupleIndex = 0; |
| 768 | 0 | numberReadIdx = 0; |
| 769 | 0 | } |
| 770 | |
|
| 771 | |
public void reset() { |
| 772 | 0 | resetData(); |
| 773 | 0 | resetRead(); |
| 774 | 0 | } |
| 775 | |
public boolean isFull() { |
| 776 | 0 | return writeTupleIndex >= batchSize; |
| 777 | |
} |
| 778 | |
|
| 779 | |
public boolean isEmpty() { |
| 780 | 0 | return writeTupleIndex == 0; |
| 781 | |
} |
| 782 | |
|
| 783 | |
public boolean isAtEnd() { |
| 784 | 0 | return readTupleIndex >= writeTupleIndex; |
| 785 | |
} |
| 786 | |
public void incrementNumber() { |
| 787 | 0 | numberReadIdx++; |
| 788 | 0 | } |
| 789 | |
|
| 790 | |
public void autoIncrementNumber() { |
| 791 | 0 | while (readTupleIndex >= getNumberEndIndex() && readTupleIndex < writeTupleIndex) |
| 792 | 0 | numberReadIdx++; |
| 793 | 0 | } |
| 794 | |
public void incrementTuple() { |
| 795 | 0 | readTupleIndex++; |
| 796 | 0 | } |
| 797 | |
public int getNumberEndIndex() { |
| 798 | 0 | if ((numberReadIdx+1) >= numberTupleIdx.size()) |
| 799 | 0 | return writeTupleIndex; |
| 800 | 0 | return numberTupleIdx.get(numberReadIdx+1); |
| 801 | |
} |
| 802 | |
public int getReadIndex() { |
| 803 | 0 | return readTupleIndex; |
| 804 | |
} |
| 805 | |
|
| 806 | |
public int getWriteIndex() { |
| 807 | 0 | return writeTupleIndex; |
| 808 | |
} |
| 809 | |
public int getNumber() { |
| 810 | 0 | assert readTupleIndex < writeTupleIndex; |
| 811 | 0 | assert numberReadIdx < numbers.size(); |
| 812 | |
|
| 813 | 0 | return numbers.get(numberReadIdx); |
| 814 | |
} |
| 815 | |
public String getIdentifier() { |
| 816 | 0 | assert readTupleIndex < writeTupleIndex; |
| 817 | 0 | return identifiers[readTupleIndex]; |
| 818 | |
} |
| 819 | |
public String getUrl() { |
| 820 | 0 | assert readTupleIndex < writeTupleIndex; |
| 821 | 0 | return urls[readTupleIndex]; |
| 822 | |
} |
| 823 | |
public int getTextLength() { |
| 824 | 0 | assert readTupleIndex < writeTupleIndex; |
| 825 | 0 | return textLengths[readTupleIndex]; |
| 826 | |
} |
| 827 | |
public void copyTuples(int endIndex, ShreddedProcessor output) throws IOException { |
| 828 | 0 | while (getReadIndex() < endIndex) { |
| 829 | 0 | output.processTuple(getIdentifier(), getUrl(), getTextLength()); |
| 830 | 0 | incrementTuple(); |
| 831 | |
} |
| 832 | 0 | } |
| 833 | |
public void copyUntilIndexNumber(int endIndex, ShreddedProcessor output) throws IOException { |
| 834 | 0 | while (getReadIndex() < endIndex) { |
| 835 | 0 | output.processNumber(getNumber()); |
| 836 | 0 | assert getNumberEndIndex() <= endIndex; |
| 837 | 0 | copyTuples(getNumberEndIndex(), output); |
| 838 | 0 | incrementNumber(); |
| 839 | |
} |
| 840 | 0 | } |
| 841 | |
public void copyUntilNumber(ShreddedBuffer other, ShreddedProcessor output) throws IOException { |
| 842 | 0 | while (!isAtEnd()) { |
| 843 | 0 | if (other != null) { |
| 844 | 0 | assert !other.isAtEnd(); |
| 845 | 0 | int c = + Utility.compare(getNumber(), other.getNumber()); |
| 846 | |
|
| 847 | 0 | if (c > 0) { |
| 848 | 0 | break; |
| 849 | |
} |
| 850 | |
|
| 851 | 0 | output.processNumber(getNumber()); |
| 852 | |
|
| 853 | 0 | copyTuples(getNumberEndIndex(), output); |
| 854 | 0 | } else { |
| 855 | 0 | output.processNumber(getNumber()); |
| 856 | 0 | copyTuples(getNumberEndIndex(), output); |
| 857 | |
} |
| 858 | 0 | incrementNumber(); |
| 859 | |
|
| 860 | |
|
| 861 | |
} |
| 862 | 0 | } |
| 863 | |
public void copyUntil(ShreddedBuffer other, ShreddedProcessor output) throws IOException { |
| 864 | 0 | copyUntilNumber(other, output); |
| 865 | 0 | } |
| 866 | |
|
| 867 | |
} |
| 868 | 0 | public static class ShreddedCombiner implements ReaderSource<NumberedDocumentData>, ShreddedSource { |
| 869 | |
public ShreddedProcessor processor; |
| 870 | |
Collection<ShreddedReader> readers; |
| 871 | 0 | boolean closeOnExit = false; |
| 872 | 0 | boolean uninitialized = true; |
| 873 | 0 | PriorityQueue<ShreddedReader> queue = new PriorityQueue<ShreddedReader>(); |
| 874 | |
|
| 875 | 0 | public ShreddedCombiner(Collection<ShreddedReader> readers, boolean closeOnExit) { |
| 876 | 0 | this.readers = readers; |
| 877 | 0 | this.closeOnExit = closeOnExit; |
| 878 | 0 | } |
| 879 | |
|
| 880 | |
public void setProcessor(Step processor) throws IncompatibleProcessorException { |
| 881 | 0 | if (processor instanceof ShreddedProcessor) { |
| 882 | 0 | this.processor = new DuplicateEliminator((ShreddedProcessor) processor); |
| 883 | 0 | } else if (processor instanceof NumberedDocumentData.Processor) { |
| 884 | 0 | this.processor = new DuplicateEliminator(new TupleUnshredder((NumberedDocumentData.Processor) processor)); |
| 885 | 0 | } else if (processor instanceof org.galagosearch.tupleflow.Processor) { |
| 886 | 0 | this.processor = new DuplicateEliminator(new TupleUnshredder((org.galagosearch.tupleflow.Processor<NumberedDocumentData>) processor)); |
| 887 | |
} else { |
| 888 | 0 | throw new IncompatibleProcessorException(processor.getClass().getName() + " is not supported by " + this.getClass().getName()); |
| 889 | |
} |
| 890 | 0 | } |
| 891 | |
|
| 892 | |
public Class<NumberedDocumentData> getOutputClass() { |
| 893 | 0 | return NumberedDocumentData.class; |
| 894 | |
} |
| 895 | |
|
| 896 | |
public void initialize() throws IOException { |
| 897 | 0 | for (ShreddedReader reader : readers) { |
| 898 | 0 | reader.fill(); |
| 899 | |
|
| 900 | 0 | if (!reader.getBuffer().isAtEnd()) |
| 901 | 0 | queue.add(reader); |
| 902 | |
} |
| 903 | |
|
| 904 | 0 | uninitialized = false; |
| 905 | 0 | } |
| 906 | |
|
| 907 | |
public void run() throws IOException { |
| 908 | 0 | initialize(); |
| 909 | |
|
| 910 | 0 | while (queue.size() > 0) { |
| 911 | 0 | ShreddedReader top = queue.poll(); |
| 912 | 0 | ShreddedReader next = null; |
| 913 | 0 | ShreddedBuffer nextBuffer = null; |
| 914 | |
|
| 915 | 0 | assert !top.getBuffer().isAtEnd(); |
| 916 | |
|
| 917 | 0 | if (queue.size() > 0) { |
| 918 | 0 | next = queue.peek(); |
| 919 | 0 | nextBuffer = next.getBuffer(); |
| 920 | 0 | assert !nextBuffer.isAtEnd(); |
| 921 | |
} |
| 922 | |
|
| 923 | 0 | top.getBuffer().copyUntil(nextBuffer, processor); |
| 924 | 0 | if (top.getBuffer().isAtEnd()) |
| 925 | 0 | top.fill(); |
| 926 | |
|
| 927 | 0 | if (!top.getBuffer().isAtEnd()) |
| 928 | 0 | queue.add(top); |
| 929 | 0 | } |
| 930 | |
|
| 931 | 0 | if (closeOnExit) |
| 932 | 0 | processor.close(); |
| 933 | 0 | } |
| 934 | |
|
| 935 | |
public NumberedDocumentData read() throws IOException { |
| 936 | 0 | if (uninitialized) |
| 937 | 0 | initialize(); |
| 938 | |
|
| 939 | 0 | NumberedDocumentData result = null; |
| 940 | |
|
| 941 | 0 | while (queue.size() > 0) { |
| 942 | 0 | ShreddedReader top = queue.poll(); |
| 943 | 0 | result = top.read(); |
| 944 | |
|
| 945 | 0 | if (result != null) { |
| 946 | 0 | if (top.getBuffer().isAtEnd()) |
| 947 | 0 | top.fill(); |
| 948 | |
|
| 949 | 0 | queue.offer(top); |
| 950 | 0 | break; |
| 951 | |
} |
| 952 | 0 | } |
| 953 | |
|
| 954 | 0 | return result; |
| 955 | |
} |
| 956 | |
} |
| 957 | 0 | public static class ShreddedReader implements Step, Comparable<ShreddedReader>, TypeReader<NumberedDocumentData>, ShreddedSource { |
| 958 | |
public ShreddedProcessor processor; |
| 959 | |
ShreddedBuffer buffer; |
| 960 | 0 | NumberedDocumentData last = new NumberedDocumentData(); |
| 961 | 0 | long updateNumberCount = -1; |
| 962 | 0 | long tupleCount = 0; |
| 963 | 0 | long bufferStartCount = 0; |
| 964 | |
ArrayInput input; |
| 965 | |
|
| 966 | 0 | public ShreddedReader(ArrayInput input) { |
| 967 | 0 | this.input = input; |
| 968 | 0 | this.buffer = new ShreddedBuffer(); |
| 969 | 0 | } |
| 970 | |
|
| 971 | 0 | public ShreddedReader(ArrayInput input, int bufferSize) { |
| 972 | 0 | this.input = input; |
| 973 | 0 | this.buffer = new ShreddedBuffer(bufferSize); |
| 974 | 0 | } |
| 975 | |
|
| 976 | |
public final int compareTo(ShreddedReader other) { |
| 977 | 0 | ShreddedBuffer otherBuffer = other.getBuffer(); |
| 978 | |
|
| 979 | 0 | if (buffer.isAtEnd() && otherBuffer.isAtEnd()) { |
| 980 | 0 | return 0; |
| 981 | 0 | } else if (buffer.isAtEnd()) { |
| 982 | 0 | return -1; |
| 983 | 0 | } else if (otherBuffer.isAtEnd()) { |
| 984 | 0 | return 1; |
| 985 | |
} |
| 986 | |
|
| 987 | 0 | int result = 0; |
| 988 | |
do { |
| 989 | 0 | result = + Utility.compare(buffer.getNumber(), otherBuffer.getNumber()); |
| 990 | 0 | if(result != 0) break; |
| 991 | |
} while (false); |
| 992 | |
|
| 993 | 0 | return result; |
| 994 | |
} |
| 995 | |
|
| 996 | |
public final ShreddedBuffer getBuffer() { |
| 997 | 0 | return buffer; |
| 998 | |
} |
| 999 | |
|
| 1000 | |
public final NumberedDocumentData read() throws IOException { |
| 1001 | 0 | if (buffer.isAtEnd()) { |
| 1002 | 0 | fill(); |
| 1003 | |
|
| 1004 | 0 | if (buffer.isAtEnd()) { |
| 1005 | 0 | return null; |
| 1006 | |
} |
| 1007 | |
} |
| 1008 | |
|
| 1009 | 0 | assert !buffer.isAtEnd(); |
| 1010 | 0 | NumberedDocumentData result = new NumberedDocumentData(); |
| 1011 | |
|
| 1012 | 0 | result.number = buffer.getNumber(); |
| 1013 | 0 | result.identifier = buffer.getIdentifier(); |
| 1014 | 0 | result.url = buffer.getUrl(); |
| 1015 | 0 | result.textLength = buffer.getTextLength(); |
| 1016 | |
|
| 1017 | 0 | buffer.incrementTuple(); |
| 1018 | 0 | buffer.autoIncrementNumber(); |
| 1019 | |
|
| 1020 | 0 | return result; |
| 1021 | |
} |
| 1022 | |
|
| 1023 | |
public final void fill() throws IOException { |
| 1024 | |
try { |
| 1025 | 0 | buffer.reset(); |
| 1026 | |
|
| 1027 | 0 | if (tupleCount != 0) { |
| 1028 | |
|
| 1029 | 0 | if(updateNumberCount - tupleCount > 0) { |
| 1030 | 0 | buffer.numbers.add(last.number); |
| 1031 | 0 | buffer.numberTupleIdx.add((int) (updateNumberCount - tupleCount)); |
| 1032 | |
} |
| 1033 | 0 | bufferStartCount = tupleCount; |
| 1034 | |
} |
| 1035 | |
|
| 1036 | 0 | while (!buffer.isFull()) { |
| 1037 | 0 | updateNumber(); |
| 1038 | 0 | buffer.processTuple(input.readString(), input.readString(), input.readInt()); |
| 1039 | 0 | tupleCount++; |
| 1040 | |
} |
| 1041 | 0 | } catch(EOFException e) {} |
| 1042 | 0 | } |
| 1043 | |
|
| 1044 | |
public final void updateNumber() throws IOException { |
| 1045 | 0 | if (updateNumberCount > tupleCount) |
| 1046 | 0 | return; |
| 1047 | |
|
| 1048 | 0 | last.number = input.readInt(); |
| 1049 | 0 | updateNumberCount = tupleCount + input.readInt(); |
| 1050 | |
|
| 1051 | 0 | buffer.processNumber(last.number); |
| 1052 | 0 | } |
| 1053 | |
|
| 1054 | |
public void run() throws IOException { |
| 1055 | |
while (true) { |
| 1056 | 0 | fill(); |
| 1057 | |
|
| 1058 | 0 | if (buffer.isAtEnd()) |
| 1059 | 0 | break; |
| 1060 | |
|
| 1061 | 0 | buffer.copyUntil(null, processor); |
| 1062 | |
} |
| 1063 | 0 | processor.close(); |
| 1064 | 0 | } |
| 1065 | |
|
| 1066 | |
public void setProcessor(Step processor) throws IncompatibleProcessorException { |
| 1067 | 0 | if (processor instanceof ShreddedProcessor) { |
| 1068 | 0 | this.processor = new DuplicateEliminator((ShreddedProcessor) processor); |
| 1069 | 0 | } else if (processor instanceof NumberedDocumentData.Processor) { |
| 1070 | 0 | this.processor = new DuplicateEliminator(new TupleUnshredder((NumberedDocumentData.Processor) processor)); |
| 1071 | 0 | } else if (processor instanceof org.galagosearch.tupleflow.Processor) { |
| 1072 | 0 | this.processor = new DuplicateEliminator(new TupleUnshredder((org.galagosearch.tupleflow.Processor<NumberedDocumentData>) processor)); |
| 1073 | |
} else { |
| 1074 | 0 | throw new IncompatibleProcessorException(processor.getClass().getName() + " is not supported by " + this.getClass().getName()); |
| 1075 | |
} |
| 1076 | 0 | } |
| 1077 | |
|
| 1078 | |
public Class<NumberedDocumentData> getOutputClass() { |
| 1079 | 0 | return NumberedDocumentData.class; |
| 1080 | |
} |
| 1081 | |
} |
| 1082 | |
|
| 1083 | |
public static class DuplicateEliminator implements ShreddedProcessor { |
| 1084 | |
public ShreddedProcessor processor; |
| 1085 | 0 | NumberedDocumentData last = new NumberedDocumentData(); |
| 1086 | 0 | boolean numberProcess = true; |
| 1087 | |
|
| 1088 | 0 | public DuplicateEliminator() {} |
| 1089 | 0 | public DuplicateEliminator(ShreddedProcessor processor) { |
| 1090 | 0 | this.processor = processor; |
| 1091 | 0 | } |
| 1092 | |
|
| 1093 | |
public void setShreddedProcessor(ShreddedProcessor processor) { |
| 1094 | 0 | this.processor = processor; |
| 1095 | 0 | } |
| 1096 | |
|
| 1097 | |
public void processNumber(int number) throws IOException { |
| 1098 | 0 | if (numberProcess || Utility.compare(number, last.number) != 0) { |
| 1099 | 0 | last.number = number; |
| 1100 | 0 | processor.processNumber(number); |
| 1101 | 0 | numberProcess = false; |
| 1102 | |
} |
| 1103 | 0 | } |
| 1104 | |
|
| 1105 | |
public void resetNumber() { |
| 1106 | 0 | numberProcess = true; |
| 1107 | 0 | } |
| 1108 | |
|
| 1109 | |
public void processTuple(String identifier, String url, int textLength) throws IOException { |
| 1110 | 0 | processor.processTuple(identifier, url, textLength); |
| 1111 | 0 | } |
| 1112 | |
|
| 1113 | |
public void close() throws IOException { |
| 1114 | 0 | processor.close(); |
| 1115 | 0 | } |
| 1116 | |
} |
| 1117 | |
public static class TupleUnshredder implements ShreddedProcessor { |
| 1118 | 0 | NumberedDocumentData last = new NumberedDocumentData(); |
| 1119 | |
public org.galagosearch.tupleflow.Processor<NumberedDocumentData> processor; |
| 1120 | |
|
| 1121 | 0 | public TupleUnshredder(NumberedDocumentData.Processor processor) { |
| 1122 | 0 | this.processor = processor; |
| 1123 | 0 | } |
| 1124 | |
|
| 1125 | 0 | public TupleUnshredder(org.galagosearch.tupleflow.Processor<NumberedDocumentData> processor) { |
| 1126 | 0 | this.processor = processor; |
| 1127 | 0 | } |
| 1128 | |
|
| 1129 | |
public NumberedDocumentData clone(NumberedDocumentData object) { |
| 1130 | 0 | NumberedDocumentData result = new NumberedDocumentData(); |
| 1131 | 0 | if (object == null) return result; |
| 1132 | 0 | result.identifier = object.identifier; |
| 1133 | 0 | result.url = object.url; |
| 1134 | 0 | result.number = object.number; |
| 1135 | 0 | result.textLength = object.textLength; |
| 1136 | 0 | return result; |
| 1137 | |
} |
| 1138 | |
|
| 1139 | |
public void processNumber(int number) throws IOException { |
| 1140 | 0 | last.number = number; |
| 1141 | 0 | } |
| 1142 | |
|
| 1143 | |
|
| 1144 | |
public void processTuple(String identifier, String url, int textLength) throws IOException { |
| 1145 | 0 | last.identifier = identifier; |
| 1146 | 0 | last.url = url; |
| 1147 | 0 | last.textLength = textLength; |
| 1148 | 0 | processor.process(clone(last)); |
| 1149 | 0 | } |
| 1150 | |
|
| 1151 | |
public void close() throws IOException { |
| 1152 | 0 | processor.close(); |
| 1153 | 0 | } |
| 1154 | |
} |
| 1155 | 124 | public static class TupleShredder implements Processor { |
| 1156 | 0 | NumberedDocumentData last = new NumberedDocumentData(); |
| 1157 | |
public ShreddedProcessor processor; |
| 1158 | |
|
| 1159 | 0 | public TupleShredder(ShreddedProcessor processor) { |
| 1160 | 0 | this.processor = processor; |
| 1161 | 0 | } |
| 1162 | |
|
| 1163 | |
public NumberedDocumentData clone(NumberedDocumentData object) { |
| 1164 | 0 | NumberedDocumentData result = new NumberedDocumentData(); |
| 1165 | 0 | if (object == null) return result; |
| 1166 | 0 | result.identifier = object.identifier; |
| 1167 | 0 | result.url = object.url; |
| 1168 | 0 | result.number = object.number; |
| 1169 | 0 | result.textLength = object.textLength; |
| 1170 | 0 | return result; |
| 1171 | |
} |
| 1172 | |
|
| 1173 | |
public void process(NumberedDocumentData object) throws IOException { |
| 1174 | 0 | boolean processAll = false; |
| 1175 | 0 | if(last == null || Utility.compare(last.number, object.number) != 0 || processAll) { processor.processNumber(object.number); processAll = true; } |
| 1176 | 0 | processor.processTuple(object.identifier, object.url, object.textLength); |
| 1177 | 0 | } |
| 1178 | |
|
| 1179 | |
public Class<NumberedDocumentData> getInputClass() { |
| 1180 | 0 | return NumberedDocumentData.class; |
| 1181 | |
} |
| 1182 | |
|
| 1183 | |
public void close() throws IOException { |
| 1184 | 0 | processor.close(); |
| 1185 | 0 | } |
| 1186 | |
} |
| 1187 | |
} |
| 1188 | |
} |