Documentation update

This commit is contained in:
Alexander Nozik 2024-07-22 10:01:48 +03:00
parent b77fc9a0d5
commit 1f1f894e0d
5 changed files with 87 additions and 8 deletions

View File

@ -4,7 +4,7 @@ plugins {
description = "IO module" description = "IO module"
val ioVersion = "0.3.1" val ioVersion = "0.4.0"
kscience { kscience {
jvm() 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 @DFExperimental
public data class MetaRef<T>( public data class MetaRef<T>(
@ -20,21 +20,36 @@ public data class MetaRef<T>(
override val descriptor: MetaDescriptor? = converter.descriptor, override val descriptor: MetaDescriptor? = converter.descriptor,
) : Described ) : Described
/**
* Get a value from provider by [ref] or return null if node with given name is missing
*/
@DFExperimental @DFExperimental
public operator fun <T> MetaProvider.get(ref: MetaRef<T>): T? = get(ref.name)?.let { ref.converter.readOrNull(it) } 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 @DFExperimental
public operator fun <T> MutableMetaProvider.set(ref: MetaRef<T>, value: T) { public operator fun <T> MutableMetaProvider.set(ref: MetaRef<T>, value: T) {
set(ref.name, ref.converter.convert(value)) set(ref.name, ref.converter.convert(value))
} }
/**
* Remove a node corresponding to [ref] from a mutable provider if it exists
*/
@DFExperimental @DFExperimental
public class MetaSpec( public fun MutableMetaProvider.remove(ref: MetaRef<*>) {
private val configuration: MetaDescriptorBuilder.() -> Unit = {}, remove(ref.name)
) : Described { }
/**
* 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 val refs: MutableList<MetaRef<*>> = mutableListOf()
private fun registerRef(ref: MetaRef<*>) { protected fun registerRef(ref: MetaRef<*>) {
refs.add(ref) refs.add(ref)
} }
@ -51,6 +66,8 @@ public class MetaSpec(
} }
} }
protected open fun MetaDescriptorBuilder.buildDescriptor(): Unit = Unit
override val descriptor: MetaDescriptor by lazy { override val descriptor: MetaDescriptor by lazy {
MetaDescriptor { MetaDescriptor {
refs.forEach { ref -> refs.forEach { ref ->
@ -58,7 +75,7 @@ public class MetaSpec(
node(ref.name, ref.descriptor) 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.Decoder
import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.encoding.Encoder
/**
* A serializer for [Value]
*/
public object ValueSerializer : KSerializer<Value> { public object ValueSerializer : KSerializer<Value> {
private val listSerializer by lazy { ListSerializer(ValueSerializer) } 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() override fun hashCode(): Int = string.hashCode()
} }
/**
* Read this string as lazily parsed value
*/
public fun String.lazyParseValue(): LazyParsedValue = LazyParsedValue(this) 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() 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 fun DoubleArray.asValue(): Value = if (isEmpty()) Null else DoubleArrayValue(this)
public val Value.doubleArray: DoubleArray public val Value.doubleArray: DoubleArray
@ -75,7 +81,9 @@ public fun MutableMetaProvider.doubleArray(
reader = { it?.doubleArray ?: doubleArrayOf(*default) }, reader = { it?.doubleArray ?: doubleArrayOf(*default) },
) )
/**
* A [Value] wrapping a [ByteArray]
*/
public class ByteArrayValue(override val value: ByteArray) : Value, Iterable<Byte> { public class ByteArrayValue(override val value: ByteArray) : Value, Iterable<Byte> {
override val type: ValueType get() = ValueType.LIST override val type: ValueType get() = ValueType.LIST
override val list: List<Value> get() = value.map { NumberValue(it) } override val list: List<Value> get() = value.map { NumberValue(it) }