Search in sources :

Example 21 with KeyValuePair

use of org.infinispan.util.KeyValuePair in project infinispan by infinispan.

the class ContinuousQueryObjectStorageTest method testContinuousQueryWithProjections.

public void testContinuousQueryWithProjections() throws InterruptedException {
    User user1 = new UserPB();
    user1.setId(1);
    user1.setName("John");
    user1.setSurname("Doe");
    user1.setGender(User.Gender.MALE);
    user1.setAge(22);
    user1.setAccountIds(new HashSet<>(Arrays.asList(1, 2)));
    user1.setNotes("Lorem ipsum dolor sit amet");
    User user2 = new UserPB();
    user2.setId(2);
    user2.setName("Spider");
    user2.setSurname("Man");
    user2.setGender(User.Gender.MALE);
    user2.setAge(32);
    user2.setAccountIds(Collections.singleton(3));
    User user3 = new UserPB();
    user3.setId(3);
    user3.setName("Spider");
    user3.setSurname("Woman");
    user3.setGender(User.Gender.FEMALE);
    user3.setAge(40);
    remoteCache.clear();
    remoteCache.put("user" + user1.getId(), user1);
    remoteCache.put("user" + user2.getId(), user2);
    remoteCache.put("user" + user3.getId(), user3);
    assertEquals(3, remoteCache.size());
    QueryFactory qf = Search.getQueryFactory(remoteCache);
    Query<Object[]> query = qf.<Object[]>create("SELECT age FROM sample_bank_account.User WHERE age <= :ageParam").setParameter("ageParam", 32);
    final BlockingQueue<KeyValuePair<String, Object[]>> joined = new LinkedBlockingQueue<>();
    final BlockingQueue<String> left = new LinkedBlockingQueue<>();
    ContinuousQueryListener<String, Object[]> listener = new ContinuousQueryListener<String, Object[]>() {

        @Override
        public void resultJoining(String key, Object[] value) {
            joined.add(new KeyValuePair<>(key, value));
        }

        @Override
        public void resultLeaving(String key) {
            left.add(key);
        }
    };
    ContinuousQuery<String, User> continuousQuery = Search.getContinuousQuery(remoteCache);
    continuousQuery.addContinuousQueryListener(query, listener);
    expectElementsInQueue(joined, 2, (kv) -> kv.getValue()[0], 32, 22);
    expectElementsInQueue(left, 0);
    expectNoMoreElementsInQueues(joined, left);
    user3.setAge(30);
    remoteCache.put("user" + user3.getId(), user3);
    expectElementsInQueue(joined, 1, (kv) -> kv.getValue()[0], 30);
    expectElementsInQueue(left, 0);
    expectNoMoreElementsInQueues(joined, left);
    user1.setAge(40);
    user2.setAge(40);
    user3.setAge(40);
    remoteCache.put("user" + user1.getId(), user1);
    remoteCache.put("user" + user2.getId(), user2);
    remoteCache.put("user" + user3.getId(), user3);
    expectElementsInQueue(joined, 0);
    expectElementsInQueue(left, 3);
    remoteCache.clear();
    user1.setAge(21);
    user2.setAge(22);
    remoteCache.put("expiredUser1", user1, 5, TimeUnit.MILLISECONDS);
    remoteCache.put("expiredUser2", user2, 5, TimeUnit.MILLISECONDS);
    expectElementsInQueue(joined, 2);
    expectElementsInQueue(left, 0);
    timeService.advance(6);
    assertNull(remoteCache.get("expiredUser1"));
    assertNull(remoteCache.get("expiredUser2"));
    expectElementsInQueue(joined, 0);
    expectElementsInQueue(left, 2);
    continuousQuery.removeContinuousQueryListener(listener);
    user2.setAge(22);
    remoteCache.put("user" + user2.getId(), user2);
    expectElementsInQueue(joined, 0);
    expectElementsInQueue(left, 0);
}
Also used : QueryFactory(org.infinispan.query.dsl.QueryFactory) User(org.infinispan.query.dsl.embedded.testdomain.User) KeyValuePair(org.infinispan.util.KeyValuePair) ContinuousQueryListener(org.infinispan.query.api.continuous.ContinuousQueryListener) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) UserPB(org.infinispan.client.hotrod.query.testdomain.protobuf.UserPB)

Example 22 with KeyValuePair

use of org.infinispan.util.KeyValuePair in project infinispan by infinispan.

the class SingleFileStore method purgeExpiredEntries.

private void purgeExpiredEntries(long now, UnicastProcessor<MarshallableEntry<K, V>> processor, List<KeyValuePair<Object, FileEntry>> entriesToPurge) {
    entriesToPurge.sort(Comparator.comparingLong(kvp -> kvp.getValue().offset));
    for (ListIterator<KeyValuePair<Object, FileEntry>> it = entriesToPurge.listIterator(); it.hasNext(); ) {
        KeyValuePair<Object, FileEntry> next = it.next();
        FileEntry fe = next.getValue();
        if (fe.isExpired(now)) {
            it.set(null);
            // Safe to unlock because the entry was locked collectExpiredEntries
            MarshallableEntry<K, V> entry = readFromDisk(fe, next.getKey(), true, true);
            processor.onNext(entry);
            try {
                free(fe);
            } catch (Exception e) {
                throw new PersistenceException(e);
            }
        }
    }
}
Also used : Arrays(java.util.Arrays) IntSets(org.infinispan.commons.util.IntSets) SortedSet(java.util.SortedSet) ListIterator(java.util.ListIterator) LogFactory(org.infinispan.util.logging.LogFactory) TopologyIracVersion(org.infinispan.container.versioning.irac.TopologyIracVersion) MarshallableEntryFactory(org.infinispan.persistence.spi.MarshallableEntryFactory) GlobalConfiguration(org.infinispan.configuration.global.GlobalConfiguration) ByteBuffer(java.nio.ByteBuffer) IracEntryVersion(org.infinispan.container.versioning.irac.IracEntryVersion) XSiteNamedCache(org.infinispan.xsite.XSiteNamedCache) AdvancedCache(org.infinispan.AdvancedCache) CompletableFutures(org.infinispan.util.concurrent.CompletableFutures) Map(java.util.Map) ConfiguredBy(org.infinispan.commons.configuration.ConfiguredBy) ByteBufferFactory(org.infinispan.commons.io.ByteBufferFactory) Path(java.nio.file.Path) EnumSet(java.util.EnumSet) KeyValuePair(org.infinispan.util.KeyValuePair) PERSISTENCE(org.infinispan.util.logging.Log.PERSISTENCE) PersistenceUtil(org.infinispan.persistence.PersistenceUtil) IracMetadata(org.infinispan.metadata.impl.IracMetadata) Predicate(java.util.function.Predicate) ByRef(org.infinispan.commons.util.ByRef) Set(java.util.Set) LockingMode(org.infinispan.transaction.LockingMode) UnicastProcessor(io.reactivex.rxjava3.processors.UnicastProcessor) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) IntSet(org.infinispan.commons.util.IntSet) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) LocalDate(java.time.LocalDate) InitializationContext(org.infinispan.persistence.spi.InitializationContext) GuardedBy(net.jcip.annotations.GuardedBy) SingleFileStoreConfiguration(org.infinispan.configuration.cache.SingleFileStoreConfiguration) PersistenceException(org.infinispan.persistence.spi.PersistenceException) HashMap(java.util.HashMap) Metadata(org.infinispan.metadata.Metadata) CompletionStages(org.infinispan.util.concurrent.CompletionStages) AbstractSegmentedStoreConfiguration(org.infinispan.configuration.cache.AbstractSegmentedStoreConfiguration) TreeSet(java.util.TreeSet) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) Log(org.infinispan.util.logging.Log) PrivateMetadata(org.infinispan.metadata.impl.PrivateMetadata) MarshallableEntry(org.infinispan.persistence.spi.MarshallableEntry) Flowable(io.reactivex.rxjava3.core.Flowable) Iterator(java.util.Iterator) NonBlockingStore(org.infinispan.persistence.spi.NonBlockingStore) Publisher(org.reactivestreams.Publisher) TransactionMode(org.infinispan.transaction.TransactionMode) IOException(java.io.IOException) SimpleClusteredVersion(org.infinispan.container.versioning.SimpleClusteredVersion) TransactionConfiguration(org.infinispan.configuration.cache.TransactionConfiguration) File(java.io.File) PrimitiveIterator(java.util.PrimitiveIterator) Flowable.defer(io.reactivex.rxjava3.core.Flowable.defer) PersistenceMarshaller(org.infinispan.marshall.persistence.PersistenceMarshaller) Configuration(org.infinispan.configuration.cache.Configuration) Comparator(java.util.Comparator) BlockingManager(org.infinispan.util.concurrent.BlockingManager) FileChannel(java.nio.channels.FileChannel) Collections(java.util.Collections) StampedLock(java.util.concurrent.locks.StampedLock) Marshaller(org.infinispan.commons.marshall.Marshaller) TimeService(org.infinispan.commons.time.TimeService) KeyValuePair(org.infinispan.util.KeyValuePair) PersistenceException(org.infinispan.persistence.spi.PersistenceException) PersistenceException(org.infinispan.persistence.spi.PersistenceException) IOException(java.io.IOException)

Example 23 with KeyValuePair

use of org.infinispan.util.KeyValuePair in project infinispan by infinispan.

the class JpaStore method bulkUpdate.

@Override
public CompletionStage<Void> bulkUpdate(Publisher<MarshallableEntry<? extends K, ? extends V>> publisher) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    Flowable.using(() -> {
        EntityManager em = emf.createEntityManager();
        EntityTransaction txn = em.getTransaction();
        return new KeyValuePair<>(em, txn);
    }, kvp -> createBatchFlowable(kvp.getKey(), kvp.getValue(), publisher), kvp -> {
        EntityTransaction txn = kvp.getValue();
        if (txn != null && txn.isActive())
            txn.rollback();
        kvp.getKey().close();
    }).doOnError(e -> {
        if (e instanceof JpaStoreException)
            throw (JpaStoreException) e;
        throw new JpaStoreException("Exception caught in bulkUpdate()", e);
    }).subscribe(RxJavaInterop.emptyConsumer(), future::completeExceptionally, () -> future.complete(null));
    return future;
}
Also used : LogFactory(org.infinispan.util.logging.LogFactory) MarshallableEntryFactory(org.infinispan.persistence.spi.MarshallableEntryFactory) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) MetadataEntityKey(org.infinispan.persistence.jpa.impl.MetadataEntityKey) MySQLDialect(org.hibernate.dialect.MySQLDialect) ConfiguredBy(org.infinispan.commons.configuration.ConfiguredBy) Metamodel(javax.persistence.metamodel.Metamodel) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) KeyValuePair(org.infinispan.util.KeyValuePair) PERSISTENCE(org.infinispan.util.logging.Log.PERSISTENCE) AbstractIterator(org.infinispan.commons.util.AbstractIterator) ExecutorAllCompletionService(org.infinispan.executors.ExecutorAllCompletionService) SingularAttribute(javax.persistence.metamodel.SingularAttribute) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) ScrollableResults(org.hibernate.ScrollableResults) RxJavaInterop(org.infinispan.commons.reactive.RxJavaInterop) Predicate(java.util.function.Predicate) SessionFactory(org.hibernate.SessionFactory) Collectors(java.util.stream.Collectors) IdentifiableType(javax.persistence.metamodel.IdentifiableType) List(java.util.List) Query(javax.persistence.Query) CompletionStage(java.util.concurrent.CompletionStage) AdvancedLoadWriteStore(org.infinispan.persistence.spi.AdvancedLoadWriteStore) GeneratedValue(javax.persistence.GeneratedValue) JpaStoreConfiguration(org.infinispan.persistence.jpa.configuration.JpaStoreConfiguration) Dialect(org.hibernate.dialect.Dialect) EntityManagerFactory(javax.persistence.EntityManagerFactory) ScrollMode(org.hibernate.ScrollMode) InitializationContext(org.infinispan.persistence.spi.InitializationContext) Store(org.infinispan.commons.persistence.Store) Criteria(org.hibernate.Criteria) Restrictions(org.hibernate.criterion.Restrictions) Session(org.hibernate.Session) CompletableFuture(java.util.concurrent.CompletableFuture) Metadata(org.infinispan.metadata.Metadata) ArrayList(java.util.ArrayList) Stats(org.infinispan.persistence.jpa.impl.Stats) Schedulers(io.reactivex.rxjava3.schedulers.Schedulers) EntityType(javax.persistence.metamodel.EntityType) Log(org.infinispan.util.logging.Log) Scheduler(io.reactivex.rxjava3.core.Scheduler) EntityNotFoundException(javax.persistence.EntityNotFoundException) CriteriaDelete(javax.persistence.criteria.CriteriaDelete) PrivateMetadata(org.infinispan.metadata.impl.PrivateMetadata) MarshallableEntry(org.infinispan.persistence.spi.MarshallableEntry) StreamSupport(java.util.stream.StreamSupport) Root(javax.persistence.criteria.Root) Type(javax.persistence.metamodel.Type) Flowable(io.reactivex.rxjava3.core.Flowable) Executor(java.util.concurrent.Executor) EntityManagerFactoryRegistry(org.infinispan.persistence.jpa.impl.EntityManagerFactoryRegistry) MetadataEntity(org.infinispan.persistence.jpa.impl.MetadataEntity) Publisher(org.reactivestreams.Publisher) EntityManager(javax.persistence.EntityManager) Projections(org.hibernate.criterion.Projections) PersistenceMarshaller(org.infinispan.marshall.persistence.PersistenceMarshaller) EntityTransaction(javax.persistence.EntityTransaction) TimeService(org.infinispan.commons.time.TimeService) EntityTransaction(javax.persistence.EntityTransaction) CompletableFuture(java.util.concurrent.CompletableFuture) EntityManager(javax.persistence.EntityManager) KeyValuePair(org.infinispan.util.KeyValuePair)

Example 24 with KeyValuePair

use of org.infinispan.util.KeyValuePair in project infinispan by infinispan.

the class ProtobufMetadataManagerInterceptor method visitReplaceCommand.

@Override
public Object visitReplaceCommand(InvocationContext ctx, ReplaceCommand command) {
    final Object key = command.getKey();
    final Object value = command.getNewValue();
    if (!ctx.isOriginLocal()) {
        return invokeNext(ctx, command);
    }
    if (!(key instanceof String)) {
        throw log.keyMustBeString(key.getClass());
    }
    if (!(value instanceof String)) {
        throw log.valueMustBeString(value.getClass());
    }
    if (!shouldIntercept(key)) {
        return invokeNext(ctx, command);
    }
    if (!((String) key).endsWith(PROTO_KEY_SUFFIX)) {
        throw log.keyMustBeStringEndingWithProto(key);
    }
    // lock global errors key
    LockControlCommand cmd = commandsFactory.buildLockControlCommand(ERRORS_KEY_SUFFIX, command.getFlagsBitSet(), null);
    InvocationStage stage = invoker.running().invokeStage(ctx, cmd);
    return makeStage(asyncInvokeNext(ctx, command, stage)).thenApply(ctx, command, (rCtx, rCommand, rv) -> {
        if (rCommand.isSuccessful()) {
            long flagsBitSet = copyFlags(rCommand);
            if (rCtx.isOriginLocal()) {
                ProgressCallback progressCallback = new ProgressCallback();
                registerProtoFile((String) key, (String) value, progressCallback);
                List<KeyValuePair<String, String>> errorUpdates = computeErrorUpdates(progressCallback);
                return updateSchemaErrorsIterator(rCtx, flagsBitSet, errorUpdates.iterator());
            }
            registerProtoFile((String) key, (String) value, EMPTY_CALLBACK);
        }
        return InvocationStage.completedNullStage();
    });
}
Also used : InvocationStage(org.infinispan.interceptors.InvocationStage) SyncInvocationStage(org.infinispan.interceptors.SyncInvocationStage) KeyValuePair(org.infinispan.util.KeyValuePair) LockControlCommand(org.infinispan.commands.control.LockControlCommand)

Example 25 with KeyValuePair

use of org.infinispan.util.KeyValuePair in project infinispan by infinispan.

the class ProtobufMetadataManagerInterceptor method handlePutKeyValueResult.

private InvocationStage handlePutKeyValueResult(InvocationContext ctx, PutKeyValueCommand putKeyValueCommand, Object rv) {
    if (putKeyValueCommand.isSuccessful()) {
        // StateConsumerImpl uses PutKeyValueCommands with InternalCacheEntry
        // values in order to preserve timestamps, so read the value from the context
        Object key = putKeyValueCommand.getKey();
        Object value = ctx.lookupEntry(key).getValue();
        if (!(value instanceof String)) {
            throw log.valueMustBeString(value.getClass());
        }
        long flagsBitSet = copyFlags(putKeyValueCommand);
        if (ctx.isOriginLocal() && !putKeyValueCommand.hasAnyFlag(FlagBitSets.PUT_FOR_STATE_TRANSFER)) {
            ProgressCallback progressCallback = new ProgressCallback();
            registerProtoFile((String) key, (String) value, progressCallback);
            List<KeyValuePair<String, String>> errorUpdates = computeErrorUpdates(progressCallback);
            InvocationStage updateStage = updateSchemaErrorsIterator(ctx, flagsBitSet, errorUpdates.iterator());
            return makeStage(updateStage.thenReturn(ctx, putKeyValueCommand, rv));
        }
        registerProtoFile((String) key, (String) value, EMPTY_CALLBACK);
    }
    return makeStage(rv);
}
Also used : InvocationStage(org.infinispan.interceptors.InvocationStage) SyncInvocationStage(org.infinispan.interceptors.SyncInvocationStage) KeyValuePair(org.infinispan.util.KeyValuePair)

Aggregations

KeyValuePair (org.infinispan.util.KeyValuePair)26 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)8 ArrayList (java.util.ArrayList)7 List (java.util.List)6 UserPB (org.infinispan.client.hotrod.query.testdomain.protobuf.UserPB)6 ContinuousQueryListener (org.infinispan.query.api.continuous.ContinuousQueryListener)6 QueryFactory (org.infinispan.query.dsl.QueryFactory)6 User (org.infinispan.query.dsl.embedded.testdomain.User)6 HotRodClient (org.infinispan.server.hotrod.test.HotRodClient)4 Iterator (java.util.Iterator)3 Map (java.util.Map)3 Set (java.util.Set)3 InvocationStage (org.infinispan.interceptors.InvocationStage)3 SyncInvocationStage (org.infinispan.interceptors.SyncInvocationStage)3 Metadata (org.infinispan.metadata.Metadata)3 Flowable (io.reactivex.rxjava3.core.Flowable)2 ByteBuffer (java.nio.ByteBuffer)2 TreeSet (java.util.TreeSet)2 CompletionStage (java.util.concurrent.CompletionStage)2 Predicate (java.util.function.Predicate)2