Java Microstream - ImmutablePair 导致 PersistenceExceptionTypeNotPersistable

Java Microstream - ImmutablePair causing PersistenceExceptionTypeNotPersistable

Apache ImmutablePair,(org.apache.commons.lang3.tuple.ImmutablePair)在存储 Java 对象图时导致 PersistenceExceptionTypeNotPersistable。

我该如何解决?如果有typeHandler,有例子吗?

Caused by: one.microstream.persistence.exceptions.PersistenceExceptionTypeNotPersistable: Type not persistable: "class org.apache.commons.lang3.tuple.ImmutablePair".
    at one.microstream.persistence.binary.internal.BinaryHandlerUnpersistable.guaranteeSpecificInstanceViablity(BinaryHandlerUnpersistable.java:66)
    at one.microstream.persistence.types.PersistenceTypeHandlerManager$Default.ensureTypeHandler(PersistenceTypeHandlerManager.java:336)
    at one.microstream.persistence.binary.types.BinaryStorer$Default.registerGuaranteed(BinaryStorer.java:557)
    at one.microstream.persistence.binary.types.BinaryStorer$Default.registerLazyOptional(BinaryStorer.java:572)
    at one.microstream.persistence.types.PersistenceObjectManager$Default.ensureObjectId(PersistenceObjectManager.java:182)
    at one.microstream.persistence.binary.types.BinaryStorer$Default.register(BinaryStorer.java:591)
    at one.microstream.persistence.binary.types.BinaryStorer$Default.apply(BinaryStorer.java:298)
    at one.microstream.persistence.binary.types.Binary.storeIterableContentAsList(Binary.java:1698)
    at one.microstream.persistence.binary.types.Binary.storeIterableAsList(Binary.java:668)
    at one.microstream.persistence.binary.internal.AbstractBinaryHandlerCustomIterableSimpleListElements.store(AbstractBinaryHandlerCustomIterableSimpleListElements.java:64)
    at one.microstream.persistence.binary.internal.AbstractBinaryHandlerCustomIterableSimpleListElements.store(AbstractBinaryHandlerCustomIterableSimpleListElements.java:1)
    at one.microstream.persistence.binary.internal.AbstractBinaryHandlerCustom.store(AbstractBinaryHandlerCustom.java:1)
    at one.microstream.persistence.binary.types.BinaryStorer$Default.storeItem(BinaryStorer.java:414)
    at one.microstream.persistence.binary.types.BinaryStorer$Default.storeGraph(BinaryStorer.java:403)
    at one.microstream.persistence.binary.types.BinaryStorer$Default.store(BinaryStorer.java:421)
    at one.microstream.persistence.types.PersistenceManager$Default.store(PersistenceManager.java:274)
    at one.microstream.storage.types.StorageConnection.store(StorageConnection.java:306)
    at one.microstream.cache.CacheStore$Default.write(CacheStore.java:112)

下面是microstream直接分享的答案,我可以确认这个是完美的。

您好,

如果 class 被认为是不可持久的,则可以为此类 class 提供自定义类型处理程序实现。在您的情况下,自定义类型处理程序可能如下所示:

import org.apache.commons.lang3.tuple.ImmutablePair;

import one.microstream.persistence.binary.internal.CustomBinaryHandler;
import one.microstream.persistence.binary.types.Binary;
import one.microstream.persistence.binary.types.BinaryField;
import one.microstream.persistence.types.PersistenceLoadHandler;
import one.microstream.reflect.XReflect;

public class ImmutablePairTypeHandler extends CustomBinaryHandler<ImmutablePair<?, ?>>
{
    @SuppressWarnings({"unchecked", "rawtypes"})
    private static Class<ImmutablePair<?, ?>> handledType()
    {
        return (Class)ImmutablePair.class; // to get ".class" to work
    }

    //the fields to be persisted
    BinaryField<ImmutablePair<?, ?>> left = Field(Object.class, ImmutablePair::getLeft);
    BinaryField<ImmutablePair<?, ?>> right = Field(Object.class, ImmutablePair::getRight);
    
    public ImmutablePairTypeHandler()
    {
        super(handledType());
    }
    
    @Override
    public ImmutablePair<?, ?> create(final Binary data, final PersistenceLoadHandler handler)
    {
        //create a empty instance, don't use nullPair() as it is a static final
        return ImmutablePair.of(null, null);
    }
    
    @Override
    public void initializeState(final Binary data, final ImmutablePair<?, ?> instance, final PersistenceLoadHandler handler)
    {
        //get the referenced Objects
        final Object left = this.left.readReference(data, handler);
        final Object right = this.right.readReference(data, handler);
            
        //as the pair is immutable we can't set the pairs fields directly
        final ImmutablePair<?,?> pair = ImmutablePair.of(left, right);
        XReflect.copyFields(pair, instance);
    }   
}

要启用此处理程序,只需注册它:

final EmbeddedStorageFoundation<?> foundation = EmbeddedStorage.Foundation(workdir);
foundation.registerTypeHandler(new ImmutablePairTypeHandler());

final EmbeddedStorageManager storage = foundation.start();