Skip to content

Commit

Permalink
fix find by name
Browse files Browse the repository at this point in the history
  • Loading branch information
makeevrserg committed Sep 17, 2024
1 parent 0180598 commit edec7a7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.flipperdevices.ifrmvp.backend.db.signal.table.CategoryTable
import com.flipperdevices.ifrmvp.backend.db.signal.table.InfraredFileTable
import com.flipperdevices.ifrmvp.backend.db.signal.table.InfraredFileToSignalTable
import com.flipperdevices.ifrmvp.backend.db.signal.table.SignalKeyTable
import com.flipperdevices.ifrmvp.backend.db.signal.table.SignalNameAliasTable
import com.flipperdevices.ifrmvp.backend.db.signal.table.SignalTable
import com.flipperdevices.ifrmvp.backend.db.signal.table.UiPresetTable
import com.flipperdevices.ifrmvp.backend.envkonfig.model.DBConnection
Expand All @@ -32,6 +33,7 @@ internal class SignalDatabaseFactory(
CategoryTable,
InfraredFileTable,
InfraredFileToSignalTable,
SignalNameAliasTable,
SignalTable,
SignalKeyTable,
UiPresetTable,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.flipperdevices.ifrmvp.backend.db.signal.table

import org.jetbrains.exposed.dao.id.LongIdTable

object SignalNameAliasTable : LongIdTable("SIGNAL_NAME_ALIAS") {
val signalId = reference("signal_id", SignalTable)
val signalName = text("signal_name")

init {
uniqueIndex(signalId, signalName)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import org.jetbrains.exposed.dao.id.LongIdTable
object SignalTable : LongIdTable("SIGNAL_TABLE") {
val brandId = reference("brand_id", BrandTable)

val name = text("name")
val type = text("type")
val protocol = text("protocol").nullable()
val address = text("address").nullable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.flipperdevices.ifrmvp.backend.db.signal.table.CategoryTable
import com.flipperdevices.ifrmvp.backend.db.signal.table.InfraredFileTable
import com.flipperdevices.ifrmvp.backend.db.signal.table.InfraredFileToSignalTable
import com.flipperdevices.ifrmvp.backend.db.signal.table.SignalKeyTable
import com.flipperdevices.ifrmvp.backend.db.signal.table.SignalNameAliasTable
import com.flipperdevices.ifrmvp.backend.db.signal.table.SignalTable
import com.flipperdevices.ifrmvp.backend.db.signal.table.UiPresetTable
import com.flipperdevices.ifrmvp.model.IfrKeyIdentifier
Expand All @@ -17,7 +18,10 @@ import com.flipperdevices.infrared.editor.model.InfraredRemote
import com.flipperdevices.infrared.editor.util.InfraredMapper
import com.flipperdevices.infrared.editor.viewmodel.InfraredKeyParser
import java.io.File
import kotlin.math.sign
import kotlin.time.measureTime
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.async
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.JoinType
import org.jetbrains.exposed.sql.andWhere
Expand Down Expand Up @@ -103,7 +107,6 @@ internal class FillerController(private val database: Database) : CoroutineScope
val parsedRemote = remote as? InfraredRemote.Parsed
val rawRemote = remote as? InfraredRemote.Raw
this[SignalTable.brandId] = brandId
this[SignalTable.name] = remote.name
this[SignalTable.type] = remote.type
this[SignalTable.protocol] = parsedRemote?.protocol
this[SignalTable.address] = parsedRemote?.address
Expand Down Expand Up @@ -143,6 +146,12 @@ internal class FillerController(private val database: Database) : CoroutineScope
""".trimIndent()
)
}

SignalNameAliasTable.batchInsert(signalIds.zip(signals), ignore = true) {
this[SignalNameAliasTable.signalName] = it.second.name
this[SignalNameAliasTable.signalId] = it.first.value
}

InfraredFileToSignalTable.batchInsert(signalIds) {
this[InfraredFileToSignalTable.infraredFileId] = irFileId
this[InfraredFileToSignalTable.signalId] = it
Expand All @@ -164,7 +173,10 @@ internal class FillerController(private val database: Database) : CoroutineScope
this[SignalKeyTable.type] = IfrKeyIdentifier.Empty.TYPE
}

is IfrKeyIdentifier.Name -> error("Identifying by name is not possible!")
is IfrKeyIdentifier.Name -> {
this[SignalKeyTable.remoteKeyName] = keyIdentifier.name
this[SignalKeyTable.type] = IfrKeyIdentifier.Sha256.TYPE
}

is IfrKeyIdentifier.Sha256 -> {
this[SignalKeyTable.remoteKeyName] = keyIdentifier.name
Expand All @@ -180,6 +192,12 @@ internal class FillerController(private val database: Database) : CoroutineScope
onColumn = SignalTable.id,
otherColumn = InfraredFileToSignalTable.signalId
)
.join(
otherTable = SignalNameAliasTable,
joinType = JoinType.LEFT,
onColumn = SignalTable.id,
otherColumn = SignalNameAliasTable.signalId
)
.select(SignalTable.id)
.where { SignalTable.brandId eq brandId }
.andWhere { InfraredFileToSignalTable.infraredFileId eq irFileId }
Expand All @@ -191,7 +209,9 @@ internal class FillerController(private val database: Database) : CoroutineScope
andWhere { SignalTable.hash eq keyIdentifier.hash }
}

is IfrKeyIdentifier.Name -> error("Identifying by name is not possible!")
is IfrKeyIdentifier.Name -> {
andWhere { SignalNameAliasTable.signalName eq keyIdentifier.name }
}
}
}
.map { it[SignalTable.id] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.flipperdevices.ifrmvp.backend.db.signal.exception.TableDaoException
import com.flipperdevices.ifrmvp.backend.db.signal.table.InfraredFileTable
import com.flipperdevices.ifrmvp.backend.db.signal.table.InfraredFileToSignalTable
import com.flipperdevices.ifrmvp.backend.db.signal.table.SignalKeyTable
import com.flipperdevices.ifrmvp.backend.db.signal.table.SignalNameAliasTable
import com.flipperdevices.ifrmvp.backend.db.signal.table.SignalTable
import com.flipperdevices.ifrmvp.backend.model.BrandModel
import com.flipperdevices.ifrmvp.backend.model.CategoryConfiguration
Expand Down Expand Up @@ -196,7 +197,7 @@ internal class SignalRouteRegistry(
SignalModel(
id = it[SignalTable.id].value,
remote = SignalModel.FlipperRemote(
name = it[SignalTable.name],
name = "empty",
type = it[SignalTable.type],
protocol = it[SignalTable.protocol],
address = it[SignalTable.address],
Expand Down Expand Up @@ -238,13 +239,13 @@ internal class SignalRouteRegistry(
}

val skippedKeys = transaction(database) {
SignalTable
SignalNameAliasTable
.selectAll()
.where { SignalTable.id inList signalRequestModel.skippedResults.map(SignalRequestModel.SignalResultData::signalId) }
.where { SignalNameAliasTable.id inList signalRequestModel.skippedResults.map(SignalRequestModel.SignalResultData::signalId) }
.mapNotNull {
val keyName = it[SignalTable.name]
val keyName = it[SignalNameAliasTable.signalName]
AnyDeviceKeyNamesProvider.getKey(keyName)
}
}.distinct()
}

val signalModel = getSignalModel(
Expand Down

0 comments on commit edec7a7

Please sign in to comment.