### Question:
IndexKeyStorage extends SimpleEntryStorage<KEY,IndexKeyStorage.KeyEntryCursor<KEY>> { @Override void add( KEY key, PageCursor pageCursor ) throws IOException { int entrySize = TYPE_SIZE + BlockEntry.keySize( layout, key ); prepareWrite( entrySize ); pageCursor.putByte( KEY_TYPE ); BlockEntry.write( pageCursor, layout, key ); } IndexKeyStorage( FileSystemAbstraction fs, File file, ByteBufferFactory.Allocator byteBufferFactory, int blockSize, Layout<KEY,?> layout );   }### Answer:
@Test void shouldDeleteFileOnCloseWithData() throws IOException { FileSystemAbstraction fs = directory.getFileSystem(); File makeSureImDeleted = directory.file( "makeSureImDeleted" ); try ( IndexKeyStorage<GenericKey> keyStorage = keyStorage( makeSureImDeleted ) ) { keyStorage.add( randomKey( 1 ) ); keyStorage.doneAdding(); assertTrue( fs.fileExists( makeSureImDeleted ), "Expected this file to exist now so that we can assert deletion later." ); } assertFalse( fs.fileExists( makeSureImDeleted ), "Expected this file to be deleted on close." ); }
@Test void shouldDeleteFileOnCloseWithDataBeforeDoneAdding() throws IOException { FileSystemAbstraction fs = directory.getFileSystem(); File makeSureImDeleted = directory.file( "makeSureImDeleted" ); try ( IndexKeyStorage<GenericKey> keyStorage = keyStorage( makeSureImDeleted ) ) { keyStorage.add( randomKey( 1 ) ); assertTrue( fs.fileExists( makeSureImDeleted ), "Expected this file to exist now so that we can assert deletion later." ); } assertFalse( fs.fileExists( makeSureImDeleted ), "Expected this file to be deleted on close." ); }
@Test void mustAllocateResourcesLazilyAndCleanUpOnClose() throws IOException { FileSystemAbstraction fs = directory.getFileSystem(); LocalMemoryTracker allocationTracker = new LocalMemoryTracker(); File file = directory.file( "file" ); try ( UnsafeDirectByteBufferAllocator bufferFactory = new UnsafeDirectByteBufferAllocator( allocationTracker ); IndexKeyStorage<GenericKey> keyStorage = keyStorage( file, bufferFactory ) ) { assertEquals( 0, allocationTracker.usedDirectMemory(), "Expected to not have any buffers allocated yet" ); assertFalse( fs.fileExists( file ), "Expected file to be created lazily" ); keyStorage.add( randomKey( 1 ) ); assertEquals( BLOCK_SIZE, allocationTracker.usedDirectMemory(), "Expected to have exactly one buffer allocated by now" ); assertTrue( fs.fileExists( file ), "Expected file to be created by now" ); } assertFalse( fs.fileExists( file ), "Expected file to be deleted on close" ); }
@Test void shouldReportCorrectCount() throws IOException { try ( IndexKeyStorage<GenericKey> keyStorage = keyStorage() ) { assertEquals( 0, keyStorage.count() ); keyStorage.add( randomKey( 1 ) ); assertEquals( 1, keyStorage.count() ); keyStorage.add( randomKey( 2 ) ); assertEquals( 2, keyStorage.count() ); keyStorage.doneAdding(); assertEquals( 2, keyStorage.count() ); } }