
com.aerospike.mapper.tools.mappers.ObjectReferenceMapper Maven / Gradle / Ivy
package com.aerospike.mapper.tools.mappers;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.aerospike.client.AerospikeException;
import com.aerospike.client.Value;
import com.aerospike.client.util.Crypto;
import com.aerospike.mapper.annotations.AerospikeReference.ReferenceType;
import com.aerospike.mapper.tools.AeroMapper;
import com.aerospike.mapper.tools.ClassCache;
import com.aerospike.mapper.tools.ClassCacheEntry;
import com.aerospike.mapper.tools.DeferredObjectLoader.DeferredObject;
public class ObjectReferenceMapper extends ObjectMapper {
// Package visibility
private final ClassCacheEntry> referencedClass;
private final AeroMapper mapper;
private final boolean lazy;
private final boolean allowBatch;
private final ReferenceType type;
public ObjectReferenceMapper(ClassCacheEntry> entry, boolean lazy, boolean allowBatch, ReferenceType type, AeroMapper mapper) {
this.referencedClass = entry;
this.mapper = mapper;
this.lazy = lazy;
this.type = type;
this.allowBatch = allowBatch;
if (ReferenceType.DIGEST.equals(this.type) && this.lazy) {
throw new AerospikeException("An object reference to a " + entry.getClass().getSimpleName() + " cannot be both lazy and map to a digest");
}
}
@Override
public Object toAerospikeFormat(Object value) {
return toAerospikeFormat(value, false, false);
}
@Override
public Object toAerospikeFormat(Object value, boolean isUnknownType, boolean isSubclassOfKnownType) {
if (value == null) {
return null;
}
// In this case we want to store a reference to the object.
ClassCacheEntry> classToUse;
if (value.getClass().equals(referencedClass.getUnderlyingClass())) {
classToUse = referencedClass;
}
else {
classToUse = ClassCache.getInstance().loadClass(value.getClass(), mapper);
isSubclassOfKnownType = true;
}
Object key = classToUse.getKey(value);
if (ReferenceType.DIGEST.equals(type)) {
key = Crypto.computeDigest(classToUse.getSetName(), Value.get(key));
}
if (isSubclassOfKnownType || isUnknownType) {
// Need to put the class name in the key so we can recreate the class
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy