Skip to content

Commit

Permalink
Adding ability to fetch "pragma auto_vacuum" value. This helps for th…
Browse files Browse the repository at this point in the history
…ose who need to run vacuum in order to fix older databases.
  • Loading branch information
robbiehanson committed Aug 27, 2014
1 parent fbfc868 commit ad44c04
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 66 deletions.
4 changes: 2 additions & 2 deletions YapDatabase/YapDatabase.m
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ + (NSString *)sqliteVersionUsing:(sqlite3 *)db

+ (int)pragma:(NSString *)pragmaSetting using:(sqlite3 *)db
{
if (pragmaSetting == nil) return 0;
if (pragmaSetting == nil) return -1;

sqlite3_stmt *statement;
NSString *pragma = [NSString stringWithFormat:@"PRAGMA %@;", pragmaSetting];
Expand All @@ -729,7 +729,7 @@ + (int)pragma:(NSString *)pragmaSetting using:(sqlite3 *)db
return NO;
}

int result = 0;
int result = -1;

status = sqlite3_step(statement);
if (status == SQLITE_ROW)
Expand Down
73 changes: 41 additions & 32 deletions YapDatabase/YapDatabaseConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,42 @@ __attribute((deprecated("Use method asyncReadWriteWithBlock:completionQueue:comp
#pragma mark Vacuum
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/**
* Upgrade Notice:
*
* The "auto_vacuum=FULL" was not properly set until YapDatabase v2.5.
* And thus if you have an app that was using YapDatabase prior to this version,
* then the existing database file will continue to operate in "auto_vacuum=NONE" mode.
* This means the existing database file won't be properly truncated as you delete information from the db.
* That is, the data will be removed, but the pages will be moved to the freelist,
* and the file itself will remain the same size on disk. (I.e. the file size can grow, but not shrink.)
* To correct this problem, you should run the vacuum operation is at least once.
* After it is run, the "auto_vacuum=FULL" mode will be set,
* and the database file size will automatically shrink in the future (as you delete data).
*
* @returns Result from "PRAGMA auto_vacuum;" command, as a readable string:
* - NONE
* - FULL
* - INCREMENTAL
* - UNKNOWN (future proofing)
*
* If the return value is NONE, then you should run the vacuum operation at some point
* in order to properly reconfigure the database.
*
* Concerning Method Invocation:
*
* You can invoke this method as a standalone method on the connection:
*
* NSString *value = [databaseConnection pragmaAutoVacuum]
*
* Or you can invoke this method within a transaction:
*
* [databaseConnection asyncReadWithBlock:^(YapDatabaseReadTransaction *transaction){
* NSString *value = [databaseConnection pragmaAutoVacuum];
* }];
**/
- (NSString *)pragmaAutoVacuum;

/**
* Performs a VACUUM on the sqlite database.
*
Expand All @@ -519,16 +555,7 @@ __attribute((deprecated("Use method asyncReadWriteWithBlock:completionQueue:comp
*
* Remember that YapDatabase operates in WAL mode, with "auto_vacuum=FULL" set.
*
* Upgrade Notice:
* The "auto_vacuum=FULL" was not properly set until YapDatabase v2.5.
* And thus if you have an app that was using YapDatabase prior to this version,
* then the existing database file will continue to operate in "auto_vacuum=NONE" mode.
* This means the existing database file won't be properly truncated as you delete information from the db.
* That is, the data will be removed, but the pages will be moved to the freelist,
* and the file itself will remain the same size on disk.
* To correct this problem, you should run the vacuum operation is at least once.
* After it is run, the "auto_vacuum=FULL" mode will be set,
* and the database file size will automatically shrink in the future (as you delete data).
* @see pragmaAutoVacuum
**/
- (void)vacuum;

Expand All @@ -542,20 +569,11 @@ __attribute((deprecated("Use method asyncReadWriteWithBlock:completionQueue:comp
* http://sqlite.org/lang_vacuum.html
*
* Remember that YapDatabase operates in WAL mode, with "auto_vacuum=FULL" set.
*
* Upgrade Notice:
* The "auto_vacuum=FULL" was not properly set until YapDatabase v2.5.
* And thus if you have an app that was using YapDatabase prior to this version,
* then the existing database file will continue to operate in "auto_vacuum=NONE" mode.
* This means the existing database file won't be properly truncated as you delete information from the db.
* That is, the data will be removed, but the pages will be moved to the freelist,
* and the file itself will remain the same size on disk.
* To correct this problem, you should run the vacuum operation is at least once.
* After it is run, the "auto_vacuum=FULL" mode will be set,
* and the database file size will automatically shrink in the future (as you delete data).
*
* An optional completion block may be used.
* The completionBlock will be invoked on the main thread (dispatch_get_main_queue()).
*
* @see pragmaAutoVacuum
**/
- (void)asyncVacuumWithCompletionBlock:(dispatch_block_t)completionBlock;

Expand All @@ -570,20 +588,11 @@ __attribute((deprecated("Use method asyncReadWriteWithBlock:completionQueue:comp
*
* Remember that YapDatabase operates in WAL mode, with "auto_vacuum=FULL" set.
*
* Upgrade Notice:
* The "auto_vacuum=FULL" was not properly set until YapDatabase v2.5.
* And thus if you have an app that was using YapDatabase prior to this version,
* then the existing database file will continue to operate in "auto_vacuum=NONE" mode.
* This means the existing database file won't be properly truncated as you delete information from the db.
* That is, the data will be removed, but the pages will be moved to the freelist,
* and the file itself will remain the same size on disk.
* To correct this problem, you should run the vacuum operation is at least once.
* After it is run, the "auto_vacuum=FULL" mode will be set,
* and the database file size will automatically shrink in the future (as you delete data).
*
* An optional completion block may be used.
* Additionally the dispatch_queue to invoke the completion block may also be specified.
* If NULL, dispatch_get_main_queue() is automatically used.
*
* @see pragmaAutoVacuum
**/
- (void)asyncVacuumWithCompletionQueue:(dispatch_queue_t)completionQueue
completionBlock:(dispatch_block_t)completionBlock;
Expand Down
88 changes: 56 additions & 32 deletions YapDatabase/YapDatabaseConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -4226,6 +4226,57 @@ - (void)removeRegisteredExtensionConnectionWithName:(NSString *)extName
#pragma mark Vacuum
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/**
* Upgrade Notice:
*
* The "auto_vacuum=FULL" was not properly set until YapDatabase v2.5.
* And thus if you have an app that was using YapDatabase prior to this version,
* then the existing database file will continue to operate in "auto_vacuum=NONE" mode.
* This means the existing database file won't be properly truncated as you delete information from the db.
* That is, the data will be removed, but the pages will be moved to the freelist,
* and the file itself will remain the same size on disk. (I.e. the file size can grow, but not shrink.)
* To correct this problem, you should run the vacuum operation is at least once.
* After it is run, the "auto_vacuum=FULL" mode will be set,
* and the database file size will automatically shrink in the future (as you delete data).
*
* @returns Result from "PRAGMA auto_vacuum;" command, as a readable string:
* - NONE
* - FULL
* - INCREMENTAL
* - UNKNOWN (future proofing)
*
* If the return value is NONE, then you should run the vacuum operation at some point
* in order to properly reconfigure the database.
*
* Concerning Method Invocation:
*
* You can invoke this method as a standalone method on the connection:
*
* NSString *value = [databaseConnection pragmaAutoVacuum]
*
* Or you can invoke this method within a transaction:
*
* [databaseConnection asyncReadWithBlock:^(YapDatabaseReadTransaction *transaction){
* NSString *value = [databaseConnection pragmaAutoVacuum];
* }];
**/
- (NSString *)pragmaAutoVacuum
{
__block int value = -1;

dispatch_block_t block = ^{ @autoreleasepool {

value = [YapDatabase pragma:@"auto_vacuum" using:db];
}};

if (dispatch_get_specific(IsOnConnectionQueueKey))
block();
else
dispatch_sync(connectionQueue, block);

return [YapDatabase pragmaValueForAutoVacuum:value];
}

/**
* Performs a VACUUM on the sqlite database.
*
Expand All @@ -4237,16 +4288,7 @@ - (void)removeRegisteredExtensionConnectionWithName:(NSString *)extName
*
* Remember that YapDatabase operates in WAL mode, with "auto_vacuum=FULL" set.
*
* Upgrade Notice:
* The "auto_vacuum=FULL" was not properly set until YapDatabase v2.5.
* And thus if you have an app that was using YapDatabase prior to this version,
* then the existing database file will continue to operate in "auto_vacuum=NONE" mode.
* This means the existing database file won't be properly truncated as you delete information from the db.
* That is, the data will be removed, but the pages will be moved to the freelist,
* and the file itself will remain the same size on disk.
* To correct this problem, you should run the vacuum operation is at least once.
* After it is run, the "auto_vacuum=FULL" mode will be set,
* and the database file size will automatically shrink in the future (as you delete data).
* @see pragmaAutoVacuum
**/
- (void)vacuum
{
Expand Down Expand Up @@ -4308,19 +4350,10 @@ - (void)vacuum
*
* Remember that YapDatabase operates in WAL mode, with "auto_vacuum=FULL" set.
*
* Upgrade Notice:
* The "auto_vacuum=FULL" was not properly set until YapDatabase v2.5.
* And thus if you have an app that was using YapDatabase prior to this version,
* then the existing database file will continue to operate in "auto_vacuum=NONE" mode.
* This means the existing database file won't be properly truncated as you delete information from the db.
* That is, the data will be removed, but the pages will be moved to the freelist,
* and the file itself will remain the same size on disk.
* To correct this problem, you should run the vacuum operation is at least once.
* After it is run, the "auto_vacuum=FULL" mode will be set,
* and the database file size will automatically shrink in the future (as you delete data).
*
* An optional completion block may be used.
* The completionBlock will be invoked on the main thread (dispatch_get_main_queue()).
*
* @see pragmaAutoVacuum
**/
- (void)asyncVacuumWithCompletionBlock:(dispatch_block_t)completionBlock
{
Expand All @@ -4338,20 +4371,11 @@ - (void)asyncVacuumWithCompletionBlock:(dispatch_block_t)completionBlock
*
* Remember that YapDatabase operates in WAL mode, with "auto_vacuum=FULL" set.
*
* Upgrade Notice:
* The "auto_vacuum=FULL" was not properly set until YapDatabase v2.5.
* And thus if you have an app that was using YapDatabase prior to this version,
* then the existing database file will continue to operate in "auto_vacuum=NONE" mode.
* This means the existing database file won't be properly truncated as you delete information from the db.
* That is, the data will be removed, but the pages will be moved to the freelist,
* and the file itself will remain the same size on disk.
* To correct this problem, you should run the vacuum operation is at least once.
* After it is run, the "auto_vacuum=FULL" mode will be set,
* and the database file size will automatically shrink in the future (as you delete data).
*
* An optional completion block may be used.
* Additionally the dispatch_queue to invoke the completion block may also be specified.
* If NULL, dispatch_get_main_queue() is automatically used.
*
* @see pragmaAutoVacuum
**/
- (void)asyncVacuumWithCompletionQueue:(dispatch_queue_t)completionQueue
completionBlock:(dispatch_block_t)completionBlock
Expand Down

0 comments on commit ad44c04

Please sign in to comment.