Commit b58872a8 authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Imported Dns rules shouldn't be deleted/inserted instantly. Instead a staging...

Imported Dns rules shouldn't be deleted/inserted instantly. Instead a staging type is now used which tracks whether the rule should be deleted at the end or if the job is aborted
parent 8a01b2f7
......@@ -2,7 +2,7 @@
"formatVersion": 1,
"database": {
"version": 6,
"identityHash": "4ca420a8d5d5420e65d499a53e6f16b1",
"identityHash": "b08f5f3d3a88161f54026fa1e88d9a93",
"entities": [
{
"tableName": "CachedResponse",
......@@ -101,7 +101,7 @@
},
{
"tableName": "DnsRule",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `type` INTEGER NOT NULL, `host` TEXT NOT NULL, `target` TEXT NOT NULL, `importedFrom` INTEGER, FOREIGN KEY(`importedFrom`) REFERENCES `HostSource`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `stagingType` INTEGER, `type` INTEGER NOT NULL, `host` TEXT NOT NULL, `target` TEXT NOT NULL, `importedFrom` INTEGER, FOREIGN KEY(`importedFrom`) REFERENCES `HostSource`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )",
"fields": [
{
"fieldPath": "id",
......@@ -109,6 +109,12 @@
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "stagingType",
"columnName": "stagingType",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "type",
"columnName": "type",
......@@ -158,12 +164,13 @@
"createSql": "CREATE INDEX `index_DnsRule_host` ON `${TABLE_NAME}` (`host`)"
},
{
"name": "index_DnsRule_host_isIpv6",
"name": "index_DnsRule_host_type",
"unique": false,
"columnNames": [
"host"
"host",
"type"
],
"createSql": "CREATE INDEX `index_DnsRule_host_isIpv6` ON `${TABLE_NAME}` (`host`)"
"createSql": "CREATE INDEX `index_DnsRule_host_type` ON `${TABLE_NAME}` (`host`, `type`)"
}
],
"foreignKeys": [
......@@ -222,7 +229,7 @@
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '4ca420a8d5d5420e65d499a53e6f16b1')"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'b08f5f3d3a88161f54026fa1e88d9a93')"
]
}
}
\ No newline at end of file
......@@ -51,7 +51,7 @@ private val MIGRATION_4_5 = migration(4, 5) {
}
private val MIGRATION_5_6 = migration(5, 6) {
Logger.logIfOpen("DB_MIGRATION", "Migrating from 5 to 6")
it.execSQL("CREATE TABLE IF NOT EXISTS `DnsRule` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `type` INTEGER NOT NULL, `host` TEXT NOT NULL, `target` TEXT NOT NULL, `importedFrom` INTEGER, FOREIGN KEY(`importedFrom`) REFERENCES `HostSource`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )")
it.execSQL("CREATE TABLE IF NOT EXISTS `DnsRule` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `type` INTEGER NOT NULL, `host` TEXT NOT NULL, `target` TEXT NOT NULL, `importedFrom` INTEGER, `stagingType` INTEGER, FOREIGN KEY(`importedFrom`) REFERENCES `HostSource`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )")
it.execSQL("CREATE TABLE IF NOT EXISTS `HostSource` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT NOT NULL, `source` TEXT NOT NULL, `enabled` INTEGER NOT NULL)")
it.execSQL("CREATE INDEX `index_DnsRule_importedFrom` ON `DnsRule` (`importedFrom`)")
it.execSQL("CREATE INDEX `index_DnsRule_host` ON `DnsRule` (`host`)")
......
......@@ -29,8 +29,17 @@ interface DnsRuleDao {
@Query("DELETE FROM DnsRule")
fun deleteAll()
@Query("DELETE FROM DnsRule WHERE importedFrom IS NOT NULL")
fun deleteAllExceptUserRules()
@Query("UPDATE DnsRule SET stagingType=1 WHERE importedFrom IS NOT NULL")
fun markNonUserRulesForDeletion()
@Query("DELETE FROM DnsRule WHERE stagingType=1")
fun deleteMarkedRules()
@Query("DELETE FROM DnsRule WHERE stagingType=2")
fun deleteStagedRules()
@Query("UPDATE DnsRule SET stagingType=NULL")
fun commitStaging()
@Insert
fun insertAll(rules:Collection<DnsRule>)
......
......@@ -40,4 +40,9 @@ data class DnsRule(
val importedFrom: Long? = null
) {
@PrimaryKey(autoGenerate = true) var id: Long = 0
// Null = none
// 1 = marked delete
// 2 = marked insert
var stagingType:Int? = null
}
\ No newline at end of file
......@@ -57,13 +57,23 @@ class RuleImportService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if(intent != null && intent.hasExtra("abort")) {
importJob?.cancel()
abortImport()
}
createNotification()
startWork()
return START_STICKY
}
private fun abortImport() {
importJob?.let {
it.cancel()
val dnsRuleDao = getDatabase().dnsRuleDao()
dnsRuleDao.deleteStagedRules()
dnsRuleDao.commitStaging()
}
importJob = null
}
private fun createNotification() {
if (notification == null) {
notification = NotificationCompat.Builder(this, Notifications.servicePersistentNotificationChannel(this))
......@@ -99,7 +109,8 @@ class RuleImportService : Service() {
private fun startWork() {
importJob = GlobalScope.launch {
getDatabase().dnsRuleDao().deleteAllExceptUserRules()
val dnsRuleDao = getDatabase().dnsRuleDao()
dnsRuleDao.markNonUserRulesForDeletion()
var count = 0
val maxCount = getDatabase().hostSourceDao().getEnabledCount()
getDatabase().hostSourceDao().getAllEnabled().forEach {
......@@ -117,6 +128,10 @@ class RuleImportService : Service() {
}
}
}
if(importJob != null && importJob?.isCancelled == false) {
dnsRuleDao.deleteMarkedRules()
dnsRuleDao.commitStaging()
}
importJob = null
stopForeground(true)
stopSelf()
......@@ -187,7 +202,7 @@ class RuleImportService : Service() {
override fun onDestroy() {
super.onDestroy()
importJob?.cancel()
abortImport()
}
override fun onBind(intent: Intent?): IBinder? {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment