minor data refactor

This commit is contained in:
Alexander Nozik 2021-02-02 12:09:44 +03:00
parent 11ba116a89
commit 6a0bfae931
12 changed files with 89 additions and 89 deletions

View File

@ -39,7 +39,7 @@ public class ReduceGroupBuilder<T : Any, R : Any>(
*/
public fun byValue(tag: String, defaultTag: String = "@default", action: JoinGroup<T, R>.() -> Unit) {
groupRules += { node ->
GroupRule.byValue(scope, tag, defaultTag).gather(inputType, node).map {
GroupRule.byMetaValue(scope, tag, defaultTag).gather(inputType, node).map {
JoinGroup<T, R>(it.key, it.value).apply(action)
}
}

View File

@ -88,7 +88,7 @@ public suspend fun <T : Any> DataSetBuilder<T>.emit(data: NamedData<T>) {
/**
* Produce lazy [Data] and emit it into the [DataSetBuilder]
*/
public suspend inline fun <reified T : Any> DataSetBuilder<T>.emitLazy(
public suspend inline fun <reified T : Any> DataSetBuilder<T>.produce(
name: String,
meta: Meta = Meta.EMPTY,
noinline producer: suspend () -> T,
@ -97,7 +97,7 @@ public suspend inline fun <reified T : Any> DataSetBuilder<T>.emitLazy(
emit(name, data)
}
public suspend inline fun <reified T : Any> DataSetBuilder<T>.emitLazy(
public suspend inline fun <reified T : Any> DataSetBuilder<T>.produce(
name: Name,
meta: Meta = Meta.EMPTY,
noinline producer: suspend () -> T,
@ -109,13 +109,13 @@ public suspend inline fun <reified T : Any> DataSetBuilder<T>.emitLazy(
/**
* Emit a static data with the fixed value
*/
public suspend fun <T : Any> DataSetBuilder<T>.emitStatic(name: String, data: T, meta: Meta = Meta.EMPTY): Unit =
public suspend fun <T : Any> DataSetBuilder<T>.static(name: String, data: T, meta: Meta = Meta.EMPTY): Unit =
emit(name, Data.static(data, meta))
public suspend fun <T : Any> DataSetBuilder<T>.emitStatic(name: Name, data: T, meta: Meta = Meta.EMPTY): Unit =
public suspend fun <T : Any> DataSetBuilder<T>.static(name: Name, data: T, meta: Meta = Meta.EMPTY): Unit =
emit(name, Data.static(data, meta))
public suspend fun <T : Any> DataSetBuilder<T>.emitStatic(
public suspend fun <T : Any> DataSetBuilder<T>.static(
name: String,
data: T,
metaBuilder: MetaBuilder.() -> Unit,

View File

@ -19,6 +19,7 @@ import hep.dataforge.meta.get
import hep.dataforge.meta.string
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlin.reflect.KClass
public interface GroupRule {
@ -33,7 +34,7 @@ public interface GroupRule {
* @param defaultTagValue
* @return
*/
public fun byValue(
public fun byMetaValue(
scope: CoroutineScope,
key: String,
defaultTagValue: String,
@ -50,31 +51,16 @@ public interface GroupRule {
map.getOrPut(tagValue) { ActiveDataTree(dataType) }.emit(data.name, data.data)
}
scope.launch {
set.updates.collect { name ->
val data = set.getData(name)
val tagValue = data?.meta[key]?.string ?: defaultTagValue
map.getOrPut(tagValue) { ActiveDataTree(dataType) }.emit(name, data)
}
}
return map
}
}
// @ValueDef(key = "byValue", required = true, info = "The name of annotation value by which grouping should be made")
// @ValueDef(
// key = "defaultValue",
// def = "default",
// info = "Default value which should be used for content in which the grouping value is not presented"
// )
// public fun byMeta(scope: CoroutineScope, config: Meta): GroupRule {
// //TODO expand grouping options
// return config["byValue"]?.string?.let {
// byValue(
// scope,
// it,
// config["defaultValue"]?.string ?: "default"
// )
// } ?: object : GroupRule {
// override suspend fun <T : Any> gather(
// dataType: KClass<T>,
// source: DataSource<T>,
// ): Map<String, DataSource<T>> = mapOf("" to source)
// }
// }
}
}

View File

@ -2,7 +2,7 @@ package hep.dataforge.data
import hep.dataforge.names.*
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.collect
import kotlin.reflect.KClass
@PublishedApi
@ -22,7 +22,7 @@ internal class StaticDataTree<T : Any>(
}
}
fun getOrCreateNode(name: Name): StaticDataTree<T> = when (name.length) {
private fun getOrCreateNode(name: Name): StaticDataTree<T> = when (name.length) {
0 -> this
1 -> {
val itemName = name.firstOrNull()!!

View File

@ -32,7 +32,6 @@ public fun <T : Any> DataSet<T>.filter(
}
}
/**
* Generate a wrapper data set with a given name prefix appended to all names
*/
@ -48,7 +47,6 @@ else object : ActiveDataSet<T> {
override val updates: Flow<Name> get() = this@withNamePrefix.updates.map { prefix + it }
}
/**
* Get a subset of data starting with a given [branchName]
*/

View File

@ -14,7 +14,7 @@ class ActionsTest {
val data: DataTree<Int> = runBlocking {
DataTree {
repeat(10) {
emitStatic(it.toString(), it)
static(it.toString(), it)
}
}
}

View File

@ -8,6 +8,22 @@ import kotlin.test.assertEquals
internal class DataTreeBuilderTest {
@Test
fun testTreeBuild() = runBlocking {
val node = DataTree<Any> {
"primary" put {
static("a", "a")
static("b", "b")
}
static("c.d", "c.d")
static("c.f", "c.f")
}
assertEquals("a", node.getData("primary.a")?.value())
assertEquals("b", node.getData("primary.b")?.value())
assertEquals("c.d", node.getData("c.d")?.value())
assertEquals("c.f", node.getData("c.f")?.value())
}
@Test
fun testDataUpdate() = runBlocking {
val updateData: DataTree<Any> = DataTree {
@ -18,11 +34,11 @@ internal class DataTreeBuilderTest {
}
val node = DataTree<Any> {
emit("primary") {
emitStatic("a", "a")
emitStatic("b", "b")
"primary" put {
static("a", "a")
static("b", "b")
}
emitStatic("root", "root")
static("root", "root")
populate(updateData)
}
@ -40,7 +56,7 @@ internal class DataTreeBuilderTest {
updateJob = launch {
repeat(10) {
delay(10)
emitStatic("value", it)
static("value", it)
}
delay(10)
}

View File

@ -49,7 +49,7 @@ class DataPropagationTest {
runBlocking {
data {
repeat(100) {
emitStatic("myData[$it]", it)
static("myData[$it]", it)
}
}
}

View File

@ -23,11 +23,11 @@ class FileDataTest {
val dataNode = runBlocking {
DataTree<String> {
emit("dir") {
emitStatic("a", "Some string") {
static("a", "Some string") {
"content" put "Some string"
}
}
emitStatic("b", "root data")
static("b", "root data")
meta {
"content" put "This is root meta node"
}

View File

@ -56,7 +56,7 @@ class SimpleWorkspaceTest {
data {
repeat(100) {
emitStatic("myData[$it]", it)
static("myData[$it]", it)
}
}