private synchronized void rearrangeSegments(short idx) {
    // all segment instance shall be abolished
    syncPageBuffer();
    segCacheMap.clear();

    ByteBuffer mirrorPage = ByteBuffer.allocate(this.pageBuffer.capacity());
    this.pageBuffer.clear();
    mirrorPage.put(this.pageBuffer);
    this.pageBuffer.clear();

    spareOffset = SchemaFileConfig.PAGE_HEADER_SIZE;

    short offset, len;
    for (short i = 0; i < segOffsetLst.size(); i++) {
      if (segOffsetLst.get(i) >= 0 && i != idx) {
        // except for target segment, compact other valid segment
        offset = segOffsetLst.get(i);

        mirrorPage.clear();
        this.pageBuffer.clear();

        mirrorPage.position(offset);
        len = WrappedSegment.getSegBufLen(mirrorPage);
        mirrorPage.limit(offset + len);

        this.segOffsetLst.set(i, spareOffset);
        this.pageBuffer.position(spareOffset);
        this.pageBuffer.put(mirrorPage);
        spareOffset += len;
      }
    }

    // a negative idx meant for only compaction
    if (idx >= 0) {
      this.pageBuffer.clear();
      this.pageBuffer.position(spareOffset);

      mirrorPage.clear();
      mirrorPage.position(segOffsetLst.get(idx));
      len = WrappedSegment.getSegBufLen(mirrorPage);
      mirrorPage.limit(mirrorPage.position() + len);

      this.pageBuffer.put(mirrorPage);
      segOffsetLst.set(idx, spareOffset);
      spareOffset += len;
    }

    spareSize =
        (short)
            (this.pageBuffer.capacity() - spareOffset - memberNum * SchemaFileConfig.SEG_OFF_DIG);
  }
Compact segments and move target segment (id at idx) to the tail of segments.<br>
Since this method may overwrite segment buffer, all existed buffer instance shall be abolished.
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/schemafile/SegmentedPage.java