Skip to content

Commit

Permalink
FieldLookup now appends descriptor instead of a property map.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralph Gasser committed Mar 27, 2024
1 parent 20cc3fd commit 278b79f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,31 @@ import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.toList
import org.vitrivr.engine.core.database.descriptor.DescriptorReader
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
*/
class FieldLookup(
override val input: Operator<Retrieved>,
private val reader: DescriptorReader<*>,
private val keys: Collection<String>

) : Transformer {
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()

val ids = inputRetrieved.map { it.id }.toSet()

if (ids.isEmpty()) {
return@flow
}

val descriptors =
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!! }
inputRetrieved.forEach { retrieved ->

val descriptor = descriptors[retrieved.id]

if (descriptor != null) {
val values = descriptor.values().toMap().mapValues { it.toString() }
val attribute = PropertyAttribute(values)
retrieved.addAttribute(attribute)
retrieved.addAttribute(DescriptorAttribute(descriptor))
}

emit(retrieved)

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,11 @@ import org.vitrivr.engine.core.model.retrievable.Retrieved
import org.vitrivr.engine.core.operators.Operator
import org.vitrivr.engine.core.operators.retrieve.TransformerFactory

class FieldLookupFactory(

) : TransformerFactory {
override fun newTransformer(
input: Operator<Retrieved>,
schema: Schema,
properties: Map<String, String>
): FieldLookup {

class FieldLookupFactory() : TransformerFactory {
override fun newTransformer(input: Operator<Retrieved>, schema: Schema, properties: Map<String, String>): FieldLookup {
val keys = properties["keys"]?.split(",")?.map { s -> s.trim() } ?: emptyList()
val field = properties["field"] ?: throw IllegalArgumentException("expected 'field' to be defined in properties")
val reader = (schema[field] ?: throw IllegalArgumentException("Field '$field' not defined in schema")).getReader()

return FieldLookup(
input, reader, keys
)
return FieldLookup(input, reader)
}
}

0 comments on commit 278b79f

Please sign in to comment.