From 8ec62fd80e90dc0dcf5a8b6465cde89801f0973c Mon Sep 17 00:00:00 2001 From: Ralph Gasser Date: Thu, 7 Dec 2023 18:23:04 +0100 Subject: [PATCH] Cottontail DB database driver now makes use of UUID values. Signed-off-by: Ralph Gasser --- .../descriptors/AbstractDescriptorReader.kt | 8 +-- .../descriptors/AbstractDescriptorWriter.kt | 6 +- .../label/StructDescriptorInitializer.kt | 4 +- .../label/StructDescriptorReader.kt | 5 +- .../label/StructDescriptorWriter.kt | 12 ++-- .../scalar/ScalarDescriptorInitializer.kt | 4 +- .../scalar/ScalarDescriptorReader.kt | 5 +- .../scalar/ScalarDescriptorWriter.kt | 10 +-- .../string/StringDescriptorReader.kt | 22 +++---- .../vector/VectorDescriptorInitializer.kt | 4 +- .../vector/VectorDescriptorReader.kt | 17 ++--- .../vector/VectorDescriptorWriter.kt | 63 ++----------------- .../retrievable/RetrievableInitializer.kt | 6 +- .../retrievable/RetrievableReader.kt | 44 ++++++------- .../retrievable/RetrievableWriter.kt | 21 ++++--- 15 files changed, 79 insertions(+), 152 deletions(-) diff --git a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/AbstractDescriptorReader.kt b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/AbstractDescriptorReader.kt index c3666c65..0ceb8598 100644 --- a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/AbstractDescriptorReader.kt +++ b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/AbstractDescriptorReader.kt @@ -8,7 +8,7 @@ import org.vitrivr.cottontail.client.language.basics.expression.Literal import org.vitrivr.cottontail.client.language.basics.predicate.Compare import org.vitrivr.cottontail.core.database.Name import org.vitrivr.cottontail.core.tuple.Tuple -import org.vitrivr.cottontail.core.values.StringValue +import org.vitrivr.cottontail.core.values.UuidValue import org.vitrivr.engine.base.database.cottontail.CottontailConnection import org.vitrivr.engine.core.database.descriptor.DescriptorReader import org.vitrivr.engine.core.model.descriptor.Descriptor @@ -38,7 +38,7 @@ abstract class AbstractDescriptorReader(final override val field override fun getBy(id: UUID, columnName: String): D? { val query = org.vitrivr.cottontail.client.language.dql.Query(this.entityName) - .where(Compare(Column(this.entityName.column(columnName)), Compare.Operator.EQUAL, Literal(id.toString()))) + .where(Compare(Column(this.entityName.column(columnName)), Compare.Operator.EQUAL, Literal(UuidValue(id)))) return try { val result = this.connection.client.query(query) val ret = if (result.hasNext()) { @@ -62,7 +62,7 @@ abstract class AbstractDescriptorReader(final override val field */ override fun exists(id: DescriptorId): Boolean { val query = org.vitrivr.cottontail.client.language.dql.Query(this.entityName).exists() - .where(Compare(Column(this.entityName.column(CottontailConnection.DESCRIPTOR_ID_COLUMN_NAME)), Compare.Operator.EQUAL, Literal(id.toString()))) + .where(Compare(Column(this.entityName.column(CottontailConnection.DESCRIPTOR_ID_COLUMN_NAME)), Compare.Operator.EQUAL, Literal(UuidValue(id)))) return try { val result = this.connection.client.query(query) result.next().asBoolean(0) ?: false @@ -95,7 +95,7 @@ abstract class AbstractDescriptorReader(final override val field */ override fun getAll(ids: Iterable): Sequence { val query = org.vitrivr.cottontail.client.language.dql.Query(this.entityName) - .where(Compare(Column(this.entityName.column(CottontailConnection.DESCRIPTOR_ID_COLUMN_NAME)), Compare.Operator.IN, org.vitrivr.cottontail.client.language.basics.expression.List(ids.map { StringValue(it.toString()) }.toTypedArray()))) + .where(Compare(Column(this.entityName.column(CottontailConnection.DESCRIPTOR_ID_COLUMN_NAME)), Compare.Operator.IN, org.vitrivr.cottontail.client.language.basics.expression.List(ids.map { UuidValue(it) }.toTypedArray()))) return try { val result = this.connection.client.query(query) result.asSequence().map { this.tupleToDescriptor(it) } diff --git a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/AbstractDescriptorWriter.kt b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/AbstractDescriptorWriter.kt index f1900490..d3486345 100644 --- a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/AbstractDescriptorWriter.kt +++ b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/AbstractDescriptorWriter.kt @@ -8,7 +8,7 @@ import org.vitrivr.cottontail.client.language.basics.expression.Literal import org.vitrivr.cottontail.client.language.basics.predicate.Compare import org.vitrivr.cottontail.client.language.dml.Delete import org.vitrivr.cottontail.core.database.Name -import org.vitrivr.cottontail.core.values.StringValue +import org.vitrivr.cottontail.core.values.UuidValue import org.vitrivr.engine.base.database.cottontail.CottontailConnection import org.vitrivr.engine.core.database.descriptor.DescriptorWriter import org.vitrivr.engine.core.model.descriptor.Descriptor @@ -37,7 +37,7 @@ abstract class AbstractDescriptorWriter(final override val field Compare( Column(this.entityName.column(CottontailConnection.DESCRIPTOR_ID_COLUMN_NAME)), Compare.Operator.EQUAL, - Literal(item.id.toString()) + Literal(UuidValue(item.id)) ) ) @@ -59,7 +59,7 @@ abstract class AbstractDescriptorWriter(final override val field * @return True on success, false otherwise. */ override fun deleteAll(items: Iterable): Boolean { - val ids = items.map { StringValue(it.id.toString()) } + val ids = items.map { UuidValue(it.id) } val delete = Delete(this.entityName).where( Compare( Column(this.entityName.column(CottontailConnection.DESCRIPTOR_ID_COLUMN_NAME)), diff --git a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/label/StructDescriptorInitializer.kt b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/label/StructDescriptorInitializer.kt index 629aee47..fc7dc65c 100644 --- a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/label/StructDescriptorInitializer.kt +++ b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/label/StructDescriptorInitializer.kt @@ -27,8 +27,8 @@ class StructDescriptorInitializer(field: Schema.Field<*, StructDescriptor>, conn override fun initialize() { /* Prepare query. */ val create = CreateEntity(this.entityName) - .column(Name.ColumnName(CottontailConnection.DESCRIPTOR_ID_COLUMN_NAME), Types.String, nullable = false, primaryKey = true, autoIncrement = false) - .column(Name.ColumnName(CottontailConnection.RETRIEVABLE_ID_COLUMN_NAME), Types.String, nullable = false, primaryKey = false, autoIncrement = false) + .column(Name.ColumnName(CottontailConnection.DESCRIPTOR_ID_COLUMN_NAME), Types.Uuid, nullable = false, primaryKey = true, autoIncrement = false) + .column(Name.ColumnName(CottontailConnection.RETRIEVABLE_ID_COLUMN_NAME), Types.Uuid, nullable = false, primaryKey = false, autoIncrement = false) for (field in this.field.analyser.prototype().schema()) { diff --git a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/label/StructDescriptorReader.kt b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/label/StructDescriptorReader.kt index 24e768a1..fc124ce6 100644 --- a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/label/StructDescriptorReader.kt +++ b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/label/StructDescriptorReader.kt @@ -10,7 +10,6 @@ import org.vitrivr.engine.core.model.descriptor.struct.StructDescriptor import org.vitrivr.engine.core.model.metamodel.Schema import org.vitrivr.engine.core.model.query.Query import org.vitrivr.engine.core.model.retrievable.Retrieved -import java.lang.IllegalStateException import java.util.* import kotlin.reflect.full.primaryConstructor @@ -59,8 +58,8 @@ class StructDescriptorReader(field: Schema.Field<*, StructDescriptor>, connectio override fun tupleToDescriptor(tuple: Tuple): StructDescriptor { val constructor = this.field.analyser.descriptorClass.primaryConstructor ?: throw IllegalStateException("Provided type ${this.field.analyser.descriptorClass} does not have a primary constructor.") val parameters: MutableList = mutableListOf( - UUID.fromString(tuple.asString(CottontailConnection.RETRIEVABLE_ID_COLUMN_NAME) ?: throw IllegalArgumentException("The provided tuple is missing the required field '${CottontailConnection.RETRIEVABLE_ID_COLUMN_NAME}'.")), - UUID.fromString(tuple.asString(CottontailConnection.DESCRIPTOR_ID_COLUMN_NAME) ?: throw IllegalArgumentException("The provided tuple is missing the required field '${CottontailConnection.DESCRIPTOR_ID_COLUMN_NAME}'.")), + tuple.asUuidValue(CottontailConnection.RETRIEVABLE_ID_COLUMN_NAME)?.value ?: throw IllegalArgumentException("The provided tuple is missing the required field '${CottontailConnection.RETRIEVABLE_ID_COLUMN_NAME}'."), + tuple.asUuidValue(CottontailConnection.DESCRIPTOR_ID_COLUMN_NAME)?.value ?: throw IllegalArgumentException("The provided tuple is missing the required field '${CottontailConnection.DESCRIPTOR_ID_COLUMN_NAME}'."), ) /* Append dynamic parameters of struct. */ diff --git a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/label/StructDescriptorWriter.kt b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/label/StructDescriptorWriter.kt index 8d7c84d5..e45350a9 100644 --- a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/label/StructDescriptorWriter.kt +++ b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/label/StructDescriptorWriter.kt @@ -9,7 +9,7 @@ import org.vitrivr.cottontail.client.language.basics.predicate.Compare import org.vitrivr.cottontail.client.language.dml.BatchInsert import org.vitrivr.cottontail.client.language.dml.Insert import org.vitrivr.cottontail.client.language.dml.Update -import org.vitrivr.cottontail.core.values.StringValue +import org.vitrivr.cottontail.core.values.UuidValue import org.vitrivr.engine.base.database.cottontail.CottontailConnection import org.vitrivr.engine.base.database.cottontail.descriptors.AbstractDescriptorWriter import org.vitrivr.engine.core.model.descriptor.struct.StructDescriptor @@ -33,8 +33,8 @@ class StructDescriptorWriter(field: Schema.Field<*, StructDescriptor>, connectio */ override fun add(item: StructDescriptor): Boolean { val insert = Insert(this.entityName).values( - CottontailConnection.DESCRIPTOR_ID_COLUMN_NAME to StringValue(item.id.toString()), - CottontailConnection.RETRIEVABLE_ID_COLUMN_NAME to StringValue(item.retrievableId.toString()), + CottontailConnection.DESCRIPTOR_ID_COLUMN_NAME to UuidValue(item.id), + CottontailConnection.RETRIEVABLE_ID_COLUMN_NAME to UuidValue(item.retrievableId ?: throw IllegalArgumentException("A struct descriptor must be associated with a retrievable ID.")), ) /* Append fields. */ @@ -78,8 +78,8 @@ class StructDescriptorWriter(field: Schema.Field<*, StructDescriptor>, connectio } val inserts: Array = Array(values.size + 2) { when (it) { - 0 -> item.id.toString() - 1 -> item.retrievableId.toString() + 0 -> item.id + 1 -> item.retrievableId else -> values[it - 2].second } } @@ -109,7 +109,7 @@ class StructDescriptorWriter(field: Schema.Field<*, StructDescriptor>, connectio Compare( Column(this.entityName.column(CottontailConnection.DESCRIPTOR_ID_COLUMN_NAME)), Compare.Operator.EQUAL, - Literal(item.id.toString()) + Literal(UuidValue(item.id)) ) ) diff --git a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/scalar/ScalarDescriptorInitializer.kt b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/scalar/ScalarDescriptorInitializer.kt index f2085835..bc8a7c15 100644 --- a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/scalar/ScalarDescriptorInitializer.kt +++ b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/scalar/ScalarDescriptorInitializer.kt @@ -31,8 +31,8 @@ class ScalarDescriptorInitializer>(field: Schema.Field<* /* Prepare query. */ val create = CreateEntity(this.entityName) - .column(Name.ColumnName(CottontailConnection.DESCRIPTOR_ID_COLUMN_NAME), Types.String, nullable = false, primaryKey = true, autoIncrement = false) - .column(Name.ColumnName(CottontailConnection.RETRIEVABLE_ID_COLUMN_NAME), Types.String, nullable = false, primaryKey = false, autoIncrement = false) + .column(Name.ColumnName(CottontailConnection.DESCRIPTOR_ID_COLUMN_NAME), Types.Uuid, nullable = false, primaryKey = true, autoIncrement = false) + .column(Name.ColumnName(CottontailConnection.RETRIEVABLE_ID_COLUMN_NAME), Types.Uuid, nullable = false, primaryKey = false, autoIncrement = false) .column(Name.ColumnName(DESCRIPTOR_COLUMN_NAME), type, nullable = false, primaryKey = false, autoIncrement = false) try { diff --git a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/scalar/ScalarDescriptorReader.kt b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/scalar/ScalarDescriptorReader.kt index 5115c707..75afb9da 100644 --- a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/scalar/ScalarDescriptorReader.kt +++ b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/scalar/ScalarDescriptorReader.kt @@ -18,7 +18,6 @@ import org.vitrivr.engine.core.model.metamodel.Schema import org.vitrivr.engine.core.model.query.Query import org.vitrivr.engine.core.model.query.bool.BooleanQuery import org.vitrivr.engine.core.model.retrievable.Retrieved -import java.util.* /** * An [AbstractDescriptorReader] for [ScalarDescriptor]s. @@ -57,8 +56,8 @@ class ScalarDescriptorReader(field: Schema.Field<*, ScalarDescriptor<*>>, connec * @return The resulting [FloatVectorDescriptor]. */ override fun tupleToDescriptor(tuple: Tuple): StringDescriptor = StringDescriptor( - UUID.fromString(tuple.asString(RETRIEVABLE_ID_COLUMN_NAME) ?: throw IllegalArgumentException("The provided tuple is missing the required field '${RETRIEVABLE_ID_COLUMN_NAME}'.")), - UUID.fromString(tuple.asString(DESCRIPTOR_ID_COLUMN_NAME) ?: throw IllegalArgumentException("The provided tuple is missing the required field '${DESCRIPTOR_ID_COLUMN_NAME}'.")), + tuple.asUuidValue(RETRIEVABLE_ID_COLUMN_NAME)?.value ?: throw IllegalArgumentException("The provided tuple is missing the required field '${RETRIEVABLE_ID_COLUMN_NAME}'."), + tuple.asUuidValue(DESCRIPTOR_ID_COLUMN_NAME)?.value ?: throw IllegalArgumentException("The provided tuple is missing the required field '${DESCRIPTOR_ID_COLUMN_NAME}'."), tuple.asString(DESCRIPTOR_COLUMN_NAME) ?: throw IllegalArgumentException("The provided tuple is missing the required field '$DESCRIPTOR_COLUMN_NAME'.") /* TODO: Use UUID once supported. */ ) } \ No newline at end of file diff --git a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/scalar/ScalarDescriptorWriter.kt b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/scalar/ScalarDescriptorWriter.kt index f1a95953..48736223 100644 --- a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/scalar/ScalarDescriptorWriter.kt +++ b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/scalar/ScalarDescriptorWriter.kt @@ -9,7 +9,7 @@ import org.vitrivr.cottontail.client.language.basics.predicate.Compare import org.vitrivr.cottontail.client.language.dml.BatchInsert import org.vitrivr.cottontail.client.language.dml.Insert import org.vitrivr.cottontail.client.language.dml.Update -import org.vitrivr.cottontail.core.values.StringValue +import org.vitrivr.cottontail.core.values.UuidValue import org.vitrivr.engine.base.database.cottontail.CottontailConnection import org.vitrivr.engine.base.database.cottontail.CottontailConnection.Companion.DESCRIPTOR_ID_COLUMN_NAME import org.vitrivr.engine.base.database.cottontail.CottontailConnection.Companion.RETRIEVABLE_ID_COLUMN_NAME @@ -40,8 +40,8 @@ class ScalarDescriptorWriter(field: Schema.Field<*, ScalarDescriptor<*>>, connec */ override fun add(item: ScalarDescriptor<*>): Boolean { val insert = Insert(this.entityName).values( - DESCRIPTOR_ID_COLUMN_NAME to StringValue(item.id.toString()), - RETRIEVABLE_ID_COLUMN_NAME to StringValue(item.retrievableId.toString()), + DESCRIPTOR_ID_COLUMN_NAME to UuidValue(item.id), + RETRIEVABLE_ID_COLUMN_NAME to UuidValue(item.retrievableId ?: throw IllegalArgumentException("A scalar descriptor must be associated with a retrievable ID.")), DESCRIPTOR_COLUMN_NAME to item.toValue() ) return try { @@ -66,7 +66,7 @@ class ScalarDescriptorWriter(field: Schema.Field<*, ScalarDescriptor<*>>, connec val insert = BatchInsert(this.entityName).columns(DESCRIPTOR_ID_COLUMN_NAME, RETRIEVABLE_ID_COLUMN_NAME, DESCRIPTOR_COLUMN_NAME) for (item in items) { size += 1 - insert.values(StringValue(item.id.toString()), StringValue(item.retrievableId.toString()), item.toValue()) + insert.values(UuidValue(item.id.toString()), UuidValue(item.retrievableId ?: throw IllegalArgumentException("A scalar descriptor must be associated with a retrievable ID.")), item.toValue()) } /* Insert values. */ @@ -91,7 +91,7 @@ class ScalarDescriptorWriter(field: Schema.Field<*, ScalarDescriptor<*>>, connec Compare( Column(this.entityName.column(DESCRIPTOR_ID_COLUMN_NAME)), Compare.Operator.EQUAL, - Literal(item.id.toString()) + Literal(UuidValue(item.id)) ) ).values(DESCRIPTOR_COLUMN_NAME to item.toValue()) diff --git a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/string/StringDescriptorReader.kt b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/string/StringDescriptorReader.kt index 9b4b394f..b7a6c473 100644 --- a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/string/StringDescriptorReader.kt +++ b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/string/StringDescriptorReader.kt @@ -8,10 +8,10 @@ import org.vitrivr.engine.base.database.cottontail.CottontailConnection.Companio import org.vitrivr.engine.base.database.cottontail.descriptors.AbstractDescriptorReader import org.vitrivr.engine.base.database.cottontail.descriptors.DESCRIPTOR_COLUMN_NAME import org.vitrivr.engine.core.model.descriptor.scalar.StringDescriptor -import org.vitrivr.engine.core.model.retrievable.Retrieved import org.vitrivr.engine.core.model.metamodel.Schema import org.vitrivr.engine.core.model.query.Query import org.vitrivr.engine.core.model.query.string.TextQuery +import org.vitrivr.engine.core.model.retrievable.Retrieved import java.util.* class StringDescriptorReader(field: Schema.Field<*, StringDescriptor>, connection: CottontailConnection) : AbstractDescriptorReader(field, connection) { @@ -35,19 +35,11 @@ class StringDescriptorReader(field: Schema.Field<*, StringDescriptor>, connectio } override fun tupleToDescriptor(tuple: Tuple): StringDescriptor { - val descriptorId = UUID.fromString( - tuple.asString(CottontailConnection.DESCRIPTOR_ID_COLUMN_NAME) - ?: throw IllegalArgumentException("The provided tuple is missing the required field '${DESCRIPTOR_ID_COLUMN_NAME}'.") - ) - val retrievableId = UUID.fromString( - tuple.asString(RETRIEVABLE_ID_COLUMN_NAME) - ?: throw IllegalArgumentException("The provided tuple is missing the required field '${RETRIEVABLE_ID_COLUMN_NAME}'.") - ) - return StringDescriptor( - descriptorId, - retrievableId, - tuple.asString(DESCRIPTOR_COLUMN_NAME) - ?: throw IllegalArgumentException("The provided tuple is missing the required field '$DESCRIPTOR_COLUMN_NAME'.") - ) + val descriptorId = tuple.asUuidValue(DESCRIPTOR_ID_COLUMN_NAME)?.value + ?: throw IllegalArgumentException("The provided tuple is missing the required field '${DESCRIPTOR_ID_COLUMN_NAME}'.") + val retrievableId = tuple.asUuidValue(RETRIEVABLE_ID_COLUMN_NAME)?.value + ?: throw IllegalArgumentException("The provided tuple is missing the required field '${RETRIEVABLE_ID_COLUMN_NAME}'.") + val value = tuple.asString(DESCRIPTOR_COLUMN_NAME) ?: throw IllegalArgumentException("The provided tuple is missing the required field '$DESCRIPTOR_COLUMN_NAME'.") + return StringDescriptor(descriptorId, retrievableId, value) } } \ No newline at end of file diff --git a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/vector/VectorDescriptorInitializer.kt b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/vector/VectorDescriptorInitializer.kt index 97f3d387..27ad9348 100644 --- a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/vector/VectorDescriptorInitializer.kt +++ b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/vector/VectorDescriptorInitializer.kt @@ -31,8 +31,8 @@ internal class VectorDescriptorInitializer(field: Schema.Field<*, VectorDescript override fun initialize() { val type = this.field.analyser.prototype().toType() val create = CreateEntity(this.entityName) - .column(Name.ColumnName(DESCRIPTOR_ID_COLUMN_NAME), Types.String, nullable = false, primaryKey = true, autoIncrement = false) - .column(Name.ColumnName(RETRIEVABLE_ID_COLUMN_NAME), Types.String, nullable = false, primaryKey = false, autoIncrement = false) + .column(Name.ColumnName(DESCRIPTOR_ID_COLUMN_NAME), Types.Uuid, nullable = false, primaryKey = true, autoIncrement = false) + .column(Name.ColumnName(RETRIEVABLE_ID_COLUMN_NAME), Types.Uuid, nullable = false, primaryKey = false, autoIncrement = false) .column(Name.ColumnName(DESCRIPTOR_COLUMN_NAME), type, nullable = false, primaryKey = false, autoIncrement = false) try { diff --git a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/vector/VectorDescriptorReader.kt b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/vector/VectorDescriptorReader.kt index 3e8b9060..ac662b9b 100644 --- a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/vector/VectorDescriptorReader.kt +++ b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/vector/VectorDescriptorReader.kt @@ -16,7 +16,6 @@ import org.vitrivr.engine.core.model.query.Query import org.vitrivr.engine.core.model.query.proximity.ProximityQuery import org.vitrivr.engine.core.model.retrievable.Retrieved import org.vitrivr.engine.core.model.retrievable.decorators.RetrievableWithScore -import java.util.* /** * An [AbstractDescriptorReader] for [FloatVectorDescriptor]s. @@ -54,22 +53,22 @@ internal class VectorDescriptorReader(field: Schema.Field<*, VectorDescriptor<*> } this.connection.client.query(cottontailQuery).asSequence().mapNotNull { - val retrievableId = it.asString(RETRIEVABLE_ID_COLUMN_NAME) ?: return@mapNotNull null + val retrievableId = it.asUuidValue(RETRIEVABLE_ID_COLUMN_NAME)?.value ?: return@mapNotNull null val distance = (it.asFloat(DISTANCE_COLUMN_NAME) ?: it.asDouble(DISTANCE_COLUMN_NAME)?.toFloat())?.let { f -> if (f.isNaN()) Float.MAX_VALUE else f } ?: return@mapNotNull null - if (query.withDescriptor) { /* TODO: Use UUID type once supported. */ + if (query.withDescriptor) { val descriptor = tupleToDescriptor(it) Retrieved.WithDistanceAndDescriptor( - UUID.fromString(retrievableId), + retrievableId, null, distance, listOf(descriptor), false ) } else { - Retrieved.WithDistance(UUID.fromString(retrievableId), null, distance, false) + Retrieved.WithDistance(retrievableId, null, distance, false) } } } @@ -84,14 +83,10 @@ internal class VectorDescriptorReader(field: Schema.Field<*, VectorDescriptor<*> * @return The resulting [VectorDescriptor]. */ override fun tupleToDescriptor(tuple: Tuple): VectorDescriptor<*> { - val descriptorId = UUID.fromString( - tuple.asString(DESCRIPTOR_ID_COLUMN_NAME) + val descriptorId = tuple.asUuidValue(DESCRIPTOR_ID_COLUMN_NAME)?.value ?: throw IllegalArgumentException("The provided tuple is missing the required field '${DESCRIPTOR_ID_COLUMN_NAME}'.") - ) - val retrievableId = UUID.fromString( - tuple.asString(RETRIEVABLE_ID_COLUMN_NAME) + val retrievableId = tuple.asUuidValue(RETRIEVABLE_ID_COLUMN_NAME)?.value ?: throw IllegalArgumentException("The provided tuple is missing the required field '${RETRIEVABLE_ID_COLUMN_NAME}'.") - ) return when (this.prototype) { is BooleanVectorDescriptor -> BooleanVectorDescriptor( descriptorId, diff --git a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/vector/VectorDescriptorWriter.kt b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/vector/VectorDescriptorWriter.kt index 4dae00a9..40ae956f 100644 --- a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/vector/VectorDescriptorWriter.kt +++ b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/descriptors/vector/VectorDescriptorWriter.kt @@ -4,14 +4,12 @@ import io.github.oshai.kotlinlogging.KLogger import io.github.oshai.kotlinlogging.KotlinLogging import io.grpc.StatusRuntimeException import org.vitrivr.cottontail.client.language.basics.expression.Column -import org.vitrivr.cottontail.client.language.basics.expression.List import org.vitrivr.cottontail.client.language.basics.expression.Literal import org.vitrivr.cottontail.client.language.basics.predicate.Compare import org.vitrivr.cottontail.client.language.dml.BatchInsert -import org.vitrivr.cottontail.client.language.dml.Delete import org.vitrivr.cottontail.client.language.dml.Insert import org.vitrivr.cottontail.client.language.dml.Update -import org.vitrivr.cottontail.core.values.StringValue +import org.vitrivr.cottontail.core.values.UuidValue import org.vitrivr.engine.base.database.cottontail.CottontailConnection import org.vitrivr.engine.base.database.cottontail.CottontailConnection.Companion.DESCRIPTOR_ID_COLUMN_NAME import org.vitrivr.engine.base.database.cottontail.CottontailConnection.Companion.RETRIEVABLE_ID_COLUMN_NAME @@ -39,8 +37,8 @@ class VectorDescriptorWriter(field: Schema.Field<*, VectorDescriptor<*>>, connec */ override fun add(item: VectorDescriptor<*>): Boolean { val insert = Insert(this.entityName).values( - DESCRIPTOR_ID_COLUMN_NAME to StringValue(item.id.toString()), - RETRIEVABLE_ID_COLUMN_NAME to StringValue(item.retrievableId.toString()), + DESCRIPTOR_ID_COLUMN_NAME to UuidValue(item.id.toString()), + RETRIEVABLE_ID_COLUMN_NAME to UuidValue(item.retrievableId ?: throw IllegalArgumentException("A vector descriptor must be associated with a retrievable ID.")), DESCRIPTOR_COLUMN_NAME to item.toValue() ) return try { @@ -65,7 +63,7 @@ class VectorDescriptorWriter(field: Schema.Field<*, VectorDescriptor<*>>, connec val insert = BatchInsert(this.entityName).columns(DESCRIPTOR_ID_COLUMN_NAME, RETRIEVABLE_ID_COLUMN_NAME, DESCRIPTOR_COLUMN_NAME) for (item in items) { size += 1 - insert.values(StringValue(item.id.toString()), StringValue(item.retrievableId.toString()), item.toValue()) + insert.values(UuidValue(item.id), UuidValue(item.retrievableId ?: throw IllegalArgumentException("A vector descriptor must be associated with a retrievable ID.")), item.toValue()) } /* Insert values. */ @@ -89,7 +87,7 @@ class VectorDescriptorWriter(field: Schema.Field<*, VectorDescriptor<*>>, connec Compare( Column(this.entityName.column(DESCRIPTOR_ID_COLUMN_NAME)), Compare.Operator.EQUAL, - Literal(item.id.toString()) + Literal(UuidValue(item.id)) ) ).values(DESCRIPTOR_COLUMN_NAME to item.toValue()) @@ -102,55 +100,4 @@ class VectorDescriptorWriter(field: Schema.Field<*, VectorDescriptor<*>>, connec false } } - - /** - * Deletes (writes) a [VectorDescriptor] using this [VectorDescriptorWriter]. - * - * @param item A [VectorDescriptor]s to delete. - * @return True on success, false otherwise. - */ - override fun delete(item: VectorDescriptor<*>): Boolean { - val delete = Delete(this.entityName).where( - Compare( - Column(this.entityName.column(DESCRIPTOR_ID_COLUMN_NAME)), - Compare.Operator.EQUAL, - Literal(item.id.toString()) - ) - ) - - /* Delete values. */ - return try { - this.connection.client.delete(delete) - true - } catch (e: StatusRuntimeException) { - logger.error(e) { "Failed to delete descriptor due to exception." } - false - } - } - - /** - * Deletes (writes) a [VectorDescriptor] using this [VectorDescriptorWriter]. - * - * @param items A [Iterable] of [VectorDescriptor]s to delete. - * @return True on success, false otherwise. - */ - override fun deleteAll(items: Iterable>): Boolean { - val ids = items.map { StringValue(it.id.toString()) } - val delete = Delete(this.entityName).where( - Compare( - Column(this.entityName.column(DESCRIPTOR_ID_COLUMN_NAME)), - Compare.Operator.IN, - List(ids.toTypedArray()) - ) - ) - - /* Delete values. */ - return try { - this.connection.client.delete(delete) - true - } catch (e: StatusRuntimeException) { - logger.error(e) { "Failed to delete descriptor due to exception." } - false - } - } } \ No newline at end of file diff --git a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/retrievable/RetrievableInitializer.kt b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/retrievable/RetrievableInitializer.kt index 60e8fd63..d9caba50 100644 --- a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/retrievable/RetrievableInitializer.kt +++ b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/retrievable/RetrievableInitializer.kt @@ -49,7 +49,7 @@ internal class RetrievableInitializer(private val connection: CottontailConnecti /* Create retrievable entity. */ val createEntity = CreateEntity(this.entityName) - .column(Name.ColumnName(RETRIEVABLE_ID_COLUMN_NAME), Types.String, nullable = false, primaryKey = true, autoIncrement = false) + .column(Name.ColumnName(RETRIEVABLE_ID_COLUMN_NAME), Types.Uuid, nullable = false, primaryKey = true, autoIncrement = false) .column(Name.ColumnName(RETRIEVABLE_TYPE_COLUMN_NAME), Types.String, nullable = true, primaryKey = false, autoIncrement = false) .ifNotExists() @@ -61,9 +61,9 @@ internal class RetrievableInitializer(private val connection: CottontailConnecti /* Create relationship entity. */ val createRelationshipEntity = CreateEntity(this.relationshipEntityName) - .column(Name.ColumnName(SUBJECT_ID_COLUMN_NAME), Types.String, nullable = false, primaryKey = false, autoIncrement = false) + .column(Name.ColumnName(SUBJECT_ID_COLUMN_NAME), Types.Uuid, nullable = false, primaryKey = false, autoIncrement = false) .column(Name.ColumnName(PREDICATE_COLUMN_NAME), Types.String, nullable = false, primaryKey = false, autoIncrement = false) - .column(Name.ColumnName(OBJECT_ID_COLUMN_NAME), Types.String, nullable = false, primaryKey = false, autoIncrement = false) + .column(Name.ColumnName(OBJECT_ID_COLUMN_NAME), Types.Uuid, nullable = false, primaryKey = false, autoIncrement = false) .ifNotExists() try { diff --git a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/retrievable/RetrievableReader.kt b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/retrievable/RetrievableReader.kt index 7456bf5b..9275f8ac 100644 --- a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/retrievable/RetrievableReader.kt +++ b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/retrievable/RetrievableReader.kt @@ -11,6 +11,7 @@ import org.vitrivr.cottontail.client.language.basics.predicate.Compare import org.vitrivr.cottontail.client.language.dql.Query import org.vitrivr.cottontail.core.database.Name import org.vitrivr.cottontail.core.values.StringValue +import org.vitrivr.cottontail.core.values.UuidValue import org.vitrivr.engine.base.database.cottontail.CottontailConnection import org.vitrivr.engine.base.database.cottontail.CottontailConnection.Companion.OBJECT_ID_COLUMN_NAME import org.vitrivr.engine.base.database.cottontail.CottontailConnection.Companion.PREDICATE_COLUMN_NAME @@ -54,19 +55,17 @@ internal class RetrievableReader(private val connection: CottontailConnection) : Compare( Column(this.entityName.column(columnName)), Compare.Operator.EQUAL, - Literal(id.toString()) + Literal(UuidValue(id)) ) ) return try { this.connection.client.query(query).use { if (it.hasNext()) { val tuple = it.next() - val retrievableId = UUID.fromString( - tuple.asString(columnName) - ?: throw IllegalArgumentException("The provided tuple is missing the required field '${columnName}'.") - ) + val retrievableId = tuple.asUuidValue(columnName)?.value + ?: throw IllegalArgumentException("The provided tuple is missing the required field '${columnName}'.") val type = tuple.asString(RETRIEVABLE_TYPE_COLUMN_NAME) - Retrieved.Default(retrievableId, type, false) /* TODO: Use UUID type once supported. */ + Retrieved.Default(retrievableId, type, false) } else { null } @@ -89,7 +88,7 @@ internal class RetrievableReader(private val connection: CottontailConnection) : Compare( Column(this.entityName.column(RETRIEVABLE_ID_COLUMN_NAME)), Compare.Operator.EQUAL, - Literal(id.toString()) + Literal(UuidValue(id)) ) ) return try { @@ -112,16 +111,14 @@ internal class RetrievableReader(private val connection: CottontailConnection) : Compare( Column(Name.ColumnName(RETRIEVABLE_ID_COLUMN_NAME)), Compare.Operator.IN, - List(ids.map { StringValue(it.toString()) }.toTypedArray()) + List(ids.map { UuidValue(it) }.toTypedArray()) ) ) return this.connection.client.query(query).asSequence().map { tuple -> - val retrievableId = UUID.fromString( - tuple.asString(RETRIEVABLE_ID_COLUMN_NAME) - ?: throw IllegalArgumentException("The provided tuple is missing the required field '${RETRIEVABLE_ID_COLUMN_NAME}'.") - ) + val retrievableId = tuple.asUuidValue(RETRIEVABLE_ID_COLUMN_NAME)?.value + ?: throw IllegalArgumentException("The provided tuple is missing the required field '${RETRIEVABLE_ID_COLUMN_NAME}'.") val type = tuple.asString(RETRIEVABLE_TYPE_COLUMN_NAME) - Retrieved.Default(retrievableId, type, false) /* TODO: Use UUID type once supported. */ + Retrieved.Default(retrievableId, type, false) } } @@ -133,12 +130,10 @@ internal class RetrievableReader(private val connection: CottontailConnection) : override fun getAll(): Sequence { val query = Query(this.entityName).select("*") return this.connection.client.query(query).asSequence().map { tuple -> - val retrievableId = UUID.fromString( - tuple.asString(RETRIEVABLE_ID_COLUMN_NAME) - ?: throw IllegalArgumentException("The provided tuple is missing the required field '${RETRIEVABLE_ID_COLUMN_NAME}'.") - ) + val retrievableId = tuple.asUuidValue(RETRIEVABLE_ID_COLUMN_NAME)?.value + ?: throw IllegalArgumentException("The provided tuple is missing the required field '${RETRIEVABLE_ID_COLUMN_NAME}'.") val type = tuple.asString(RETRIEVABLE_TYPE_COLUMN_NAME) - Retrieved.Default(retrievableId, type, false) /* TODO: Use UUID type once supported. */ + Retrieved.Default(retrievableId, type, false) } } @@ -169,7 +164,7 @@ internal class RetrievableReader(private val connection: CottontailConnection) : Compare( Column(Name.ColumnName(SUBJECT_ID_COLUMN_NAME)), Compare.Operator.IN, - List(subjectIds.map { StringValue(it.toString()) }.toTypedArray()) + List(subjectIds.map { UuidValue(it) }.toTypedArray()) ) } else { null @@ -187,7 +182,7 @@ internal class RetrievableReader(private val connection: CottontailConnection) : Compare( Column(Name.ColumnName(OBJECT_ID_COLUMN_NAME)), Compare.Operator.IN, - List(objectIds.map { StringValue(it.toString()) }.toTypedArray()) + List(objectIds.map { UuidValue(it) }.toTypedArray()) ) } else { null @@ -207,14 +202,13 @@ internal class RetrievableReader(private val connection: CottontailConnection) : } return this.connection.client.query(query).asSequence().map { tuple -> - val s = tuple.asString(SUBJECT_ID_COLUMN_NAME) + val s = tuple.asUuidValue(SUBJECT_ID_COLUMN_NAME)?.value ?: throw IllegalArgumentException("The provided tuple is missing the required field '${SUBJECT_ID_COLUMN_NAME}'.") - val p = tuple.asString(PREDICATE_COLUMN_NAME) + val p = tuple.asStringValue(PREDICATE_COLUMN_NAME)?.value ?: throw IllegalArgumentException("The provided tuple is missing the required field '${PREDICATE_COLUMN_NAME}'.") - val o = tuple.asString(OBJECT_ID_COLUMN_NAME) + val o = tuple.asUuidValue(OBJECT_ID_COLUMN_NAME)?.value ?: throw IllegalArgumentException("The provided tuple is missing the required field '${OBJECT_ID_COLUMN_NAME}'.") - - Triple(UUID.fromString(s), p, UUID.fromString(o)) + Triple(s, p, o) } } diff --git a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/retrievable/RetrievableWriter.kt b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/retrievable/RetrievableWriter.kt index 9bce4862..a959b9b0 100644 --- a/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/retrievable/RetrievableWriter.kt +++ b/vitrivr-engine-base/src/main/kotlin/org/vitrivr/engine/base/database/cottontail/retrievable/RetrievableWriter.kt @@ -12,6 +12,7 @@ import org.vitrivr.cottontail.client.language.dml.Delete import org.vitrivr.cottontail.client.language.dml.Insert import org.vitrivr.cottontail.core.database.Name import org.vitrivr.cottontail.core.values.StringValue +import org.vitrivr.cottontail.core.values.UuidValue import org.vitrivr.engine.base.database.cottontail.CottontailConnection import org.vitrivr.engine.base.database.cottontail.CottontailConnection.Companion.OBJECT_ID_COLUMN_NAME import org.vitrivr.engine.base.database.cottontail.CottontailConnection.Companion.PREDICATE_COLUMN_NAME @@ -46,8 +47,8 @@ internal class RetrievableWriter(private val connection: CottontailConnection) : */ override fun add(item: Retrievable): Boolean { val insert = Insert(this.entityName) - .any(RETRIEVABLE_ID_COLUMN_NAME, item.id.toString()) - .any(RETRIEVABLE_TYPE_COLUMN_NAME, item.type) + .value(RETRIEVABLE_ID_COLUMN_NAME, UuidValue(item.id)) + .any(RETRIEVABLE_TYPE_COLUMN_NAME, item.type?.let { StringValue(it) }) return try { return this.connection.client.insert(insert).use { it.hasNext() @@ -69,7 +70,7 @@ internal class RetrievableWriter(private val connection: CottontailConnection) : val insert = BatchInsert(this.entityName).columns(RETRIEVABLE_ID_COLUMN_NAME, RETRIEVABLE_TYPE_COLUMN_NAME) for (item in items) { size += 1 - insert.any(item.id.toString(), item.type) + insert.any(item.id, item.type) } /* Insert values. */ @@ -100,9 +101,9 @@ internal class RetrievableWriter(private val connection: CottontailConnection) : */ override fun connect(subject: RetrievableId, predicate: String, `object`: RetrievableId): Boolean { val insert = Insert(this.relationshipEntityName) - .value(SUBJECT_ID_COLUMN_NAME, StringValue(subject.toString())) + .value(SUBJECT_ID_COLUMN_NAME, UuidValue(subject)) .value(PREDICATE_COLUMN_NAME, StringValue(predicate)) - .value(OBJECT_ID_COLUMN_NAME, StringValue(`object`.toString())) + .value(OBJECT_ID_COLUMN_NAME, UuidValue(`object`)) /* Insert values. */ return try { @@ -119,7 +120,7 @@ internal class RetrievableWriter(private val connection: CottontailConnection) : val insert = BatchInsert(this.relationshipEntityName).columns(SUBJECT_ID_COLUMN_NAME, PREDICATE_COLUMN_NAME, OBJECT_ID_COLUMN_NAME) subjects.zip(objects).forEach { (subject, obj) -> size += 1 - insert.values(StringValue(subject.toString()), StringValue(predicate), StringValue(obj.toString())) + insert.values(UuidValue(subject), StringValue(predicate), UuidValue(obj)) } return try { @@ -145,8 +146,8 @@ internal class RetrievableWriter(private val connection: CottontailConnection) : And( Compare(Column(PREDICATE_COLUMN_NAME), Compare.Operator.EQUAL, Literal(StringValue(predicate))), And( - Compare(Column(OBJECT_ID_COLUMN_NAME), Compare.Operator.EQUAL, Literal(StringValue(`object`.toString()))), - Compare(Column(SUBJECT_ID_COLUMN_NAME), Compare.Operator.EQUAL, Literal(StringValue(subject.toString()))) + Compare(Column(OBJECT_ID_COLUMN_NAME), Compare.Operator.EQUAL, Literal(UuidValue(`object`))), + Compare(Column(SUBJECT_ID_COLUMN_NAME), Compare.Operator.EQUAL, Literal(UuidValue(subject))) ) ) ) @@ -172,7 +173,7 @@ internal class RetrievableWriter(private val connection: CottontailConnection) : Compare( Column(this.entityName.column(RETRIEVABLE_ID_COLUMN_NAME)), Compare.Operator.EQUAL, - Literal(item.id.toString()) + Literal(UuidValue(item.id)) ) ) @@ -193,7 +194,7 @@ internal class RetrievableWriter(private val connection: CottontailConnection) : * @return True on success, false otherwise. */ override fun deleteAll(items: Iterable): Boolean { - val ids = items.map { StringValue(it.id.toString()) } + val ids = items.map { UuidValue(it.id) } val delete = Delete(this.entityName).where( Compare( Column(this.entityName.column(RETRIEVABLE_ID_COLUMN_NAME)),