SGF-343 - Optimize the SDG implementation of CrudRepository.save(Iterable<S> enttiies) to use GemFire's Region.putAll(Map<K, V> values) operation.

This commit is contained in:
John Blum
2014-10-16 13:59:36 -07:00
parent 46f13be9c3
commit 3d08dc1eaa
6 changed files with 416 additions and 70 deletions

View File

@@ -58,10 +58,11 @@ import com.gemstone.gemfire.cache.query.SelectResults;
import com.gemstone.gemfire.cache.query.TypeMismatchException;
/**
* The GemfireTemplateTest class is a test suite of test case testing the contract and functionality of the
* GemfirTemplate class.
* The GemfireTemplateTest class is a test suite of test cases testing the contract and functionality of the SDG
* GemfireTemplate class.
*
* @author Costin Leau
* @author David Turanski
* @author John Blum
* @see org.junit.Test
* @see org.junit.runner.RunWith
@@ -69,8 +70,8 @@ import com.gemstone.gemfire.cache.query.TypeMismatchException;
* @see org.springframework.data.gemfire.test.GemfireTestApplicationContextInitializer
* @see org.springframework.test.context.ContextConfiguration
*/
@ContextConfiguration(locations="basic-template.xml", initializers=GemfireTestApplicationContextInitializer.class)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="basic-template.xml", initializers=GemfireTestApplicationContextInitializer.class)
@SuppressWarnings("unused")
public class GemfireTemplateTest {

View File

@@ -19,13 +19,13 @@ import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.Collection;
import javax.annotation.Resource;
import org.junit.Before;
@@ -48,6 +48,9 @@ import com.gemstone.gemfire.cache.util.CacheListenerAdapter;
* Integration tests for {@link SimpleGemfireRepository}.
*
* @author Oliver Gierke
* @author John Blum
* @see org.junit.Test
* @see org.springframework.data.gemfire.repository.support.SimpleGemfireRepository
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("../../basic-template.xml")
@@ -63,8 +66,8 @@ public class SimpleGemfireRepositoryIntegrationTest {
RegionClearListener regionClearListener;
@SuppressWarnings("unchecked")
@Before
@SuppressWarnings("unchecked")
public void setUp() {
simpleRegion.clear();
regionClearListener = new RegionClearListener();
@@ -128,6 +131,24 @@ public class SimpleGemfireRepositoryIntegrationTest {
assertThat(result, not(hasItems(dave)));
}
@Test
public void testSaveEntities() {
assertTrue(template.getRegion().isEmpty());
Person johnBlum = new Person(1l, "John", "Blum");
Person jonBloom = new Person(2l, "Jon", "Bloom");
Person juanBlume = new Person(3l, "Juan", "Blume");
repository.save(Arrays.asList(johnBlum, jonBloom, juanBlume));
assertFalse(template.getRegion().isEmpty());
assertEquals(3, template.getRegion().size());
assertEquals(johnBlum, template.get(johnBlum.id));
assertEquals(jonBloom, template.get(jonBloom.id));
assertEquals(juanBlume, template.get(juanBlume.id));
}
@SuppressWarnings("rawtypes")
public static class RegionClearListener extends CacheListenerAdapter {
public boolean eventFired;
@@ -137,4 +158,5 @@ public class SimpleGemfireRepositoryIntegrationTest {
eventFired = true;
}
}
}

View File

@@ -0,0 +1,303 @@
/*
* Copyright 2010-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.repository.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.data.gemfire.GemfireTemplate;
import org.springframework.data.gemfire.repository.Wrapper;
import org.springframework.data.gemfire.repository.sample.Animal;
import org.springframework.data.gemfire.test.support.CollectionUtils;
import org.springframework.data.repository.core.EntityInformation;
import com.gemstone.gemfire.cache.Region;
/**
* The SimpleGemfireRepositoryUnitTest class is a test suite of test cases testing the contract and functionality
* of the SimpleGemfireRepository class.
*
* @author John Blum
* @see org.junit.Test
* @see org.mockito.Mockito
* @see org.springframework.data.gemfire.repository.support.SimpleGemfireRepository
* @since 1.4.5
*/
@SuppressWarnings("unchecked")
public class SimpleGemfireRepositoryUnitTest {
protected Map<Long, Animal> asMap(Iterable<Animal> animals) {
Map<Long, Animal> animalMap = new HashMap<Long, Animal>();
for (Animal animal : animals) {
animalMap.put(animal.getId(), animal);
}
return animalMap;
}
protected Animal createAnimal(final String name) {
Animal animal = new Animal();
animal.setName(name);
return animal;
}
protected Animal createAnimal(final Long id, final String name) {
Animal animal = createAnimal(name);
animal.setId(id);
return animal;
}
protected GemfireTemplate createGemfireTemplate(final Region<?, ?> region) {
return new GemfireTemplate(region);
}
protected EntityInformation<Animal, Long> mockEntityInformation() {
EntityInformation<Animal, Long> mockEntityInformation = mock(EntityInformation.class);
doAnswer(new Answer<Long>() {
private final AtomicLong idSequence = new AtomicLong(0l);
@Override public Long answer(final InvocationOnMock invocation) throws Throwable {
Animal argument = Animal.class.cast(invocation.getArguments()[0]);
Long id = argument.getId();
id = (id != null ? id : idSequence.incrementAndGet());
argument.setId(id);
return id;
}
}).when(mockEntityInformation).getId(any(Animal.class));
return mockEntityInformation;
}
@Test
public void testSave() {
Region<Long, Animal> mockRegion = mock(Region.class, "testSave");
SimpleGemfireRepository<Animal, Long> repository = new SimpleGemfireRepository<Animal, Long>(
createGemfireTemplate(mockRegion), mockEntityInformation());
Animal dog = repository.save(createAnimal("dog"));
assertNotNull(dog);
assertEquals(1l, dog.getId().longValue());
assertEquals("dog", dog.getName());
verify(mockRegion, times(1)).put(eq(1l), eq(dog));
}
@Test
public void testSaveEntities() {
List<Animal> animals = new ArrayList<Animal>(3);
animals.add(createAnimal("bird"));
animals.add(createAnimal("cat"));
animals.add(createAnimal("dog"));
Region<Long, Animal> mockRegion = mock(Region.class, "testSaveAll");
SimpleGemfireRepository<Animal, Long> repository = new SimpleGemfireRepository<Animal, Long>(
createGemfireTemplate(mockRegion), mockEntityInformation());
Iterable<Animal> savedAnimals = repository.save(animals);
assertNotNull(savedAnimals);
verify(mockRegion, times(1)).putAll(eq(asMap(savedAnimals)));
}
@Test
public void testSaveWrapper() {
Animal dog = createAnimal(1l, "dog");
Wrapper dogWrapper = new Wrapper(dog, dog.getId());
Region<Long, Animal> mockRegion = mock(Region.class, "testSaveWrapper");
SimpleGemfireRepository<Animal, Long> repository = new SimpleGemfireRepository<Animal, Long>(
createGemfireTemplate(mockRegion), mockEntityInformation());
repository.save(dogWrapper);
verify(mockRegion, times(1)).put(eq(dog.getId()), eq(dog));
}
@Test
public void testExists() {
final Animal dog = createAnimal(1l, "dog");
Region<Long, Animal> mockRegion = mock(Region.class, "testFindOne");
when(mockRegion.get(any(Long.class))).then(new Answer<Animal>() {
@Override public Animal answer(final InvocationOnMock invocation) throws Throwable {
return (dog.getId().equals(invocation.getArguments()[0]) ? dog : null);
}
});
SimpleGemfireRepository<Animal, Long> repository = new SimpleGemfireRepository<Animal, Long>(
createGemfireTemplate(mockRegion), mockEntityInformation());
assertTrue(repository.exists(1l));
assertFalse(repository.exists(10l));
}
@Test
public void testFindOne() {
final Animal dog = createAnimal(1l, "dog");
Region<Long, Animal> mockRegion = mock(Region.class, "testFindOne");
when(mockRegion.get(any(Long.class))).then(new Answer<Animal>() {
@Override public Animal answer(final InvocationOnMock invocation) throws Throwable {
return (dog.getId().equals(invocation.getArguments()[0]) ? dog : null);
}
});
SimpleGemfireRepository<Animal, Long> repository = new SimpleGemfireRepository<Animal, Long>(
createGemfireTemplate(mockRegion), mockEntityInformation());
assertEquals(dog, repository.findOne(1l));
assertNull(repository.findOne(10l));
}
@Test
public void testFindAll() {
long id = 0;
final List<Animal> animals = new ArrayList<Animal>(3);
animals.add(createAnimal(++id, "bird"));
animals.add(createAnimal(++id, "cat"));
animals.add(createAnimal(++id, "dog"));
Region<Long, Animal> mockRegion = mock(Region.class, "testSaveAll");
when(mockRegion.getAll(any(Collection.class))).then(new Answer<Map<Long, Animal>>() {
@Override public Map<Long, Animal> answer(final InvocationOnMock invocation) throws Throwable {
Collection<Long> keys = (Collection<Long>) invocation.getArguments()[0];
Map<Long, Animal> result = new HashMap<Long, Animal>(keys.size());
for (Animal animal : animals) {
if (keys.contains(animal.getId())) {
result.put(animal.getId(), animal);
}
}
return result;
}
});
SimpleGemfireRepository<Animal, Long> repository = new SimpleGemfireRepository<Animal, Long>(
createGemfireTemplate(mockRegion), mockEntityInformation());
Collection<Animal> animalsFound = repository.findAll(Arrays.asList(1l, 3l));
assertNotNull(animalsFound);
assertEquals(2, animalsFound.size());
assertTrue(animalsFound.containsAll(CollectionUtils.subList(animals, 0, 2)));
verify(mockRegion, times(1)).getAll(eq(Arrays.asList(1l, 3l)));
}
@Test
public void testDeleteById() {
Region<Long, Animal> mockRegion = mock(Region.class, "testDeleteById");
SimpleGemfireRepository<Animal, Long> repository = new SimpleGemfireRepository<Animal, Long>(
createGemfireTemplate(mockRegion), mockEntityInformation());
repository.delete(1l);
verify(mockRegion, times(1)).remove(eq(1l));
}
@Test
public void testDeleteEntity() {
Region<Long, Animal> mockRegion = mock(Region.class, "testDeleteEntity");
SimpleGemfireRepository<Animal, Long> repository = new SimpleGemfireRepository<Animal, Long>(
createGemfireTemplate(mockRegion), mockEntityInformation());
repository.delete(createAnimal(1l, "dog"));
verify(mockRegion, times(1)).remove(eq(1l));
}
@Test
public void testDeleteEntities() {
Region<Long, Animal> mockRegion = mock(Region.class, "testDeleteEntities");
SimpleGemfireRepository<Animal, Long> repository = new SimpleGemfireRepository<Animal, Long>(
createGemfireTemplate(mockRegion), mockEntityInformation());
repository.delete(Arrays.asList(createAnimal(1l, "bird"), createAnimal(2l, "cat"), createAnimal(3l, "dog")));
verify(mockRegion, times(1)).remove(eq(1l));
verify(mockRegion, times(1)).remove(eq(2l));
verify(mockRegion, times(1)).remove(eq(3l));
}
@Test
public void testDeleteAllWithClear() {
Region<Long, Animal> mockRegion = mock(Region.class, "testDeleteEntities");
SimpleGemfireRepository<Animal, Long> repository = new SimpleGemfireRepository<Animal, Long>(
createGemfireTemplate(mockRegion), mockEntityInformation());
repository.deleteAll();
verify(mockRegion, times(1)).clear();
}
@Test
public void testDeleteAllWithKeys() {
Region<Long, Animal> mockRegion = mock(Region.class, "testDeleteEntities");
doThrow(new UnsupportedOperationException("Not Implemented!")).when(mockRegion).clear();
when(mockRegion.keySet()).thenReturn(new HashSet<Long>(Arrays.asList(1l, 2l, 3l)));
SimpleGemfireRepository<Animal, Long> repository = new SimpleGemfireRepository<Animal, Long>(
createGemfireTemplate(mockRegion), mockEntityInformation());
repository.deleteAll();
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));
}
}

View File

@@ -16,8 +16,10 @@
package org.springframework.data.gemfire.test.support;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
/**
* The CollectionUtils class is a utility class for working with the Java Collections Framework.
@@ -46,4 +48,12 @@ public abstract class CollectionUtils {
};
}
public static <T> List<T> subList(final List<T> source, final int... indices) {
List<T> result = new ArrayList<T>(indices.length);
for (int index : indices) {
result.add(source.get(index));
}
return result;
}
}