Skip to content

Commit c952a84

Browse files
rvansagalderz
authored andcommitted
HHH-10023 Make hibernate-infinispan compiled with Infinispan 7.x but runnable with Infinispan 8.x
1 parent 6fdc4ca commit c952a84

File tree

10 files changed

+142
-46
lines changed

10 files changed

+142
-46
lines changed

hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/timestamp/ClusteredTimestampsRegionImpl.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package org.hibernate.cache.infinispan.timestamp;
88

99
import java.util.Map;
10-
import java.util.Set;
1110
import java.util.concurrent.ConcurrentHashMap;
1211
import javax.transaction.Transaction;
1312

@@ -17,6 +16,8 @@
1716

1817
import org.hibernate.engine.spi.SessionImplementor;
1918
import org.infinispan.AdvancedCache;
19+
import org.infinispan.commons.util.CloseableIterable;
20+
import org.infinispan.container.entries.CacheEntry;
2021
import org.infinispan.context.Flag;
2122
import org.infinispan.notifications.Listener;
2223
import org.infinispan.notifications.cachelistener.annotation.CacheEntryModified;
@@ -121,9 +122,14 @@ public void destroy() throws CacheException {
121122
* Brings all data from the distributed cache into our local cache.
122123
*/
123124
private void populateLocalCache() {
124-
final Set children = cache.keySet();
125-
for ( Object key : children ) {
126-
get( null, key );
125+
CloseableIterable<CacheEntry<Object, Void>> iterable = Caches.keys(cache);
126+
try {
127+
for (CacheEntry<Object, Void> entry : iterable) {
128+
get(null, entry.getKey());
129+
}
130+
}
131+
finally {
132+
iterable.close();
127133
}
128134
}
129135

hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/util/Caches.java

Lines changed: 90 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,21 @@
66
*/
77
package org.hibernate.cache.infinispan.util;
88

9+
import java.util.ArrayList;
10+
import java.util.HashSet;
11+
import java.util.Iterator;
12+
import java.util.Set;
913
import java.util.concurrent.Callable;
1014
import javax.transaction.Status;
1115
import javax.transaction.TransactionManager;
1216

1317
import org.infinispan.AdvancedCache;
18+
import org.infinispan.commons.util.CloseableIterable;
1419
import org.infinispan.commons.util.CloseableIterator;
20+
import org.infinispan.container.entries.CacheEntry;
1521
import org.infinispan.context.Flag;
22+
import org.infinispan.filter.AcceptAllKeyValueFilter;
23+
import org.infinispan.filter.NullValueConverter;
1624
import org.infinispan.remoting.rpc.RpcManager;
1725
import org.infinispan.remoting.rpc.RpcOptions;
1826

@@ -263,17 +271,95 @@ public static boolean isClustered(AdvancedCache cache) {
263271
}
264272

265273
public static void removeAll(AdvancedCache cache) {
266-
CloseableIterator it = cache.keySet().iterator();
274+
CloseableIterator it = keys(cache).iterator();
267275
try {
268276
while (it.hasNext()) {
269-
// Necessary to get next element
270-
it.next();
271-
it.remove();
277+
// Cannot use it.next(); it.remove() due to ISPN-5653
278+
cache.remove(it.next());
272279
}
273280
}
274281
finally {
275282
it.close();
276283
}
277284
}
278285

286+
/**
287+
* This interface is provided for convenient fluent use of CloseableIterable
288+
*/
289+
public interface CollectableCloseableIterable extends CloseableIterable {
290+
Set toSet();
291+
}
292+
293+
public static CollectableCloseableIterable keys(AdvancedCache cache) {
294+
// HHH-10023: we can't use keySet()
295+
final CloseableIterable<CacheEntry<Object, Void>> entryIterable = cache
296+
.filterEntries( AcceptAllKeyValueFilter.getInstance() )
297+
.converter( NullValueConverter.getInstance() );
298+
return new CollectableCloseableIterable() {
299+
@Override
300+
public void close() {
301+
entryIterable.close();
302+
}
303+
304+
@Override
305+
public CloseableIterator iterator() {
306+
final CloseableIterator<CacheEntry<Object, Void>> entryIterator = entryIterable.iterator();
307+
return new CloseableIterator() {
308+
@Override
309+
public void close() {
310+
entryIterator.close();
311+
}
312+
313+
@Override
314+
public boolean hasNext() {
315+
return entryIterator.hasNext();
316+
}
317+
318+
@Override
319+
public Object next() {
320+
return entryIterator.next().getKey();
321+
}
322+
};
323+
}
324+
325+
@Override
326+
public String toString() {
327+
CloseableIterator<CacheEntry<Object, Void>> it = entryIterable.iterator();
328+
try {
329+
if (!it.hasNext()) {
330+
return "[]";
331+
}
332+
333+
StringBuilder sb = new StringBuilder();
334+
sb.append('[');
335+
for (; ; ) {
336+
CacheEntry<Object, Void> entry = it.next();
337+
sb.append(entry.getKey());
338+
if (!it.hasNext()) {
339+
return sb.append(']').toString();
340+
}
341+
sb.append(',').append(' ');
342+
}
343+
}
344+
finally {
345+
it.close();
346+
}
347+
}
348+
349+
@Override
350+
public Set toSet() {
351+
HashSet set = new HashSet();
352+
CloseableIterator it = iterator();
353+
try {
354+
while (it.hasNext()) {
355+
set.add(it.next());
356+
}
357+
}
358+
finally {
359+
it.close();
360+
}
361+
return set;
362+
}
363+
};
364+
}
279365
}

hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/AbstractGeneralDataRegionTestCase.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
2323
import org.hibernate.cache.infinispan.InfinispanRegionFactory;
2424
import org.hibernate.cache.infinispan.impl.BaseGeneralDataRegion;
25+
import org.hibernate.cache.infinispan.util.Caches;
2526
import org.hibernate.cache.spi.GeneralDataRegion;
2627
import org.hibernate.cache.spi.QueryResultsRegion;
2728
import org.hibernate.cache.spi.Region;
@@ -33,6 +34,7 @@
3334
import org.hibernate.test.cache.infinispan.util.BatchModeJtaPlatform;
3435
import org.hibernate.test.cache.infinispan.util.CacheTestUtil;
3536
import org.infinispan.AdvancedCache;
37+
import org.infinispan.commons.util.CloseableIterable;
3638
import org.infinispan.transaction.tm.BatchModeTransactionManager;
3739
import org.jboss.logging.Logger;
3840
import org.junit.Test;
@@ -188,11 +190,11 @@ private void evictOrRemoveAllTest(String configName) throws Exception {
188190
SessionImplementor remoteSession = (SessionImplementor) sessionFactories.get(1).openSession();
189191

190192
try {
191-
Set keys = localCache.keySet();
192-
assertEquals( "No valid children in " + keys, 0, getValidKeyCount( keys ) );
193+
Set localKeys = Caches.keys(localCache).toSet();
194+
assertEquals( "No valid children in " + localKeys, 0, localKeys.size() );
193195

194-
keys = remoteCache.keySet();
195-
assertEquals( "No valid children in " + keys, 0, getValidKeyCount( keys ) );
196+
Set remoteKeys = Caches.keys(remoteCache).toSet();
197+
assertEquals( "No valid children in " + remoteKeys, 0, remoteKeys.size() );
196198

197199
assertNull( "local is clean", localRegion.get(null, KEY ) );
198200
assertNull( "remote is clean", remoteRegion.get(null, KEY ) );
@@ -215,13 +217,15 @@ private void evictOrRemoveAllTest(String configName) throws Exception {
215217
sleep( 250 );
216218
// This should re-establish the region root node in the optimistic case
217219
assertNull( localRegion.get(null, KEY ) );
218-
assertEquals( "No valid children in " + keys, 0, getValidKeyCount( localCache.keySet() ) );
220+
localKeys = Caches.keys(localCache).toSet();
221+
assertEquals( "No valid children in " + localKeys, 0, localKeys.size() );
219222

220223
// Re-establishing the region root on the local node doesn't
221224
// propagate it to other nodes. Do a get on the remote node to re-establish
222225
// This only adds a node in the case of optimistic locking
223226
assertEquals( null, remoteRegion.get(null, KEY ) );
224-
assertEquals( "No valid children in " + keys, 0, getValidKeyCount( remoteCache.keySet() ) );
227+
remoteKeys = Caches.keys(remoteCache).toSet();
228+
assertEquals( "No valid children in " + remoteKeys, 0, remoteKeys.size() );
225229

226230
assertEquals( "local is clean", null, localRegion.get(null, KEY ) );
227231
assertEquals( "remote is clean", null, remoteRegion.get(null, KEY ) );

hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/AbstractNonFunctionalTestCase.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,4 @@ protected void sleep(long ms) {
9696
protected void avoidConcurrentFlush() {
9797
testSupport.avoidConcurrentFlush();
9898
}
99-
100-
protected int getValidKeyCount(Set keys) {
101-
return keys.size();
102-
}
103-
10499
}

hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/collection/AbstractCollectionRegionAccessStrategyTestCase.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,9 @@ private void evictOrRemoveAllTest(final boolean evict) throws Exception {
418418

419419
final Object KEY = TestingKeyFactory.generateCollectionCacheKey( KEY_BASE + testCount++ );
420420

421-
assertEquals( 0, getValidKeyCount( localCollectionRegion.getCache().keySet() ) );
421+
assertEquals( 0, localCollectionRegion.getCache().size() );
422422

423-
assertEquals( 0, getValidKeyCount( remoteCollectionRegion.getCache().keySet() ) );
423+
assertEquals( 0, remoteCollectionRegion.getCache().size() );
424424

425425
assertNull( "local is clean", localAccessStrategy.get(null, KEY, System.currentTimeMillis() ) );
426426
assertNull( "remote is clean", remoteAccessStrategy.get(null, KEY, System.currentTimeMillis() ) );
@@ -447,19 +447,19 @@ public Void call() throws Exception {
447447
// This should re-establish the region root node
448448
assertNull( localAccessStrategy.get(null, KEY, System.currentTimeMillis() ) );
449449

450-
assertEquals( 0, getValidKeyCount( localCollectionRegion.getCache().keySet() ) );
450+
assertEquals( 0, localCollectionRegion.getCache().size() );
451451

452452
// Re-establishing the region root on the local node doesn't
453453
// propagate it to other nodes. Do a get on the remote node to re-establish
454454
assertEquals( null, remoteAccessStrategy.get(null, KEY, System.currentTimeMillis() ) );
455455

456-
assertEquals( 0, getValidKeyCount( remoteCollectionRegion.getCache().keySet() ) );
456+
assertEquals( 0, remoteCollectionRegion.getCache().size() );
457457

458458
// Test whether the get above messes up the optimistic version
459459
remoteAccessStrategy.putFromLoad(null, KEY, VALUE1, System.currentTimeMillis(), new Integer( 1 ) );
460460
assertEquals( VALUE1, remoteAccessStrategy.get(null, KEY, System.currentTimeMillis() ) );
461461

462-
assertEquals( 1, getValidKeyCount( remoteCollectionRegion.getCache().keySet() ) );
462+
assertEquals( 1, remoteCollectionRegion.getCache().size() );
463463

464464
// Wait for async propagation of the putFromLoad
465465
sleep( 250 );

hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/entity/AbstractEntityRegionAccessStrategyTestCase.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -506,8 +506,8 @@ public void testEvictAll() throws Exception {
506506

507507
private void evictOrRemoveTest(final boolean evict) throws Exception {
508508
final Object KEY = TestingKeyFactory.generateEntityCacheKey( KEY_BASE + testCount++ );
509-
assertEquals(0, getValidKeyCount(localEntityRegion.getCache().keySet()));
510-
assertEquals(0, getValidKeyCount(remoteEntityRegion.getCache().keySet()));
509+
assertEquals(0, localEntityRegion.getCache().size());
510+
assertEquals(0, remoteEntityRegion.getCache().size());
511511

512512
assertNull("local is clean", localAccessStrategy.get(null, KEY, System.currentTimeMillis()));
513513
assertNull("remote is clean", remoteAccessStrategy.get(null, KEY, System.currentTimeMillis()));
@@ -528,15 +528,15 @@ public Void call() throws Exception {
528528
}
529529
});
530530
assertEquals(null, localAccessStrategy.get(null, KEY, System.currentTimeMillis()));
531-
assertEquals(0, getValidKeyCount(localEntityRegion.getCache().keySet()));
531+
assertEquals(0, localEntityRegion.getCache().size());
532532
assertEquals(null, remoteAccessStrategy.get(null, KEY, System.currentTimeMillis()));
533-
assertEquals(0, getValidKeyCount(remoteEntityRegion.getCache().keySet()));
533+
assertEquals(0, remoteEntityRegion.getCache().size());
534534
}
535535

536536
private void evictOrRemoveAllTest(final boolean evict) throws Exception {
537537
final Object KEY = TestingKeyFactory.generateEntityCacheKey( KEY_BASE + testCount++ );
538-
assertEquals(0, getValidKeyCount(localEntityRegion.getCache().keySet()));
539-
assertEquals(0, getValidKeyCount(remoteEntityRegion.getCache().keySet()));
538+
assertEquals(0, localEntityRegion.getCache().size());
539+
assertEquals(0, remoteEntityRegion.getCache().size());
540540
assertNull("local is clean", localAccessStrategy.get(null, KEY, System.currentTimeMillis()));
541541
assertNull("remote is clean", remoteAccessStrategy.get(null, KEY, System.currentTimeMillis()));
542542

@@ -567,17 +567,17 @@ public Void call() throws Exception {
567567

568568
// This should re-establish the region root node in the optimistic case
569569
assertNull(localAccessStrategy.get(null, KEY, System.currentTimeMillis()));
570-
assertEquals(0, getValidKeyCount(localEntityRegion.getCache().keySet()));
570+
assertEquals(0, localEntityRegion.getCache().size());
571571

572572
// Re-establishing the region root on the local node doesn't
573573
// propagate it to other nodes. Do a get on the remote node to re-establish
574574
assertEquals(null, remoteAccessStrategy.get(null, KEY, System.currentTimeMillis()));
575-
assertEquals(0, getValidKeyCount(remoteEntityRegion.getCache().keySet()));
575+
assertEquals(0, remoteEntityRegion.getCache().size());
576576

577577
// Test whether the get above messes up the optimistic version
578578
remoteAccessStrategy.putFromLoad(null, KEY, VALUE1, System.currentTimeMillis(), new Integer(1));
579579
assertEquals(VALUE1, remoteAccessStrategy.get(null, KEY, System.currentTimeMillis()));
580-
assertEquals(1, getValidKeyCount(remoteEntityRegion.getCache().keySet()));
580+
assertEquals(1, remoteEntityRegion.getCache().size());
581581

582582
// Wait for async propagation
583583
sleep(250);

hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/MultiTenancyTestCase.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
import org.hibernate.boot.SessionFactoryBuilder;
88
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
99
import org.hibernate.cache.infinispan.entity.EntityRegionImpl;
10+
import org.hibernate.cache.infinispan.util.Caches;
1011
import org.hibernate.engine.jdbc.connections.spi.AbstractMultiTenantConnectionProvider;
1112
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
1213
import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;
1314
import org.hibernate.test.cache.infinispan.tm.XaConnectionProvider;
1415
import org.hibernate.testing.env.ConnectionProviderBuilder;
16+
import org.infinispan.AdvancedCache;
17+
import org.infinispan.commons.util.CloseableIterable;
1518
import org.infinispan.commons.util.CloseableIteratorSet;
1619
import org.infinispan.context.Flag;
1720
import org.junit.Test;
@@ -110,9 +113,10 @@ public Void call() throws Exception {
110113
// });
111114
// }
112115
EntityRegionImpl region = (EntityRegionImpl) sessionFactory().getSecondLevelCacheRegion(Item.class.getName());
113-
CloseableIteratorSet keySet = region.getCache().withFlags(Flag.CACHE_MODE_LOCAL).keySet();
114-
assertEquals(1, keySet.size());
115-
assertEquals("OldCacheKeyImplementation", keySet.iterator().next().getClass().getSimpleName());
116+
AdvancedCache localCache = region.getCache().withFlags(Flag.CACHE_MODE_LOCAL);
117+
CloseableIterable keys = Caches.keys(localCache);
118+
assertEquals(1, localCache.size());
119+
assertEquals("OldCacheKeyImplementation", keys.iterator().next().getClass().getSimpleName());
116120
}
117121

118122
}

hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/NoTenancyTestCase.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
import org.hibernate.Session;
66
import org.hibernate.cache.infinispan.entity.EntityRegionImpl;
7+
import org.hibernate.cache.infinispan.util.Caches;
8+
import org.infinispan.AdvancedCache;
9+
import org.infinispan.commons.util.CloseableIterable;
710
import org.infinispan.commons.util.CloseableIteratorSet;
811
import org.infinispan.context.Flag;
912
import org.junit.Test;
@@ -48,9 +51,10 @@ public Void call() throws Exception {
4851

4952
}
5053
EntityRegionImpl region = (EntityRegionImpl) sessionFactory().getSecondLevelCacheRegion(Item.class.getName());
51-
CloseableIteratorSet keySet = region.getCache().withFlags(Flag.CACHE_MODE_LOCAL).keySet();
52-
assertEquals(1, keySet.size());
53-
assertEquals(sessionFactory().getClassMetadata(Item.class).getIdentifierType().getReturnedClass(), keySet.iterator().next().getClass());
54+
AdvancedCache localCache = region.getCache().withFlags(Flag.CACHE_MODE_LOCAL);
55+
CloseableIterable keys = Caches.keys(localCache);
56+
assertEquals(1, localCache.size());
57+
assertEquals(sessionFactory().getClassMetadata(Item.class).getIdentifierType().getReturnedClass(), keys.iterator().next().getClass());
5458
}
5559

5660
}

hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/cluster/EntityCollectionInvalidationTestCase.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ public void testAll() throws Exception {
124124
assertLoadedFromCache( remoteListener, ids.customerId, ids.contactIds );
125125

126126
// After modification, local cache should have been invalidated and hence should be empty
127-
assertEquals( 0, getValidKeyCount( localCollectionCache.keySet() ) );
128-
assertEquals( 0, getValidKeyCount( localCustomerCache.keySet() ) );
127+
assertEquals( 0, localCollectionCache.size() );
128+
assertEquals( 0, localCustomerCache.size() );
129129
}
130130
finally {
131131
// cleanup the db
@@ -313,10 +313,6 @@ private void assertLoadedFromCache(MyListener listener, Integer custId, Set cont
313313
);
314314
}
315315

316-
protected int getValidKeyCount(Set keys) {
317-
return keys.size();
318-
}
319-
320316
@Listener
321317
public static class MyListener {
322318
private static final Log log = LogFactory.getLog( MyListener.class );

hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/cluster/NaturalIdInvalidationTestCase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.hibernate.Session;
1515
import org.hibernate.SessionFactory;
1616
import org.hibernate.Transaction;
17+
import org.hibernate.cache.infinispan.util.Caches;
1718
import org.hibernate.criterion.Restrictions;
1819
import org.hibernate.test.cache.infinispan.functional.Citizen;
1920
import org.hibernate.test.cache.infinispan.functional.NaturalIdOnManyToOne;
@@ -124,7 +125,7 @@ public void testAll() throws Exception {
124125
deleteCitizenWithCriteria(remoteTM, remoteFactory);
125126
sleep(250);
126127

127-
Set localKeys = localNaturalIdCache.keySet();
128+
Set localKeys = Caches.keys(localNaturalIdCache.getAdvancedCache()).toSet();
128129
assertEquals(1, localKeys.size());
129130
// Only key left is the one for the citizen *not* in France
130131
localKeys.toString().contains("000");

0 commit comments

Comments
 (0)