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