replace protoc with Wire

This commit is contained in:
Alexander Nozik 2021-01-28 12:31:17 +03:00
parent 387b6bbcb4
commit 62d33ddacd
4 changed files with 45 additions and 66 deletions

View File

@ -1,12 +1,7 @@
import com.google.protobuf.gradle.proto
import com.google.protobuf.gradle.protobuf
import com.google.protobuf.gradle.protoc
plugins { plugins {
java
kotlin("jvm") kotlin("jvm")
id("ru.mipt.npm.kscience") id("ru.mipt.npm.kscience")
id("com.google.protobuf") version "0.8.14" id("com.squareup.wire") version "3.5.0"
} }
kscience{ kscience{
@ -18,42 +13,9 @@ val dataforgeVersion: String by rootProject.extra
dependencies { dependencies {
api(project(":numass-data-model")) api(project(":numass-data-model"))
api("hep.dataforge:dataforge-workspace:$dataforgeVersion") api("hep.dataforge:dataforge-workspace:$dataforgeVersion")
implementation("com.google.protobuf:protobuf-java:3.14.0")
implementation("javax.annotation:javax.annotation-api:1.3.1") implementation("javax.annotation:javax.annotation-api:1.3.1")
} }
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> { wire{
dependsOn(":numass-data-proto:generateProto") kotlin{}
}
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"
} }

View File

@ -37,22 +37,21 @@ import java.util.zip.Inflater
*/ */
public class ProtoNumassPoint( public class ProtoNumassPoint(
override val meta: Meta, override val meta: Meta,
private val protoBuilder: () -> NumassProto.Point, private val protoBuilder: () -> Point,
) : NumassPoint { ) : NumassPoint {
private val proto: NumassProto.Point get() = protoBuilder() private val proto: Point get() = protoBuilder()
override val blocks: List<NumassBlock> override val blocks: List<NumassBlock>
get() = proto.channelsList get() = proto.channels.flatMap { channel ->
.flatMap { channel -> channel.blocks
channel.blocksList
.map { block -> ProtoBlock(channel.id.toInt(), block, this) } .map { block -> ProtoBlock(channel.id.toInt(), block, this) }
.sortedBy { it.startTime } .sortedBy { it.startTime }
} }
override val channels: Map<Int, NumassBlock> override val channels: Map<Int, NumassBlock>
get() = proto.channelsList.groupBy { it.id.toInt() }.mapValues { entry -> get() = proto.channels.groupBy { it.id.toInt() }.mapValues { entry ->
MetaBlock(entry.value.flatMap { it.blocksList }.map { ProtoBlock(entry.key, it, this) }) 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 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? { public fun fromEnvelope(envelope: Envelope): ProtoNumassPoint? {
val proto = envelope.useData { val proto = envelope.useData {
NumassProto.Point.parseFrom(it) Point.ADAPTER.decode(it)
} }
return proto?.let { ProtoNumassPoint(envelope.meta) { it } } return proto?.let { ProtoNumassPoint(envelope.meta) { it } }
} }
@ -115,7 +114,7 @@ public class ProtoNumassPoint(
public class ProtoBlock( public class ProtoBlock(
override val channel: Int, override val channel: Int,
private val block: NumassProto.Point.Channel.Block, private val block: Point.Channel.Block,
public val parent: NumassPoint? = null, public val parent: NumassPoint? = null,
) : NumassBlock { ) : NumassBlock {
@ -136,14 +135,20 @@ public class ProtoBlock(
} }
override val events: Flow<NumassEvent> override val events: Flow<NumassEvent>
get() = if (block.hasEvents()) { get() = if (block.events != null) {
val events = block.events 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) 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 { } else {
emptyFlow<NumassEvent>() emptyFlow<NumassEvent>()
} }
@ -151,10 +156,10 @@ public class ProtoBlock(
override val frames: Flow<NumassFrame> override val frames: Flow<NumassFrame>
get() { get() {
val tickSize = Duration.ofNanos(block.binSize) val tickSize = Duration.ofNanos(block.bin_size)
return block.framesList.asFlow().map { frame -> return block.frames.asFlow().map { frame ->
val time = startTime.plusNanos(frame.time) val time = startTime.plusNanos(frame.time)
val frameData = frame.data.asReadOnlyByteBuffer() val frameData = frame.data_.asByteBuffer()
NumassFrame(time, tickSize, frameData.asShortBuffer()) NumassFrame(time, tickSize, frameData.asShortBuffer())
} }
} }

View File

@ -21,6 +21,8 @@ class TestNumassDirectory {
assertEquals("2018-04-13T22:01:46", testSet.meta["end_time"].string) assertEquals("2018-04-13T22:01:46", testSet.meta["end_time"].string)
assertEquals(ListValue.EMPTY, testSet.meta["comments"].value) assertEquals(ListValue.EMPTY, testSet.meta["comments"].value)
assertEquals(31, testSet.points.size) 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)
} }
} }

View File

@ -1,8 +1,8 @@
pluginManagement { pluginManagement {
repositories { repositories {
mavenLocal()
jcenter()
gradlePluginPortal() gradlePluginPortal()
mavenCentral()
jcenter()
maven("https://dl.bintray.com/kotlin/kotlin-eap") maven("https://dl.bintray.com/kotlin/kotlin-eap")
maven("https://dl.bintray.com/kotlin/kotlin-dev") maven("https://dl.bintray.com/kotlin/kotlin-dev")
maven("https://dl.bintray.com/mipt-npm/dataforge") maven("https://dl.bintray.com/mipt-npm/dataforge")
@ -10,8 +10,8 @@ pluginManagement {
maven("https://dl.bintray.com/mipt-npm/dev") maven("https://dl.bintray.com/mipt-npm/dev")
} }
val toolsVersion = "0.7.1" val toolsVersion = "0.7.3-1.4.30-RC"
val kotlinVersion = "1.4.21" val kotlinVersion = "1.4.30-RC"
plugins { plugins {
id("ru.mipt.npm.project") version toolsVersion id("ru.mipt.npm.project") version toolsVersion
@ -22,6 +22,16 @@ pluginManagement {
kotlin("jvm") version kotlinVersion kotlin("jvm") version kotlinVersion
kotlin("js") 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") include("numass-data-model")