Persist logs by renaming table (hopefully reduces lock time)

This commit is contained in:
relikd
2020-07-27 21:16:14 +02:00
parent b774e2152c
commit b1d49c6765
2 changed files with 13 additions and 11 deletions

View File

@@ -33,11 +33,9 @@ extension SQLiteDatabase {
do {
try run(sql: "SELECT 1 FROM req LIMIT 1;") // fails if req doesnt exist
createFunction("domainof") { ($0.first as! String).extractDomain() }
try run(sql: """
BEGIN TRANSACTION;
transaction("""
INSERT INTO heap(ts,fqdn,domain,opt) SELECT ts,domain,domainof(domain),nullif(logOpt,0) FROM req;
DROP TABLE req;
COMMIT;
""")
} catch { /* no need to migrate */ }
}
@@ -119,13 +117,12 @@ extension SQLiteDatabase {
/// - Returns: `nil` in case no entries were transmitted.
@discardableResult func dnsLogsPersist() -> SQLiteRowRange? {
guard lastRowId(.cache) > 0 else { return nil }
transaction("ALTER TABLE cache RENAME TO tmp_cache; \(CreateTable.cache)")
let before = lastRowId(.heap) + 1
createFunction("domainof") { ($0.first as! String).extractDomain() }
try? run(sql:"""
BEGIN TRANSACTION;
INSERT INTO heap(ts,fqdn,domain,opt) SELECT ts,dns,domainof(dns),nullif(opt&1,0) FROM cache;
DELETE FROM cache;
COMMIT;
transaction("""
INSERT INTO heap(ts,fqdn,domain,opt) SELECT ts,dns,domainof(dns),nullif(opt&1,0) FROM tmp_cache;
DROP TABLE tmp_cache;
""")
let after = lastRowId(.heap)
return (before > after) ? nil : (before, after)

View File

@@ -91,15 +91,20 @@ class SQLiteDatabase {
}
}
/// `BEGIN TRANSACTION; \(sql); COMMIT;` on exception rollback.
func transaction(_ sql: String) {
do { try run(sql: "BEGIN TRANSACTION; \(sql); COMMIT;") }
catch { rollback() }
}
func ifStep(_ stmt: OpaquePointer, _ expected: Int32) throws {
guard sqlite3_step(stmt) == expected else {
throw SQLiteError.Step(message: errorMessage)
}
}
func vacuum() {
try? run(sql: "VACUUM;")
}
func vacuum() { NSLog("[SQL] VACUUM"); try? run(sql: "VACUUM;"); }
func rollback() { NSLog("[SQL] ROLLBACK"); try? run(sql: "ROLLBACK;"); }
}