Search in sources :

Example 46 with Person

use of org.infinispan.query.test.Person in project infinispan by infinispan.

the class ClusteredCacheTest method testMerge.

public void testMerge() throws Exception {
    prepareTestData();
    TransactionManager transactionManager = cache2.getAdvancedCache().getTransactionManager();
    assertEquals(3, createSelectAllQuery(cache2).execute().list().size());
    String key = "newGoat";
    person4 = new Person(key, "eats something", 42);
    // merge a new key
    if (transactionsEnabled())
        transactionManager.begin();
    cache2.merge(key, person4, (k1, v3) -> new Person(key, "eats something", 42));
    if (transactionsEnabled())
        transactionManager.commit();
    StaticTestingErrorHandler.assertAllGood(cache1, cache2);
    List<Person> found = createSelectAllQuery(cache2).execute().list();
    assertEquals(4, found.size());
    assertTrue(found.contains(person4));
    // merge and replace existing key
    if (transactionsEnabled())
        transactionManager.begin();
    cache2.merge(key, new Person(key, "hola", 42), (v1, v2) -> new Person(v1.getName() + "_" + v2.getName(), v1.getBlurb() + "_" + v2.getBlurb(), v1.getAge() + v2.getAge()));
    if (transactionsEnabled())
        transactionManager.commit();
    StaticTestingErrorHandler.assertAllGood(cache1, cache2);
    found = createSelectAllQuery(cache2).execute().list();
    assertEquals(4, found.size());
    assertFalse(found.contains(person4));
    assertTrue(found.contains(new Person("newGoat_newGoat", "eats something_hola", 84)));
    // remove if merge result is null
    if (transactionsEnabled())
        transactionManager.begin();
    cache2.merge(key, person4, (k, v) -> null);
    if (transactionsEnabled())
        transactionManager.commit();
    StaticTestingErrorHandler.assertAllGood(cache1, cache2);
    found = createSelectAllQuery(cache2).execute().list();
    assertEquals(3, found.size());
    assertFalse(found.contains(person4));
    assertFalse(found.contains(new Person("newGoat_newGoat", "eats something_hola", 84)));
}
Also used : TransactionManager(javax.transaction.TransactionManager) Person(org.infinispan.query.test.Person)

Example 47 with Person

use of org.infinispan.query.test.Person in project infinispan by infinispan.

the class ClusteredCacheTest method testComputeIfAbsent.

public void testComputeIfAbsent() throws Exception {
    prepareTestData();
    TransactionManager transactionManager = cache2.getAdvancedCache().getTransactionManager();
    assertEquals(3, createSelectAllQuery(cache2).execute().list().size());
    String key = "newGoat";
    person4 = new Person(key, "eats something", 42);
    // compute a new key
    Function<Object, Person> mappingFunction = (Function<Object, Person> & Serializable) k -> new Person(k.toString(), "eats something", 42);
    if (transactionsEnabled())
        transactionManager.begin();
    cache2.computeIfAbsent(key, mappingFunction);
    if (transactionsEnabled())
        transactionManager.commit();
    StaticTestingErrorHandler.assertAllGood(cache1, cache2);
    List<Person> found = createSelectAllQuery(cache2).execute().list();
    assertEquals(4, found.size());
    assertTrue(found.contains(person4));
    // do nothing for existing keys
    if (transactionsEnabled())
        transactionManager.begin();
    cache2.computeIfAbsent(key1, mappingFunction);
    if (transactionsEnabled())
        transactionManager.commit();
    StaticTestingErrorHandler.assertAllGood(cache1, cache2);
    found = createSelectAllQuery(cache2).execute().list();
    assertEquals(4, found.size());
    assertFalse(found.contains(new Person(key1, "eats something", 42)));
    assertTrue(found.contains(person1));
    // do nothing if null
    Function<Object, Person> mappingToNull = (Function<Object, Person> & Serializable) k -> null;
    if (transactionsEnabled())
        transactionManager.begin();
    cache2.computeIfAbsent("anotherKey", mappingToNull);
    if (transactionsEnabled())
        transactionManager.commit();
    StaticTestingErrorHandler.assertAllGood(cache1, cache2);
    found = createSelectAllQuery(cache2).execute().list();
    assertEquals(4, found.size());
    assertFalse(found.contains(null));
}
Also used : TransactionManager(javax.transaction.TransactionManager) Person(org.infinispan.query.test.Person)

Example 48 with Person

use of org.infinispan.query.test.Person in project infinispan by infinispan.

the class ClusteredCacheTest method testPutAsync.

public void testPutAsync() throws Exception {
    prepareTestData();
    assertEquals(3, createSelectAllQuery(cache2).execute().list().size());
    person4 = new Person();
    person4.setName("New Goat");
    person4.setBlurb("Also eats grass");
    Future<Person> f = cache2.putAsync("newGoat", person4);
    f.get();
    assertTrue(f.isDone());
    List<Person> found = createSelectAllQuery(cache2).execute().list();
    assertEquals(4, found.size());
    assertTrue(found.contains(person4));
    Person person5 = new Person();
    person5.setName("Abnormal Goat");
    person5.setBlurb("Plays with grass.");
    f = cache2.putAsync("newGoat", person5);
    f.get();
    assertTrue(f.isDone());
    found = createSelectAllQuery(cache2).execute().list();
    assertEquals(4, found.size());
    assertFalse(found.contains(person4));
    assertTrue(found.contains(person5));
    StaticTestingErrorHandler.assertAllGood(cache1, cache2);
}
Also used : Person(org.infinispan.query.test.Person)

Example 49 with Person

use of org.infinispan.query.test.Person in project infinispan by infinispan.

the class ClusteredCacheTest method testAdded.

public void testAdded() throws Exception {
    prepareTestData();
    cacheQuery = createQuery(cache2, "blurb:'eats'");
    List<Person> found = cacheQuery.execute().list();
    assertEquals(2, found.size());
    assertTrue(found.contains(person2));
    assertTrue(found.contains(person3));
    assertFalse("This should not contain object person4", found.contains(person4));
    person4 = new Person();
    person4.setName("Mighty Goat");
    person4.setBlurb("Also eats grass");
    cache1.put("mighty", person4);
    cacheQuery = createQuery(cache2, "blurb:'eats'");
    found = cacheQuery.execute().list();
    assertEquals(3, found.size());
    assertTrue(found.contains(person2));
    assertTrue(found.contains(person3));
    assertTrue("This should now contain object person4", found.contains(person4));
    StaticTestingErrorHandler.assertAllGood(cache1, cache2);
}
Also used : Person(org.infinispan.query.test.Person)

Example 50 with Person

use of org.infinispan.query.test.Person in project infinispan by infinispan.

the class ClusteredCacheTest method testComputeIfPresent.

public void testComputeIfPresent() throws Exception {
    prepareTestData();
    TransactionManager transactionManager = cache2.getAdvancedCache().getTransactionManager();
    assertEquals(3, createSelectAllQuery(cache2).execute().list().size());
    // compute and replace existing key
    BiFunction<Object, Person, Person> remappingExisting = (BiFunction<Object, Person, Person> & Serializable) (k, v) -> new Person("replaced", "personOneChanged", 42);
    if (transactionsEnabled())
        transactionManager.begin();
    cache2.computeIfPresent(key1, remappingExisting);
    if (transactionsEnabled())
        transactionManager.commit();
    StaticTestingErrorHandler.assertAllGood(cache1, cache2);
    List<Person> found = createSelectAllQuery(cache2).execute().list();
    assertEquals(3, found.size());
    assertFalse(found.contains(person1));
    assertTrue(found.contains(new Person("replaced", "personOneChanged", 42)));
    String newKey = "newGoat";
    person4 = new Person(newKey, "eats something", 42);
    // do nothing for non existing keys
    BiFunction<Object, Person, Person> remappingFunction = (BiFunction<Object, Person, Person> & Serializable) (k, v) -> new Person(k.toString(), "eats something", 42);
    if (transactionsEnabled())
        transactionManager.begin();
    cache2.computeIfPresent(newKey, remappingFunction);
    if (transactionsEnabled())
        transactionManager.commit();
    StaticTestingErrorHandler.assertAllGood(cache1, cache2);
    found = createSelectAllQuery(cache2).execute().list();
    assertEquals(3, found.size());
    assertFalse(found.contains(person4));
    // remove if compute result is null
    BiFunction<Object, Person, Person> remappingToNull = (BiFunction<Object, Person, Person> & Serializable) (k, v) -> null;
    if (transactionsEnabled())
        transactionManager.begin();
    cache2.computeIfPresent(key2, remappingToNull);
    if (transactionsEnabled())
        transactionManager.commit();
    StaticTestingErrorHandler.assertAllGood(cache1, cache2);
    found = createSelectAllQuery(cache2).execute().list();
    assertFalse(found.contains(person2));
}
Also used : TransactionManager(javax.transaction.TransactionManager) Person(org.infinispan.query.test.Person)

Aggregations

Person (org.infinispan.query.test.Person)82 QueryFactory (org.infinispan.query.dsl.QueryFactory)21 Test (org.testng.annotations.Test)11 ObjectFilter (org.infinispan.objectfilter.ObjectFilter)6 SingleCacheManagerTest (org.infinispan.test.SingleCacheManagerTest)6 TransactionManager (javax.transaction.TransactionManager)5 MultipleCacheManagersTest (org.infinispan.test.MultipleCacheManagersTest)5 MagicKey (org.infinispan.distribution.MagicKey)3 AnotherGrassEater (org.infinispan.query.test.AnotherGrassEater)3 ArrayList (java.util.ArrayList)2 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)2 NumberFormat (java.text.NumberFormat)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 MBeanServer (javax.management.MBeanServer)1 ObjectName (javax.management.ObjectName)1 RollbackException (javax.transaction.RollbackException)1 CacheException (org.infinispan.commons.CacheException)1 CacheEntry (org.infinispan.container.entries.CacheEntry)1