Skip to content

Commit

Permalink
adds transformer for benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
net-cscience-raphael committed Dec 4, 2024
1 parent 753014b commit ba9a936
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ class ScalarDescriptorReader(field: Schema.Field<*, ScalarDescriptor<*, *>>, con
val descriptorId = result.getObject(DESCRIPTOR_ID_COLUMN_NAME, UUID::class.java)
val retrievableId = result.getObject(RETRIEVABLE_ID_COLUMN_NAME, UUID::class.java)
return when (this.prototype) {
is BooleanDescriptor -> BooleanDescriptor(descriptorId, retrievableId, Value.Boolean(result.getBoolean(VALUE_ATTRIBUTE_NAME)))
is ByteDescriptor -> ByteDescriptor(descriptorId, retrievableId, Value.Byte(result.getByte(VALUE_ATTRIBUTE_NAME)))
is ShortDescriptor -> ShortDescriptor(descriptorId, retrievableId, Value.Short(result.getShort(VALUE_ATTRIBUTE_NAME)))
is IntDescriptor -> IntDescriptor(descriptorId, retrievableId, Value.Int(result.getInt(VALUE_ATTRIBUTE_NAME)))
is LongDescriptor -> LongDescriptor(descriptorId, retrievableId, Value.Long(result.getLong(VALUE_ATTRIBUTE_NAME)))
is FloatDescriptor -> FloatDescriptor(descriptorId, retrievableId, Value.Float(result.getFloat(VALUE_ATTRIBUTE_NAME)))
is DoubleDescriptor -> DoubleDescriptor(descriptorId, retrievableId, Value.Double(result.getDouble(VALUE_ATTRIBUTE_NAME)))
is StringDescriptor -> StringDescriptor(descriptorId, retrievableId, Value.String(result.getString(VALUE_ATTRIBUTE_NAME)))
is TextDescriptor -> TextDescriptor(descriptorId, retrievableId, Value.Text(result.getString(VALUE_ATTRIBUTE_NAME)))
is BooleanDescriptor -> BooleanDescriptor(descriptorId, retrievableId, Value.Boolean(result.getBoolean(VALUE_ATTRIBUTE_NAME)), this.field as Schema.Field<*, BooleanDescriptor>)
is ByteDescriptor -> ByteDescriptor(descriptorId, retrievableId, Value.Byte(result.getByte(VALUE_ATTRIBUTE_NAME)), this.field as Schema.Field<*, ByteDescriptor>)
is ShortDescriptor -> ShortDescriptor(descriptorId, retrievableId, Value.Short(result.getShort(VALUE_ATTRIBUTE_NAME)), this.field as Schema.Field<*, ShortDescriptor>)
is IntDescriptor -> IntDescriptor(descriptorId, retrievableId, Value.Int(result.getInt(VALUE_ATTRIBUTE_NAME)), this.field as Schema.Field<*, IntDescriptor>)
is LongDescriptor -> LongDescriptor(descriptorId, retrievableId, Value.Long(result.getLong(VALUE_ATTRIBUTE_NAME)), this.field as Schema.Field<*, LongDescriptor>)
is FloatDescriptor -> FloatDescriptor(descriptorId, retrievableId, Value.Float(result.getFloat(VALUE_ATTRIBUTE_NAME)), this.field as Schema.Field<*, FloatDescriptor>)
is DoubleDescriptor -> DoubleDescriptor(descriptorId, retrievableId, Value.Double(result.getDouble(VALUE_ATTRIBUTE_NAME)), this.field as Schema.Field<*, DoubleDescriptor>)
is StringDescriptor -> StringDescriptor(descriptorId, retrievableId, Value.String(result.getString(VALUE_ATTRIBUTE_NAME)), this.field as Schema.Field<*, StringDescriptor>)
is TextDescriptor -> TextDescriptor(descriptorId, retrievableId, Value.Text(result.getString(VALUE_ATTRIBUTE_NAME)),this.field as Schema.Field<*, TextDescriptor>)
}
}

Expand All @@ -65,7 +65,7 @@ class ScalarDescriptorReader(field: Schema.Field<*, ScalarDescriptor<*, *>>, con
*/
private fun queryFulltext(query: SimpleFulltextQuery): Sequence<ScalarDescriptor<*, *>> {
val queryString = query.value.value.split(" ").map { "$it:*" }.joinToString(" | ") { it }
val statement = "SELECT * FROM \"${tableName.lowercase()}\" WHERE ${query.attributeName} @@ to_tsquery(?)"
val statement = "SELECT * FROM v3c.\"${tableName.lowercase()}\" WHERE ${query.attributeName} @@ to_tsquery(?)"
return sequence {
this@ScalarDescriptorReader.connection.jdbc.prepareStatement(statement).use { stmt ->
stmt.setString(1, queryString)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.vitrivr.engine.query.operators.transform.benchmark

import io.github.oshai.kotlinlogging.KLogger
import io.github.oshai.kotlinlogging.KotlinLogging
import org.apache.logging.log4j.message.Message
import org.vitrivr.engine.query.aggregate.logger
import java.nio.file.Path
import java.util.concurrent.BlockingQueue
import java.util.concurrent.LinkedBlockingQueue


class BenchmarkLogger(logfile: Path) : Runnable {
private val logger: KLogger = KotlinLogging.logger {}

private val queue: BlockingQueue<BenchmarkMessage> = LinkedBlockingQueue()

infix fun log(message: BenchmarkMessage) {
queue.add(message)
}

override fun run() {
while (true) {
try {
val log = queue.take()
logger.info { log }
} catch (e: InterruptedException) {
e.printStackTrace()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.vitrivr.engine.query.operators.transform.benchmark

import kotlinx.serialization.Serializable

@Serializable
data class BenchmarkMessage (
val name: String,
val source: String,
val timestamp: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.vitrivr.engine.query.operators.transform.benchmark

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emitAll
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.Retrievable
import org.vitrivr.engine.core.model.retrievable.Retrieved
import org.vitrivr.engine.core.model.retrievable.attributes.PropertyAttribute
import org.vitrivr.engine.core.model.types.Value
import org.vitrivr.engine.core.operators.Operator
import org.vitrivr.engine.core.operators.general.Transformer
import java.nio.file.Path
import javax.management.Descriptor

/**
* Appends [Descriptor] to a [Retrieved] based on the values of a [Schema.Field], if available.
*
* @version 1.1.2
* @author Luca Rossetto
* @author Ralph Gasser
*/
class TimeBenchmark(
override val input: Operator<out Retrievable>,
val path: Path,
val pretty: String,
override val name: String
) : Transformer {

companion object {
@Volatile
private var bl: BenchmarkLogger? = null
}

init {
if (bl == null) {
bl = BenchmarkLogger(path)
Thread(bl).start()
}
}


override fun toFlow(scope: CoroutineScope): Flow<Retrievable> = flow {
bl!! log BenchmarkMessage(name, pretty, System.currentTimeMillis().toString())
emitAll(input.toFlow(scope))
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.vitrivr.engine.query.operators.transform.benchmark

import org.vitrivr.engine.core.context.Context
import org.vitrivr.engine.core.context.QueryContext
import org.vitrivr.engine.core.model.retrievable.Retrievable
import org.vitrivr.engine.core.operators.Operator
import org.vitrivr.engine.core.operators.general.TransformerFactory
import kotlin.io.path.Path

class TimeBenchmarkFactory() : TransformerFactory {
override fun newTransformer(name: String, input: Operator<out Retrievable>, context: Context): TimeBenchmark {
require(context is QueryContext)
val logfilePath = Path(context[name, "logfile"]?.toString() ?: "benchmark.log")
val prettyName = context[name, "pretty"]?.toString() ?: name
return TimeBenchmark(input, logfilePath, prettyName, name)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ import java.sql.Date
import javax.management.Descriptor

/**
* Appends [Descriptor] to a [Retrieved] based on the values of a [Schema.Field], if available.
* Checks if a retrieved has a descriptor for a given field and key.
* On missing key, the filter follows the skip strategy.
* If the key is found, the value is compared to the provided value and the retrieved is filtered accordingly to the comparison operator.
*
* @version 1.1.2
* @version 1.1.3
* @author Luca Rossetto
* @author Ralph Gasser
* @author Raphael Waltenspuel
*/
class LateFilter(
override val input: Operator<out Retrievable>,
Expand All @@ -37,7 +40,7 @@ class LateFilter(
/* appends late filter */
val limit: Int = Int.MAX_VALUE,
/* on missing key skip */
val skip: Skip = Skip fromString "error",
val skip: Skip = Skip fromString "error",

override val name: String
) : Transformer {
Expand All @@ -55,8 +58,8 @@ class LateFilter(
/* Emit retrievable with added attribute. */
inputRetrieved.forEach { retrieved ->

val descriptors = retrieved.findDescriptor { it.field?.fieldName == fieldName }
if (descriptors.isEmpty() || descriptors.first().values().containsKey(keys[0]).not()) {
val descriptors = retrieved.findDescriptor() { it.field?.fieldName == fieldName }
if (descriptors.isEmpty()) {
when (skip) {
Skip.ERROR -> throw IllegalArgumentException("no descriptor found for field $fieldName")
Skip.WARN -> logger.warn { "no descriptor found for field $fieldName" }.also { return@forEach }
Expand All @@ -69,6 +72,7 @@ class LateFilter(
val attribute = keys.map {
(when (values[it]) {
is Value.String -> Pair(it to (values[it] as Value.String), Value.of(value.toString()))
is Value.Text -> Pair(it to (values[it] as Value.Text), Value.of(value.toString()))
is Value.Boolean -> Pair(it to (values[it] as Value.Boolean), Value.of(value.toBoolean()))
is Value.Int -> Pair(it to (values[it] as Value.Int), Value.of(value.toInt()))
is Value.Long -> Pair(it to (values[it] as Value.Long), Value.of(value.toLong()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class FieldLookup(
retrieved.addAttribute(PropertyAttribute(keys.map{
it to (when(values[it]){
is Value.String -> (values[it] as Value.String).value
is Value.Text -> (values[it] as Value.Text).value
is Value.Boolean -> (values[it] as Value.Boolean).value
is Value.Int -> (values[it] as Value.Int).value
is Value.Long -> (values[it] as Value.Long).value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ org.vitrivr.engine.query.operators.transform.lookup.ObjectFieldLookupFactory
org.vitrivr.engine.query.operators.transform.filter.FieldLookupLateFilterFactory
org.vitrivr.engine.query.operators.transform.filter.LateFilterFactory
org.vitrivr.engine.query.operators.transform.scoring.ScoreExaggeratorFactory
org.vitrivr.engine.query.operators.transform.benchmark.TimeBenchmarkFactory

0 comments on commit ba9a936

Please sign in to comment.