Refactor serialization

This commit is contained in:
Andrey Stoyan 2022-06-04 18:20:39 +03:00
parent ba3354b4a2
commit 50a9e7d314
2 changed files with 7 additions and 34 deletions

View File

@ -1,15 +1,11 @@
package space.kscience.dataforge.distributed.serialization package space.kscience.dataforge.distributed.serialization
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import space.kscience.dataforge.data.Data import space.kscience.dataforge.data.Data
import space.kscience.dataforge.data.Goal import space.kscience.dataforge.data.StaticData
import space.kscience.dataforge.data.await import space.kscience.dataforge.data.await
import space.kscience.dataforge.meta.Meta
import space.kscience.dataforge.meta.MetaSerializer import space.kscience.dataforge.meta.MetaSerializer
import kotlin.reflect.KType import kotlin.reflect.KType
@ -22,10 +18,10 @@ internal data class DataPrototype(
val data: String, val data: String,
) { ) {
fun <T : Any> toData(type: KType, serializer: KSerializer<T>): Data<T> = fun <T : Any> toData(type: KType, serializer: KSerializer<T>): Data<T> =
SimpleData( StaticData(
type = type, type = type,
value = Json.decodeFromString(serializer, data),
meta = Json.decodeFromString(MetaSerializer, meta), meta = Json.decodeFromString(MetaSerializer, meta),
data = Json.decodeFromString(serializer, data)
) )
companion object { companion object {
@ -36,21 +32,3 @@ internal data class DataPrototype(
} }
} }
} }
/**
* Trivial [Data] implementation.
*/
private class SimpleData<T : Any>(
override val type: KType,
override val meta: Meta,
val data: T,
) : Data<T> {
override val dependencies: Collection<Goal<*>>
get() = emptyList()
override val deferred: Deferred<T>
get() = CompletableDeferred(data)
override fun async(coroutineScope: CoroutineScope): Deferred<T> = deferred
override fun reset() = Unit
}

View File

@ -10,6 +10,7 @@ import space.kscience.dataforge.data.NamedData
import space.kscience.dataforge.data.asIterable import space.kscience.dataforge.data.asIterable
import space.kscience.dataforge.data.component1 import space.kscience.dataforge.data.component1
import space.kscience.dataforge.data.component2 import space.kscience.dataforge.data.component2
import space.kscience.dataforge.data.named
import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.meta.Meta
import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.Name
import kotlin.reflect.KType import kotlin.reflect.KType
@ -23,7 +24,7 @@ internal data class DataSetPrototype(val meta: Meta, val data: Map<String, DataP
val data = data val data = data
.mapKeys { (name, _) -> Name.of(name) } .mapKeys { (name, _) -> Name.of(name) }
.mapValues { (_, dataPrototype) -> dataPrototype.toData(type, serializer) } .mapValues { (_, dataPrototype) -> dataPrototype.toData(type, serializer) }
return SerializableDataSetImpl(type, data, meta) return SimpleDataSet(type, data, meta)
} }
companion object { companion object {
@ -40,22 +41,16 @@ internal data class DataSetPrototype(val meta: Meta, val data: Map<String, DataP
/** /**
* Trivial [DataSet] implementation. * Trivial [DataSet] implementation.
*/ */
private class SerializableDataSetImpl<T : Any>( private class SimpleDataSet<T : Any>(
override val dataType: KType, override val dataType: KType,
private val data: Map<Name, Data<T>>, private val data: Map<Name, Data<T>>,
override val meta: Meta, override val meta: Meta,
) : DataSet<T> { ) : DataSet<T> {
/**
* Trivial named data implementation.
*/
private class SimpleNamedData<T : Any>(override val name: Name, override val data: Data<T>) :
NamedData<T>, Data<T> by data
override fun iterator(): Iterator<NamedData<T>> = override fun iterator(): Iterator<NamedData<T>> =
data data
.asSequence() .asSequence()
.map { (name, data) -> SimpleNamedData(name, data) } .map { (name, data) -> data.named(name) }
.iterator() .iterator()
override fun get(name: Name): Data<T>? = data[name] override fun get(name: Name): Data<T>? = data[name]