v0.8.2 #80

Open
altavir wants to merge 63 commits from dev into master
5 changed files with 87 additions and 8 deletions
Showing only changes of commit 1f1f894e0d - Show all commits

View File

@ -4,7 +4,7 @@ plugins {
description = "IO module"
val ioVersion = "0.3.1"
val ioVersion = "0.4.0"
kscience {
jvm()

View File

@ -0,0 +1,51 @@
package pace.kscience.dataforge.io.proto
import kotlinx.io.writeString
import space.kscience.dataforge.io.Envelope
import space.kscience.dataforge.meta.asValue
import kotlin.concurrent.thread
import kotlin.time.measureTime
public fun main() {
val envelope = Envelope {
meta {
"a" put 22
"node" put {
"b" put "DDD"
"c" put 11.1
"d" put {
"d1" put {
"d11" put "aaa"
"d12" put "bbb"
}
"d2" put 2
}
"array" put doubleArrayOf(1.0, 2.0, 3.0)
"array2d" put listOf(
doubleArrayOf(1.0, 2.0, 3.0).asValue(),
doubleArrayOf(1.0, 2.0, 3.0).asValue()
).asValue()
}
}
data {
writeString("Hello world!")
}
}
val format = ProtoEnvelopeFormat
measureTime {
val threads = List(100) {
thread {
repeat(100000) {
val buffer = kotlinx.io.Buffer()
format.writeTo(buffer, envelope)
// println(buffer.size)
val r = format.readFrom(buffer)
}
}
}
threads.forEach { it.join() }
}.also { println(it) }
}

View File

@ -11,7 +11,7 @@ import kotlin.properties.ReadOnlyProperty
/**
* A reference to a read-only value of type [T] inside [MetaProvider]
* A reference to a read-only value of type [T] inside [MetaProvider] or writable value in [MutableMetaProvider]
*/
@DFExperimental
public data class MetaRef<T>(
@ -20,21 +20,36 @@ public data class MetaRef<T>(
override val descriptor: MetaDescriptor? = converter.descriptor,
) : Described
/**
* Get a value from provider by [ref] or return null if node with given name is missing
*/
@DFExperimental
public operator fun <T> MetaProvider.get(ref: MetaRef<T>): T? = get(ref.name)?.let { ref.converter.readOrNull(it) }
/**
* Set a value in a mutable provider by [ref]
*/
@DFExperimental
public operator fun <T> MutableMetaProvider.set(ref: MetaRef<T>, value: T) {
set(ref.name, ref.converter.convert(value))
}
/**
* Remove a node corresponding to [ref] from a mutable provider if it exists
*/
@DFExperimental
public class MetaSpec(
private val configuration: MetaDescriptorBuilder.() -> Unit = {},
) : Described {
public fun MutableMetaProvider.remove(ref: MetaRef<*>) {
remove(ref.name)
}
/**
* A base class for [Meta] specification that stores references to meta nodes
*/
@DFExperimental
public abstract class MetaSpec : Described {
private val refs: MutableList<MetaRef<*>> = mutableListOf()
private fun registerRef(ref: MetaRef<*>) {
protected fun registerRef(ref: MetaRef<*>) {
refs.add(ref)
}
@ -51,6 +66,8 @@ public class MetaSpec(
}
}
protected open fun MetaDescriptorBuilder.buildDescriptor(): Unit = Unit
override val descriptor: MetaDescriptor by lazy {
MetaDescriptor {
refs.forEach { ref ->
@ -58,7 +75,7 @@ public class MetaSpec(
node(ref.name, ref.descriptor)
}
}
configuration()
buildDescriptor()
}
}
}

View File

@ -8,6 +8,9 @@ import kotlinx.serialization.descriptors.element
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
/**
* A serializer for [Value]
*/
public object ValueSerializer : KSerializer<Value> {
private val listSerializer by lazy { ListSerializer(ValueSerializer) }

View File

@ -21,6 +21,9 @@ public class LazyParsedValue(public val string: String) : Value {
override fun hashCode(): Int = string.hashCode()
}
/**
* Read this string as lazily parsed value
*/
public fun String.lazyParseValue(): LazyParsedValue = LazyParsedValue(this)
/**
@ -47,6 +50,9 @@ public class DoubleArrayValue(override val value: DoubleArray) : Value, Iterable
override fun iterator(): Iterator<Double> = value.iterator()
}
/**
* A zero-copy wrapping of this [DoubleArray] in a [Value]
*/
public fun DoubleArray.asValue(): Value = if (isEmpty()) Null else DoubleArrayValue(this)
public val Value.doubleArray: DoubleArray
@ -75,7 +81,9 @@ public fun MutableMetaProvider.doubleArray(
reader = { it?.doubleArray ?: doubleArrayOf(*default) },
)
/**
* A [Value] wrapping a [ByteArray]
*/
public class ByteArrayValue(override val value: ByteArray) : Value, Iterable<Byte> {
override val type: ValueType get() = ValueType.LIST
override val list: List<Value> get() = value.map { NumberValue(it) }