Search in sources :

Example 1 with Externalizer

use of org.infinispan.commons.marshall.Externalizer in project infinispan by infinispan.

the class CommandsFactoryImpl method getValueMatcher.

private ValueMatcher getValueMatcher(Object o) {
    SerializeFunctionWith ann = o.getClass().getAnnotation(SerializeFunctionWith.class);
    if (ann != null)
        return ValueMatcher.valueOf(ann.valueMatcher().toString());
    Externalizer ext = ((GlobalMarshaller) marshaller).findExternalizerFor(o);
    if (ext instanceof LambdaExternalizer)
        return ValueMatcher.valueOf(((LambdaExternalizer) ext).valueMatcher(o).toString());
    return ValueMatcher.MATCH_ALWAYS;
}
Also used : Externalizer(org.infinispan.commons.marshall.Externalizer) LambdaExternalizer(org.infinispan.commons.marshall.LambdaExternalizer) GlobalMarshaller(org.infinispan.marshall.core.GlobalMarshaller) LambdaExternalizer(org.infinispan.commons.marshall.LambdaExternalizer) SerializeFunctionWith(org.infinispan.commons.marshall.SerializeFunctionWith)

Example 2 with Externalizer

use of org.infinispan.commons.marshall.Externalizer in project infinispan by infinispan.

the class GlobalMarshaller method writeArray.

private void writeArray(Class<?> clazz, Object array, BytesObjectOutput out) throws IOException {
    out.writeByte(ID_ARRAY);
    Class<?> componentType = clazz.getComponentType();
    int length = Array.getLength(array);
    boolean singleType = true;
    Class<?> elementType = null;
    int flags;
    if (length == 0) {
        flags = FLAG_ARRAY_EMPTY;
    } else {
        if (length <= Primitives.SMALL_ARRAY_MAX) {
            flags = FLAG_ARRAY_SMALL;
        } else if (length <= Primitives.MEDIUM_ARRAY_MAX) {
            flags = FLAG_ARRAY_MEDIUM;
        } else {
            flags = FLAG_ARRAY_LARGE;
        }
        Object firstElement = Array.get(array, 0);
        if (firstElement != null) {
            elementType = firstElement.getClass();
        }
        for (int i = 1; i < length; ++i) {
            Object element = Array.get(array, i);
            if (element == null) {
                if (elementType != null) {
                    singleType = false;
                    break;
                }
            } else if (element.getClass() != elementType) {
                singleType = false;
                break;
            }
        }
    }
    boolean componentTypeMatch = false;
    if (singleType) {
        flags |= FLAG_SINGLE_TYPE;
        if (elementType == null) {
            flags |= FLAG_ALL_NULL;
        } else if (elementType == componentType) {
            flags |= FLAG_COMPONENT_TYPE_MATCH;
            componentTypeMatch = true;
        }
    }
    AdvancedExternalizer ext;
    if ((ext = getExternalizer(internalExts, componentType)) != null) {
        writeFlagsWithExternalizer(out, componentType, componentTypeMatch, ext, flags, ID_INTERNAL);
    } else if ((ext = getExternalizer(externalExts, componentType)) != null) {
        writeFlagsWithExternalizer(out, componentType, componentTypeMatch, ext, flags, ID_EXTERNAL);
    } else {
        // We cannot use annotated externalizer to specify the component type, so we will
        // clear the component type match flag and use class identifier or full name.
        // Theoretically we could write annotated externalizer and component type saving one byte
        // but it's not worth the complexity.
        componentTypeMatch = false;
        flags &= ~FLAG_COMPONENT_TYPE_MATCH;
        int classId;
        if ((classId = classIdentifiers.getId(componentType)) != -1) {
            out.writeByte(flags | ID_CLASS);
            if (classId < ClassIds.MAX_ID) {
                out.writeByte(classId);
            } else {
                out.writeByte(ClassIds.MAX_ID);
                out.writeInt(classId);
            }
        } else {
            out.writeByte(flags | ID_UNKNOWN);
            out.writeObject(componentType);
        }
    }
    if (length == 0) {
    } else if (length <= Primitives.SMALL_ARRAY_MAX) {
        out.writeByte(length - Primitives.SMALL_ARRAY_MIN);
    } else if (length <= Primitives.MEDIUM_ARRAY_MAX) {
        out.writeShort(length - Primitives.MEDIUM_ARRAY_MIN);
    } else {
        out.writeInt(length);
    }
    if (singleType) {
        Externalizer elementExt;
        int primitiveId;
        if (elementType == null) {
            return;
        } else if (componentTypeMatch) {
            // Note: ext can be null here!
            elementExt = ext;
        } else if ((elementExt = getExternalizer(internalExts, elementType)) != null) {
            out.writeByte(ID_INTERNAL);
            out.writeByte(((AdvancedExternalizer) elementExt).getId());
        } else if ((elementExt = getExternalizer(externalExts, elementType)) != null) {
            out.writeByte(ID_EXTERNAL);
            out.writeInt(((AdvancedExternalizer) elementExt).getId());
        } else if ((elementExt = findAnnotatedExternalizer(elementType)) != null) {
            // We could try if the externalizer class is registered in ClassIdentifier but most likely it is not,
            // because if an user registered it, he could rather explicitly register AdvancedExternalizer instead.
            out.writeByte(ID_ANNOTATED);
            out.writeObject(elementExt.getClass());
        } else if ((primitiveId = Primitives.PRIMITIVES.getOrDefault(elementType, NOT_FOUND)) != NOT_FOUND) {
            out.writeByte(ID_PRIMITIVE);
            out.writeByte(primitiveId);
            for (int i = 0; i < length; ++i) {
                Object element = Array.get(array, i);
                assert element != null;
                Primitives.writeRawPrimitive(element, out, primitiveId);
            }
            // We are finished
            return;
        } else {
            out.writeByte(ID_UNKNOWN);
        // Do not write element type!
        }
        if (elementExt != null) {
            for (int i = 0; i < length; ++i) {
                Object element = Array.get(array, i);
                assert element != null;
                elementExt.writeObject(out, element);
            }
        } else {
            // component type matches but this type does not have an externalizer
            for (int i = 0; i < length; ++i) {
                Object element = Array.get(array, i);
                assert element != null;
                writeRawUnknown(element, out);
            }
        }
    } else {
        for (int i = 0; i < length; ++i) {
            Object element = Array.get(array, i);
            writeNullableObject(element, out);
        }
    }
}
Also used : Externalizer(org.infinispan.commons.marshall.Externalizer) AdvancedExternalizer(org.infinispan.commons.marshall.AdvancedExternalizer) ThrowableExternalizer(org.infinispan.marshall.exts.ThrowableExternalizer) AdvancedExternalizer(org.infinispan.commons.marshall.AdvancedExternalizer)

Example 3 with Externalizer

use of org.infinispan.commons.marshall.Externalizer in project infinispan by infinispan.

the class GlobalMarshaller method findExternalizerFor.

public <T> Externalizer<T> findExternalizerFor(Object obj) {
    Class<?> clazz = obj.getClass();
    Externalizer ext = getExternalizer(internalExts, clazz);
    if (ext == null) {
        ext = getExternalizer(externalExts, clazz);
        if (ext == null)
            ext = findAnnotatedExternalizer(clazz);
    }
    return ext;
}
Also used : Externalizer(org.infinispan.commons.marshall.Externalizer) AdvancedExternalizer(org.infinispan.commons.marshall.AdvancedExternalizer) ThrowableExternalizer(org.infinispan.marshall.exts.ThrowableExternalizer)

Aggregations

Externalizer (org.infinispan.commons.marshall.Externalizer)3 AdvancedExternalizer (org.infinispan.commons.marshall.AdvancedExternalizer)2 ThrowableExternalizer (org.infinispan.marshall.exts.ThrowableExternalizer)2 LambdaExternalizer (org.infinispan.commons.marshall.LambdaExternalizer)1 SerializeFunctionWith (org.infinispan.commons.marshall.SerializeFunctionWith)1 GlobalMarshaller (org.infinispan.marshall.core.GlobalMarshaller)1