Skip to content

Commit

Permalink
Adds a ObjectFieldLookup that facilitates lookup of fields for Retrie…
Browse files Browse the repository at this point in the history
…vables that are in on 'object' relation to the core retrievable.
  • Loading branch information
Ralph Gasser committed Apr 3, 2024
1 parent e5718c6 commit 64fb907
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.vitrivr.engine.query.transform
package org.vitrivr.engine.query.transform.lookup

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.vitrivr.engine.query.transform
package org.vitrivr.engine.query.transform.lookup

import org.vitrivr.engine.core.model.metamodel.Schema
import org.vitrivr.engine.core.model.retrievable.Retrieved
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.vitrivr.engine.query.transform.lookup

import kotlinx.coroutines.CoroutineScope
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.Relationship
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.RelationshipAttribute
import org.vitrivr.engine.core.operators.Operator
import org.vitrivr.engine.core.operators.retrieve.Transformer

/**
* Appends [DescriptorAttribute] to a [Retrieved] in a specified object [Relationship] based on lookup values of a [Schema.Field], if available.
*
* @version 1.0.0
* @author Luca Rossetto
* @author Ralph Gasser
*/
class ObjectFieldLookup(override val input: Operator<Retrieved>, private val reader: DescriptorReader<*>, private val predicates: Set<String>) : Transformer {
override fun toFlow(scope: CoroutineScope): Flow<Retrieved> = flow {
/* Parse input IDs.*/
val inputRetrieved = input.toFlow(scope).toList()

/* Fetch Retrievable entries that should be enriched. */
val enrich = inputRetrieved.mapNotNull { it.filteredAttribute(RelationshipAttribute::class.java) }.flatMap { r ->
r.relationships.filter { this@ObjectFieldLookup.predicates.isEmpty() || it.pred in this@ObjectFieldLookup.predicates }.mapNotNull { it.obj.second }
}
val ids = enrich.map { it.id }
val descriptors = if (ids.isNotEmpty()) {
this@ObjectFieldLookup.reader.getAllBy(ids, "retrievableId").filter { it.retrievableId != null }.associateBy { it.retrievableId!! }
} else {
emptyMap()
}

/* Emit retrievable with added attribute. */
enrich.forEach {
val descriptor = descriptors[it.id]
if (descriptor != null) {
it.addAttribute(DescriptorAttribute(descriptor))
}
}

/* Emit input. */
inputRetrieved.forEach { emit(it) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.vitrivr.engine.query.transform.lookup

import org.vitrivr.engine.core.model.metamodel.Schema
import org.vitrivr.engine.core.model.retrievable.Retrieved
import org.vitrivr.engine.core.operators.Operator
import org.vitrivr.engine.core.operators.retrieve.TransformerFactory


/**
* [TransformerFactory] for [ObjectFieldLookup].
*
* @version 1.0.0
* @author Ralph Gasser
*/
class ObjectFieldLookupFactory() : TransformerFactory {
override fun newTransformer(input: Operator<Retrieved>, schema: Schema, properties: Map<String, String>): ObjectFieldLookup {
val field = properties["field"] ?: throw IllegalArgumentException("Expected 'field' to be defined in properties")
val predicates = properties["predicates"]?.split(",")?.toSet() ?: emptySet()
val reader = (schema[field] ?: throw IllegalArgumentException("Field '$field' not defined in schema")).getReader()
return ObjectFieldLookup(input, reader, predicates)
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
org.vitrivr.engine.query.transform.RelationExpanderFactory
org.vitrivr.engine.query.transform.ScoreAggregatorFactory
org.vitrivr.engine.query.transform.FieldLookupFactory
org.vitrivr.engine.query.transform.lookup.FieldLookupFactory
org.vitrivr.engine.query.transform.lookup.ObjectFieldLookupFactory

0 comments on commit 64fb907

Please sign in to comment.