use of org.infinispan.encoding.DataConversion in project infinispan by infinispan.
the class ScriptConversions method convertParameters.
public Map<String, ?> convertParameters(TaskContext context) {
if (!context.getParameters().isPresent())
return null;
Map<String, ?> contextParams = context.getParameters().get();
Map<String, Object> converted = new HashMap<>(contextParams.size());
if (context.getCache().isPresent()) {
DataConversion valueDataConversion = context.getCache().get().getAdvancedCache().getValueDataConversion();
MediaType requestMediaType = valueDataConversion.getRequestMediaType();
contextParams.forEach((s, o) -> {
Object c = requestMediaType == null ? o : encoderRegistry.convert(o, valueDataConversion.getRequestMediaType(), APPLICATION_OBJECT);
converted.put(s, c);
});
return converted;
} else {
return contextParams;
}
}
use of org.infinispan.encoding.DataConversion in project infinispan by infinispan.
the class AbstractSchemaJdbcStore method createTableOperations.
@Override
protected TableOperations<K, V> createTableOperations(InitializationContext ctx, C config) throws SQLException {
AdvancedCache<K, V> advancedCache = ctx.getCache().getAdvancedCache();
// We use a type as the protostream -> json conversion leaves it as a String instead of byte[]
MediaType jsonStringType = MediaType.fromString(MediaType.APPLICATION_JSON_TYPE + ";type=String");
// This seems like a bug that `withRequestMediaType` isn't injected...
DataConversion keyDataConversion = advancedCache.getKeyDataConversion().withRequestMediaType(jsonStringType);
DataConversion valueDataConversion = advancedCache.getValueDataConversion().withRequestMediaType(jsonStringType);
ComponentRegistry componentRegistry = advancedCache.getComponentRegistry();
componentRegistry.wireDependencies(keyDataConversion, true);
componentRegistry.wireDependencies(valueDataConversion, true);
Parameter[] parameters = generateParameterInformation(config, connectionFactory);
assert parameters.length != 0;
Parameter[] primaryParameters = determinePrimaryParameters(config, parameters);
assert primaryParameters.length != 0;
assert Arrays.stream(primaryParameters).allMatch(Parameter::isPrimaryIdentifier);
// We have to use the user serialization context as it will have the schemas they registered
ImmutableSerializationContext serializationContext = componentRegistry.getComponent(SerializationContextRegistry.class).getUserCtx();
ProtoSchemaOptions<K, V, C> options = verifySchemaAndCreateOptions(serializationContext, config.getSchemaJdbcConfiguration(), parameters, primaryParameters, keyDataConversion, valueDataConversion, ctx.getMarshallableEntryFactory());
return actualCreateTableOperations(options);
}
use of org.infinispan.encoding.DataConversion in project infinispan by infinispan.
the class IndexWorker method apply.
@Override
public Void apply(EmbeddedCacheManager embeddedCacheManager) {
AdvancedCache<Object, Object> cache = SecurityActions.getUnwrappedCache(embeddedCacheManager.getCache(cacheName)).getAdvancedCache();
DataConversion valueDataConversion = cache.getValueDataConversion();
AdvancedCache<Object, Object> reindexCache = cache.withStorageMediaType();
SearchMapping searchMapping = ComponentRegistryUtils.getSearchMapping(cache);
TimeService timeService = ComponentRegistryUtils.getTimeService(cache);
MassIndexerProgressNotifier notifier = new MassIndexerProgressNotifier(searchMapping, timeService);
IndexUpdater indexUpdater = new IndexUpdater(searchMapping);
KeyPartitioner keyPartitioner = ComponentRegistryUtils.getKeyPartitioner(cache);
if (keys == null || keys.size() == 0) {
preIndex(cache, indexUpdater);
MassIndexerProgressState progressState = new MassIndexerProgressState(notifier);
if (!skipIndex) {
try (Stream<CacheEntry<Object, Object>> stream = reindexCache.getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL).cacheEntrySet().stream()) {
stream.forEach(entry -> {
Object key = entry.getKey();
Object value = valueDataConversion.extractIndexable(entry.getValue());
int segment = keyPartitioner.getSegment(key);
if (value != null && indexedTypes.contains(indexUpdater.toConvertedEntityJavaClass(value))) {
progressState.addItem(key, value, indexUpdater.updateIndex(key, value, segment));
}
});
}
}
postIndex(indexUpdater, progressState, notifier);
} else {
DataConversion keyDataConversion = cache.getKeyDataConversion();
Set<Class<?>> classSet = new HashSet<>();
for (Object key : keys) {
Object storedKey = keyDataConversion.toStorage(key);
Object unwrappedKey = keyDataConversion.extractIndexable(storedKey);
Object value = cache.get(key);
if (value != null) {
indexUpdater.updateIndex(unwrappedKey, value, keyPartitioner.getSegment(storedKey));
classSet.add(value.getClass());
}
}
indexUpdater.flush(classSet);
indexUpdater.refresh(classSet);
}
return null;
}
use of org.infinispan.encoding.DataConversion in project infinispan by infinispan.
the class LazySearchMapping method createMapping.
private SearchMapping createMapping() {
IndexingConfiguration indexingConfiguration = cache.getCacheConfiguration().indexing();
Set<String> indexedEntityTypes = indexingConfiguration.indexedEntityTypes();
DataConversion valueDataConversion = cache.getAdvancedCache().getValueDataConversion();
SearchMapping searchMapping = null;
if (commonBuilding != null) {
SearchMappingBuilder builder = SerializationContextSearchMapping.createBuilder(commonBuilding, entityLoader, indexedEntityTypes, serCtx);
searchMapping = builder != null ? builder.build() : null;
}
if (indexingConfiguration.enabled()) {
if (valueDataConversion.getStorageMediaType().match(APPLICATION_PROTOSTREAM)) {
// Try to resolve the indexed type names to protobuf type names.
Set<String> knownTypes = protobufMetadataManager.getSerializationContext().getGenericDescriptors().keySet();
for (String typeName : indexedEntityTypes) {
if (!knownTypes.contains(typeName)) {
if (searchMapping != null)
searchMapping.close();
throw log.unknownType(typeName);
}
if (searchMapping == null || searchMapping.indexedEntity(typeName) == null) {
if (searchMapping != null)
searchMapping.close();
throw log.typeNotIndexed(typeName);
}
}
}
}
return searchMapping;
}
use of org.infinispan.encoding.DataConversion in project keycloak by keycloak.
the class PersistenceManagerImpl method preloadEntry.
private Single<Object> preloadEntry(long flags, MarshallableEntry<Object, Object> me, DataConversion keyDataConversion, DataConversion valueDataConversion) {
// CallInterceptor will preserve the timestamps if the metadata is an InternalMetadataImpl instance
InternalMetadataImpl metadata = new InternalMetadataImpl(me.getMetadata(), me.created(), me.lastUsed());
Object key = keyDataConversion.toStorage(me.getKey());
Object value = valueDataConversion.toStorage(me.getValue());
PutKeyValueCommand cmd = commandsFactory.wired().buildPutKeyValueCommand(key, value, keyPartitioner.getSegment(key), metadata, flags);
cmd.setInternalMetadata(me.getInternalMetadata());
CompletionStage<Object> stage;
if (configuration.transaction().transactionMode().isTransactional() && transactionManager != null) {
final Transaction transaction = suspendIfNeeded();
CompletionStage<Transaction> putStage;
try {
beginIfNeeded();
putStage = invocationHelper.wired().invokeAsync(cmd, 1).thenApply(ignore -> {
try {
return transactionManager.suspend();
} catch (SystemException e) {
throw new PersistenceException("Unable to preload!", e);
}
});
} catch (Exception e) {
throw new PersistenceException("Unable to preload!", e);
}
// noinspection unchecked
stage = (CompletionStage) blockingManager.whenCompleteBlocking(putStage, (pendingTransaction, t) -> {
try {
transactionManager.resume(pendingTransaction);
commitIfNeeded(t == null);
} catch (InvalidTransactionException | SystemException e) {
throw new PersistenceException("Unable to preload!", e);
} finally {
resumeIfNeeded(transaction);
}
}, me.getKey());
} else {
stage = invocationHelper.wired().invokeAsync(cmd, 1);
}
return Maybe.fromCompletionStage(stage).defaultIfEmpty(me);
}
Aggregations