Class Segment

  • All Implemented Interfaces:
    ISegment

    public class Segment
    extends java.lang.Object
    implements ISegment
    This class initiate a segment object with corresponding bytes. Implements add, get, remove records methods.
    Act like a wrapper of a bytebuffer which reflects a segment.
    • Field Summary

    • Constructor Summary

      Constructors 
      Constructor Description
      Segment​(int size)  
      Segment​(java.nio.ByteBuffer buffer)  
      Segment​(java.nio.ByteBuffer buffer, boolean override)
      Init Segment with a buffer, which contains all information about this segment
    • Constructor Detail

      • Segment

        public Segment​(java.nio.ByteBuffer buffer,
                       boolean override)
        Init Segment with a buffer, which contains all information about this segment

        For a page no more than 16 kib, a signed short is enough to index all bytes inside a segment.

        Segment Structure:

      • 25 byte: header
      • 1 short: length, segment length
      • 1 short: freeAddr, start offset of records
      • 1 short: recordNum, amount of records in this segment
      • 1 short: pairLength, length of key-address in bytes
      • 1 long (8 bytes): prevSegIndex, previous segment index
      • 1 long (8 bytes): nextSegIndex, next segment index
      • 1 bit: delFlag, delete flag
        (--- checksum, parent record address, max/min record key may be contained further ---)
      • var length: key-address pairs, begin at 25 bytes offset, length of pairLength
        ... empty space ...
      • var length: records
  • Method Detail

    • initAsSegment

      public static ISegment initAsSegment​(java.nio.ByteBuffer buffer)
    • loadAsSegment

      public static ISegment loadAsSegment​(java.nio.ByteBuffer buffer)
    • insertRecord

      public int insertRecord​(java.lang.String key,
                              java.nio.ByteBuffer buf)
                       throws RecordDuplicatedException
      Description copied from interface: ISegment
      check whether enough space, notice that pairLength including 3 parts: [var length] key string itself, [int, 4 bytes] length of key string, [short, 2 bytes] key address
      Specified by:
      insertRecord in interface ISegment
      Returns:
      -1 for segment overflow, otherwise for spare space
      Throws:
      RecordDuplicatedException
    • getRecordAsIMNode

      public IMNode getRecordAsIMNode​(java.lang.String key)
                               throws org.apache.iotdb.commons.exception.MetadataException
      Description copied from interface: ISegment
      get a MNode by its name or alias
      Specified by:
      getRecordAsIMNode in interface ISegment
      Parameters:
      key - name or alias of the target node
      Returns:
      node instance
      Throws:
      org.apache.iotdb.commons.exception.MetadataException
    • hasRecordKey

      public boolean hasRecordKey​(java.lang.String key)
      Specified by:
      hasRecordKey in interface ISegment
    • hasRecordAlias

      public boolean hasRecordAlias​(java.lang.String alias)
      Specified by:
      hasRecordAlias in interface ISegment
    • getAllRecords

      public java.util.Queue<IMNode> getAllRecords()
                                            throws org.apache.iotdb.commons.exception.MetadataException
      Specified by:
      getAllRecords in interface ISegment
      Throws:
      org.apache.iotdb.commons.exception.MetadataException
    • removeRecord

      public int removeRecord​(java.lang.String key)
      Specified by:
      removeRecord in interface ISegment
    • syncBuffer

      public void syncBuffer()
      Description copied from interface: ISegment
      Records are always sync with buffer, but header and key-address list are not. This method sync these values to the buffer.
      Specified by:
      syncBuffer in interface ISegment
    • delete

      public void delete()
      Specified by:
      delete in interface ISegment
    • size

      public short size()
      Specified by:
      size in interface ISegment
    • extendsTo

      public void extendsTo​(java.nio.ByteBuffer newBuffer)
      Description copied from interface: ISegment
      This method will write info into a buffer equal or larger to existed one. There is no need to call sync before this method, since it will flush header and key-offset list directly.
      Specified by:
      extendsTo in interface ISegment
      Parameters:
      newBuffer - target buffer
    • setPrevSegAddress

      public void setPrevSegAddress​(long prevSegAddress)
      Specified by:
      setPrevSegAddress in interface ISegment
    • setNextSegAddress

      public void setNextSegAddress​(long nextSegAddress)
      Specified by:
      setNextSegAddress in interface ISegment
    • getBufferCopy

      public java.nio.ByteBuffer getBufferCopy()
    • updateRecordSegAddr

      protected void updateRecordSegAddr​(java.lang.String key,
                                         long newSegAddr)
    • toString

      public java.lang.String toString()
      Specified by:
      toString in interface ISegment
      Overrides:
      toString in class java.lang.Object
    • inspect

      public java.lang.String inspect()
      Specified by:
      inspect in interface ISegment
    • getRecord

      public java.nio.ByteBuffer getRecord​(java.lang.String key)
    • getInnerBuffer

      public java.nio.ByteBuffer getInnerBuffer()
    • getKeyOffsetList

      public java.util.List<org.apache.iotdb.tsfile.utils.Pair<java.lang.String,​java.lang.Short>> getKeyOffsetList()