Skip to content

Commit

Permalink
Minor optimisation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralph Gasser committed Apr 3, 2024
1 parent 0b3ec88 commit 53f9b0d
Showing 1 changed file with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,33 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.toList
import org.vitrivr.engine.core.database.descriptor.DescriptorReader
import org.vitrivr.engine.core.model.metamodel.Schema
import org.vitrivr.engine.core.model.retrievable.Retrieved
import org.vitrivr.engine.core.model.retrievable.attributes.DescriptorAttribute
import org.vitrivr.engine.core.model.retrievable.attributes.PropertyAttribute
import org.vitrivr.engine.core.operators.Operator
import org.vitrivr.engine.core.operators.retrieve.Transformer

/**
* Appends stringified key-value pairs to a [Retrieved] based on the values of a [Field], if available
* Appends [DescriptorAttribute] to a [Retrieved] based on the values of a [Schema.Field], if available.
*
* @version 1.1.0
* @author Luca Rossetto
* @author Ralph Gasser
*/
class FieldLookup(override val input: Operator<Retrieved>, private val reader: DescriptorReader<*>) : Transformer {
override fun toFlow(scope: CoroutineScope): Flow<Retrieved> = flow {
/* Parse input IDs.*/
val inputRetrieved = input.toFlow(scope).toList()

/* Fetch entries for the provided IDs. */
val ids = inputRetrieved.map { it.id }.toSet()
if (ids.isEmpty()) {
return@flow
val descriptors = if (ids.isEmpty()) {
emptyMap()
} else {
this@FieldLookup.reader.getAllBy(ids, "retrievableId").filter { it.retrievableId != null }.associateBy { it.retrievableId!! }
}

/* Perform lookup. */
val descriptors = reader.getAllBy(ids, "retrievableId").filter { it.retrievableId != null }.associateBy { it.retrievableId!! }
/* Emit retrievable with added attribute. */
inputRetrieved.forEach { retrieved ->
val descriptor = descriptors[retrieved.id]
if (descriptor != null) {
Expand Down

0 comments on commit 53f9b0d

Please sign in to comment.