ChronicleWire 是否支持实体中的可选字段?

Does ChronicleWire support optional fields in entities?

我正在试验 ChronicleWire。如 features 中所述,开箱即用地支持可选字段。

我刚刚创建了一个带有一个可选(可为空)字段的简单自描述实体:

public class Foo extends SelfDescribingMarshallable {

  private String name;
  private Baz baz;
}

其中 baz 可能为 null 也可能不是 null 且 implements/extends 可编组。

当我尝试将实体放入 ChronicleMap 时出现错误:

Exception in thread "main" java.lang.NullPointerException
    at net.openhft.chronicle.bytes.BytesMarshaller$BytesMarshallableFieldAccess.getValue(BytesMarshaller.java:211)
    at net.openhft.chronicle.bytes.BytesMarshaller$FieldAccess.write(BytesMarshaller.java:152)
    at net.openhft.chronicle.bytes.BytesMarshaller.writeMarshallable(BytesMarshaller.java:70)
    at net.openhft.chronicle.bytes.BytesUtil.writeMarshallable(BytesUtil.java:295)
    at net.openhft.chronicle.bytes.BytesMarshallable.writeMarshallable(BytesMarshallable.java:48)
    at net.openhft.chronicle.bytes.BytesMarshaller$BytesMarshallableFieldAccess.getValue(BytesMarshaller.java:211)
    at net.openhft.chronicle.bytes.BytesMarshaller$FieldAccess.write(BytesMarshaller.java:152)

当我尝试使用 java optional 而不是我的实体更改为:

    public class Foo extends SelfDescribingMarshallable {

      private String name;
      private Optional<Baz> baz = Optional.empty();
    }

然后出现另一个错误:

Caused by: java.lang.IllegalArgumentException: type=class java.util.Optional is unsupported, it must either be of type Marshallable, String or AutoBoxed primitive Object
    at net.openhft.chronicle.wire.ValueOut.object(ValueOut.java:682)
    at net.openhft.chronicle.wire.ValueOut.untypedObject(ValueOut.java:795)
    at net.openhft.chronicle.wire.ValueOut.object(ValueOut.java:519)
    at net.openhft.chronicle.wire.WireMarshaller$ObjectFieldAccess.getValue(WireMarshaller.java:669)
    at net.openhft.chronicle.wire.WireMarshaller$FieldAccess.write(WireMarshaller.java:518)
    at net.openhft.chronicle.wire.WireMarshaller.writeMarshallable(WireMarshaller.java:199)
    at net.openhft.chronicle.wire.Marshallable.writeMarshallable(Marshallable.java:132)

我没有放弃,尝试实现自己的可选。这是:

@AllArgsConstructor(staticName = "of")
@NoArgsConstructor(staticName = "empty")
public class OptionalValue<T extends Marshallable> implements Marshallable {

    @Nullable
    private T value;

    @Override
    public void readMarshallable(@NotNull WireIn wire) throws IORuntimeException {
        var val = wire.read("value");
        if (!val.isNull()) {
            val.marshallable(value);
        }
    }

    @Override
    public void writeMarshallable(@NotNull WireOut wire) {
        if (value == null) {
            wire.write("value").nu11();
        } else {
            wire.write("value").marshallable(value);
        }
    }

    boolean isEmpty() { return value == null; }

    T get() { return value; }
}

在这种情况下,我看到另一个错误:

Caused by: java.lang.ClassCastException

    at net.openhft.chronicle.core.util.ObjectUtils.asCCE(ObjectUtils.java:294)
    at net.openhft.chronicle.core.util.ObjectUtils$ConversionFunction.apply(ObjectUtils.java:624)
    at net.openhft.chronicle.core.util.ObjectUtils$ConversionFunction.apply(ObjectUtils.java:592)
    at net.openhft.chronicle.core.ClassLocal.computeValue(ClassLocal.java:54)
    at java.base/java.lang.ClassValue.getFromHashMap(ClassValue.java:226)
    at java.base/java.lang.ClassValue.getFromBackup(ClassValue.java:208)
    at java.base/java.lang.ClassValue.get(ClassValue.java:114)
    at net.openhft.chronicle.core.util.ObjectUtils.convertTo0(ObjectUtils.java:257)
    ... 28 more
Caused by: java.lang.NoSuchMethodException: com.redacted.entity.OptionalValue.<init>(java.lang.String)
Caused by: java.lang.NoSuchMethodException: com.redacted.entity.OptionalValue.<init>(java.lang.String)

    at java.base/java.lang.Class.getConstructor0(Class.java:3349)
    at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2553)
    at net.openhft.chronicle.core.util.ObjectUtils$ConversionFunction.apply(ObjectUtils.java:620)

那么有人知道如何正确修复或使用它吗? ChronicleWire 2.22ae6

由于SelfDescribingMarshallableBytesMarsahallable,Map 更喜欢使用这种较低级别的序列化。但是,因为它的级别太低,所以不支持 null 值。

您可以通过设置 valueMarshaller

告诉构建器使用 Marshallable
.valueMarshaller(new MarshallableReaderWriter<>(Foo.class))