-
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.
Signed-off-by: Ralph Gasser <[email protected]>
- Loading branch information
1 parent
d77363e
commit 1cc07d8
Showing
2 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
...ne-index/src/main/kotlin/org/vitrivr/engine/index/transform/ContentSamplingTransformer.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,46 @@ | ||
package org.vitrivr.engine.index.transform | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.filter | ||
import org.vitrivr.engine.core.context.IndexContext | ||
import org.vitrivr.engine.core.model.content.decorators.SourcedContent | ||
import org.vitrivr.engine.core.model.content.element.ContentElement | ||
import org.vitrivr.engine.core.operators.Operator | ||
import org.vitrivr.engine.core.operators.ingest.Decoder | ||
import org.vitrivr.engine.core.operators.ingest.Transformer | ||
import org.vitrivr.engine.core.operators.ingest.TransformerFactory | ||
import org.vitrivr.engine.core.source.Source | ||
import kotlin.reflect.KClass | ||
|
||
/** | ||
* A [Transformer] that samples the input [Flow] and only passes through every n-th element. | ||
* | ||
* @author Ralph Gasser | ||
* @version 1.0.0 | ||
*/ | ||
class ContentSamplingTransformer : TransformerFactory { | ||
override fun newOperator(input: Decoder, context: IndexContext, parameters: Map<String, String>): Transformer = Instance(input, parameters["sample"]?.toIntOrNull() ?: 10) | ||
override fun newOperator(input: Transformer, context: IndexContext, parameters: Map<String, String>): Transformer = Instance(input, parameters["sample"]?.toIntOrNull() ?: 10) | ||
|
||
private class Instance(override val input: Operator<ContentElement<*>>, private val sample: Int) : Transformer { | ||
override fun toFlow(scope: CoroutineScope): Flow<ContentElement<*>> { | ||
val sources = mutableMapOf<KClass<out ContentElement<*>>, Source>() | ||
val counters = mutableMapOf<KClass<out ContentElement<*>>, Int>() | ||
return this.input.toFlow(scope).filter { value: ContentElement<*> -> | ||
if (value is SourcedContent) { /* Only source content can be sampled. */ | ||
val clazz = value::class | ||
if (sources[clazz] == null || sources[clazz] != value.source) { | ||
sources[clazz] = value.source | ||
counters[clazz] = 0 | ||
} | ||
val pass = (counters[clazz]!! % this.sample == 0) | ||
counters[clazz] = counters[clazz]!! + 1 | ||
pass | ||
} else { | ||
true | ||
} | ||
} | ||
} | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
...n/resources/META-INF/services/org.vitrivr.engine.core.operators.ingest.TransformerFactory
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
org.vitrivr.engine.index.transform.PassthroughTransformer | ||
org.vitrivr.engine.index.transform.PassthroughTransformer | ||
org.vitrivr.engine.index.transform.ContentSamplingTransformer |