From 0c4fd3ed2ddc44147c495d4f3aebbf3864ee3e3c Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Wed, 29 Apr 2026 16:35:06 -0700 Subject: [PATCH] Remove no-longer-used vacuumIfNeeded. --- .../Sources/RSDatabase/DatabaseQueue.swift | 32 +-------------- .../RSDatabase/FMDatabase+Extras.swift | 41 ++----------------- 2 files changed, 6 insertions(+), 67 deletions(-) diff --git a/Modules/RSDatabase/Sources/RSDatabase/DatabaseQueue.swift b/Modules/RSDatabase/Sources/RSDatabase/DatabaseQueue.swift index 40e558fd5..e2b6e33bc 100644 --- a/Modules/RSDatabase/Sources/RSDatabase/DatabaseQueue.swift +++ b/Modules/RSDatabase/Sources/RSDatabase/DatabaseQueue.swift @@ -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 { diff --git a/Modules/RSDatabase/Sources/RSDatabase/FMDatabase+Extras.swift b/Modules/RSDatabase/Sources/RSDatabase/FMDatabase+Extras.swift index 0de956bdf..c192c449b 100644 --- a/Modules/RSDatabase/Sources/RSDatabase/FMDatabase+Extras.swift +++ b/Modules/RSDatabase/Sources/RSDatabase/FMDatabase+Extras.swift @@ -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) {