@Override protected void encode(ByteSequencesWriter writer, ByteArrayDataOutput output, byte[] buffer, BytesRef spare, long weight) throws IOException { if (spare.length + 4 >= buffer.length) { buffer = ArrayUtil.grow(buffer, spare.length + 4); } output.reset(buffer); output.writeBytes(spare.bytes, spare.offset, spare.length); output.writeInt(encodeWeight(weight)); writer.write(buffer, 0, output.getPosition()); }
private Sort.ByteSequencesReader sort() throws IOException { String prefix = getClass().getSimpleName(); File directory = Sort.defaultTempDir(); tempInput = File.createTempFile(prefix, ".input", directory); tempSorted = File.createTempFile(prefix, ".sorted", directory); final Sort.ByteSequencesWriter writer = new Sort.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.weight()); } writer.close(); new Sort(tieBreakByCostComparator).sort(tempInput, tempSorted); ByteSequencesReader reader = new Sort.ByteSequencesReader(tempSorted); success = true; return reader; } finally { if (success) { IOUtils.close(writer); } else { try { IOUtils.closeWhileHandlingException(writer); } finally { close(); } } } }
/** encodes an entry (bytes+weight) to the provided writer */ protected void encode(ByteSequencesWriter writer, ByteArrayDataOutput output, byte[] buffer, BytesRef spare, long weight) throws IOException { if (spare.length + 8 >= buffer.length) { buffer = ArrayUtil.grow(buffer, spare.length + 8); } output.reset(buffer); output.writeBytes(spare.bytes, spare.offset, spare.length); output.writeLong(weight); writer.write(buffer, 0, output.getPosition()); }
private File writeAll(String name, byte[][] data) throws IOException { File file = new File(tempDir, name); ByteSequencesWriter w = new Sort.ByteSequencesWriter(file); for (byte [] datum : data) { w.write(datum); } w.close(); return file; }
@Override protected void encode(ByteSequencesWriter writer, ByteArrayDataOutput output, byte[] buffer, BytesRef spare, BytesRef payload, long weight) throws IOException { if (spare.length + 4 >= buffer.length) { buffer = ArrayUtil.grow(buffer, spare.length + 4); } output.reset(buffer); output.writeBytes(spare.bytes, spare.offset, spare.length); output.writeInt(encodeWeight(weight)); writer.write(buffer, 0, output.getPosition()); }
private Sort.ByteSequencesReader sort() throws IOException { String prefix = getClass().getSimpleName(); File directory = Sort.defaultTempDir(); tempInput = File.createTempFile(prefix, ".input", directory); tempSorted = File.createTempFile(prefix, ".sorted", directory); final Sort.ByteSequencesWriter writer = new Sort.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.weight()); } writer.close(); new Sort(tieBreakByCostComparator).sort(tempInput, tempSorted); ByteSequencesReader reader = new Sort.ByteSequencesReader(tempSorted); success = true; return reader; } finally { if (success) { IOUtils.close(writer); } else { try { IOUtils.closeWhileHandlingException(writer); } finally { close(); } } } }
/** encodes an entry (bytes+(payload)+weight) to the provided writer */ protected void encode(ByteSequencesWriter writer, ByteArrayDataOutput output, byte[] buffer, BytesRef spare, BytesRef payload, long weight) throws IOException { int requiredLength = spare.length + 8 + ((hasPayloads) ? 2 + payload.length : 0); if (requiredLength >= buffer.length) { buffer = ArrayUtil.grow(buffer, requiredLength); } output.reset(buffer); output.writeBytes(spare.bytes, spare.offset, spare.length); if (hasPayloads) { output.writeBytes(payload.bytes, payload.offset, payload.length); output.writeShort((short) payload.length); } output.writeLong(weight); writer.write(buffer, 0, output.getPosition()); }