Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent WAL locking by resetting prepared statements after use #129

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions YYCache/YYKVStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,10 @@ - (BOOL)_dbInitialize {
- (void)_dbCheckpoint {
if (![self _dbCheck]) return;
// Cause a checkpoint to occur, merge `sqlite-wal` file to `sqlite` file.
sqlite3_wal_checkpoint(_db, NULL);
int result = sqlite3_wal_checkpoint(_db, NULL);
if (result != SQLITE_OK && _errorLogsEnabled) {
NSLog(@"%s line:%d sqlite WAL checkpoint error (%d)", __FUNCTION__, __LINE__, result);
}
}

- (BOOL)_dbExecute:(NSString *)sql {
Expand Down Expand Up @@ -197,7 +200,14 @@ - (sqlite3_stmt *)_dbPrepareStmt:(NSString *)sql {
}
CFDictionarySetValue(_dbStmtCache, (__bridge const void *)(sql), stmt);
} else {
sqlite3_reset(stmt);
if (sqlite3_stmt_busy(stmt)) {
//just in case someone will forget to sqlite3_reset cached statement
//causing WAL file lock
if (_errorLogsEnabled) {
NSLog(@"%s line:%d WARN: cached statement for query \"%@\" was not reset.", __FUNCTION__, __LINE__, sql);
}
sqlite3_reset(stmt);
}
}
return stmt;
}
Expand Down Expand Up @@ -239,6 +249,7 @@ - (BOOL)_dbSaveWithKey:(NSString *)key value:(NSData *)value fileName:(NSString
sqlite3_bind_blob(stmt, 7, extendedData.bytes, (int)extendedData.length, 0);

int result = sqlite3_step(stmt);
sqlite3_reset(stmt);
if (result != SQLITE_DONE) {
if (_errorLogsEnabled) NSLog(@"%s line:%d sqlite insert error (%d): %s", __FUNCTION__, __LINE__, result, sqlite3_errmsg(_db));
return NO;
Expand All @@ -253,6 +264,7 @@ - (BOOL)_dbUpdateAccessTimeWithKey:(NSString *)key {
sqlite3_bind_int(stmt, 1, (int)time(NULL));
sqlite3_bind_text(stmt, 2, key.UTF8String, -1, NULL);
int result = sqlite3_step(stmt);
sqlite3_reset(stmt);
if (result != SQLITE_DONE) {
if (_errorLogsEnabled) NSLog(@"%s line:%d sqlite update error (%d): %s", __FUNCTION__, __LINE__, result, sqlite3_errmsg(_db));
return NO;
Expand Down Expand Up @@ -289,6 +301,7 @@ - (BOOL)_dbDeleteItemWithKey:(NSString *)key {
sqlite3_bind_text(stmt, 1, key.UTF8String, -1, NULL);

int result = sqlite3_step(stmt);
sqlite3_reset(stmt);
if (result != SQLITE_DONE) {
if (_errorLogsEnabled) NSLog(@"%s line:%d db delete error (%d): %s", __FUNCTION__, __LINE__, result, sqlite3_errmsg(_db));
return NO;
Expand Down Expand Up @@ -322,6 +335,7 @@ - (BOOL)_dbDeleteItemsWithSizeLargerThan:(int)size {
if (!stmt) return NO;
sqlite3_bind_int(stmt, 1, size);
int result = sqlite3_step(stmt);
sqlite3_reset(stmt);
if (result != SQLITE_DONE) {
if (_errorLogsEnabled) NSLog(@"%s line:%d sqlite delete error (%d): %s", __FUNCTION__, __LINE__, result, sqlite3_errmsg(_db));
return NO;
Expand All @@ -335,6 +349,7 @@ - (BOOL)_dbDeleteItemsWithTimeEarlierThan:(int)time {
if (!stmt) return NO;
sqlite3_bind_int(stmt, 1, time);
int result = sqlite3_step(stmt);
sqlite3_reset(stmt);
if (result != SQLITE_DONE) {
if (_errorLogsEnabled) NSLog(@"%s line:%d sqlite delete error (%d): %s", __FUNCTION__, __LINE__, result, sqlite3_errmsg(_db));
return NO;
Expand Down Expand Up @@ -380,6 +395,7 @@ - (YYKVStorageItem *)_dbGetItemWithKey:(NSString *)key excludeInlineData:(BOOL)e
if (_errorLogsEnabled) NSLog(@"%s line:%d sqlite query error (%d): %s", __FUNCTION__, __LINE__, result, sqlite3_errmsg(_db));
}
}
sqlite3_reset(stmt);
return item;
}

Expand Down Expand Up @@ -428,12 +444,14 @@ - (NSData *)_dbGetValueWithKey:(NSString *)key {
if (result == SQLITE_ROW) {
const void *inline_data = sqlite3_column_blob(stmt, 0);
int inline_data_bytes = sqlite3_column_bytes(stmt, 0);
sqlite3_reset(stmt);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sqlite3_reset(stmt); should add before line:444 if (!inline_data || inline_data_bytes <= 0) return nil;

if (!inline_data || inline_data_bytes <= 0) return nil;
return [NSData dataWithBytes:inline_data length:inline_data_bytes];
} else {
if (result != SQLITE_DONE) {
if (_errorLogsEnabled) NSLog(@"%s line:%d sqlite query error (%d): %s", __FUNCTION__, __LINE__, result, sqlite3_errmsg(_db));
}
sqlite3_reset(stmt);
return nil;
}
}
Expand All @@ -447,13 +465,15 @@ - (NSString *)_dbGetFilenameWithKey:(NSString *)key {
if (result == SQLITE_ROW) {
char *filename = (char *)sqlite3_column_text(stmt, 0);
if (filename && *filename != 0) {
sqlite3_reset(stmt);
return [NSString stringWithUTF8String:filename];
}
} else {
if (result != SQLITE_DONE) {
if (_errorLogsEnabled) NSLog(@"%s line:%d sqlite query error (%d): %s", __FUNCTION__, __LINE__, result, sqlite3_errmsg(_db));
}
}
sqlite3_reset(stmt);
return nil;
}

Expand Down Expand Up @@ -512,6 +532,7 @@ - (NSMutableArray *)_dbGetFilenamesWithSizeLargerThan:(int)size {
break;
}
} while (1);
sqlite3_reset(stmt);
return filenames;
}

Expand All @@ -538,6 +559,7 @@ - (NSMutableArray *)_dbGetFilenamesWithTimeEarlierThan:(int)time {
break;
}
} while (1);
sqlite3_reset(stmt);
return filenames;
}

Expand Down Expand Up @@ -570,6 +592,7 @@ - (NSMutableArray *)_dbGetItemSizeInfoOrderByTimeAscWithLimit:(int)count {
break;
}
} while (1);
sqlite3_reset(stmt);
return items;
}

Expand All @@ -581,9 +604,12 @@ - (int)_dbGetItemCountWithKey:(NSString *)key {
int result = sqlite3_step(stmt);
if (result != SQLITE_ROW) {
if (_errorLogsEnabled) NSLog(@"%s line:%d sqlite query error (%d): %s", __FUNCTION__, __LINE__, result, sqlite3_errmsg(_db));
sqlite3_reset(stmt);
return -1;
}
return sqlite3_column_int(stmt, 0);
int count = sqlite3_column_int(stmt, 0);
sqlite3_reset(stmt);
return count;
}

- (int)_dbGetTotalItemSize {
Expand All @@ -593,9 +619,12 @@ - (int)_dbGetTotalItemSize {
int result = sqlite3_step(stmt);
if (result != SQLITE_ROW) {
if (_errorLogsEnabled) NSLog(@"%s line:%d sqlite query error (%d): %s", __FUNCTION__, __LINE__, result, sqlite3_errmsg(_db));
sqlite3_reset(stmt);
return -1;
}
return sqlite3_column_int(stmt, 0);
int size = sqlite3_column_int(stmt, 0);
sqlite3_reset(stmt);
return size;
}

- (int)_dbGetTotalItemCount {
Expand All @@ -607,7 +636,9 @@ - (int)_dbGetTotalItemCount {
if (_errorLogsEnabled) NSLog(@"%s line:%d sqlite query error (%d): %s", __FUNCTION__, __LINE__, result, sqlite3_errmsg(_db));
return -1;
}
return sqlite3_column_int(stmt, 0);
int count = sqlite3_column_int(stmt, 0);
sqlite3_reset(stmt);
return count;
}


Expand Down
3 changes: 2 additions & 1 deletion YYCache/YYMemoryCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,9 @@ - (id)objectForKey:(id)key {
node->_time = CACurrentMediaTime();
[_lru bringNodeToHead:node];
}
id result = node ? node->_value : nil;
pthread_mutex_unlock(&_lock);
return node ? node->_value : nil;
return result;
}

- (void)setObject:(id)object forKey:(id)key {
Expand Down