Remove no-longer-used vacuumIfNeeded.

This commit is contained in:
Brent Simmons committed 2026-04-29 16:35:06 -07:00
1 parent b64de0cd6e
commit 0c4fd3ed2d
2 files changed
+6 -67

No files matched your search

@@ -8,7 +8,6 @@
import Foundation
import os
import os
import SQLite3
import RSDatabaseObjC
@@ -163,42 +162,15 @@ public final class DatabaseQueue: Sendable {
/// Compact the database. This should be done from time to time —
/// weekly-ish? — to keep up the performance level of a database.
/// Generally a thing to do at startup, if it’s been a while
/// since the last vacuum() call. You almost certainly want to call
/// vacuumIfNeeded instead.
/// since the last vacuum() call.
public func vacuum() {
runInDatabase { result in
Self.logger.debug("DatabaseQueue: vacuum")
guard let database = try? result.get() else {
return
}
database.executeStatements("vacuum;")
database.vacuum()
}
}
/// Vacuum the database if it’s been more than `daysBetweenVacuums` since the last vacuum.
/// Normally you would call this right after initing a DatabaseQueue.
///
/// - Returns: true if database will be vacuumed.
@discardableResult
public func vacuumIfNeeded(daysBetweenVacuums: Int) -> Bool {
let defaultsKey = "DatabaseQueue-LastVacuumDate-\(databasePath)"
let minimumVacuumInterval = TimeInterval(daysBetweenVacuums * (60 * 60 * 24)) // Doesn’t have to be precise
let now = Date()
let cutoffDate = now - minimumVacuumInterval
if let lastVacuumDate = UserDefaults.standard.object(forKey: defaultsKey) as? Date {
if lastVacuumDate < cutoffDate {
vacuum()
UserDefaults.standard.set(now, forKey: defaultsKey)
return true
}
return false
}
// Never vacuumed — almost certainly a new database.
// Just set the LastVacuumDate pref to now and skip vacuuming.
UserDefaults.standard.set(now, forKey: defaultsKey)
return false
}
}
private extension DatabaseQueue {
@@ -30,47 +30,14 @@ public extension FMDatabase {
commit()
}
/// Vacuum the database if it’s been more than `daysBetweenVacuums` since the last vacuum.
/// Call this as part of an init method.
func vacuumIfNeeded(daysBetweenVacuums: Int, filepath: String) {
let defaultsKey = "FMDatabase-LastVacuumDate-\(filepath)"
let now = Date()
guard let lastVacuumDate = UserDefaults.standard.object(forKey: defaultsKey) as? Date else {
// Never vacuumed — probably a new database.
// Set the LastVacuumDate pref to now and skip vacuuming.
UserDefaults.standard.set(now, forKey: defaultsKey)
return
}
let minimumVacuumInterval = TimeInterval(daysBetweenVacuums * (60 * 60 * 24)) // Doesn’t have to be precise
let cutoffDate = now - minimumVacuumInterval
if lastVacuumDate < cutoffDate {
vacuum()
UserDefaults.standard.set(now, forKey: defaultsKey)
}
}
private static let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "FMDatabase")
func vacuum() {
let shortName: String
if let path = databasePath() {
let components = (path as NSString).pathComponents
let count = components.count
if count >= 2 {
shortName = components[count - 2] + "/" + components[count - 1]
} else {
shortName = components.last ?? path
}
} else {
shortName = "unknown"
}
let start = CFAbsoluteTimeGetCurrent()
let path = databasePath() ?? "unknown"
let start = Date()
executeStatements("vacuum;")
let duration = CFAbsoluteTimeGetCurrent() - start
Self.logger.debug("VACUUM \(shortName) took \(duration, format: .fixed(precision: 4)) seconds")
let duration = Date().timeIntervalSince(start)
Self.logger.debug("VACUUM \(path, privacy: .public) took \(duration, format: .fixed(precision: 4), privacy: .public) seconds")
}
func runCreateStatements(_ statements: String) {