protected StoreFileChannel getStoreFileChannel( FileChannel channel ) { return new StoreFileChannel( channel ); }
@Override public void writeAll( ByteBuffer src ) throws IOException { long bytesToWrite = src.limit() - src.position(); int bytesWritten; while ( (bytesToWrite -= bytesWritten = write( src )) > 0 ) { if ( bytesWritten < 0 ) { throw new IOException( "Unable to write to disk, reported bytes written was " + bytesWritten ); } } }
@Override public void flush() throws IOException { force( false ); }
@Test void shouldHandlePartialWrites() throws Exception { // Given FileChannel mockChannel = mock(FileChannel.class); when(mockChannel.write( any(ByteBuffer.class), anyLong() )).thenReturn( 4 ); ByteBuffer buffer = ByteBuffer.wrap( "Hello, world!".getBytes( StandardCharsets.UTF_8 ) ); StoreFileChannel channel = new StoreFileChannel( mockChannel ); // When channel.writeAll( buffer, 20 ); // Then verify( mockChannel ).write( buffer, 20 ); verify( mockChannel ).write( buffer, 24 ); verify( mockChannel ).write( buffer, 28 ); verify( mockChannel ).write( buffer, 32 ); verifyNoMoreInteractions( mockChannel ); } }
private File createNeoStoreFile() throws IOException { File neoStoreFile = databaseLayout.metadataStore(); fsRule.get().create( neoStoreFile ).close(); return neoStoreFile; } }
@ParameterizedTest @ValueSource( ints = {0, 1} ) @DisabledOnOs( OS.WINDOWS ) void mustUnlockFileWhenThePageSwapperIsClosed( int noChannelStriping ) throws Exception { PageSwapperFactory factory = createSwapperFactory(); factory.open( fileSystem, Configuration.EMPTY ); File file = testDir.file( "file" ); fileSystem.create( file ).close(); createSwapper( factory, file, 4, NO_CALLBACK, false, false ).close(); try ( StoreFileChannel channel = fileSystem.open( file, OpenMode.READ_WRITE ); FileLock fileLock = channel.tryLock() ) { assertThat( fileLock, is( not( nullValue() ) ) ); } }
@Override public int read( ByteBuffer dst ) throws IOException { ByteBuffer byteBuffer = ByteBuffer.allocate( 1 ); int read = super.read( byteBuffer ); if ( read > 0 ) { byteBuffer.flip(); dst.put( byteBuffer.get() ); } return read; } }
@Override public long position() throws IOException { return translateOutgoing( super.position() ); }
@Override public long size() throws IOException { return translateOutgoing( super.size() ); }
@Override public JumpingFileChannel truncate( long size ) throws IOException { super.truncate( translateIncoming( size, true ) ); return this; }
@ParameterizedTest @ValueSource( ints = {0, 1} ) @DisabledOnOs( OS.WINDOWS ) void creatingSwapperForInternallyLockedFileMustThrow( int noChannelStriping ) throws Exception { PageSwapperFactory factory = createSwapperFactory(); factory.open( fileSystem, Configuration.EMPTY ); File file = testDir.file( "file" ); StoreFileChannel channel = fileSystem.create( file ); try ( FileLock fileLock = channel.tryLock() ) { assertThat( fileLock, is( not( nullValue() ) ) ); assertThrows( FileLockException.class, () -> createSwapper( factory, file, 4, NO_CALLBACK, true, bool( noChannelStriping ) ) ); } }
@Test void archiveDirectoryWithSubdirectories() throws IOException { File archiveFile = testDirectory.file( "directoryWithSubdirectoriesArchive.zip" ); File directoryArchive = testDirectory.directory( "directoryWithSubdirs" ); File subdir1 = new File( directoryArchive, "subdir1" ); File subdir2 = new File( directoryArchive, "subdir" ); fileSystem.mkdir( subdir1 ); fileSystem.mkdir( subdir2 ); fileSystem.create( new File( directoryArchive, "a" ) ).close(); fileSystem.create( new File( directoryArchive, "b" ) ).close(); fileSystem.create( new File( subdir1, "c" ) ).close(); fileSystem.create( new File( subdir2, "d" ) ).close(); ZipUtils.zip( fileSystem, directoryArchive, archiveFile ); assertTrue( fileSystem.fileExists( archiveFile ) ); assertEquals( 6, countArchiveEntries( archiveFile ) ); }
@Override public int read( ByteBuffer dst, long position ) throws IOException { return super.read( dst, translateIncoming( position ) ); } }
@Override public JumpingFileChannel position( long newPosition ) throws IOException { super.position( translateIncoming( newPosition ) ); return this; }
File file = testDir.file( "file" ); fileSystem.create( file ).close();
@Override public StoreChannel open( File fileName, OpenMode openMode ) throws IOException { return new StoreFileChannel( FileUtils.open( path( fileName ), openMode ) ); }
@Override public int write( ByteBuffer src ) throws IOException { byte b = src.get(); ByteBuffer byteBuffer = ByteBuffer.wrap( new byte[]{b} ); return super.write( byteBuffer ); }
@Override public void flush() throws IOException { force( false ); }
@Test void archiveDirectory() throws IOException { File archiveFile = testDirectory.file( "directoryArchive.zip" ); File directory = testDirectory.directory( "directory" ); fileSystem.create( new File( directory, "a" ) ).close(); fileSystem.create( new File( directory, "b" ) ).close(); ZipUtils.zip( fileSystem, directory, archiveFile ); assertTrue( fileSystem.fileExists( archiveFile ) ); assertEquals( 2, countArchiveEntries( archiveFile ) ); }
@Override public synchronized StoreChannel create( File fileName ) throws IOException { File parentFile = fileName.getParentFile(); if ( parentFile != null /*means that this is the 'default location'*/ && !fileExists( parentFile ) ) { throw new FileNotFoundException( "'" + fileName + "' (The system cannot find the path specified)" ); } EphemeralFileData data = files.computeIfAbsent( canonicalFile( fileName ), key -> new EphemeralFileData( clock ) ); return new StoreFileChannel( new EphemeralFileChannel( data, new FileStillOpenException( fileName.getPath() ) ) ); }