private ByteSequencesReader sort() throws IOException { String prefix = getClass().getSimpleName(); Path directory = OfflineSorter.getDefaultTempDir(); tempInput = Files.createTempFile(directory, prefix, ".input"); tempSorted = Files.createTempFile(directory, prefix, ".sorted"); final OfflineSorter.ByteSequencesWriter writer = new OfflineSorter.ByteSequencesWriter(tempInput); boolean success = false; try { BytesRef spare; byte[] buffer = new byte[0]; ByteArrayDataOutput output = new ByteArrayDataOutput(buffer); while ((spare = source.next()) != null) { encode(writer, output, buffer, spare, source.payload(), source.contexts(), source.weight()); } writer.close(); new OfflineSorter(tieBreakByCostComparator).sort(tempInput, tempSorted); ByteSequencesReader reader = new OfflineSorter.ByteSequencesReader(tempSorted); success = true; return reader; } finally { if (success) { IOUtils.close(writer); } else { try { IOUtils.closeWhileHandlingException(writer); } finally { close(); } } } }
@Override public BytesRefIterator iterator() throws IOException { if (sorted == null) { closeWriter(); sorted = Files.createTempFile(OfflineSorter.getDefaultTempDir(), "RefSorter-", ".sorted"); boolean success = false; try { sort.sort(input, sorted); success = true; } finally { if (success) { Files.delete(input); } else { IOUtils.deleteFilesIgnoringExceptions(input); } } input = null; } return new ByteSequenceIterator(new OfflineSorter.ByteSequencesReader(sorted)); }
/** Will buffer all sequences to a temporary file and then sort (all on-disk). */ public ExternalRefSorter(OfflineSorter sort) throws IOException { this.sort = sort; this.input = Files.createTempFile(OfflineSorter.getDefaultTempDir(), "RefSorter-", ".raw"); this.writer = new OfflineSorter.ByteSequencesWriter(input); }
@Override public Comparator<BytesRef> getComparator() { return sort.getComparator(); }