Search in sources :

Example 1 with AdvancedExternalizer

use of org.infinispan.commons.marshall.AdvancedExternalizer 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 2 with AdvancedExternalizer

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

the class ClassToExternalizerMap method fillReverseMap.

private void fillReverseMap(IdToExternalizerMap reverse) {
    for (AdvancedExternalizer ext : values) {
        if (ext != null) {
            AdvancedExternalizer prev = reverse.get(ext.getId());
            if (prev != null && !prev.equals(ext))
                throw CONTAINER.duplicateExternalizerIdFound(ext.getId(), prev.getClass().getName());
            reverse.put(ext.getId(), ext);
        }
    }
}
Also used : AdvancedExternalizer(org.infinispan.commons.marshall.AdvancedExternalizer)

Example 3 with AdvancedExternalizer

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

the class CounterModuleLifecycle method cacheManagerStarting.

@Override
public void cacheManagerStarting(GlobalComponentRegistry gcr, GlobalConfiguration globalConfiguration) {
    final Map<Integer, AdvancedExternalizer<?>> externalizerMap = globalConfiguration.serialization().advancedExternalizers();
    // Only required by GlobalMarshaller
    addAdvancedExternalizer(externalizerMap, ResetFunction.EXTERNALIZER);
    addAdvancedExternalizer(externalizerMap, ReadFunction.EXTERNALIZER);
    addAdvancedExternalizer(externalizerMap, InitializeCounterFunction.EXTERNALIZER);
    addAdvancedExternalizer(externalizerMap, AddFunction.EXTERNALIZER);
    addAdvancedExternalizer(externalizerMap, CompareAndSwapFunction.EXTERNALIZER);
    addAdvancedExternalizer(externalizerMap, CreateAndCASFunction.EXTERNALIZER);
    addAdvancedExternalizer(externalizerMap, CreateAndAddFunction.EXTERNALIZER);
    addAdvancedExternalizer(externalizerMap, RemoveFunction.EXTERNALIZER);
    BasicComponentRegistry bcr = gcr.getComponent(BasicComponentRegistry.class);
    EmbeddedCacheManager cacheManager = bcr.getComponent(EmbeddedCacheManager.class).wired();
    InternalCacheRegistry internalCacheRegistry = bcr.getComponent(InternalCacheRegistry.class).running();
    SerializationContextRegistry ctxRegistry = gcr.getComponent(SerializationContextRegistry.class);
    ctxRegistry.addContextInitializer(SerializationContextRegistry.MarshallerType.PERSISTENCE, new PersistenceContextInitializerImpl());
    CounterManagerConfiguration counterManagerConfiguration = extractConfiguration(globalConfiguration);
    if (gcr.getGlobalConfiguration().isClustered()) {
        // only attempts to create the caches if the cache manager is clustered.
        registerCounterCache(internalCacheRegistry, counterManagerConfiguration);
    } else {
        // local only cache manager.
        registerLocalCounterCache(internalCacheRegistry);
    }
    registerCounterNotificationManager(bcr);
    registerCounterManager(cacheManager, bcr, globalConfiguration);
}
Also used : BasicComponentRegistry(org.infinispan.factories.impl.BasicComponentRegistry) SerializationContextRegistry(org.infinispan.marshall.protostream.impl.SerializationContextRegistry) PersistenceContextInitializerImpl(org.infinispan.counter.impl.persistence.PersistenceContextInitializerImpl) InternalCacheRegistry(org.infinispan.registry.InternalCacheRegistry) AdvancedExternalizer(org.infinispan.commons.marshall.AdvancedExternalizer) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) CounterManagerConfiguration(org.infinispan.counter.configuration.CounterManagerConfiguration)

Example 4 with AdvancedExternalizer

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

the class LifecycleCallbacks method cacheManagerStarting.

@Override
public void cacheManagerStarting(GlobalComponentRegistry gcr, GlobalConfiguration globalConfiguration) {
    SerializationContextRegistry ctxRegistry = gcr.getComponent(SerializationContextRegistry.class);
    PersistenceContextInitializerImpl sci = new PersistenceContextInitializerImpl();
    ctxRegistry.addContextInitializer(SerializationContextRegistry.MarshallerType.PERSISTENCE, sci);
    ctxRegistry.addContextInitializer(SerializationContextRegistry.MarshallerType.GLOBAL, sci);
    Map<Integer, AdvancedExternalizer<?>> map = globalConfiguration.serialization().advancedExternalizers();
    add(map, new SuppliedExternalizer<>(ExternalizerIds.READ_WITH_EXPIRY, ReadWithExpiry.class, ReadWithExpiry::new));
    add(map, new SuppliedExternalizer<>(ExternalizerIds.GET_AND_PUT, GetAndPut.class, GetAndPut::new));
    add(map, new SuppliedExternalizer<>(ExternalizerIds.GET_AND_REPLACE, GetAndReplace.class, GetAndReplace::new));
    add(map, new Invoke.Externalizer());
    add(map, new SuppliedExternalizer<>(ExternalizerIds.PUT, Put.class, Put::new));
    add(map, new SuppliedExternalizer<>(ExternalizerIds.PUT_IF_ABSENT, PutIfAbsent.class, PutIfAbsent::new));
    add(map, new SingletonExternalizer<>(ExternalizerIds.REMOVE, Remove.getInstance()));
    add(map, new SuppliedExternalizer<>(ExternalizerIds.REMOVE_CONDITIONALLY, RemoveConditionally.class, RemoveConditionally::new));
    add(map, new SuppliedExternalizer<>(ExternalizerIds.REPLACE, Replace.class, Replace::new));
    add(map, new SingletonExternalizer<>(ExternalizerIds.GET_AND_REMOVE, GetAndRemove.getInstance()));
    add(map, new ReplaceConditionally.Externalizer());
    // that cannot be classloaded.
    if (canLoad("javax.cache.processor.MutableEntry", MutableEntrySnapshot.Externalizer.class.getClassLoader())) {
        add(map, new MutableEntrySnapshot.Externalizer());
    }
    gcr.getCacheManager().getClassAllowList().addClasses(DefaultCacheKey.class);
}
Also used : ReplaceConditionally(org.infinispan.jcache.embedded.functions.ReplaceConditionally) Replace(org.infinispan.jcache.embedded.functions.Replace) GetAndReplace(org.infinispan.jcache.embedded.functions.GetAndReplace) GetAndPut(org.infinispan.jcache.embedded.functions.GetAndPut) RemoveConditionally(org.infinispan.jcache.embedded.functions.RemoveConditionally) MutableEntrySnapshot(org.infinispan.jcache.embedded.functions.MutableEntrySnapshot) PutIfAbsent(org.infinispan.jcache.embedded.functions.PutIfAbsent) ReadWithExpiry(org.infinispan.jcache.embedded.functions.ReadWithExpiry) Put(org.infinispan.jcache.embedded.functions.Put) GetAndPut(org.infinispan.jcache.embedded.functions.GetAndPut) Invoke(org.infinispan.jcache.embedded.functions.Invoke) SingletonExternalizer(org.infinispan.commons.marshall.SingletonExternalizer) AdvancedExternalizer(org.infinispan.commons.marshall.AdvancedExternalizer) SuppliedExternalizer(org.infinispan.commons.marshall.SuppliedExternalizer) SerializationContextRegistry(org.infinispan.marshall.protostream.impl.SerializationContextRegistry) GetAndReplace(org.infinispan.jcache.embedded.functions.GetAndReplace) AdvancedExternalizer(org.infinispan.commons.marshall.AdvancedExternalizer)

Example 5 with AdvancedExternalizer

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

the class StoreMigrator method run.

void run(boolean output) throws Exception {
    String batchSizeProp = properties.getProperty(BATCH + "." + SIZE);
    int batchLimit = batchSizeProp != null ? Integer.parseInt(batchSizeProp) : DEFAULT_BATCH_SIZE;
    try (EmbeddedCacheManager manager = TargetStoreFactory.getCacheManager(properties);
        StoreIterator sourceReader = StoreIteratorFactory.get(properties)) {
        Map<Integer, AdvancedExternalizer<?>> externalizers = manager.getCacheManagerConfiguration().serialization().advancedExternalizers();
        Set<Class> externalizerClasses = externalizers.values().stream().flatMap(e -> e.getTypeClasses().stream()).collect(Collectors.toSet());
        AdvancedCache targetCache = TargetStoreFactory.getTargetCache(manager, properties);
        // Txs used so that writes to the DB are batched. Migrator will always operate locally Tx overhead should be negligible
        TransactionManager tm = targetCache.getTransactionManager();
        int txBatchSize = 0;
        for (MarshallableEntry entry : sourceReader) {
            if (warnAndIgnoreInternalClasses(entry.getKey(), externalizerClasses, output) || warnAndIgnoreInternalClasses(entry.getValue(), externalizerClasses, output))
                continue;
            if (txBatchSize == 0)
                tm.begin();
            targetCache.put(entry.getKey(), entry.getValue());
            txBatchSize++;
            if (txBatchSize == batchLimit) {
                txBatchSize = 0;
                tm.commit();
            }
        }
        if (tm.getStatus() == Status.STATUS_ACTIVE)
            tm.commit();
    }
}
Also used : InternalCacheEntry(org.infinispan.container.entries.InternalCacheEntry) TransientMortalCacheValue(org.infinispan.container.entries.TransientMortalCacheValue) ImmortalCacheEntry(org.infinispan.container.entries.ImmortalCacheEntry) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) InternalMetadataImpl(org.infinispan.metadata.impl.InternalMetadataImpl) MetadataTransientMortalCacheValue(org.infinispan.container.entries.metadata.MetadataTransientMortalCacheValue) TransientCacheEntry(org.infinispan.container.entries.TransientCacheEntry) AdvancedCache(org.infinispan.AdvancedCache) Version(org.infinispan.commons.util.Version) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) Map(java.util.Map) MetadataTransientCacheValue(org.infinispan.container.entries.metadata.MetadataTransientCacheValue) MarshallableEntry(org.infinispan.persistence.spi.MarshallableEntry) MetadataMortalCacheValue(org.infinispan.container.entries.metadata.MetadataMortalCacheValue) BATCH(org.infinispan.tools.store.migrator.Element.BATCH) MetadataImmortalCacheValue(org.infinispan.container.entries.metadata.MetadataImmortalCacheValue) KeyValuePair(org.infinispan.util.KeyValuePair) TransientMortalCacheEntry(org.infinispan.container.entries.TransientMortalCacheEntry) PrintStream(java.io.PrintStream) MortalCacheValue(org.infinispan.container.entries.MortalCacheValue) Properties(java.util.Properties) TransientCacheValue(org.infinispan.container.entries.TransientCacheValue) MetadataTransientCacheEntry(org.infinispan.container.entries.metadata.MetadataTransientCacheEntry) ByteBufferImpl(org.infinispan.commons.io.ByteBufferImpl) MetadataImmortalCacheEntry(org.infinispan.container.entries.metadata.MetadataImmortalCacheEntry) Set(java.util.Set) Collectors(java.util.stream.Collectors) MetadataMortalCacheEntry(org.infinispan.container.entries.metadata.MetadataMortalCacheEntry) SIZE(org.infinispan.tools.store.migrator.Element.SIZE) AdvancedExternalizer(org.infinispan.commons.marshall.AdvancedExternalizer) ImmortalCacheValue(org.infinispan.container.entries.ImmortalCacheValue) Status(javax.transaction.Status) MortalCacheEntry(org.infinispan.container.entries.MortalCacheEntry) InternalCacheValue(org.infinispan.container.entries.InternalCacheValue) MetadataTransientMortalCacheEntry(org.infinispan.container.entries.metadata.MetadataTransientMortalCacheEntry) FileReader(java.io.FileReader) TransactionManager(javax.transaction.TransactionManager) Collections(java.util.Collections) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) TransactionManager(javax.transaction.TransactionManager) MarshallableEntry(org.infinispan.persistence.spi.MarshallableEntry) AdvancedExternalizer(org.infinispan.commons.marshall.AdvancedExternalizer) AdvancedCache(org.infinispan.AdvancedCache)

Aggregations

AdvancedExternalizer (org.infinispan.commons.marshall.AdvancedExternalizer)16 SerializationContextRegistry (org.infinispan.marshall.protostream.impl.SerializationContextRegistry)6 Map (java.util.Map)3 CacheConfigurationException (org.infinispan.commons.CacheConfigurationException)3 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)3 HashMap (java.util.HashMap)2 Configuration (org.infinispan.configuration.cache.Configuration)2 GlobalConfiguration (org.infinispan.configuration.global.GlobalConfiguration)2 BasicComponentRegistry (org.infinispan.factories.impl.BasicComponentRegistry)2 Infinispan8Marshaller (org.infinispan.tools.store.migrator.marshaller.infinispan8.Infinispan8Marshaller)2 Infinispan9Marshaller (org.infinispan.tools.store.migrator.marshaller.infinispan9.Infinispan9Marshaller)2 File (java.io.File)1 FileReader (java.io.FileReader)1 PrintStream (java.io.PrintStream)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 Properties (java.util.Properties)1 Set (java.util.Set)1