Class SegmentBufferWriter


  • public class SegmentBufferWriter
    extends java.lang.Object
    This class encapsulates the state of a segment being written. It provides methods for writing primitive data types and for pre-allocating buffer space in the current segment. Should the current segment not have enough space left the current segment is flushed and a fresh one is allocated.

    The common usage pattern is:

        SegmentBufferWriter writer = ...
        writer.prepare(...)  // allocate buffer
        writer.writeXYZ(...)
     
    The behaviour of this class is undefined should the pre-allocated buffer be overrun be calling any of the write methods.

    Instances of this class are not thread safe

    • Constructor Detail

      • SegmentBufferWriter

        public SegmentBufferWriter​(@NotNull
                                   @NotNull SegmentIdProvider idProvider,
                                   @NotNull
                                   @NotNull SegmentReader reader,
                                   @Nullable
                                   @Nullable java.lang.String wid,
                                   @NotNull
                                   @NotNull GCGeneration gcGeneration)
    • Method Detail

      • execute

        @NotNull
        public @NotNull RecordId execute​(@NotNull
                                         @NotNull GCGeneration gcGeneration,
                                         @NotNull
                                         @NotNull org.apache.jackrabbit.oak.segment.WriteOperationHandler.WriteOperation writeOperation)
                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • getGCGeneration

        @NotNull
        public @NotNull GCGeneration getGCGeneration()
      • writeByte

        public void writeByte​(byte value)
      • writeShort

        public void writeShort​(short value)
      • writeInt

        public void writeInt​(int value)
      • writeLong

        public void writeLong​(long value)
      • writeRecordId

        public void writeRecordId​(RecordId recordId)
        Write a record ID.
        Parameters:
        recordId - the record ID.
      • writeBytes

        public void writeBytes​(byte[] data,
                               int offset,
                               int length)
      • flush

        public void flush​(@NotNull
                          @NotNull SegmentStore store)
                   throws java.io.IOException
        Adds a segment header to the buffer and writes a segment to the segment store. This is done automatically (called from prepare) when there is not enough space for a record. It can also be called explicitly.
        Throws:
        java.io.IOException
      • prepare

        public RecordId prepare​(RecordType type,
                                int size,
                                java.util.Collection<RecordId> ids,
                                SegmentStore store)
                         throws java.io.IOException
        Before writing a record (which are written backwards, from the end of the file to the beginning), this method is called, to ensure there is enough space. A new segment is also created if there is not enough space in the segment lookup table or elsewhere.

        This method does not actually write into the segment, just allocates the space (flushing the segment if needed and starting a new one), and sets the write position (records are written from the end to the beginning, but within a record from left to right).

        Parameters:
        type - the record type (only used for root records)
        size - the size of the record, excluding the size used for the record ids
        ids - the record ids
        store - the SegmentStore instance to write full segments to
        Returns:
        a new record id
        Throws:
        java.io.IOException