static void writeHeader(OutputStreamDataOutput out, BytesRef ref) throws IOException { CodecUtil.writeHeader(out, TRANSLOG_CODEC, VERSION); out.writeInt(ref.length); out.writeBytes(ref.bytes, ref.offset, ref.length); }
@Override public void append(long value, long ord, int docID) throws IOException { out.writeLong(value); out.writeLong(ord); out.writeInt(docID); countWritten++; }
/** * Serialize a MonitorQuery into a BytesRef * @param mq the MonitorQuery * @return the serialized bytes */ public static BytesRef serialize(MonitorQuery mq) { ByteArrayOutputStream os = new ByteArrayOutputStream(); try (OutputStreamDataOutput data = new OutputStreamDataOutput(os)) { data.writeString(mq.getId()); data.writeString(mq.getQuery()); data.writeInt(mq.getMetadata().size()); for (Map.Entry<String, String> entry : mq.getMetadata().entrySet()) { data.writeString(entry.getKey()); data.writeString(entry.getValue()); } return new BytesRef(os.toByteArray()); } catch (IOException e) { throw new RuntimeException(e); // shouldn't happen, we're writing to a bytearray! } }
public static TranslogWriter create(Type type, ShardId shardId, String translogUUID, long fileGeneration, Path file, Callback<ChannelReference> onClose, int bufferSize, ChannelFactory channelFactory) throws IOException { final BytesRef ref = new BytesRef(translogUUID); final int headerLength = getHeaderLength(ref.length); final FileChannel channel = channelFactory.open(file); try { // This OutputStreamDataOutput is intentionally not closed because // closing it will close the FileChannel final OutputStreamDataOutput out = new OutputStreamDataOutput(java.nio.channels.Channels.newOutputStream(channel)); CodecUtil.writeHeader(out, TRANSLOG_CODEC, VERSION); out.writeInt(ref.length); out.writeBytes(ref.bytes, ref.offset, ref.length); channel.force(true); writeCheckpoint(headerLength, 0, file.getParent(), fileGeneration, StandardOpenOption.WRITE); final TranslogWriter writer = type.create(shardId, fileGeneration, new ChannelReference(file, fileGeneration, channel, onClose), bufferSize); return writer; } catch (Throwable throwable){ // if we fail to bake the file-generation into the checkpoint we stick with the file and once we recover and that // file exists we remove it. We only apply this logic to the checkpoint.generation+1 any other file with a higher generation is an error condition IOUtils.closeWhileHandlingException(channel); throw throwable; } } /** If this {@code TranslogWriter} was closed as a side-effect of a tragic exception,