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