From 62d33ddacd344e04bc13bd73f1edbdabc2d81799 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Thu, 28 Jan 2021 12:31:17 +0300 Subject: [PATCH] replace protoc with Wire --- numass-data-proto/build.gradle.kts | 44 ++---------------- .../inr/mass/data/proto/ProtoNumassPoint.kt | 45 ++++++++++--------- .../mass/data/proto/TestNumassDirectory.kt | 4 +- settings.gradle.kts | 18 ++++++-- 4 files changed, 45 insertions(+), 66 deletions(-) diff --git a/numass-data-proto/build.gradle.kts b/numass-data-proto/build.gradle.kts index 365153a..3513fd2 100644 --- a/numass-data-proto/build.gradle.kts +++ b/numass-data-proto/build.gradle.kts @@ -1,12 +1,7 @@ -import com.google.protobuf.gradle.proto -import com.google.protobuf.gradle.protobuf -import com.google.protobuf.gradle.protoc - plugins { - java kotlin("jvm") id("ru.mipt.npm.kscience") - id("com.google.protobuf") version "0.8.14" + id("com.squareup.wire") version "3.5.0" } kscience{ @@ -18,42 +13,9 @@ val dataforgeVersion: String by rootProject.extra dependencies { api(project(":numass-data-model")) api("hep.dataforge:dataforge-workspace:$dataforgeVersion") - implementation("com.google.protobuf:protobuf-java:3.14.0") implementation("javax.annotation:javax.annotation-api:1.3.1") } -tasks.withType { - dependsOn(":numass-data-proto:generateProto") -} - -sourceSets { - create("proto") { - proto { - srcDir("src/main/proto") - } - } - create("gen"){ - java{ - srcDir("gen/main/java") - } - } -} - -//kotlin{ -// sourceSets{ -// main{ -// de -// } -// } -//} - - -protobuf { - // Configure the protoc executable - protoc { - // Download from repositories - artifact = "com.google.protobuf:protoc:3.14.0" - } - - generatedFilesBaseDir = "$projectDir/gen" +wire{ + kotlin{} } diff --git a/numass-data-proto/src/main/kotlin/ru/inr/mass/data/proto/ProtoNumassPoint.kt b/numass-data-proto/src/main/kotlin/ru/inr/mass/data/proto/ProtoNumassPoint.kt index 1102f22..22728d3 100644 --- a/numass-data-proto/src/main/kotlin/ru/inr/mass/data/proto/ProtoNumassPoint.kt +++ b/numass-data-proto/src/main/kotlin/ru/inr/mass/data/proto/ProtoNumassPoint.kt @@ -37,22 +37,21 @@ import java.util.zip.Inflater */ public class ProtoNumassPoint( override val meta: Meta, - private val protoBuilder: () -> NumassProto.Point, + private val protoBuilder: () -> Point, ) : NumassPoint { - private val proto: NumassProto.Point get() = protoBuilder() + private val proto: Point get() = protoBuilder() override val blocks: List - get() = proto.channelsList - .flatMap { channel -> - channel.blocksList - .map { block -> ProtoBlock(channel.id.toInt(), block, this) } - .sortedBy { it.startTime } - } + get() = proto.channels.flatMap { channel -> + channel.blocks + .map { block -> ProtoBlock(channel.id.toInt(), block, this) } + .sortedBy { it.startTime } + } override val channels: Map - get() = proto.channelsList.groupBy { it.id.toInt() }.mapValues { entry -> - MetaBlock(entry.value.flatMap { it.blocksList }.map { ProtoBlock(entry.key, it, this) }) + get() = proto.channels.groupBy { it.id.toInt() }.mapValues { entry -> + MetaBlock(entry.value.flatMap { it.blocks }.map { ProtoBlock(entry.key, it, this) }) } override val voltage: Double get() = meta["external_meta.HV1_value"].double ?: super.voltage @@ -105,7 +104,7 @@ public class ProtoNumassPoint( public fun fromEnvelope(envelope: Envelope): ProtoNumassPoint? { val proto = envelope.useData { - NumassProto.Point.parseFrom(it) + Point.ADAPTER.decode(it) } return proto?.let { ProtoNumassPoint(envelope.meta) { it } } } @@ -115,7 +114,7 @@ public class ProtoNumassPoint( public class ProtoBlock( override val channel: Int, - private val block: NumassProto.Point.Channel.Block, + private val block: Point.Channel.Block, public val parent: NumassPoint? = null, ) : NumassBlock { @@ -136,14 +135,20 @@ public class ProtoBlock( } override val events: Flow - get() = if (block.hasEvents()) { + get() = if (block.events != null) { val events = block.events - if (events.timesCount != events.amplitudesCount) { + val amplitudes = events.amplitudes + val times = events.times + + if (times.size != amplitudes.size) { LoggerFactory.getLogger(javaClass) - .error("The block is broken. Number of times is ${events.timesCount} and number of amplitudes is ${events.amplitudesCount}") + .error("The block is broken. Number of times is ${times.size} and number of amplitudes is ${amplitudes.size}") } - (0..events.timesCount).asFlow() - .map { i -> NumassEvent(events.getAmplitudes(i).toShort(), events.getTimes(i), this) } + + amplitudes.zip(times) { amp, time -> + NumassEvent(amp.toShort(), time, this) + }.asFlow() + } else { emptyFlow() } @@ -151,10 +156,10 @@ public class ProtoBlock( override val frames: Flow get() { - val tickSize = Duration.ofNanos(block.binSize) - return block.framesList.asFlow().map { frame -> + val tickSize = Duration.ofNanos(block.bin_size) + return block.frames.asFlow().map { frame -> val time = startTime.plusNanos(frame.time) - val frameData = frame.data.asReadOnlyByteBuffer() + val frameData = frame.data_.asByteBuffer() NumassFrame(time, tickSize, frameData.asShortBuffer()) } } diff --git a/numass-data-proto/src/test/kotlin/ru/inr/mass/data/proto/TestNumassDirectory.kt b/numass-data-proto/src/test/kotlin/ru/inr/mass/data/proto/TestNumassDirectory.kt index 52e0646..f2bfba8 100644 --- a/numass-data-proto/src/test/kotlin/ru/inr/mass/data/proto/TestNumassDirectory.kt +++ b/numass-data-proto/src/test/kotlin/ru/inr/mass/data/proto/TestNumassDirectory.kt @@ -21,6 +21,8 @@ class TestNumassDirectory { assertEquals("2018-04-13T22:01:46", testSet.meta["end_time"].string) assertEquals(ListValue.EMPTY, testSet.meta["comments"].value) assertEquals(31, testSet.points.size) - assertEquals("2018-04-13T21:56:09", testSet.points.find { it.index == 22 }?.meta["end_time"].string) + val point22 = testSet.points.find { it.index == 22 }!! + point22.blocks + assertEquals("2018-04-13T21:56:09", point22.meta["end_time"].string) } } \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index 5caafe0..98761a6 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,8 +1,8 @@ pluginManagement { repositories { - mavenLocal() - jcenter() gradlePluginPortal() + mavenCentral() + jcenter() maven("https://dl.bintray.com/kotlin/kotlin-eap") maven("https://dl.bintray.com/kotlin/kotlin-dev") maven("https://dl.bintray.com/mipt-npm/dataforge") @@ -10,8 +10,8 @@ pluginManagement { maven("https://dl.bintray.com/mipt-npm/dev") } - val toolsVersion = "0.7.1" - val kotlinVersion = "1.4.21" + val toolsVersion = "0.7.3-1.4.30-RC" + val kotlinVersion = "1.4.30-RC" plugins { id("ru.mipt.npm.project") version toolsVersion @@ -22,6 +22,16 @@ pluginManagement { kotlin("jvm") version kotlinVersion kotlin("js") version kotlinVersion } + + resolutionStrategy { + eachPlugin { + if (requested.id.id == "com.squareup.wire") { + // For some reason, Gradle does a lookup on the wrong coordinates: + // 'com.squareup.wire:com.squareup.wire.gradle.plugin' instead of the one below. + useModule("com.squareup.wire:wire-gradle-plugin:${requested.version}") + } + } + } } include("numass-data-model")