@Override
  public synchronized int insertRecord(String key, ByteBuffer buf)
      throws RecordDuplicatedException {
    buf.clear();

    // key and body store adjacently
    byte[] ikBytes = key.getBytes();
    int recordStartAddr = freeAddr - buf.capacity() - 4 - ikBytes.length;

    int newPairLength = pairLength + 2;
    if (recordStartAddr < SchemaFileConfig.SEG_HEADER_SIZE + newPairLength) {
      return -1;
    }
    pairLength = (short) newPairLength;

    int tarIdx = binaryInsertOnKeys(key);

    // fixme EXPENSIVE cross check of duplication between name and alias
    if (aliasFlag && getIndexByAlias(key) != -1) {
      throw new RecordDuplicatedException(
          String.format("Record [%s] has conflict name with alias of its siblings.", key));
    }

    // check alias-key duplication, set flag if necessary
    String alias = RecordUtils.getRecordAlias(buf);
    if (alias != null && !alias.equals("")) {
      if (binarySearchOnKeys(alias) >= 0 || getIndexByAlias(alias) != -1) {
        throw new RecordDuplicatedException(
            String.format("Record [%s] has conflict alias [%s] with its siblings.", key, alias));
      }
      aliasFlag = true;
    }

    buf.clear();
    this.buffer.clear();
    this.buffer.position(recordStartAddr);
    ReadWriteIOUtils.write(key, this.buffer);
    this.buffer.put(buf);

    this.buffer.clear().position(SchemaFileConfig.SEG_HEADER_SIZE + tarIdx * 2);
    int shiftOffsets = recordNum - tarIdx;
    if (shiftOffsets > 0) {
      short[] shifts = new short[shiftOffsets];
      this.buffer.asShortBuffer().get(shifts);
      this.buffer.position(SchemaFileConfig.SEG_HEADER_SIZE + tarIdx * 2);
      this.buffer.putShort((short) recordStartAddr);
      this.buffer.asShortBuffer().put(shifts);
    } else {
      this.buffer.putShort((short) recordStartAddr);
    }

    this.freeAddr = (short) recordStartAddr;
    this.recordNum++;

    penuKey = lastKey;
    lastKey = key;
    return recordStartAddr - pairLength - SchemaFileConfig.SEG_HEADER_SIZE;
  }
<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