private ISegment<ByteBuffer, ICachedMNode> relocateSegment(
      ISegment<?, ?> seg, short segIdx, short newSize) throws MetadataException {
    if (seg.size() == SchemaFileConfig.SEG_MAX_SIZ || getSpareSize() + seg.size() < newSize) {
      return null;
    }

    // try to allocate space directly from spareOffset or rearrange and extend in place
    ByteBuffer newBuffer = allocSpareBufferSlice(newSize);
    if (newBuffer == null) {
      rearrangeSegments(segIdx);
      return extendSegmentInPlace(segIdx, seg.size(), newSize);
    }

    // allocate buffer slice successfully
    seg.extendsTo(newBuffer);
    ISegment<ByteBuffer, ICachedMNode> newSeg = WrappedSegment.loadAsSegment(newBuffer);

    // since this buffer is allocated from pageSpareOffset, new spare offset can simply add size up
    segOffsetLst.set(segIdx, spareOffset);
    segCacheMap.put(segIdx, newSeg);
    spareOffset += newSeg.size();
    spareSize -= (short) (newSize - seg.size());

    return newSeg;
  }
Allocate a new segment to extend specified segment, modify cache map and list.

<p><b> The new segment could be allocated from spare space or rearranged space.</b>

@param seg original segment instance
@param segIdx original segment index
@param newSize target segment size
@return reallocated segment instance, null if no enough spare space
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/schemafile/SegmentedPage.java