Skip to content

Commit

Permalink
Cottontail DB database driver now makes use of UUID values.
Browse files Browse the repository at this point in the history
Signed-off-by: Ralph Gasser <[email protected]>
  • Loading branch information
ppanopticon committed Dec 7, 2023
1 parent aa648d3 commit 8ec62fd
Show file tree
Hide file tree
Showing 15 changed files with 79 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -38,7 +38,7 @@ abstract class AbstractDescriptorReader<D : Descriptor>(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()) {
Expand All @@ -62,7 +62,7 @@ abstract class AbstractDescriptorReader<D : Descriptor>(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
Expand Down Expand Up @@ -95,7 +95,7 @@ abstract class AbstractDescriptorReader<D : Descriptor>(final override val field
*/
override fun getAll(ids: Iterable<UUID>): Sequence<D> {
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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -37,7 +37,7 @@ abstract class AbstractDescriptorWriter<D : Descriptor>(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))
)
)

Expand All @@ -59,7 +59,7 @@ abstract class AbstractDescriptorWriter<D : Descriptor>(final override val field
* @return True on success, false otherwise.
*/
override fun deleteAll(items: Iterable<D>): 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)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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<Any?> = 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. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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. */
Expand Down Expand Up @@ -78,8 +78,8 @@ class StructDescriptorWriter(field: Schema.Field<*, StructDescriptor>, connectio
}
val inserts: Array<Any?> = 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
}
}
Expand Down Expand Up @@ -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))
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class ScalarDescriptorInitializer<T : ScalarDescriptor<*>>(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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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. */
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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. */
Expand All @@ -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())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<StringDescriptor>(field, connection) {
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
}
}
Expand All @@ -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,
Expand Down
Loading

0 comments on commit 8ec62fd

Please sign in to comment.