@Override
  public int updateRecord(String key, ByteBuffer uBuffer) throws MetadataException {

    int idx = binarySearchOnKeys(key);
    if (idx < 0) {
      throw new MetadataException(String.format("Record[key:%s] Not Existed.", key));
    }

    this.buffer.clear();
    uBuffer.clear();
    this.buffer.position(getOffsetByIndex(idx) + 4 + key.getBytes().length);

    short oriLen = RecordUtils.getRecordLength(this.buffer);
    short newLen = (short) uBuffer.capacity();
    if (oriLen >= newLen) {
      // update in place
      this.buffer.limit(this.buffer.position() + oriLen);
      this.buffer.put(uBuffer);
    } else {
      // allocate new space for record, update offset array, freeAddr
      if (SchemaFileConfig.SEG_HEADER_SIZE + pairLength + newLen + 4 + key.getBytes().length
          > freeAddr) {
        // no enough consecutive spare space
        return -1;
      }

      freeAddr = (short) (freeAddr - newLen - 4 - key.getBytes().length);
      // it will not mark old record as expired
      this.buffer.position(freeAddr);
      ReadWriteIOUtils.write(key, this.buffer);
      this.buffer.put(uBuffer);

      this.buffer.position(SchemaFileConfig.SEG_HEADER_SIZE + 2 * idx);
      this.buffer.putShort(freeAddr);
    }

    return idx;
  }
<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