protected void compactRecords() {
    // compact by existed item on keyAddressList
    ByteBuffer tempBuf = ByteBuffer.allocate(this.buffer.capacity() - this.freeAddr);
    int accSiz = 0;
    short[] newOffsets = new short[recordNum];

    String migKey;
    short migOffset;
    int recLen;
    for (int i = 0; i < recordNum; i++) {
      migOffset = getOffsetByIndex(i);
      migKey = getKeyByOffset(migOffset);
      this.buffer.position(migOffset + 4 + migKey.getBytes().length);
      recLen = getRecordLength();

      this.buffer.clear();
      this.buffer.position(migOffset);
      this.buffer.limit(migOffset + 4 + migKey.getBytes().length + recLen);

      accSiz += this.buffer.remaining();

      newOffsets[i] = (short) (this.buffer.capacity() - accSiz);
      tempBuf.position(tempBuf.capacity() - accSiz);
      tempBuf.put(this.buffer);
    }

    tempBuf.clear();
    tempBuf.position(tempBuf.capacity() - accSiz);
    this.freeAddr = (short) (this.buffer.capacity() - accSiz);

    this.buffer.clear();
    this.buffer.position(SEG_HEADER_SIZE);
    this.buffer.asShortBuffer().put(newOffsets);

    this.buffer.position(this.freeAddr);
    this.buffer.put(tempBuf);

    this.syncBuffer();
  }
<b>Segment Structure:</b>
<li>(25 byte: header)
<li>1 short: length, segment length
<li>1 short: freeAddr, start offset of records
<li>1 short: recordNum, amount of records in this segment
<li>1 short: pairLength, length of key-address in bytes
<li>1 long (8 bytes): prevSegIndex, previous segment index
<li>1 long (8 bytes): nextSegIndex, next segment index
<li>1 bit: delFlag, delete flag
<li>1 bit: aliasFlag, whether alias existed<br>
    (--- checksum, parent record address, max/min record key may be contained further ---)
<li><s>var length: key-address pairs, begin at 25 bytes offset, length of pairLength </s><br>
<li>var length: ORDERED record offset<br>
    ... empty space ...
<li>var length: (record key, record body) * recordNum
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/schemafile/WrappedSegment.java