-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from vitrivr/feature/filtercriteria
[Feature] Filter Criteria for Late Filtering
- Loading branch information
Showing
17 changed files
with
436 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...src/main/kotlin/org/vitrivr/engine/query/operators/transform/benchmark/BenchmarkLogger.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.vitrivr.engine.query.operators.transform.benchmark | ||
|
||
import io.github.oshai.kotlinlogging.KLogger | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.encodeToJsonElement | ||
import java.io.* | ||
import java.nio.file.Path | ||
import java.util.concurrent.BlockingQueue | ||
import java.util.concurrent.LinkedBlockingQueue | ||
|
||
|
||
class BenchmarkLogger(val 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) { | ||
|
||
val log = queue.take() | ||
logger.info { log } | ||
|
||
|
||
FileOutputStream(File(logfile.toString()), true).bufferedWriter().use { writer -> | ||
writer.appendLine("${Json.encodeToJsonElement(log).toString()},") | ||
writer.close() | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...rc/main/kotlin/org/vitrivr/engine/query/operators/transform/benchmark/BenchmarkMessage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
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, | ||
val inputSize: Int, | ||
) |
54 changes: 54 additions & 0 deletions
54
...y/src/main/kotlin/org/vitrivr/engine/query/operators/transform/benchmark/TimeBenchmark.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
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 java.time.LocalDateTime | ||
import java.util.Timer | ||
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 { | ||
val inputRetrieved = input.toFlow(scope).toList() | ||
bl!! log BenchmarkMessage(name, pretty, LocalDateTime.now().toString(), inputRetrieved.size) | ||
inputRetrieved.forEach { emit(it) } | ||
} | ||
} | ||
|
||
|
17 changes: 17 additions & 0 deletions
17
...ain/kotlin/org/vitrivr/engine/query/operators/transform/benchmark/TimeBenchmarkFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
.../main/kotlin/org/vitrivr/engine/query/operators/transform/filter/FieldLookupLateFilter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package org.vitrivr.engine.query.operators.transform.filter | ||
|
||
import io.github.oshai.kotlinlogging.KLogger | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
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.metamodel.Schema | ||
import org.vitrivr.engine.core.model.query.basics.ComparisonOperator | ||
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.sql.Date | ||
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 FieldLookupLateFilter( | ||
override val input: Operator<out Retrievable>, | ||
/* The reader for a given field. */ | ||
private val reader: DescriptorReader<*>, | ||
/* keys to filter on */ | ||
val keys: List<String>, | ||
/* boolean operator*/ | ||
val comparison: ComparisonOperator = ComparisonOperator.EQ, | ||
/* value to compare to */ | ||
val value: String, | ||
/* append field*/ | ||
val append: Boolean, | ||
/* appends late filter */ | ||
val limit: Int = Int.MAX_VALUE, | ||
override val name: String | ||
) : Transformer { | ||
private val logger: KLogger = KotlinLogging.logger {} | ||
|
||
override fun toFlow(scope: CoroutineScope): Flow<Retrievable> = flow { | ||
/* Parse input IDs.*/ | ||
val inputRetrieved = input.toFlow(scope).toList() | ||
|
||
/* Fetch entries for the provided IDs. */ | ||
val ids = inputRetrieved.map { it.id }.toSet() | ||
val descriptors = if (ids.isEmpty()) { | ||
emptyMap() | ||
} else { | ||
this@FieldLookupLateFilter.reader.getAllForRetrievable(ids).associateBy { it.retrievableId!! } | ||
} | ||
|
||
// Multi keys for | ||
if (keys.size > 1) | ||
throw IllegalArgumentException("only one key is supported yet") | ||
|
||
var emitted = 0 | ||
/* Emit retrievable with added attribute. */ | ||
inputRetrieved.forEach { retrieved -> | ||
val descriptor = descriptors[retrieved.id] | ||
if (descriptor != null) { | ||
//retrieved.addDescriptor(descriptor) | ||
/* Somewhat experimental. Goal: Attach information in a meaningful manner, such that it can be serialised */ | ||
val values = descriptor.values().toMap() | ||
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())) | ||
is Value.Float -> Pair(it to (values[it] as Value.Float), Value.of(value.toFloat())) | ||
is Value.Double -> Pair(it to (values[it] as Value.Double), Value.of(value.toDouble())) | ||
is Value.Byte -> Pair(it to (values[it] as Value.Byte), Value.of(value.toByte())) | ||
is Value.Short -> Pair(it to (values[it] as Value.Short), Value.of(value.toShort())) | ||
is Value.DateTime -> Pair(it to (values[it] as Value.DateTime), Value.of(Date.valueOf(value))) | ||
else -> Pair(it to null, null) | ||
}) | ||
} | ||
|
||
retrieved.takeIf { append == true }?.let { | ||
retrieved.addDescriptor(descriptor) | ||
retrieved.addAttribute(PropertyAttribute(attribute.map { it.first.first.toString() to it.first.second!!.value.toString() } | ||
.toMap())) | ||
} | ||
|
||
attribute[0].takeIf { it.first.second != null && it.second != null }?.let { | ||
it.takeIf { ++emitted <= limit && comparison.compare(it.first.second!!, it.second!!) }?.let { | ||
emit(retrieved) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.