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:
51
main/Data Source/DomainFilter.swift
Normal file
51
main/Data Source/DomainFilter.swift
Normal file
@@ -0,0 +1,51 @@
|
||||
import Foundation
|
||||
|
||||
enum DomainFilter {
|
||||
static private var data: [String: FilterOptions] = {
|
||||
AppDB?.loadFilters() ?? [:]
|
||||
}()
|
||||
|
||||
/// Get filter with given `domain` name
|
||||
@inline(__always) static subscript(_ domain: String) -> FilterOptions? {
|
||||
data[domain]
|
||||
}
|
||||
|
||||
/// Update local memory object by loading values from persistent db.
|
||||
/// - Note: Will trigger `NotifyDNSFilterChanged` notification.
|
||||
static func reload() {
|
||||
data = AppDB?.loadFilters() ?? [:]
|
||||
NotifyDNSFilterChanged.post()
|
||||
}
|
||||
|
||||
/// Get list of domains (sorted by name) which do contain the given filter
|
||||
static func list(where matching: FilterOptions) -> [String] {
|
||||
data.compactMap { $1.contains(matching) ? $0 : nil }.sorted()
|
||||
}
|
||||
|
||||
/// Get total number of blocked and ignored domains. Shown in settings overview.
|
||||
static func counts() -> (blocked: Int, ignored: Int) {
|
||||
data.reduce(into: (0, 0)) {
|
||||
if $1.1.contains(.blocked) { $0.0 += 1 }
|
||||
if $1.1.contains(.ignored) { $0.1 += 1 } }
|
||||
}
|
||||
|
||||
/// Union `filter` with set.
|
||||
/// - Note: Will trigger `NotifyDNSFilterChanged` notification.
|
||||
static func update(_ domain: String, add filter: FilterOptions) {
|
||||
update(domain, set: (data[domain] ?? FilterOptions()).union(filter))
|
||||
}
|
||||
|
||||
/// Subtract `filter` from set.
|
||||
/// - Note: Will trigger `NotifyDNSFilterChanged` notification.
|
||||
static func update(_ domain: String, remove filter: FilterOptions) {
|
||||
update(domain, set: data[domain]?.subtracting(filter))
|
||||
}
|
||||
|
||||
/// Update persistent db, local memory object, and post notification to subscribers
|
||||
/// - Parameter set: Remove a filter with `nil` or `.none`
|
||||
static private func update(_ domain: String, set: FilterOptions?) {
|
||||
AppDB?.setFilter(domain, set)
|
||||
data[domain] = (set == FilterOptions.none) ? nil : set
|
||||
NotifyDNSFilterChanged.post(domain)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user