Skip to content

Commit

Permalink
Fix file content reader
Browse files Browse the repository at this point in the history
It would before only read 100 bytes.
  • Loading branch information
RagnarW committed Mar 14, 2018
1 parent 5c123b4 commit dd6da53
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 52 deletions.
Expand Up @@ -258,7 +258,7 @@ private File clientFileToDatabaseFile( File file ) throws IOException
return new File( new File( temporaryDirectory, "graph-db" ), relativePathToDatabaseDir ); return new File( new File( temporaryDirectory, "graph-db" ), relativePathToDatabaseDir );
} }


private void fileContentEquals( File fileA, File fileB ) private void fileContentEquals( File fileA, File fileB ) throws IOException
{ {
assertNotEquals( fileA.getPath(), fileB.getPath() ); assertNotEquals( fileA.getPath(), fileB.getPath() );
String message = String.format( "Expected file: %s\ndoes not match actual file: %s", fileA, fileB ); String message = String.format( "Expected file: %s\ndoes not match actual file: %s", fileA, fileB );
Expand Down
Expand Up @@ -19,29 +19,28 @@
*/ */
package org.neo4j.causalclustering.catchup.storecopy; package org.neo4j.causalclustering.catchup.storecopy;


import org.apache.commons.compress.utils.Charsets;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;


import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.Reader;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.time.Clock; import java.time.Clock;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.function.Predicate;
import java.util.function.Supplier;


import org.neo4j.causalclustering.catchup.CatchUpClient; import org.neo4j.causalclustering.catchup.CatchUpClient;
import org.neo4j.causalclustering.catchup.CatchupAddressProvider; import org.neo4j.causalclustering.catchup.CatchupAddressProvider;
import org.neo4j.causalclustering.handlers.VoidPipelineWrapperFactory; import org.neo4j.causalclustering.handlers.VoidPipelineWrapperFactory;
import org.neo4j.function.ThrowingSupplier;
import org.neo4j.helpers.AdvertisedSocketAddress; import org.neo4j.helpers.AdvertisedSocketAddress;
import org.neo4j.io.fs.DefaultFileSystemAbstraction; import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.io.fs.FileSystemAbstraction; import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.fs.OpenMode;
import org.neo4j.io.fs.StoreChannel; import org.neo4j.io.fs.StoreChannel;
import org.neo4j.kernel.monitoring.Monitors; import org.neo4j.kernel.monitoring.Monitors;
import org.neo4j.logging.FormattedLogProvider; import org.neo4j.logging.FormattedLogProvider;
Expand Down Expand Up @@ -107,7 +106,7 @@ public void shutdown()
} }


@Test @Test
public void canPerformCatchup() throws StoreCopyFailedException public void canPerformCatchup() throws StoreCopyFailedException, IOException
{ {
// given remote node has a store // given remote node has a store
catchupServerRule.before(); // assume it is running catchupServerRule.before(); // assume it is running
Expand All @@ -128,7 +127,7 @@ public void canPerformCatchup() throws StoreCopyFailedException
} }


@Test @Test
public void failedFileCopyShouldRetry() throws StoreCopyFailedException public void failedFileCopyShouldRetry() throws StoreCopyFailedException, IOException
{ {
// given a file will fail twice before succeeding // given a file will fail twice before succeeding
fileB.setRemainingFailed( 2 ); fileB.setRemainingFailed( 2 );
Expand Down Expand Up @@ -160,7 +159,7 @@ public void failedFileCopyShouldRetry() throws StoreCopyFailedException
} }


@Test @Test
public void reconnectingWorks() throws StoreCopyFailedException public void reconnectingWorks() throws StoreCopyFailedException, IOException
{ {
// given a remote catchup will fail midway // given a remote catchup will fail midway
catchupServerRule.before(); catchupServerRule.before();
Expand Down Expand Up @@ -194,59 +193,26 @@ private File relative( String filename )
return testDirectory.file( filename ); return testDirectory.file( filename );
} }


private String fileContent( File file ) private String fileContent( File file ) throws IOException
{ {
return fileContent( file, fileSystemAbstraction ); return fileContent( file, fileSystemAbstraction );
} }


private static StringBuilder serverFileContentsStringBuilder( File file, FileSystemAbstraction fileSystemAbstraction ) static String fileContent( File file, FileSystemAbstraction fsa ) throws IOException
{ {
try ( StoreChannel storeChannel = fileSystemAbstraction.open( file, OpenMode.READ ) ) int chunkSize = 128;
StringBuilder stringBuilder = new StringBuilder();
try ( Reader reader = fsa.openAsReader( file, Charsets.UTF_8 ) )
{ {
final int MAX_BUFFER_SIZE = 100; CharBuffer charBuffer = CharBuffer.wrap( new char[chunkSize] );
ByteBuffer byteBuffer = ByteBuffer.wrap( new byte[MAX_BUFFER_SIZE] ); while ( reader.read( charBuffer ) != -1 )
StringBuilder stringBuilder = new StringBuilder();
Predicate<Integer> inRange = betweenZeroAndRange( MAX_BUFFER_SIZE );
Supplier<Integer> readNext = unchecked( () -> storeChannel.read( byteBuffer ) );
for ( int readBytes = readNext.get(); inRange.test( readBytes ); readBytes = readNext.get() )
{ {
for ( byte index = 0; index < readBytes; index++ ) charBuffer.flip();
{ stringBuilder.append( charBuffer );
char actual = (char) byteBuffer.get( index ); charBuffer.clear();
stringBuilder.append( actual );
}
} }
return stringBuilder;
} }
catch ( IOException e ) return stringBuilder.toString();
{
throw new RuntimeException( e );
}
}

static String fileContent( File file, FileSystemAbstraction fileSystemAbstraction )
{
return serverFileContentsStringBuilder( file, fileSystemAbstraction ).toString();
}

private static Supplier<Integer> unchecked( ThrowingSupplier<Integer,?> throwableSupplier )
{
return () ->
{
try
{
return throwableSupplier.get();
}
catch ( Throwable throwable )
{
throw new RuntimeException( throwable );
}
};
}

private static Predicate<Integer> betweenZeroAndRange( int RANGE )
{
return bytes -> bytes > 0 && bytes <= RANGE;
} }


private String clientFileContents( InMemoryFileSystemStream storeFileStreams, String filename ) private String clientFileContents( InMemoryFileSystemStream storeFileStreams, String filename )
Expand Down

0 comments on commit dd6da53

Please sign in to comment.