From 9e9c3f2a9b94db68bb0c3178960db79d57999f6c Mon Sep 17 00:00:00 2001 From: Ralph Gasser Date: Mon, 2 Dec 2024 13:57:29 +0100 Subject: [PATCH] Optimization. --- .../descriptor/AbstractDescriptorReader.kt | 34 ++----------------- 1 file changed, 3 insertions(+), 31 deletions(-) diff --git a/vitrivr-engine-module-pgvector/src/main/kotlin/org/vitrivr/engine/database/pgvector/descriptor/AbstractDescriptorReader.kt b/vitrivr-engine-module-pgvector/src/main/kotlin/org/vitrivr/engine/database/pgvector/descriptor/AbstractDescriptorReader.kt index 94f80991..48438299 100644 --- a/vitrivr-engine-module-pgvector/src/main/kotlin/org/vitrivr/engine/database/pgvector/descriptor/AbstractDescriptorReader.kt +++ b/vitrivr-engine-module-pgvector/src/main/kotlin/org/vitrivr/engine/database/pgvector/descriptor/AbstractDescriptorReader.kt @@ -12,8 +12,6 @@ import org.vitrivr.engine.core.model.query.bool.Logical import org.vitrivr.engine.core.model.retrievable.RetrievableId import org.vitrivr.engine.core.model.retrievable.Retrieved import org.vitrivr.engine.database.pgvector.* -import org.vitrivr.engine.database.pgvector.descriptor.scalar.ScalarDescriptorReader -import org.vitrivr.engine.database.pgvector.descriptor.struct.StructDescriptorReader import java.sql.PreparedStatement import java.sql.ResultSet import java.util.* @@ -213,35 +211,9 @@ abstract class AbstractDescriptorReader>(final override val fi * @return Set of [RetrievableId]s that match the [BooleanPredicate]. */ protected fun getMatches(predicate: BooleanPredicate): Set = when (predicate) { - is Comparison<*> -> { - val field = predicate.field - val reader = field.getReader() - when (reader) { - is ScalarDescriptorReader -> reader.query(Query(predicate)).map { it.id }.toSet() - is StructDescriptorReader -> reader.query(Query(predicate)).map { it.id }.toSet() - else -> throw IllegalArgumentException("Cannot resolve predicate $predicate.") - } - } - - is Logical.And -> { - val intersection = mutableSetOf() - for ((index, child) in predicate.predicates.withIndex()) { - if (index == 0) { - intersection.addAll(getMatches(child)) - } else { - intersection.intersect(getMatches(child)) - } - } - intersection - } - - is Logical.Or -> { - val union = mutableSetOf() - for (child in predicate.predicates) { - union.addAll(getMatches(child)) - } - union - } + is Comparison<*> -> predicate.field.getReader().query(Query(predicate)).map { it.id }.toSet() + is Logical.And -> predicate.predicates.map { getMatches(it) }.reduce { acc, set -> acc.intersect(set) } + is Logical.Or -> predicate.predicates.flatMap { getMatches(it) }.toMutableSet() } /**