Non-suspend data tree builder

This commit is contained in:
Andrey Stoyan 2022-05-31 22:50:09 +03:00
parent 9c55d26be5
commit 196854429a
3 changed files with 23 additions and 15 deletions

View File

@ -1,9 +1,18 @@
package space.kscience.dataforge.data package space.kscience.dataforge.data
import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.collect
import space.kscience.dataforge.misc.DFExperimental import space.kscience.dataforge.misc.DFExperimental
import space.kscience.dataforge.names.* import space.kscience.dataforge.names.Name
import space.kscience.dataforge.names.NameToken
import space.kscience.dataforge.names.asName
import space.kscience.dataforge.names.cutFirst
import space.kscience.dataforge.names.cutLast
import space.kscience.dataforge.names.firstOrNull
import space.kscience.dataforge.names.isEmpty
import space.kscience.dataforge.names.lastOrNull
import space.kscience.dataforge.names.length
import space.kscience.dataforge.names.plus
import kotlin.collections.set
import kotlin.reflect.KType import kotlin.reflect.KType
import kotlin.reflect.typeOf import kotlin.reflect.typeOf
@ -61,6 +70,12 @@ internal class StaticDataTree<T : Any>(
} }
} }
@Suppress("FunctionName")
public fun <T : Any> DataTree(dataType: KType): DataTree<T> = StaticDataTree(dataType)
@Suppress("FunctionName")
public inline fun <reified T : Any> DataTree(): DataTree<T> = DataTree(typeOf<T>())
@Suppress("FunctionName") @Suppress("FunctionName")
public suspend fun <T : Any> DataTree( public suspend fun <T : Any> DataTree(
dataType: KType, dataType: KType,
@ -73,6 +88,6 @@ public suspend inline fun <reified T : Any> DataTree(
): DataTree<T> = DataTree(typeOf<T>(), block) ): DataTree<T> = DataTree(typeOf<T>(), block)
@OptIn(DFExperimental::class) @OptIn(DFExperimental::class)
public suspend fun <T : Any> DataSet<T>.seal(): DataTree<T> = DataTree(dataType){ public suspend fun <T : Any> DataSet<T>.seal(): DataTree<T> = DataTree(dataType) {
populate(this@seal) populate(this@seal)
} }

View File

@ -1,12 +1,10 @@
package space.kscience.dataforge.distributed package space.kscience.dataforge.distributed
import io.ktor.utils.io.core.* import io.ktor.utils.io.core.*
import io.lambdarpc.coding.coders.JsonCoder
import io.lambdarpc.dsl.LibService import io.lambdarpc.dsl.LibService
import io.lambdarpc.dsl.def import io.lambdarpc.dsl.def
import io.lambdarpc.dsl.j import io.lambdarpc.dsl.j
import io.lambdarpc.utils.ServiceId import io.lambdarpc.utils.ServiceId
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import space.kscience.dataforge.context.Context import space.kscience.dataforge.context.Context
import space.kscience.dataforge.context.Global import space.kscience.dataforge.context.Global
@ -32,7 +30,7 @@ public class ServiceWorkspace(
port: Int? = null, port: Int? = null,
override val context: Context = Global.buildContext("workspace".asName()), override val context: Context = Global.buildContext("workspace".asName()),
private val dataSerializer: KSerializer<Any>? = null, private val dataSerializer: KSerializer<Any>? = null,
data: DataSet<*> = runBlocking { DataTree<Any> {} }, data: DataSet<*> = DataTree<Any>(),
override val targets: Map<String, Meta> = mapOf(), override val targets: Map<String, Meta> = mapOf(),
) : Workspace, Closeable { ) : Workspace, Closeable {
private val _port: Int? = port private val _port: Int? = port
@ -113,9 +111,6 @@ public class ServiceWorkspace(
public companion object { public companion object {
internal val serviceId = ServiceId("d41b95b1-828b-4444-8ff0-6f9c92a79246") internal val serviceId = ServiceId("d41b95b1-828b-4444-8ff0-6f9c92a79246")
internal val execute by serviceId.def( internal val execute by serviceId.def(j<Name>(), j(MetaSerializer), j<TaskRegistry>(), j<DataSetPrototype>())
JsonCoder(Name.serializer()), JsonCoder(MetaSerializer), j<TaskRegistry>(),
j<DataSetPrototype>()
)
} }
} }

View File

@ -24,15 +24,13 @@ internal class RemoteCallTest {
private lateinit var workspace: Workspace private lateinit var workspace: Workspace
@BeforeAll @BeforeAll
fun before() { fun before() = runBlocking {
worker1 = ServiceWorkspace( worker1 = ServiceWorkspace(
context = Global.buildContext("worker1".asName()) { context = Global.buildContext("worker1".asName()) {
plugin(MyPlugin1) plugin(MyPlugin1)
}, },
data = runBlocking { data = DataTree<Any> {
DataTree<Any> { static("int", 42)
static("int", 42)
}
}, },
) )
worker1.start() worker1.start()