Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement progress for BZ2 file import
  • Loading branch information
Facundo committed Apr 29, 2017
1 parent df467e6 commit 16e6af7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 39 deletions.
15 changes: 4 additions & 11 deletions Source/SPDataImport.m
Expand Up @@ -162,6 +162,7 @@ - (void)closeAndStopProgressSheet
{
SPMainQSync(^{
[NSApp endSheet:singleProgressSheet];
[singleProgressBar setIndeterminate:YES];
[singleProgressSheet orderOut:nil];
[singleProgressBar stopAnimation:self];
[singleProgressBar setMaxValue:100];
Expand Down Expand Up @@ -397,16 +398,12 @@ - (void)importSQLFile:(NSString *)filename
fileTotalLength = (NSUInteger)[[[[NSFileManager defaultManager] attributesOfItemAtPath:filename error:NULL] objectForKey:NSFileSize] longLongValue];
if (!fileTotalLength) fileTotalLength = 1;

// If importing a bzipped file, use indeterminate progress bars as no progress is available
BOOL useIndeterminate = NO;
if ([sqlFileHandle compressionFormat] == SPBzip2Compression) useIndeterminate = YES;

SPMainQSync(^{
// Reset progress interface
[errorsView setString:@""];
[singleProgressTitle setStringValue:NSLocalizedString(@"Importing SQL", @"text showing that the application is importing SQL")];
[singleProgressText setStringValue:NSLocalizedString(@"Reading...", @"text showing that app is reading dump")];
[singleProgressBar setIndeterminate:useIndeterminate];
[singleProgressBar setIndeterminate:NO];
[singleProgressBar setMaxValue:fileTotalLength];
[singleProgressBar setUsesThreadedAnimation:YES];
[singleProgressBar startAnimation:self];
Expand Down Expand Up @@ -787,16 +784,12 @@ - (void)importCSVFile:(NSString *)filename
if (!fileTotalLength) fileTotalLength = 1;
fileIsCompressed = ([csvFileHandle compressionFormat] != SPNoCompression);

// If importing a bzipped file, use indeterminate progress bars as no progress is available
BOOL useIndeterminate = NO;
if ([csvFileHandle compressionFormat] == SPBzip2Compression) useIndeterminate = YES;

// Reset progress interface
SPMainQSync(^{
[errorsView setString:@""];
[singleProgressTitle setStringValue:NSLocalizedString(@"Importing CSV", @"text showing that the application is importing CSV")];
[singleProgressText setStringValue:NSLocalizedString(@"Reading...", @"text showing that app is reading dump")];
[singleProgressBar setIndeterminate:YES];
[singleProgressBar setIndeterminate:NO];
[singleProgressBar setUsesThreadedAnimation:YES];
[singleProgressBar startAnimation:self];

Expand Down Expand Up @@ -971,8 +964,8 @@ - (void)importCSVFile:(NSString *)filename

// Reset progress interface and open the progress sheet
SPMainQSync(^{
[singleProgressBar setIndeterminate:useIndeterminate];
[singleProgressBar setMaxValue:fileTotalLength];
[singleProgressBar setIndeterminate:NO];
[singleProgressBar startAnimation:self];
[NSApp beginSheet:singleProgressSheet modalForWindow:[tableDocumentInstance parentWindow] modalDelegate:self didEndSelector:nil contextInfo:nil];
[singleProgressSheet makeKeyWindow];
Expand Down
4 changes: 2 additions & 2 deletions Source/SPFileHandle.h
Expand Up @@ -28,7 +28,7 @@
//
// More info at <https://github.com/sequelpro/sequelpro>

union SPSomeFileHandle;
struct SPRawFileHandles;
/**
* @class SPFileHandle SPFileHandle.h
*
Expand All @@ -40,7 +40,7 @@ union SPSomeFileHandle;
*/
@interface SPFileHandle : NSObject
{
union SPSomeFileHandle *wrappedFile;
struct SPRawFileHandles *wrappedFile;
char *wrappedFilePath;

NSMutableData *buffer;
Expand Down
66 changes: 40 additions & 26 deletions Source/SPFileHandle.m
Expand Up @@ -37,7 +37,7 @@
// waits until some has been written out. This can affect speed and memory usage.
#define SPFH_MAX_WRITE_BUFFER_SIZE 1048576

union SPSomeFileHandle {
struct SPRawFileHandles {
FILE *file;
BZFILE *bzfile;
gzFile *gzfile;
Expand All @@ -46,6 +46,7 @@
@interface SPFileHandle ()

- (void)_writeBufferToData;
- (void)_closeFileHandles;

@end

Expand Down Expand Up @@ -132,11 +133,11 @@ - (id)initWithFile:(FILE *)theFile fromPath:(const char *)path mode:(int)mode

if (isBzip2) {
compressionFormat = SPBzip2Compression;
wrappedFile->bzfile = BZ2_bzopen(path, "rb");
wrappedFile->bzfile = BZ2_bzReadOpen(NULL, theFile, 0, 0, NULL, 0);
}
}
// Default to plain
if(compressionFormat == SPNoCompression) {
// We need to save the file handle both in plain and BZ2 format
if(compressionFormat == SPNoCompression || compressionFormat == SPBzip2Compression) {
wrappedFile->file = theFile;
}
else {
Expand Down Expand Up @@ -231,7 +232,7 @@ - (NSMutableData *)readDataToEndOfFile
}

/**
* Returns the on-disk (raw/uncompressed) length of data read so far.
* Returns the on-disk (raw/compressed) length of data read so far.
* This includes any compression headers within the data, and can be used
* for progress bars when processing files.
*/
Expand All @@ -243,7 +244,7 @@ - (NSUInteger)realDataReadLength
return gzoffset(wrappedFile->gzfile);
}
else if(compressionFormat == SPBzip2Compression) {
return 0;
return ftell(wrappedFile->file);
}
else {
return ftell(wrappedFile->file);
Expand All @@ -263,15 +264,7 @@ - (void)setCompressionFormat:(SPFileCompressionFormat)useCompressionFormat
if (compressionFormat == useCompressionFormat) return;

// Regardless of the supplied argument, close the current file according to how it was previously opened
if (compressionFormat == SPGzipCompression) {
gzclose(wrappedFile->gzfile);
}
else if (compressionFormat == SPBzip2Compression) {
BZ2_bzclose(wrappedFile->bzfile);
}
else {
fclose(wrappedFile->file);
}
[self _closeFileHandles];

if (dataWritten) [NSException raise:NSInternalInconsistencyException format:@"Cannot change compression settings when data has already been written."];

Expand All @@ -282,7 +275,8 @@ - (void)setCompressionFormat:(SPFileCompressionFormat)useCompressionFormat
gzbuffer(wrappedFile->gzfile, 131072);
}
else if (compressionFormat == SPBzip2Compression) {
wrappedFile->bzfile = BZ2_bzopen(wrappedFilePath, "wb");
wrappedFile->file = fopen(wrappedFilePath, "wb");
wrappedFile->bzfile = BZ2_bzWriteOpen(NULL, wrappedFile->file, 9, 0, 0);
}
else {
wrappedFile->file = fopen(wrappedFilePath, "wb");
Expand Down Expand Up @@ -343,16 +337,7 @@ - (void)closeFile
{
if (!fileIsClosed) {
[self synchronizeFile];

if (compressionFormat == SPGzipCompression) {
gzclose(wrappedFile->gzfile);
}
else if (compressionFormat == SPBzip2Compression) {
BZ2_bzclose(wrappedFile->bzfile);
}
else {
fclose(wrappedFile->file);
}
[self _closeFileHandles];

if (processingThread) {
if ([processingThread isExecuting]) {
Expand Down Expand Up @@ -442,6 +427,35 @@ - (void)_writeBufferToData
[writePool drain];
}

/**
* Close any open file handles
*/
- (void)_closeFileHandles
{
if (compressionFormat == SPGzipCompression) {
gzclose(wrappedFile->gzfile);
wrappedFile->gzfile = NULL;
}
else if (compressionFormat == SPBzip2Compression) {
if (fileMode == O_RDONLY) {
BZ2_bzReadClose(NULL, wrappedFile->bzfile);
}
else if (fileMode == O_WRONLY) {
BZ2_bzWriteClose(NULL, wrappedFile->bzfile, 0, NULL, NULL);
}
else {
[NSException raise:NSInvalidArgumentException format:@"SPFileHandle only supports read-only and write-only file modes"];
}
fclose(wrappedFile->file);
wrappedFile->bzfile = NULL;
wrappedFile->file = NULL;
}
else {
fclose(wrappedFile->file);
wrappedFile->file = NULL;
}
}

#pragma mark -

/**
Expand Down

0 comments on commit 16e6af7

Please sign in to comment.