From aa59a78ad88c0e6e8df9f7d015c0c11ababa489b Mon Sep 17 00:00:00 2001 From: kasemir Date: Fri, 28 Jun 2024 12:56:07 -0400 Subject: [PATCH] RDB Archive: Correct writing of 'enum' metadata `Display.displayOf(VEnum)` or `..(VString)` returns a non-`null` display, which prevented the writing of enum metadata. Now VEnum is detected and its labels are written. --- services/archive-engine/.classpath | 2 + .../archive/writer/rdb/RDBArchiveWriter.java | 54 ++++++++++++------- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/services/archive-engine/.classpath b/services/archive-engine/.classpath index 2225c2c8b6..12e57c1a54 100644 --- a/services/archive-engine/.classpath +++ b/services/archive-engine/.classpath @@ -7,6 +7,8 @@ + + diff --git a/services/archive-engine/src/main/java/org/csstudio/archive/writer/rdb/RDBArchiveWriter.java b/services/archive-engine/src/main/java/org/csstudio/archive/writer/rdb/RDBArchiveWriter.java index 02eda778c4..157c6e02c2 100644 --- a/services/archive-engine/src/main/java/org/csstudio/archive/writer/rdb/RDBArchiveWriter.java +++ b/services/archive-engine/src/main/java/org/csstudio/archive/writer/rdb/RDBArchiveWriter.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011-2020 Oak Ridge National Laboratory. + * Copyright (c) 2011-2024 Oak Ridge National Laboratory. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -178,11 +178,18 @@ public WriteChannel getChannel(final String name) throws Exception @Override public void addSample(final WriteChannel channel, final VType sample) throws Exception { - final RDBWriteChannel rdb_channel = (RDBWriteChannel) channel; - writeMetaData(rdb_channel, sample); - batchSample(rdb_channel, sample); - batched_channel.add(rdb_channel); - batched_samples.add(sample); + try + { + final RDBWriteChannel rdb_channel = (RDBWriteChannel) channel; + writeMetaData(rdb_channel, sample); + batchSample(rdb_channel, sample); + batched_channel.add(rdb_channel); + batched_samples.add(sample); + } + catch (Exception ex) + { // Wrap with channel info + throw new Exception("Cannot add sample for " + channel, ex); + } } /** Write meta data if it was never written or has changed @@ -191,12 +198,33 @@ public void addSample(final WriteChannel channel, final VType sample) throws Exc */ private void writeMetaData(final RDBWriteChannel channel, final VType sample) throws Exception { + // Three cases: Enum, numeric, string. + // // Note that Strings have no meta data. But we don't know at this point // if it's really a string channel, or of this is just a special // string value like "disconnected". // In order to not delete any existing meta data, - // we just do nothing for strings + // we just do nothing for strings. + if (sample instanceof VString) + return; + + if (sample instanceof VEnum) + { + final List labels = ((VEnum)sample).getDisplay().getChoices(); + if (MetaDataHelper.equals(labels, channel.getMetadata())) + return; + + // Clear numeric meta data, set enumerated in RDB + NumericMetaDataHelper.delete(connection, sql, channel); + EnumMetaDataHelper.delete(connection, sql, channel); + EnumMetaDataHelper.insert(connection, sql, channel, labels); + channel.setMetaData(labels); + return; + } + + // Note that Display.displayOf(VEnum) or ..(VString) will return a non-null display, + // but we already handled those cases final Display display = Display.displayOf(sample); if (display != null) { @@ -209,18 +237,6 @@ private void writeMetaData(final RDBWriteChannel channel, final VType sample) th NumericMetaDataHelper.insert(connection, sql, channel, display); channel.setMetaData(display); } - else if (sample instanceof VEnum) - { - final List labels = ((VEnum)sample).getDisplay().getChoices(); - if (MetaDataHelper.equals(labels, channel.getMetadata())) - return; - - // Clear numeric meta data, set enumerated in RDB - NumericMetaDataHelper.delete(connection, sql, channel); - EnumMetaDataHelper.delete(connection, sql, channel); - EnumMetaDataHelper.insert(connection, sql, channel, labels); - channel.setMetaData(labels); - } } private static Instant getTimestamp(final VType value)