Skip to content

Commit

Permalink
[BACKLOG-24582] - Updated FileStreamMap logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Angel Ramos committed Oct 16, 2018
1 parent df49e05 commit d9e45ab
Showing 1 changed file with 49 additions and 42 deletions.
Expand Up @@ -59,13 +59,15 @@ interface IFileStreamsCollection {
void add( String filename, FileStream fileStreams ); void add( String filename, FileStream fileStreams );
} }


public class FileStreamsKey implements Comparable<FileStreamsKey> { public class FileStreamsCollectionEntry {
String fileName; private String fileName;
long index = 0; private long index = 0;
private FileStream fileStream;


public FileStreamsKey( String fileName, long index ) { public FileStreamsCollectionEntry( String fileName, long index, FileStream fileStream ) {
this.fileName = fileName; this.fileName = fileName;
this.index = index; this.index = index;
this.fileStream = fileStream;
} }


public String getFileName() { public String getFileName() {
Expand All @@ -84,17 +86,12 @@ public void setIndex( int index ) {
this.index = index; this.index = index;
} }


@Override public FileStream getFileStream() {
public int compareTo( FileStreamsKey o ) { return fileStream;
if ( o.fileName.equals( fileName ) ) {
return 0;
}
return Long.compare( index, o.index );
} }


@Override public void setFileStream( FileStream fileStream ) {
public boolean equals( Object obj ) { this.fileStream = fileStream;
return fileName.equals( obj );
} }
} }


Expand Down Expand Up @@ -267,16 +264,25 @@ public void add( String filename, FileStream fileStreams ) {
} }
} }


public class FileStreamsMap extends TreeMap<FileStreamsKey, FileStream> implements IFileStreamsCollection { public class FileStreamsMap implements IFileStreamsCollection {
int numOpenFiles = 0; private int numOpenFiles = 0;
private TreeMap<String, FileStreamsCollectionEntry> fileNameMap = new TreeMap<>();
private TreeMap<Long, FileStreamsCollectionEntry> indexMap = new TreeMap<>();


@Override @Override
public void add( String filename, FileStream fileWriterOutputStream ) { public int size() {
return fileNameMap.size();
}

@Override
public void add( String fileName, FileStream fileWriterOutputStream ) {
long index = 0; long index = 0;
if ( size() > 0 ) { if ( size() > 0 ) {
index = lastKey().index + 1; index = indexMap.lastKey() + 1;
} }
put( new FileStreamsKey( filename, index ), fileWriterOutputStream ); FileStreamsCollectionEntry newEntry = new FileStreamsCollectionEntry( fileName, index, fileWriterOutputStream );
fileNameMap.put( fileName, newEntry );
indexMap.put( index, newEntry );
if ( fileWriterOutputStream.isOpen() ) { if ( fileWriterOutputStream.isOpen() ) {
numOpenFiles++; numOpenFiles++;
} }
Expand All @@ -285,29 +291,27 @@ public void add( String filename, FileStream fileWriterOutputStream ) {


@Override @Override
public FileStream getStream( String filename ) { public FileStream getStream( String filename ) {
return get( new FileStreamsKey( filename, -1 ) ); if ( fileNameMap.containsKey( filename ) ) {
} return fileNameMap.get( filename ).getFileStream();

} else {
private FileStream remove( String filename ) { return null;
return remove( new FileStreamsKey( filename, -1 ) ); }
} }


@Override @Override
public String getLastFileName() { public String getLastFileName() {
String filename = null; String filename = null;
Map.Entry<TextFileOutputData.FileStreamsKey, FileStream> lastEntry = lastEntry(); if ( indexMap.size() > 0 ) {
if ( lastEntry != null ) { filename = indexMap.lastEntry().getValue().getFileName();
filename = lastEntry.getKey( ).getFileName( );
} }
return filename; return filename;
} }


@Override @Override
public FileStream getLastStream() { public FileStream getLastStream() {
FileStream lastStream = null; FileStream lastStream = null;
Map.Entry<TextFileOutputData.FileStreamsKey, FileStream> lastEntry = lastEntry(); if ( indexMap.size() > 0 ) {
if ( lastEntry != null ) { lastStream = indexMap.lastEntry().getValue().getFileStream();
lastStream = lastEntry.getValue( );
} }
return lastStream; return lastStream;
} }
Expand All @@ -321,11 +325,13 @@ public int getNumOpenFiles() {
public void closeOldestOpenFile( boolean removeFileFromCollection ) throws IOException { public void closeOldestOpenFile( boolean removeFileFromCollection ) throws IOException {
FileStream oldestOpenStream = null; FileStream oldestOpenStream = null;
String oldestOpenFileName = null; String oldestOpenFileName = null;
for ( Map.Entry<TextFileOutputData.FileStreamsKey, FileStream> mapEntry : entrySet() ) { Long oldestOpenFileIndex = null;
FileStream existingStream = mapEntry.getValue(); for ( Map.Entry<Long, FileStreamsCollectionEntry> mapEntry : indexMap.entrySet() ) {
if ( existingStream.isOpen() ) { FileStreamsCollectionEntry existingStream = mapEntry.getValue();
oldestOpenStream = existingStream; if ( existingStream.getFileStream().isOpen() ) {
oldestOpenFileName = mapEntry.getKey().getFileName(); oldestOpenStream = existingStream.getFileStream();
oldestOpenFileName = existingStream.getFileName();
oldestOpenFileIndex = existingStream.getIndex();
break; break;
} }
} }
Expand All @@ -334,19 +340,20 @@ public void closeOldestOpenFile( boolean removeFileFromCollection ) throws IOExc
oldestOpenStream.close(); oldestOpenStream.close();
numOpenFiles--; numOpenFiles--;
if ( removeFileFromCollection ) { if ( removeFileFromCollection ) {
remove( oldestOpenFileName ); fileNameMap.remove( oldestOpenFileName );
indexMap.remove( oldestOpenFileIndex );
} }
} }
} }


@Override @Override
public void flushOpenFiles( boolean closeAfterFlush ) { public void flushOpenFiles( boolean closeAfterFlush ) {
for ( FileStream outputStream : values() ) { for ( FileStreamsCollectionEntry collectionEntry : indexMap.values() ) {
if ( outputStream.isDirty() ) { if ( collectionEntry.getFileStream().isDirty() ) {
try { try {
outputStream.flush(); collectionEntry.getFileStream().flush();
if ( closeAfterFlush ) { if ( closeAfterFlush ) {
outputStream.close(); collectionEntry.getFileStream().close();
} }
} catch ( IOException e ) { } catch ( IOException e ) {
e.printStackTrace(); e.printStackTrace();
Expand All @@ -367,10 +374,10 @@ public void closeFile( String filename ) throws IOException {


@Override @Override
public void closeStream( OutputStream outputStream ) throws IOException { public void closeStream( OutputStream outputStream ) throws IOException {
for ( Map.Entry<TextFileOutputData.FileStreamsKey, FileStream> mapEntry : entrySet() ) { for ( Map.Entry<Long, FileStreamsCollectionEntry> mapEntry : indexMap.entrySet() ) {
FileStream fileStream = mapEntry.getValue(); FileStream fileStream = mapEntry.getValue().getFileStream();
if ( ( fileStream.getBufferedOutputStream() == outputStream ) || ( fileStream.getCompressedOutputStream() == outputStream ) || ( fileStream.getFileOutputStream() == outputStream ) ) { if ( ( fileStream.getBufferedOutputStream() == outputStream ) || ( fileStream.getCompressedOutputStream() == outputStream ) || ( fileStream.getFileOutputStream() == outputStream ) ) {
closeFile( mapEntry.getKey().getFileName() ); closeFile( mapEntry.getValue().getFileName() );
} }
} }
} }
Expand Down

0 comments on commit d9e45ab

Please sign in to comment.