Skip to content

Commit

Permalink
Merge pull request #11293 from MishaDemianenko/3.4-do-not-use-native-…
Browse files Browse the repository at this point in the history
…buffers-for-no-reason

Remove causeless direct byte buffers usages.
  • Loading branch information
MishaDemianenko committed Mar 20, 2018
2 parents e2a5f5d + ae73703 commit 2aabc7b
Show file tree
Hide file tree
Showing 13 changed files with 16 additions and 31 deletions.
Expand Up @@ -843,7 +843,7 @@ public long transferFrom( ReadableByteChannel src, long position, long count ) t
try
{
long transferred = 0;
ByteBuffer intermediary = ByteBuffer.allocateDirect( (int) ByteUnit.mebiBytes( 8 ) );
ByteBuffer intermediary = ByteBuffer.allocate( (int) ByteUnit.mebiBytes( 8 ) );
while ( transferred < count )
{
intermediary.clear();
Expand Down Expand Up @@ -1196,22 +1196,7 @@ private void copyByteBufferContents( ByteBuffer from, ByteBuffer to )

private ByteBuffer allocate( long capacity )
{
try
{
return ByteBuffer.allocateDirect( Math.toIntExact( capacity ) );
}
catch ( OutOfMemoryError oom )
{
try
{
return ByteBuffer.allocate( Math.toIntExact( capacity ) );
}
catch ( OutOfMemoryError secondOom )
{
oom.addSuppressed( secondOom );
throw oom;
}
}
return ByteBuffer.allocate( Math.toIntExact( capacity ) );
}

void free()
Expand Down
Expand Up @@ -20,7 +20,6 @@
package org.neo4j.io.pagecache;

import java.io.File;
import java.io.IOException;
import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
Expand All @@ -45,7 +44,7 @@ public class StubPageCursor extends PageCursor

public StubPageCursor( long initialPageId, int pageSize )
{
this( initialPageId, ByteBuffer.allocateDirect( pageSize ) );
this( initialPageId, ByteBuffer.allocate( pageSize ) );
}

public StubPageCursor( long initialPageId, ByteBuffer buffer )
Expand Down
Expand Up @@ -423,7 +423,7 @@ private byte[] array( long page )

private ByteBuffer wrap( byte[] bytes )
{
ByteBuffer buffer = ByteBuffer.allocateDirect( bytes.length );
ByteBuffer buffer = ByteBuffer.allocate( bytes.length );
for ( byte b : bytes )
{
buffer.put( b );
Expand Down
Expand Up @@ -95,7 +95,7 @@ public final void assertRecordsWrittenCorrectly( File file, StoreChannel channel
{
int recordSize = getRecordSize();
long recordsInFile = channel.size() / recordSize;
ByteBuffer buffer = ByteBuffer.allocateDirect( recordSize );
ByteBuffer buffer = ByteBuffer.allocate( recordSize );
StubPageCursor cursor = new StubPageCursor( 0, buffer );
for ( int i = 0; i < recordsInFile; i++ )
{
Expand Down
Expand Up @@ -28,7 +28,7 @@
public class ChannelInputStream extends InputStream
{
private final StoreChannel channel;
private final ByteBuffer buffer = ByteBuffer.allocateDirect( 8096 );
private final ByteBuffer buffer = ByteBuffer.allocate( 8096 );
private int position;

public ChannelInputStream( StoreChannel channel )
Expand Down
Expand Up @@ -28,7 +28,7 @@
public class ChannelOutputStream extends OutputStream
{
private final StoreChannel channel;
private final ByteBuffer buffer = ByteBuffer.allocateDirect( 8096 );
private final ByteBuffer buffer = ByteBuffer.allocate( 8096 );

public ChannelOutputStream( StoreChannel channel, boolean append ) throws IOException
{
Expand Down
Expand Up @@ -45,7 +45,7 @@ public static LogHeader readLogHeader( FileSystemAbstraction fileSystem, File fi
{
try ( StoreChannel channel = fileSystem.open( file, OpenMode.READ ) )
{
return readLogHeader( ByteBuffer.allocateDirect( LOG_HEADER_SIZE ), channel, strict, file );
return readLogHeader( ByteBuffer.allocate( LOG_HEADER_SIZE ), channel, strict, file );
}
}

Expand Down
Expand Up @@ -31,7 +31,7 @@
import org.neo4j.io.fs.StoreChannel;
import org.neo4j.test.rule.fs.EphemeralFileSystemRule;

import static java.nio.ByteBuffer.allocateDirect;
import static java.nio.ByteBuffer.allocate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.neo4j.helpers.collection.Iterators.asSet;
Expand All @@ -49,7 +49,7 @@ public void smoke() throws Exception
StoreChannel channel = fs.open( new File( "yo" ), OpenMode.READ_WRITE );

// Clear it because we depend on it to be zeros where we haven't written
ByteBuffer buffer = allocateDirect( 23 );
ByteBuffer buffer = allocate( 23 );
buffer.put( new byte[23] ); // zeros
buffer.flip();
channel.write( buffer );
Expand Down
Expand Up @@ -105,7 +105,7 @@ static void filterTransactionLogFile( FileSystemAbstraction fileSystem, File fil
filter.file( file );
try ( StoreChannel in = fileSystem.open( file, OpenMode.READ ) )
{
LogHeader logHeader = readLogHeader( ByteBuffer.allocateDirect( LOG_HEADER_SIZE ), in, true, file );
LogHeader logHeader = readLogHeader( ByteBuffer.allocate( LOG_HEADER_SIZE ), in, true, file );
PhysicalLogVersionedStoreChannel inChannel =
new PhysicalLogVersionedStoreChannel( in, logHeader.logVersion, logHeader.logFormatVersion );
ReadableLogChannel inBuffer = new ReadAheadLogChannel( inChannel );
Expand Down
Expand Up @@ -68,7 +68,7 @@ public static List<LogEntry> logEntries( FileSystemAbstraction fileSystem, Strin
StoreChannel fileChannel = fileSystem.open( logFile, OpenMode.READ );

// Always a header
LogHeader header = readLogHeader( ByteBuffer.allocateDirect( LOG_HEADER_SIZE ), fileChannel, true, logFile );
LogHeader header = readLogHeader( ByteBuffer.allocate( LOG_HEADER_SIZE ), fileChannel, true, logFile );

// Read all log entries
PhysicalLogVersionedStoreChannel versionedStoreChannel =
Expand Down
Expand Up @@ -38,7 +38,7 @@ public ClientCrashingWriter( MadeUpClient client, int crashAtSize )
@Override
public void write( ReadableByteChannel data )
{
ByteBuffer buffer = ByteBuffer.allocateDirect( 1000 );
ByteBuffer buffer = ByteBuffer.allocate( 1000 );
while ( true )
{
buffer.clear();
Expand Down
3 changes: 2 additions & 1 deletion enterprise/com/src/test/java/org/neo4j/com/ServerTest.java
Expand Up @@ -30,6 +30,7 @@

import org.neo4j.com.monitor.RequestMonitor;
import org.neo4j.helpers.HostnamePort;
import org.neo4j.io.ByteUnit;
import org.neo4j.kernel.impl.transaction.log.TransactionIdStore;
import org.neo4j.kernel.impl.transaction.log.entry.VersionAwareLogEntryReader;
import org.neo4j.kernel.monitoring.ByteCounterMonitor;
Expand Down Expand Up @@ -78,7 +79,7 @@ public void shouldSendExceptionBackToClientOnInvalidChecksum() throws Throwable
// Then
try
{
protocol.deserializeResponse( channel.asBlockingReadHandler(), ByteBuffer.allocateDirect( 1024 ), 1,
protocol.deserializeResponse( channel.asBlockingReadHandler(), ByteBuffer.allocate( (int) ByteUnit.kibiBytes( 1 ) ), 1,
VOID_DESERIALIZER, mock( ResourceReleaser.class ), new VersionAwareLogEntryReader<>() );
fail( "Should have failed." );
}
Expand Down
Expand Up @@ -84,7 +84,7 @@ public static LogEntryCursor openLogEntryCursor( FileSystemAbstraction fileSyste
public static LogVersionedStoreChannel openVersionedChannel( FileSystemAbstraction fileSystem, File file ) throws IOException
{
StoreChannel fileChannel = fileSystem.open( file, OpenMode.READ );
LogHeader logHeader = readLogHeader( ByteBuffer.allocateDirect( LOG_HEADER_SIZE ), fileChannel, true, file );
LogHeader logHeader = readLogHeader( ByteBuffer.allocate( LOG_HEADER_SIZE ), fileChannel, true, file );
PhysicalLogVersionedStoreChannel channel =
new PhysicalLogVersionedStoreChannel( fileChannel, logHeader.logVersion, logHeader.logFormatVersion );
return channel;
Expand Down

0 comments on commit 2aabc7b

Please sign in to comment.