Search in sources :

Example 26 with DataConversion

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;
    }
}
Also used : DataConversion(org.infinispan.encoding.DataConversion) HashMap(java.util.HashMap) MediaType(org.infinispan.commons.dataconversion.MediaType)

Example 27 with DataConversion

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);
}
Also used : DataConversion(org.infinispan.encoding.DataConversion) SerializationContextRegistry(org.infinispan.marshall.protostream.impl.SerializationContextRegistry) ComponentRegistry(org.infinispan.factories.ComponentRegistry) ImmutableSerializationContext(org.infinispan.protostream.ImmutableSerializationContext) MediaType(org.infinispan.commons.dataconversion.MediaType)

Example 28 with DataConversion

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;
}
Also used : SearchMapping(org.infinispan.search.mapper.mapping.SearchMapping) TimeService(org.infinispan.commons.time.TimeService) CacheEntry(org.infinispan.container.entries.CacheEntry) DataConversion(org.infinispan.encoding.DataConversion) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) HashSet(java.util.HashSet)

Example 29 with DataConversion

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;
}
Also used : DataConversion(org.infinispan.encoding.DataConversion) SearchMappingBuilder(org.infinispan.search.mapper.mapping.SearchMappingBuilder) SearchMapping(org.infinispan.search.mapper.mapping.SearchMapping) SerializationContextSearchMapping(org.infinispan.query.remote.impl.mapping.SerializationContextSearchMapping) IndexingConfiguration(org.infinispan.configuration.cache.IndexingConfiguration)

Example 30 with DataConversion

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);
}
Also used : WriteCommand(org.infinispan.commands.write.WriteCommand) ComponentName(org.infinispan.factories.annotations.ComponentName) LogFactory(org.infinispan.util.logging.LogFactory) InvocationHelper(org.infinispan.cache.impl.InvocationHelper) GlobalConfiguration(org.infinispan.configuration.global.GlobalConfiguration) KnownComponentNames(org.infinispan.factories.KnownComponentNames) StoreUnavailableException(org.infinispan.persistence.spi.StoreUnavailableException) Scopes(org.infinispan.factories.scopes.Scopes) AdvancedCache(org.infinispan.AdvancedCache) TxInvocationContext(org.infinispan.context.impl.TxInvocationContext) InitializationContextImpl(org.infinispan.persistence.InitializationContextImpl) Transaction(javax.transaction.Transaction) MethodHandles(java.lang.invoke.MethodHandles) ByRef(org.infinispan.commons.util.ByRef) Set(java.util.Set) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) IntSet(org.infinispan.commons.util.IntSet) CompletionStage(java.util.concurrent.CompletionStage) ComposedSegmentedLoadWriteStore(org.infinispan.persistence.support.ComposedSegmentedLoadWriteStore) DelegatingNonBlockingStore(org.infinispan.persistence.support.DelegatingNonBlockingStore) InternalCacheValue(org.infinispan.container.entries.InternalCacheValue) DataConversion(org.infinispan.encoding.DataConversion) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) Single(io.reactivex.rxjava3.core.Single) TransactionalStoreInterceptor(org.infinispan.interceptors.impl.TransactionalStoreInterceptor) NonBlockingStoreAdapter(org.infinispan.persistence.support.NonBlockingStoreAdapter) EnumUtil(org.infinispan.commons.util.EnumUtil) InternalExpirationManager(org.infinispan.expiration.impl.InternalExpirationManager) CacheLoaderInterceptor(org.infinispan.interceptors.impl.CacheLoaderInterceptor) Start(org.infinispan.factories.annotations.Start) ArrayList(java.util.ArrayList) Maybe(io.reactivex.rxjava3.core.Maybe) StoreConfiguration(org.infinispan.configuration.cache.StoreConfiguration) MarshallableEntry(org.infinispan.persistence.spi.MarshallableEntry) DataWriteCommand(org.infinispan.commands.write.DataWriteCommand) AsyncInterceptor(org.infinispan.interceptors.AsyncInterceptor) InternalEntryFactory(org.infinispan.container.impl.InternalEntryFactory) Properties(java.util.Properties) Executor(java.util.concurrent.Executor) Publisher(org.reactivestreams.Publisher) InvalidTransactionException(javax.transaction.InvalidTransactionException) IOException(java.io.IOException) CacheNotifier(org.infinispan.notifications.cachelistener.CacheNotifier) Inject(org.infinispan.factories.annotations.Inject) MVCCEntry(org.infinispan.container.entries.MVCCEntry) InvalidateCommand(org.infinispan.commands.write.InvalidateCommand) Function(io.reactivex.rxjava3.functions.Function) PersistenceMarshaller(org.infinispan.marshall.persistence.PersistenceMarshaller) SystemException(javax.transaction.SystemException) StampedLock(java.util.concurrent.locks.StampedLock) DistributionManager(org.infinispan.distribution.DistributionManager) AggregateCompletionStage(org.infinispan.util.concurrent.AggregateCompletionStage) IntSets(org.infinispan.commons.util.IntSets) MarshallableEntryFactory(org.infinispan.persistence.spi.MarshallableEntryFactory) Stop(org.infinispan.factories.annotations.Stop) InvocationContext(org.infinispan.context.InvocationContext) ComponentRef(org.infinispan.factories.impl.ComponentRef) SingleSegmentPublisher(org.infinispan.persistence.support.SingleSegmentPublisher) CompletableFutures(org.infinispan.util.concurrent.CompletableFutures) Lifecycle(org.infinispan.commons.api.Lifecycle) Scope(org.infinispan.factories.scopes.Scope) ByteBufferFactory(org.infinispan.commons.io.ByteBufferFactory) PERSISTENCE(org.infinispan.util.logging.Log.PERSISTENCE) CommandsFactory(org.infinispan.commands.CommandsFactory) AsyncNonBlockingStore(org.infinispan.persistence.async.AsyncNonBlockingStore) Predicate(java.util.function.Predicate) Collection(java.util.Collection) AbstractCacheTransaction(org.infinispan.transaction.impl.AbstractCacheTransaction) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand) GuardedBy(net.jcip.annotations.GuardedBy) SegmentPublisherWrapper(org.infinispan.persistence.support.SegmentPublisherWrapper) PersistenceException(org.infinispan.persistence.spi.PersistenceException) LocalOnlyCacheLoader(org.infinispan.persistence.spi.LocalOnlyCacheLoader) CompletionStages(org.infinispan.util.concurrent.CompletionStages) CompletableFuture(java.util.concurrent.CompletableFuture) AbstractSegmentedStoreConfiguration(org.infinispan.configuration.cache.AbstractSegmentedStoreConfiguration) FlagBitSets(org.infinispan.context.impl.FlagBitSets) InternalMetadataImpl(org.infinispan.metadata.impl.InternalMetadataImpl) HashSet(java.util.HashSet) Characteristic(org.infinispan.persistence.spi.NonBlockingStore.Characteristic) BiPredicate(java.util.function.BiPredicate) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) Log(org.infinispan.util.logging.Log) CacheWriterInterceptor(org.infinispan.interceptors.impl.CacheWriterInterceptor) Flowable(io.reactivex.rxjava3.core.Flowable) Iterator(java.util.Iterator) NonBlockingStore(org.infinispan.persistence.spi.NonBlockingStore) Util(org.infinispan.commons.util.Util) NonBlockingManager(org.infinispan.util.concurrent.NonBlockingManager) Completable(io.reactivex.rxjava3.core.Completable) PutMapCommand(org.infinispan.commands.write.PutMapCommand) NotSupportedException(javax.transaction.NotSupportedException) PersistenceUtil(org.infinispan.persistence.internal.PersistenceUtil) Configuration(org.infinispan.configuration.cache.Configuration) BlockingManager(org.infinispan.util.concurrent.BlockingManager) TransactionManager(javax.transaction.TransactionManager) TimeService(org.infinispan.commons.time.TimeService) InputStream(java.io.InputStream) Transaction(javax.transaction.Transaction) AbstractCacheTransaction(org.infinispan.transaction.impl.AbstractCacheTransaction) SystemException(javax.transaction.SystemException) InternalMetadataImpl(org.infinispan.metadata.impl.InternalMetadataImpl) PersistenceException(org.infinispan.persistence.spi.PersistenceException) InvalidTransactionException(javax.transaction.InvalidTransactionException) StoreUnavailableException(org.infinispan.persistence.spi.StoreUnavailableException) InvalidTransactionException(javax.transaction.InvalidTransactionException) IOException(java.io.IOException) SystemException(javax.transaction.SystemException) PersistenceException(org.infinispan.persistence.spi.PersistenceException) NotSupportedException(javax.transaction.NotSupportedException) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand)

Aggregations

DataConversion (org.infinispan.encoding.DataConversion)41 MVCCEntry (org.infinispan.container.entries.MVCCEntry)9 KeyPartitioner (org.infinispan.distribution.ch.KeyPartitioner)9 ArrayList (java.util.ArrayList)8 AdvancedCache (org.infinispan.AdvancedCache)8 Util (org.infinispan.commons.util.Util)8 EnumUtil (org.infinispan.commons.util.EnumUtil)7 InvocationContext (org.infinispan.context.InvocationContext)7 Completable (io.reactivex.rxjava3.core.Completable)6 Flowable (io.reactivex.rxjava3.core.Flowable)6 Set (java.util.Set)6 CompletionStage (java.util.concurrent.CompletionStage)6 Function (java.util.function.Function)6 Transaction (javax.transaction.Transaction)6 TransactionManager (javax.transaction.TransactionManager)6 MediaType (org.infinispan.commons.dataconversion.MediaType)6 TimeService (org.infinispan.commons.time.TimeService)6 Configuration (org.infinispan.configuration.cache.Configuration)6 CacheEntry (org.infinispan.container.entries.CacheEntry)6 FlagBitSets (org.infinispan.context.impl.FlagBitSets)6