Skip to content

Commit f874dd0

Browse files
committed
Fix WAL expansion issue
ibireme/YYCache#163
1 parent ef9755b commit f874dd0

1 file changed

Lines changed: 43 additions & 8 deletions

File tree

‎Sources/RECache/KVStorage.swift‎

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,10 @@ public final class KVStorage {
781781
private func dbCheckpoint() {
782782
if !dbCheck() { return }
783783
// Cause a checkpoint to occur, merge `sqlite-wal` file to `sqlite` file.
784-
sqlite3_wal_checkpoint(db, nil)
784+
let result = sqlite3_wal_checkpoint(db, nil)
785+
if result != SQLITE_OK && errorLogsEnabled {
786+
NSLog("\(#function) line:\(#line) sqlite WAL checkpoint error (\(result))")
787+
}
785788
}
786789

787790
@discardableResult
@@ -813,7 +816,14 @@ public final class KVStorage {
813816
}
814817
dbStmtCache?[sql] = stmt
815818
} else {
816-
sqlite3_reset(stmt)
819+
if sqlite3_stmt_busy(stmt) != 0 {
820+
// just in case someone will forget to sqlite3_reset cached statement
821+
// causing WAL file lock
822+
if errorLogsEnabled {
823+
NSLog("\(#function) line:\(#line) WARN: cached statement for query \"\(sql)\" was not reset.")
824+
}
825+
sqlite3_reset(stmt)
826+
}
817827
}
818828
return stmt
819829
}
@@ -867,6 +877,7 @@ public final class KVStorage {
867877
}
868878

869879
let result = sqlite3_step(stmt)
880+
sqlite3_reset(stmt)
870881
if result != SQLITE_DONE {
871882
if errorLogsEnabled {
872883
NSLog("\(#function) line:\(#line) sqlite insert error (\(result)): \(sqlite3_errmsg(db).flatMap { String(cString: $0) } ?? "")")
@@ -883,6 +894,7 @@ public final class KVStorage {
883894
sqlite3_bind_int(stmt, 1, Int32(time(nil)))
884895
_ = key.withCString { sqlite3_bind_text(stmt, 2, $0, -1, SQLITE_TRANSIENT) }
885896
let result = sqlite3_step(stmt)
897+
sqlite3_reset(stmt)
886898
if result != SQLITE_DONE {
887899
if errorLogsEnabled {
888900
NSLog("\(#function) line:\(#line) sqlite update error (\(result)): \(sqlite3_errmsg(db).flatMap { String(cString: $0) } ?? "")")
@@ -926,6 +938,7 @@ public final class KVStorage {
926938
_ = key.withCString { sqlite3_bind_text(stmt, 1, $0, -1, SQLITE_TRANSIENT) }
927939

928940
let result = sqlite3_step(stmt)
941+
sqlite3_reset(stmt)
929942
if result != SQLITE_DONE {
930943
if errorLogsEnabled {
931944
NSLog("\(#function) line:\(#line) db delete error (\(result)): \(sqlite3_errmsg(db).flatMap { String(cString: $0) } ?? "")")
@@ -966,6 +979,7 @@ public final class KVStorage {
966979
guard let stmt = dbPrepareStmt(sql) else { return false }
967980
sqlite3_bind_int(stmt, 1, size)
968981
let result = sqlite3_step(stmt)
982+
sqlite3_reset(stmt)
969983
if result != SQLITE_DONE {
970984
if errorLogsEnabled {
971985
NSLog("\(#function) line:\(#line) sqlite delete error (\(result)): \(sqlite3_errmsg(db).flatMap { String(cString: $0) } ?? "")")
@@ -981,6 +995,7 @@ public final class KVStorage {
981995
guard let stmt = dbPrepareStmt(sql) else { return false }
982996
sqlite3_bind_int(stmt, 1, time)
983997
let result = sqlite3_step(stmt)
998+
sqlite3_reset(stmt)
984999
if result != SQLITE_DONE {
9851000
if errorLogsEnabled {
9861001
NSLog("\(#function) line:\(#line) sqlite delete error (\(result)): \(sqlite3_errmsg(db).flatMap { String(cString: $0) } ?? "")")
@@ -1031,16 +1046,18 @@ public final class KVStorage {
10311046
_ = key.withCString { sqlite3_bind_text(stmt, 1, $0, -1, SQLITE_TRANSIENT) }
10321047

10331048
let result = sqlite3_step(stmt)
1049+
var item: KVStorageItem?
10341050
if result == SQLITE_ROW {
1035-
return dbGetItem(fromStmt: stmt, excludeInlineData: excludeInlineData)
1051+
item = dbGetItem(fromStmt: stmt, excludeInlineData: excludeInlineData)
10361052
} else {
10371053
if result != SQLITE_DONE {
10381054
if errorLogsEnabled {
10391055
NSLog("\(#function) line:\(#line) sqlite query error (\(result)): \(sqlite3_errmsg(db).flatMap { String(cString: $0) } ?? "")")
10401056
}
10411057
}
1042-
return nil
10431058
}
1059+
sqlite3_reset(stmt)
1060+
return item
10441061
}
10451062

10461063
private func dbGetItem(withKeys keys: [String], excludeInlineData: Bool) -> [KVStorageItem]? {
@@ -1091,6 +1108,7 @@ public final class KVStorage {
10911108
if result == SQLITE_ROW {
10921109
let inlineData = sqlite3_column_blob(stmt, 0)
10931110
let inlineDataBytes = sqlite3_column_bytes(stmt, 0)
1111+
sqlite3_reset(stmt)
10941112
if inlineData == nil || inlineDataBytes <= 0 { return nil }
10951113
return Data(bytes: inlineData!, count: Int(inlineDataBytes))
10961114
} else {
@@ -1099,6 +1117,7 @@ public final class KVStorage {
10991117
NSLog("\(#function) line:\(#line) sqlite query error (\(result)): \(sqlite3_errmsg(db).flatMap { String(cString: $0) } ?? "")")
11001118
}
11011119
}
1120+
sqlite3_reset(stmt)
11021121
return nil
11031122
}
11041123
}
@@ -1111,7 +1130,10 @@ public final class KVStorage {
11111130
if result == SQLITE_ROW {
11121131
if let filename = sqlite3_column_text(stmt, 0) {
11131132
let name = String(cString: filename)
1114-
if !name.isEmpty { return name }
1133+
if !name.isEmpty {
1134+
sqlite3_reset(stmt)
1135+
return name
1136+
}
11151137
}
11161138
} else {
11171139
if result != SQLITE_DONE {
@@ -1120,6 +1142,7 @@ public final class KVStorage {
11201142
}
11211143
}
11221144
}
1145+
sqlite3_reset(stmt)
11231146
return nil
11241147
}
11251148

@@ -1185,6 +1208,7 @@ public final class KVStorage {
11851208
break
11861209
}
11871210
}
1211+
sqlite3_reset(stmt)
11881212
return filenames
11891213
}
11901214

@@ -1213,6 +1237,7 @@ public final class KVStorage {
12131237
break
12141238
}
12151239
}
1240+
sqlite3_reset(stmt)
12161241
return filenames
12171242
}
12181243

@@ -1248,6 +1273,7 @@ public final class KVStorage {
12481273
break
12491274
}
12501275
}
1276+
sqlite3_reset(stmt)
12511277
return items
12521278
}
12531279

@@ -1260,9 +1286,12 @@ public final class KVStorage {
12601286
if errorLogsEnabled {
12611287
NSLog("\(#function) line:\(#line) sqlite query error (\(result)): \(sqlite3_errmsg(db).flatMap { String(cString: $0) } ?? "")")
12621288
}
1289+
sqlite3_reset(stmt)
12631290
return -1
12641291
}
1265-
return sqlite3_column_int(stmt, 0)
1292+
let count = sqlite3_column_int(stmt, 0)
1293+
sqlite3_reset(stmt)
1294+
return count
12661295
}
12671296

12681297
private func dbGetTotalItemSize() -> Int32 {
@@ -1273,9 +1302,12 @@ public final class KVStorage {
12731302
if errorLogsEnabled {
12741303
NSLog("\(#function) line:\(#line) sqlite query error (\(result)): \(sqlite3_errmsg(db).flatMap { String(cString: $0) } ?? "")")
12751304
}
1305+
sqlite3_reset(stmt)
12761306
return -1
12771307
}
1278-
return sqlite3_column_int(stmt, 0)
1308+
let size = sqlite3_column_int(stmt, 0)
1309+
sqlite3_reset(stmt)
1310+
return size
12791311
}
12801312

12811313
private func dbGetTotalItemCount() -> Int32 {
@@ -1286,9 +1318,12 @@ public final class KVStorage {
12861318
if errorLogsEnabled {
12871319
NSLog("\(#function) line:\(#line) sqlite query error (\(result)): \(sqlite3_errmsg(db).flatMap { String(cString: $0) } ?? "")")
12881320
}
1321+
sqlite3_reset(stmt)
12891322
return -1
12901323
}
1291-
return sqlite3_column_int(stmt, 0)
1324+
let count = sqlite3_column_int(stmt, 0)
1325+
sqlite3_reset(stmt)
1326+
return count
12921327
}
12931328

12941329
// MARK: - file

0 commit comments

Comments
 (0)