Refactoring I.
- Revamp whole DB to Display flow - Filter Pipeline, arbitrary filtering and sorting - Binary tree arrays for faster lookup & manipulation - DB: introducing custom functions - DB scheme: split req into heap & cache - cache written by GlassVPN only - heap written by Main App only - Introducing DB separation: DBCore, DBCommon, DBAppOnly - Introducing DB data sources: TestDataSource, GroupedDomainDataSource, RecordingsDB, DomainFilter - Background sync: Move entries from cache to heap and notify all observers - GlassVPN: Binary tree filter lookup - GlassVPN: Reusing prepared statement
This commit is contained in:
44
main/Data Source/SyncUpdate.swift
Normal file
44
main/Data Source/SyncUpdate.swift
Normal file
@@ -0,0 +1,44 @@
|
||||
import Foundation
|
||||
|
||||
class SyncUpdate {
|
||||
private var timer: Timer!
|
||||
private var paused: Int = 1 // first start() will decrement
|
||||
private(set) var tsEarliest: Timestamp
|
||||
|
||||
init(periodic interval: TimeInterval) {
|
||||
tsEarliest = Pref.DateFilter.lastXMinTimestamp() ?? 0
|
||||
NotifyDateFilterChanged.observe(call: #selector(didChangeDateFilter), on: self)
|
||||
timer = Timer.repeating(interval, call: #selector(periodicUpdate), on: self)
|
||||
}
|
||||
|
||||
@objc private func didChangeDateFilter() {
|
||||
let lastXFilter = Pref.DateFilter.lastXMinTimestamp() ?? 0
|
||||
if tsEarliest < lastXFilter {
|
||||
if let excess = AppDB?.dnsLogsRowRange(between: tsEarliest, and: lastXFilter) {
|
||||
NotifySyncRemove.post(excess)
|
||||
}
|
||||
} else if tsEarliest > lastXFilter {
|
||||
if let missing = AppDB?.dnsLogsRowRange(between: lastXFilter, and: tsEarliest) {
|
||||
NotifySyncInsert.post(missing)
|
||||
}
|
||||
}
|
||||
tsEarliest = lastXFilter
|
||||
}
|
||||
|
||||
func pause() { paused += 1 }
|
||||
func start() { if paused > 0 { paused -= 1 } }
|
||||
|
||||
@objc private func periodicUpdate() {
|
||||
guard paused == 0, let db = AppDB else { return }
|
||||
if let inserted = db.dnsLogsPersist() { // move cache -> heap
|
||||
NotifySyncInsert.post(inserted)
|
||||
}
|
||||
if let lastXFilter = Pref.DateFilter.lastXMinTimestamp(), tsEarliest < lastXFilter {
|
||||
if let removed = db.dnsLogsRowRange(between: tsEarliest, and: lastXFilter) {
|
||||
NotifySyncRemove.post(removed)
|
||||
}
|
||||
tsEarliest = lastXFilter
|
||||
}
|
||||
// TODO: periodic hard delete old logs (will reset rowids!)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user