Skip to content

Commit

Permalink
Merge pull request #6272 from lutovich/3.0-close-AutoCloseables
Browse files Browse the repository at this point in the history
Ensure all AutoCloseables are closed
  • Loading branch information
johan-neo committed Jan 28, 2016
2 parents cbaf238 + 853a1b7 commit 6cdc110
Show file tree
Hide file tree
Showing 23 changed files with 231 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Stream;

/**
* This FileSystemAbstract implementation delegates all calls to a given {@link FileSystem} implementation.
Expand Down Expand Up @@ -175,45 +174,30 @@ public boolean renameFile( File from, File to ) throws IOException
@Override
public File[] listFiles( File directory )
{
List<File> files = new ArrayList<>();
try
try ( Stream<Path> listing = Files.list( path( directory ) ) )
{
for ( Path path : Files.newDirectoryStream( path( directory ) ) )
{
files.add( path.toFile() );
}
return listing.map( Path::toFile ).toArray( File[]::new );
}
catch ( IOException e )
{
return null;
}
return files.toArray( new File[files.size()] );
}

@Override
public File[] listFiles( File directory, final FilenameFilter filter )
{
List<File> files = new ArrayList<>();
try
try ( Stream<Path> listing = Files.list( path( directory ) ) )
{
DirectoryStream.Filter<Path> dirfilter = new DirectoryStream.Filter<Path>()
{
@Override
public boolean accept( Path entry ) throws IOException
{
return filter.accept( entry.getParent().toFile(), entry.getFileName().toString() );
}
};
for ( Path path : Files.newDirectoryStream( path( directory ), dirfilter ) )
{
files.add( path.toFile() );
}
return listing
.filter( entry -> filter.accept( entry.getParent().toFile(), entry.getFileName().toString() ) )
.map( Path::toFile )
.toArray( File[]::new );
}
catch ( IOException e )
{
return null;
}
return files.toArray( new File[files.size()] );
}

@Override
Expand Down Expand Up @@ -244,18 +228,21 @@ public void copyRecursively( File fromDirectory, File toDirectory ) throws IOExc

private void copyRecursively( Path source, Path target ) throws IOException
{
for ( Path sourcePath : Files.newDirectoryStream( source ) )
try ( DirectoryStream<Path> directoryStream = Files.newDirectoryStream( source ) )
{
Path targetPath = target.resolve( sourcePath.getFileName() );
if ( Files.isDirectory( sourcePath ) )
{
Files.createDirectories( targetPath );
copyRecursively( sourcePath, targetPath );
}
else
for ( Path sourcePath : directoryStream )
{
Files.copy( sourcePath, targetPath,
StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES );
Path targetPath = target.resolve( sourcePath.getFileName() );
if ( Files.isDirectory( sourcePath ) )
{
Files.createDirectories( targetPath );
copyRecursively( sourcePath, targetPath );
}
else
{
Files.copy( sourcePath, targetPath,
StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES );
}
}
}
}
Expand Down
18 changes: 2 additions & 16 deletions community/io/src/main/java/org/neo4j/io/fs/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,9 @@ public static void copyFile( File srcFile, File dstFile ) throws IOException
{
//noinspection ResultOfMethodCallIgnored
dstFile.getParentFile().mkdirs();
FileInputStream input = null;
FileOutputStream output = null;
try
try ( FileInputStream input = new FileInputStream( srcFile );
FileOutputStream output = new FileOutputStream( dstFile ); )
{
input = new FileInputStream( srcFile );
output = new FileOutputStream( dstFile );
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int bytesRead;
Expand All @@ -298,17 +295,6 @@ public static void copyFile( File srcFile, File dstFile ) throws IOException
// Because the message from this cause may not mention which file it's about
throw new IOException( "Could not copy '" + srcFile + "' to '" + dstFile + "'", e );
}
finally
{
if ( input != null )
{
input.close();
}
if ( output != null )
{
output.close();
}
}
}

public static void copyRecursively( File fromDirectory, File toDirectory ) throws IOException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,8 @@ private void copyRecursivelyFromOtherFs( File from, FileSystemAbstraction fromFs

private void copyFile( File from, FileSystemAbstraction fromFs, File to, ByteBuffer buffer ) throws IOException
{
StoreChannel source = fromFs.open( from, "r" );
StoreChannel sink = this.open( to, "rw" );
try
try ( StoreChannel source = fromFs.open( from, "r" );
StoreChannel sink = this.open( to, "rw" ) )
{
for ( int available; (available = (int) (source.size() - source.position())) > 0; )
{
Expand All @@ -540,17 +539,6 @@ private void copyFile( File from, FileSystemAbstraction fromFs, File to, ByteBuf
sink.write( buffer );
}
}
finally
{
if ( source != null )
{
source.close();
}
if ( sink != null )
{
sink.close();
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Map;
Expand Down Expand Up @@ -123,7 +124,10 @@ void addTo( String line, String... path ) throws IOException
file.getParentFile().mkdirs();
}

newFilePrintWriter( file, StandardCharsets.UTF_8 ).append( line ).append( "\n" ).close();
try ( PrintWriter writer = newFilePrintWriter( file, StandardCharsets.UTF_8 ) )
{
writer.append( line ).append( "\n" );
}
}

private String path( String[] path )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.neo4j.kernel.api.exceptions.InvalidTransactionTypeKernelException;
import org.neo4j.kernel.api.exceptions.LabelNotFoundKernelException;
import org.neo4j.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException;
import org.neo4j.kernel.api.exceptions.PropertyNotFoundException;
import org.neo4j.kernel.api.exceptions.RelationshipTypeIdNotFoundKernelException;
import org.neo4j.kernel.api.exceptions.TransactionFailureException;
import org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException;
Expand Down Expand Up @@ -77,7 +76,6 @@
import org.neo4j.kernel.impl.api.operations.SchemaWriteOperations;
import org.neo4j.kernel.impl.api.state.ConstraintIndexCreator;
import org.neo4j.kernel.impl.api.store.RelationshipIterator;
import org.neo4j.kernel.impl.core.TokenNotFoundException;
import org.neo4j.kernel.impl.index.IndexEntityType;
import org.neo4j.kernel.impl.index.LegacyIndexStore;
import org.neo4j.kernel.impl.util.Cursors;
Expand All @@ -99,7 +97,6 @@
import static org.neo4j.collection.primitive.PrimitiveLongCollections.single;
import static org.neo4j.helpers.collection.Iterables.filter;
import static org.neo4j.helpers.collection.IteratorUtil.iterator;
import static org.neo4j.helpers.collection.IteratorUtil.resourceIterator;
import static org.neo4j.helpers.collection.IteratorUtil.singleOrNull;
import static org.neo4j.kernel.api.StatementConstants.NO_SUCH_NODE;
import static org.neo4j.kernel.impl.api.PropertyValueComparison.COMPARE_NUMBERS;
Expand Down Expand Up @@ -294,7 +291,8 @@ public Cursor<NodeItem> nodeCursorGetFromUniqueIndexSeek( KernelStatement statem
// TODO Filter this properly
StorageStatement storeStatement = statement.getStoreStatement();
IndexReader reader = storeStatement.getFreshIndexReader( index );
return storeStatement.acquireIteratorNodeCursor( reader.seek( value ) );
PrimitiveLongIterator seekResult = PrimitiveLongCollections.resourceIterator( reader.seek( value ), reader );
return storeStatement.acquireIteratorNodeCursor( seekResult );
}

// </Cursors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.neo4j.storageengine.api.NodeItem;

import static java.util.Arrays.copyOf;

import static org.neo4j.collection.primitive.PrimitiveLongCollections.EMPTY_LONG_ARRAY;

public class LabelStoreScanIterator extends PrefetchingResourceIterator<NodeLabelUpdate>
Expand All @@ -57,14 +56,16 @@ protected NodeLabelUpdate fetchNextOrNull()
while ( nodeIds.hasNext() )
{
long nodeId = nodeIds.next();
Cursor<NodeItem> nodeCursor = readOperations.nodeCursor( nodeId );
if ( nodeCursor.next() )
try ( Cursor<NodeItem> nodeCursor = readOperations.nodeCursor( nodeId ) )
{
Cursor<LabelItem> labelCursor = nodeCursor.get().labels();
long[] labels = allLabels( labelCursor );
if ( labels.length > 0 )
if ( nodeCursor.next() )
{
return NodeLabelUpdate.labelChanges( nodeId, EMPTY_LONG_ARRAY, labels );
Cursor<LabelItem> labelCursor = nodeCursor.get().labels();
long[] labels = allLabels( labelCursor );
if ( labels.length > 0 )
{
return NodeLabelUpdate.labelChanges( nodeId, EMPTY_LONG_ARRAY, labels );
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.IOException;

import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.fs.StoreChannel;
import org.neo4j.kernel.impl.transaction.log.LogPosition;
import org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel;
import org.neo4j.kernel.impl.transaction.log.PhysicalLogFile;
Expand All @@ -46,20 +47,22 @@ public StoreMigratorCheckPointer( File storeDir, FileSystemAbstraction fileSyste

/**
* Write a check point in the log file with the given version
*
* <p>
* It will create the file with header containing the log version and lastCommittedTx given as arguments
*
* @param logVersion the log version to open
* @param lastCommittedTx the last committed tx id
*/
public void checkPoint( long logVersion, long lastCommittedTx) throws IOException
public void checkPoint( long logVersion, long lastCommittedTx ) throws IOException
{
PhysicalLogFiles logFiles = new PhysicalLogFiles( storeDir, fileSystem );
File logFileForVersion = logFiles.getLogFileForVersion( logVersion );
if ( !fileSystem.fileExists( logFileForVersion ) )
{
fileSystem.create( logFileForVersion );
writeLogHeader( fileSystem, logFileForVersion, logVersion, lastCommittedTx );
try ( StoreChannel channel = fileSystem.create( logFileForVersion ) )
{
writeLogHeader( channel, logVersion, lastCommittedTx );
}
}

try ( LogVersionedStoreChannel storeChannel =
Expand All @@ -70,7 +73,7 @@ public void checkPoint( long logVersion, long lastCommittedTx) throws IOExceptio
try ( PositionAwarePhysicalFlushableChannel channel =
new PositionAwarePhysicalFlushableChannel( storeChannel ) )
{
final TransactionLogWriter writer = new TransactionLogWriter( new LogEntryWriter( channel ) );
TransactionLogWriter writer = new TransactionLogWriter( new LogEntryWriter( channel ) );
writer.checkPoint( new LogPosition( logVersion, offset ) );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
package org.neo4j.kernel.info;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.lang.management.CompilationMXBean;
Expand All @@ -35,7 +34,7 @@
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
Expand All @@ -45,14 +44,13 @@
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.stream.Stream;

import org.neo4j.kernel.impl.util.OsBeanUtil;
import org.neo4j.logging.Logger;

import static java.net.NetworkInterface.getNetworkInterfaces;

import static org.neo4j.helpers.Format.bytes;
import static org.neo4j.io.fs.FileUtils.newBufferedFileReader;

enum SystemDiagnostics implements DiagnosticsProvider
{
Expand Down Expand Up @@ -244,18 +242,9 @@ public boolean accept( File path )
File scheduler = new File( subdir, "queue/scheduler" );
if ( scheduler.isFile() )
{
try
try ( Stream<String> lines = Files.lines( scheduler.toPath() ) )
{
BufferedReader reader = newBufferedFileReader( scheduler, StandardCharsets.UTF_8 );
try
{
for ( String line; null != ( line = reader.readLine() ); )
logger.log( line );
}
finally
{
reader.close();
}
lines.forEach( logger::log );
}
catch ( IOException e )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ public interface StorageStatement extends AutoCloseable
/**
* Returns an {@link IndexReader} for searching entity ids given property values. One reader is allocated
* and kept per index throughout the life of a statement, making the returned reader repeatable-read isolation.
* <p>
* <b>NOTE:</b>
* Reader returned from this method should not be closed. All such readers will be closed during {@link #close()}
* of the current statement.
*
* @param index {@link IndexDescriptor} to get reader for.
* @return {@link IndexReader} capable of searching entity ids given property values.
Expand All @@ -120,6 +124,9 @@ public interface StorageStatement extends AutoCloseable
* Returns an {@link IndexReader} for searching entity ids given property values. A new reader is allocated
* every call to this method, which means that newly committed data since the last call to this method
* will be visible in the returned reader.
* <p>
* <b>NOTE:</b>
* It is caller's responsibility to close the returned reader.
*
* @param index {@link IndexDescriptor} to get reader for.
* @return {@link IndexReader} capable of searching entity ids given property values.
Expand Down

0 comments on commit 6cdc110

Please sign in to comment.