public boolean addIfRoom(KV<byte[], byte[]> record) {
    checkState(!sortCalled, "Records can only be added before sort()");

    long recordBytes = estimateRecordBytes(record);
    if (roomInBuffer(numBytes + recordBytes, records.size() + 1L)) {
      records.add(record);
      numBytes += recordBytes;
      return true;
    } else {
      return false;
    }
  }
@Test
  public void testAddIfRoom() {
    InMemorySorter.Options options = new InMemorySorter.Options();
    options.setMemoryMB(1);
    InMemorySorter sorter = InMemorySorter.create(options);

    // Should be a few kb less than what the total buffer supports
    KV<byte[], byte[]> bigRecord = KV.of(new byte[1024 * 500], new byte[1024 * 500]);

    // First add should succeed, second add should fail due to insufficient room
    Assert.assertTrue(sorter.addIfRoom(bigRecord));
    Assert.assertFalse(sorter.addIfRoom(bigRecord));
  }