Skip to content

Commit

Permalink
First version of FieldLookup
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaro committed Nov 29, 2023
1 parent 35ec3b1 commit 45854f4
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ interface Retrieved : Retrievable {

interface RetrievedWithRelationship : Retrieved, RetrievableWithRelationship

interface RetrievedWithProperties: Retrieved, RetrievableWithProperties

data class Default(override val id: UUID, override val type: String?, override val transient: Boolean) : Retrieved


Expand Down Expand Up @@ -105,6 +107,14 @@ interface Retrieved : Retrievable {
is RetrievedWithDistance -> DistancePlusScore(retrieved, score)
else -> RetrievedPlusScore(retrieved, score)
}

fun PlusProperties(retrieved: Retrieved, properties: Map<String, String> = mutableMapOf()) = when(retrieved) {
is RetrievedWithProperties -> retrieved

//TODO other combinations

else -> RetrievedPlusProperties(retrieved, properties)
}
}

data class RetrievedPlusScore(private val retrieved: Retrieved, override val score: Float) : Retrieved by retrieved, RetrievedWithScore
Expand All @@ -126,4 +136,13 @@ interface Retrieved : Retrievable {
retrieved: RetrievedWithScore,
override val relationships: Set<Relationship> = mutableSetOf()
) : RetrievedWithScore by retrieved, RetrievedWithRelationship

class RetrievedPlusProperties(
retrieved: Retrieved,
override val properties: Map<String, String> = mutableMapOf()
) : Retrieved by retrieved, RetrievedWithProperties




}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.vitrivr.engine.core.model.retrievable.decorators

import org.vitrivr.engine.core.model.retrievable.Retrievable

/**
* A [Retrievable] with arbitrary Properties
*
* @version 1.0.0
*/
interface RetrievableWithProperties : Retrievable {

val properties: Map<String, String>

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.vitrivr.engine.query.transform

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.retrievable.Retrieved
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<Retrieved, Retrieved.RetrievedWithProperties> {
override fun toFlow(scope: CoroutineScope): Flow<Retrieved.RetrievedWithProperties> = flow {

val inputRetrieved = input.toFlow(scope).toList()

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

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

val descriptors = reader.getAll(ids).filter { it.retrievableId != null }.associateBy { it.retrievableId!! }

inputRetrieved.forEach {

val withProperties = Retrieved.PlusProperties(it)

val descriptor = descriptors[it.id]

if (descriptor != null) {
val values = descriptor.values().toMap()
keys.forEach { key ->
(withProperties.properties as MutableMap)[key] = values[key].toString()
}
}

emit(withProperties)

}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.vitrivr.engine.query.transform

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

class FieldLookupFactory(

) : TransformerFactory<Retrieved, Retrieved.RetrievedWithProperties> {
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
)
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
org.vitrivr.engine.query.transform.RelationExpanderFactory
org.vitrivr.engine.query.transform.ScoreAggregatorFactory
org.vitrivr.engine.query.transform.ScoreAggregatorFactory
org.vitrivr.engine.query.transform.FieldLookupFactory

0 comments on commit 45854f4

Please sign in to comment.