0 votes
asked in MicroStream for Java by (240 points)
How to resolve this?

org.apache.commons.lang3.tuple.Pair

javax.cache.integration.CacheWriterException: one.microstream.persistence.exceptions.PersistenceExceptionTypeNotPersistable: Type not persistable: "class org.apache.commons.lang3.tuple.ImmutablePair".

1 Answer

0 votes
answered by (9.7k points)

Hello,

If a class is considered as unpersitable it is possible to provide a custom type handler implementation for such classes. In your case a custom type handler may look like:

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);
	}	
}

To enable this handler just register it:

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

final EmbeddedStorageManager storage = foundation.start();

 

Notes: Every question must be a separate forum post. Headline: Formulate your question shortly and precisely. Thank you!
Powered by Question2Answer
...