Skip to content

Commit 2b081c2

Browse files
authored
Merge pull request #2776 from fcarreiro/master
Progress meter for BZ2 file import
2 parents df467e6 + 16e6af7 commit 2b081c2

File tree

3 files changed

+46
-39
lines changed

3 files changed

+46
-39
lines changed

Source/SPDataImport.m

+4-11
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ - (void)closeAndStopProgressSheet
162162
{
163163
SPMainQSync(^{
164164
[NSApp endSheet:singleProgressSheet];
165+
[singleProgressBar setIndeterminate:YES];
165166
[singleProgressSheet orderOut:nil];
166167
[singleProgressBar stopAnimation:self];
167168
[singleProgressBar setMaxValue:100];
@@ -397,16 +398,12 @@ - (void)importSQLFile:(NSString *)filename
397398
fileTotalLength = (NSUInteger)[[[[NSFileManager defaultManager] attributesOfItemAtPath:filename error:NULL] objectForKey:NSFileSize] longLongValue];
398399
if (!fileTotalLength) fileTotalLength = 1;
399400

400-
// If importing a bzipped file, use indeterminate progress bars as no progress is available
401-
BOOL useIndeterminate = NO;
402-
if ([sqlFileHandle compressionFormat] == SPBzip2Compression) useIndeterminate = YES;
403-
404401
SPMainQSync(^{
405402
// Reset progress interface
406403
[errorsView setString:@""];
407404
[singleProgressTitle setStringValue:NSLocalizedString(@"Importing SQL", @"text showing that the application is importing SQL")];
408405
[singleProgressText setStringValue:NSLocalizedString(@"Reading...", @"text showing that app is reading dump")];
409-
[singleProgressBar setIndeterminate:useIndeterminate];
406+
[singleProgressBar setIndeterminate:NO];
410407
[singleProgressBar setMaxValue:fileTotalLength];
411408
[singleProgressBar setUsesThreadedAnimation:YES];
412409
[singleProgressBar startAnimation:self];
@@ -787,16 +784,12 @@ - (void)importCSVFile:(NSString *)filename
787784
if (!fileTotalLength) fileTotalLength = 1;
788785
fileIsCompressed = ([csvFileHandle compressionFormat] != SPNoCompression);
789786

790-
// If importing a bzipped file, use indeterminate progress bars as no progress is available
791-
BOOL useIndeterminate = NO;
792-
if ([csvFileHandle compressionFormat] == SPBzip2Compression) useIndeterminate = YES;
793-
794787
// Reset progress interface
795788
SPMainQSync(^{
796789
[errorsView setString:@""];
797790
[singleProgressTitle setStringValue:NSLocalizedString(@"Importing CSV", @"text showing that the application is importing CSV")];
798791
[singleProgressText setStringValue:NSLocalizedString(@"Reading...", @"text showing that app is reading dump")];
799-
[singleProgressBar setIndeterminate:YES];
792+
[singleProgressBar setIndeterminate:NO];
800793
[singleProgressBar setUsesThreadedAnimation:YES];
801794
[singleProgressBar startAnimation:self];
802795

@@ -971,8 +964,8 @@ - (void)importCSVFile:(NSString *)filename
971964

972965
// Reset progress interface and open the progress sheet
973966
SPMainQSync(^{
974-
[singleProgressBar setIndeterminate:useIndeterminate];
975967
[singleProgressBar setMaxValue:fileTotalLength];
968+
[singleProgressBar setIndeterminate:NO];
976969
[singleProgressBar startAnimation:self];
977970
[NSApp beginSheet:singleProgressSheet modalForWindow:[tableDocumentInstance parentWindow] modalDelegate:self didEndSelector:nil contextInfo:nil];
978971
[singleProgressSheet makeKeyWindow];

Source/SPFileHandle.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
//
2929
// More info at <https://github.com/sequelpro/sequelpro>
3030

31-
union SPSomeFileHandle;
31+
struct SPRawFileHandles;
3232
/**
3333
* @class SPFileHandle SPFileHandle.h
3434
*
@@ -40,7 +40,7 @@ union SPSomeFileHandle;
4040
*/
4141
@interface SPFileHandle : NSObject
4242
{
43-
union SPSomeFileHandle *wrappedFile;
43+
struct SPRawFileHandles *wrappedFile;
4444
char *wrappedFilePath;
4545

4646
NSMutableData *buffer;

Source/SPFileHandle.m

+40-26
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
// waits until some has been written out. This can affect speed and memory usage.
3838
#define SPFH_MAX_WRITE_BUFFER_SIZE 1048576
3939

40-
union SPSomeFileHandle {
40+
struct SPRawFileHandles {
4141
FILE *file;
4242
BZFILE *bzfile;
4343
gzFile *gzfile;
@@ -46,6 +46,7 @@
4646
@interface SPFileHandle ()
4747

4848
- (void)_writeBufferToData;
49+
- (void)_closeFileHandles;
4950

5051
@end
5152

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

133134
if (isBzip2) {
134135
compressionFormat = SPBzip2Compression;
135-
wrappedFile->bzfile = BZ2_bzopen(path, "rb");
136+
wrappedFile->bzfile = BZ2_bzReadOpen(NULL, theFile, 0, 0, NULL, 0);
136137
}
137138
}
138-
// Default to plain
139-
if(compressionFormat == SPNoCompression) {
139+
// We need to save the file handle both in plain and BZ2 format
140+
if(compressionFormat == SPNoCompression || compressionFormat == SPBzip2Compression) {
140141
wrappedFile->file = theFile;
141142
}
142143
else {
@@ -231,7 +232,7 @@ - (NSMutableData *)readDataToEndOfFile
231232
}
232233

233234
/**
234-
* Returns the on-disk (raw/uncompressed) length of data read so far.
235+
* Returns the on-disk (raw/compressed) length of data read so far.
235236
* This includes any compression headers within the data, and can be used
236237
* for progress bars when processing files.
237238
*/
@@ -243,7 +244,7 @@ - (NSUInteger)realDataReadLength
243244
return gzoffset(wrappedFile->gzfile);
244245
}
245246
else if(compressionFormat == SPBzip2Compression) {
246-
return 0;
247+
return ftell(wrappedFile->file);
247248
}
248249
else {
249250
return ftell(wrappedFile->file);
@@ -263,15 +264,7 @@ - (void)setCompressionFormat:(SPFileCompressionFormat)useCompressionFormat
263264
if (compressionFormat == useCompressionFormat) return;
264265

265266
// Regardless of the supplied argument, close the current file according to how it was previously opened
266-
if (compressionFormat == SPGzipCompression) {
267-
gzclose(wrappedFile->gzfile);
268-
}
269-
else if (compressionFormat == SPBzip2Compression) {
270-
BZ2_bzclose(wrappedFile->bzfile);
271-
}
272-
else {
273-
fclose(wrappedFile->file);
274-
}
267+
[self _closeFileHandles];
275268

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

@@ -282,7 +275,8 @@ - (void)setCompressionFormat:(SPFileCompressionFormat)useCompressionFormat
282275
gzbuffer(wrappedFile->gzfile, 131072);
283276
}
284277
else if (compressionFormat == SPBzip2Compression) {
285-
wrappedFile->bzfile = BZ2_bzopen(wrappedFilePath, "wb");
278+
wrappedFile->file = fopen(wrappedFilePath, "wb");
279+
wrappedFile->bzfile = BZ2_bzWriteOpen(NULL, wrappedFile->file, 9, 0, 0);
286280
}
287281
else {
288282
wrappedFile->file = fopen(wrappedFilePath, "wb");
@@ -343,16 +337,7 @@ - (void)closeFile
343337
{
344338
if (!fileIsClosed) {
345339
[self synchronizeFile];
346-
347-
if (compressionFormat == SPGzipCompression) {
348-
gzclose(wrappedFile->gzfile);
349-
}
350-
else if (compressionFormat == SPBzip2Compression) {
351-
BZ2_bzclose(wrappedFile->bzfile);
352-
}
353-
else {
354-
fclose(wrappedFile->file);
355-
}
340+
[self _closeFileHandles];
356341

357342
if (processingThread) {
358343
if ([processingThread isExecuting]) {
@@ -442,6 +427,35 @@ - (void)_writeBufferToData
442427
[writePool drain];
443428
}
444429

430+
/**
431+
* Close any open file handles
432+
*/
433+
- (void)_closeFileHandles
434+
{
435+
if (compressionFormat == SPGzipCompression) {
436+
gzclose(wrappedFile->gzfile);
437+
wrappedFile->gzfile = NULL;
438+
}
439+
else if (compressionFormat == SPBzip2Compression) {
440+
if (fileMode == O_RDONLY) {
441+
BZ2_bzReadClose(NULL, wrappedFile->bzfile);
442+
}
443+
else if (fileMode == O_WRONLY) {
444+
BZ2_bzWriteClose(NULL, wrappedFile->bzfile, 0, NULL, NULL);
445+
}
446+
else {
447+
[NSException raise:NSInvalidArgumentException format:@"SPFileHandle only supports read-only and write-only file modes"];
448+
}
449+
fclose(wrappedFile->file);
450+
wrappedFile->bzfile = NULL;
451+
wrappedFile->file = NULL;
452+
}
453+
else {
454+
fclose(wrappedFile->file);
455+
wrappedFile->file = NULL;
456+
}
457+
}
458+
445459
#pragma mark -
446460

447461
/**

0 commit comments

Comments
 (0)