From 6a958da6b0632dad6a15abb60f3e17a1918e8728 Mon Sep 17 00:00:00 2001 From: John Blum Date: Mon, 13 Apr 2015 19:31:52 -0700 Subject: [PATCH] SGF-357 - Optimize SimpleGemfireRepository.deleteAll() by using the new Region.removeAll() operation. --- .../support/SimpleGemfireRepository.java | 7 +-- .../SimpleGemfireRepositoryUnitTest.java | 48 ++++++++++--------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/springframework/data/gemfire/repository/support/SimpleGemfireRepository.java b/src/main/java/org/springframework/data/gemfire/repository/support/SimpleGemfireRepository.java index cb4d1064..6d93e5ce 100644 --- a/src/main/java/org/springframework/data/gemfire/repository/support/SimpleGemfireRepository.java +++ b/src/main/java/org/springframework/data/gemfire/repository/support/SimpleGemfireRepository.java @@ -31,6 +31,7 @@ import com.gemstone.gemfire.cache.query.SelectResults; * @see java.io.Serializable * @see org.springframework.data.gemfire.GemfireTemplate * @see org.springframework.data.gemfire.repository.GemfireRepository + * @see com.gemstone.gemfire.cache.Cache * @see com.gemstone.gemfire.cache.Region */ public class SimpleGemfireRepository implements GemfireRepository { @@ -243,10 +244,10 @@ public class SimpleGemfireRepository implements Gemf return (cacheTransactionManager != null && cacheTransactionManager.exists()); } + /* (non-Javadoc) */ + @SuppressWarnings("unchecked") void doRegionClear(final Region region) { - for (Object key : region.keySet()) { - region.remove(key); - } + region.removeAll(region.keySet()); } /* diff --git a/src/test/java/org/springframework/data/gemfire/repository/support/SimpleGemfireRepositoryUnitTest.java b/src/test/java/org/springframework/data/gemfire/repository/support/SimpleGemfireRepositoryUnitTest.java index c0805c47..52501cf1 100644 --- a/src/test/java/org/springframework/data/gemfire/repository/support/SimpleGemfireRepositoryUnitTest.java +++ b/src/test/java/org/springframework/data/gemfire/repository/support/SimpleGemfireRepositoryUnitTest.java @@ -37,6 +37,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.concurrent.atomic.AtomicLong; import org.junit.Test; @@ -304,9 +305,10 @@ public class SimpleGemfireRepositoryUnitTest { @Test public void testDeleteAllWithClear() { - Cache mockCache = mockCache("testDeleteAllWithClear", false); + Cache mockCache = mockCache("testDeleteAllWithClear.MockCache", false); - Region mockRegion = mockRegion("testDeleteAllWithClear", mockCache, DataPolicy.REPLICATE); + Region mockRegion = mockRegion("testDeleteAllWithClear.MockRegion", + mockCache, DataPolicy.REPLICATE); SimpleGemfireRepository gemfireRepository = new SimpleGemfireRepository( createGemfireTemplate(mockRegion), mockEntityInformation()); @@ -320,14 +322,16 @@ public class SimpleGemfireRepositoryUnitTest { } @Test - public void testDeleteAllWithKeysWhenClearThrowsUnsupportedOperationException() { - Cache mockCache = mockCache("testDeleteAllWithKeysWhenClearThrowsUnsupportedOperationException", false); + public void testDeleteAllWithKeysWhenClearThrowsException() { + Cache mockCache = mockCache("testDeleteAllWithKeysWhenClearThrowsException.MockCache", false); - Region mockRegion = mockRegion("testDeleteAllWithKeysWhenClearThrowsUnsupportedOperationException", + Region mockRegion = mockRegion("testDeleteAllWithKeysWhenClearThrowsException.MockRegion", mockCache, DataPolicy.PERSISTENT_REPLICATE); + Set keys = new HashSet(Arrays.asList(1l, 2l, 3l)); + doThrow(new UnsupportedOperationException("Not Implemented!")).when(mockRegion).clear(); - when(mockRegion.keySet()).thenReturn(new HashSet(Arrays.asList(1l, 2l, 3l))); + when(mockRegion.keySet()).thenReturn(keys); SimpleGemfireRepository gemfireRepository = new SimpleGemfireRepository( createGemfireTemplate(mockRegion), mockEntityInformation()); @@ -338,19 +342,19 @@ public class SimpleGemfireRepositoryUnitTest { verify(mockRegion, times(2)).getAttributes(); verify(mockRegion, times(2)).getRegionService(); verify(mockRegion, times(1)).clear(); - verify(mockRegion, times(1)).remove(eq(1l)); - verify(mockRegion, times(1)).remove(eq(2l)); - verify(mockRegion, times(1)).remove(eq(3l)); + verify(mockRegion, times(1)).removeAll(eq(keys)); } @Test public void testDeleteAllWithKeysWhenPartitionRegion() { - Cache mockCache = mockCache("testDeleteAllWithKeysWhenPartitionRegion", false); + Cache mockCache = mockCache("testDeleteAllWithKeysWhenPartitionRegion.MockCache", false); - Region mockRegion = mockRegion("testDeleteAllWithKeysWhenPartitionRegion", mockCache, - DataPolicy.PERSISTENT_PARTITION); + Region mockRegion = mockRegion("testDeleteAllWithKeysWhenPartitionRegion.MockRegion", + mockCache, DataPolicy.PERSISTENT_PARTITION); - when(mockRegion.keySet()).thenReturn(new HashSet(Arrays.asList(1l, 2l, 3l))); + Set keys = new HashSet(Arrays.asList(1l, 2l, 3l)); + + when(mockRegion.keySet()).thenReturn(keys); SimpleGemfireRepository gemfireRepository = new SimpleGemfireRepository( createGemfireTemplate(mockRegion), mockEntityInformation()); @@ -361,19 +365,19 @@ public class SimpleGemfireRepositoryUnitTest { verify(mockRegion, times(2)).getAttributes(); verify(mockRegion, times(0)).getRegionService(); verify(mockRegion, times(0)).clear(); - verify(mockRegion, times(1)).remove(eq(1l)); - verify(mockRegion, times(1)).remove(eq(2l)); - verify(mockRegion, times(1)).remove(eq(3l)); + verify(mockRegion, times(1)).removeAll(eq(keys)); } @Test public void testDeleteAllWithKeysWhenTransactionPresent() { - Cache mockCache = mockCache("testDeleteAllWithKeysWhenTransactionPresent", true); + Cache mockCache = mockCache("testDeleteAllWithKeysWhenTransactionPresent.MockCache", true); - Region mockRegion = mockRegion("testDeleteAllWithKeysWhenTransactionPresent", mockCache, - DataPolicy.REPLICATE); + Region mockRegion = mockRegion("testDeleteAllWithKeysWhenTransactionPresent.MockRegion", + mockCache, DataPolicy.REPLICATE); - when(mockRegion.keySet()).thenReturn(new HashSet(Arrays.asList(1l, 2l, 3l))); + Set keys = new HashSet(Arrays.asList(1l, 2l, 3l)); + + when(mockRegion.keySet()).thenReturn(keys); SimpleGemfireRepository gemfireRepository = new SimpleGemfireRepository( createGemfireTemplate(mockRegion), mockEntityInformation()); @@ -384,9 +388,7 @@ public class SimpleGemfireRepositoryUnitTest { verify(mockRegion, times(2)).getAttributes(); verify(mockRegion, times(2)).getRegionService(); verify(mockRegion, times(0)).clear(); - verify(mockRegion, times(1)).remove(eq(1l)); - verify(mockRegion, times(1)).remove(eq(2l)); - verify(mockRegion, times(1)).remove(eq(3l)); + verify(mockRegion, times(1)).removeAll(eq(keys)); } }