0

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)
4

1 回答 1

0

以下是微流直接分享的答案,我可以确认它完美无缺。

你好,

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

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();
于 2020-08-05T15:26:15.697 回答