List of usage examples for org.apache.lucene.store OutputStreamDataOutput OutputStreamDataOutput
public OutputStreamDataOutput(OutputStream os)
From source file:org.elasticsearch.index.translog.ChecksummedTranslogStream.java
License:Apache License
@Override public int writeHeader(FileChannel channel) throws IOException { // This OutputStreamDataOutput is intentionally not closed because // closing it will close the FileChannel OutputStreamDataOutput out = new OutputStreamDataOutput(Channels.newOutputStream(channel)); CodecUtil.writeHeader(out, TranslogStreams.TRANSLOG_CODEC, VERSION); return CodecUtil.headerLength(TranslogStreams.TRANSLOG_CODEC); }
From source file:org.elasticsearch.index.translog.TranslogHeader.java
License:Apache License
/** * Writes this header with the latest format into the file channel *//*from w w w . j av a 2s. c om*/ void write(final FileChannel channel) throws IOException { // This output is intentionally not closed because closing it will close the FileChannel. @SuppressWarnings({ "IOResourceOpenedButNotSafelyClosed", "resource" }) final BufferedChecksumStreamOutput out = new BufferedChecksumStreamOutput( new OutputStreamStreamOutput(java.nio.channels.Channels.newOutputStream(channel))); CodecUtil.writeHeader(new OutputStreamDataOutput(out), TRANSLOG_CODEC, CURRENT_VERSION); // Write uuid final BytesRef uuid = new BytesRef(translogUUID); out.writeInt(uuid.length); out.writeBytes(uuid.bytes, uuid.offset, uuid.length); // Write primary term out.writeLong(primaryTerm); // Checksum header out.writeInt((int) out.getChecksum()); out.flush(); channel.force(true); assert channel.position() == headerSizeInBytes : "Header is not fully written; header size [" + headerSizeInBytes + "], channel position [" + channel.position() + "]"; }
From source file:org.elasticsearch.index.translog.TranslogHeaderTests.java
License:Apache License
static void writeHeaderWithoutTerm(FileChannel channel, String translogUUID) throws IOException { final OutputStreamStreamOutput out = new OutputStreamStreamOutput(Channels.newOutputStream(channel)); CodecUtil.writeHeader(new OutputStreamDataOutput(out), TranslogHeader.TRANSLOG_CODEC, TranslogHeader.VERSION_CHECKPOINTS); final BytesRef uuid = new BytesRef(translogUUID); out.writeInt(uuid.length);//from w w w. ja v a2s . c om out.writeBytes(uuid.bytes, uuid.offset, uuid.length); channel.force(true); assertThat(channel.position(), equalTo(43L)); }
From source file:org.elasticsearch.index.translog.TranslogWriter.java
License:Apache License
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 = CodecUtil.headerLength(TRANSLOG_CODEC) + ref.length + RamUsageEstimator.NUM_BYTES_INT; final FileChannel channel = channelFactory.open(file); try {//from w ww. j av a2s . c o m // 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(false); 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) { IOUtils.closeWhileHandlingException(channel); try { Files.delete(file); // remove the file as well } catch (IOException ex) { throwable.addSuppressed(ex); } throw throwable; } }
From source file:org.elasticsearch.index.translog.TruncateTranslogCommand.java
License:Apache License
/** * Write a translog containing the given translog UUID to the given location. Returns the number of bytes written. *///ww w .j a va 2s .c om public static int writeEmptyTranslog(Path filename, String translogUUID) throws IOException { final BytesRef translogRef = new BytesRef(translogUUID); try (FileChannel fc = FileChannel.open(filename, StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE_NEW); OutputStreamDataOutput out = new OutputStreamDataOutput(Channels.newOutputStream(fc))) { TranslogWriter.writeHeader(out, translogRef); fc.force(true); } return TranslogWriter.getHeaderLength(translogRef.length); }
From source file:uk.co.flax.luwak.MonitorQuery.java
License:Apache License
/** * Serialize a MonitorQuery into a BytesRef * @param mq the MonitorQuery/*from ww w . j a va 2 s.c o m*/ * @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! } }