Commit 16cf83d3 authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Avoid inserting duplicate hosts by using an unique index

parent d968485f
......@@ -2,7 +2,7 @@
"formatVersion": 1,
"database": {
"version": 7,
"identityHash": "4aa62cae2ae68bc4d2a314f44806a334",
"identityHash": "7446a9cf3961a8a8d4beb8cde8f5c79c",
"entities": [
{
"tableName": "CachedResponse",
......@@ -163,12 +163,12 @@
},
{
"name": "index_DnsRule_host_type",
"unique": false,
"unique": true,
"columnNames": [
"host",
"type"
],
"createSql": "CREATE INDEX `index_DnsRule_host_type` ON `${TABLE_NAME}` (`host`, `type`)"
"createSql": "CREATE UNIQUE INDEX `index_DnsRule_host_type` ON `${TABLE_NAME}` (`host`, `type`)"
}
],
"foreignKeys": [
......@@ -227,7 +227,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, '4aa62cae2ae68bc4d2a314f44806a334')"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '7446a9cf3961a8a8d4beb8cde8f5c79c')"
]
}
}
\ No newline at end of file
......@@ -57,7 +57,7 @@ abstract class AppDatabase : RoomDatabase() {
execSQL("DROP INDEX `index_DnsRule_host_type`")
execSQL("CREATE INDEX `index_DnsRule_importedFrom` ON `DnsRule` (`importedFrom`)")
execSQL("CREATE INDEX `index_DnsRule_host_type` ON `DnsRule` (`host`, `type`)")
execSQL("CREATE UNIQUE INDEX `index_DnsRule_host_type` ON `DnsRule` (`host`, `type`)")
}
}
}
......
......@@ -54,13 +54,15 @@ private val MIGRATION_5_6 = migration(5, 6) {
it.execSQL("CREATE TABLE IF NOT EXISTS `DnsRule` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `stagingType` INTEGER, `type` INTEGER NOT NULL, `host` TEXT NOT NULL, `target` TEXT NOT NULL, `ipv6Target` TEXT, `importedFrom` INTEGER, FOREIGN KEY(`importedFrom`) REFERENCES `HostSource`(`id`) ON UPDATE NO ACTION ON DELETE NO ACTION )")
it.execSQL("CREATE TABLE IF NOT EXISTS `HostSource` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `enabled` INTEGER NOT NULL, `name` TEXT NOT NULL, `source` TEXT NOT NULL)")
it.execSQL("CREATE INDEX `index_DnsRule_importedFrom` ON `DnsRule` (`importedFrom`)")
it.execSQL("CREATE INDEX `index_DnsRule_host_type` ON `DnsRule` (`host`, `type`)")
it.execSQL("CREATE UNIQUE INDEX `index_DnsRule_host_type` ON `DnsRule` (`host`, `type`)")
Logger.logIfOpen("DB_MIGRATION", "Migration from 5 to 6 completed")
}
private val MIGRATION_6_7 = migration(6,7) {
Logger.logIfOpen("DB_MIGRATION", "Migrating from 6 to 7")
it.execSQL("DROP INDEX `index_DnsRule_host`")
it.execSQL("DROP INDEX `index_DnsRule_host_type`")
it.execSQL("CREATE UNIQUE INDEX `index_DnsRule_host_type` ON `DnsRule` (`host`, `type`)")
Logger.logIfOpen("DB_MIGRATION", "Migration from 6 to 7 completed")
}
......
......@@ -51,6 +51,9 @@ interface DnsRuleDao {
@Insert
fun insertAll(rules: Collection<DnsRule>)
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insertAllIgnoreConflict(rules: Collection<DnsRule>)
@Query("SELECT COUNT(*) FROM DnsRule")
fun getCount(): Long
......
......@@ -30,7 +30,7 @@ import org.minidns.record.Record
childColumns = arrayOf("importedFrom"),
onDelete = ForeignKey.NO_ACTION
)],
indices = [Index("importedFrom"), Index("host", "type")]
indices = [Index("importedFrom"), Index("host", "type", unique = true)]
)
@TypeConverters(DnsTypeConverter::class)
data class DnsRule(
......
......@@ -266,7 +266,7 @@ class RuleImportService : Service() {
parsers[it]!!.first
} ?: parsers.keys.first()]!!.second
if (hosts.size > ruleCommitSize || forceCommit) {
getDatabase().dnsRuleDao().insertAll(hosts)
getDatabase().dnsRuleDao().insertAllIgnoreConflict(hosts)
ruleCount += hosts.size
hosts.clear()
}
......
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