diff --git a/apache-geode/pom.xml b/apache-geode/pom.xml new file mode 100644 index 0000000000..a3f6604ac4 --- /dev/null +++ b/apache-geode/pom.xml @@ -0,0 +1,46 @@ + + + 4.0.0 + + com.baeldung + apache-geode + 1.0-SNAPSHOT + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + 1.6.0 + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + org.apache.geode + geode-core + ${geode.core} + + + junit + junit + RELEASE + + + + \ No newline at end of file diff --git a/apache-geode/src/main/java/com/baeldung/geode/Customer.java b/apache-geode/src/main/java/com/baeldung/geode/Customer.java new file mode 100644 index 0000000000..82ee5ecaeb --- /dev/null +++ b/apache-geode/src/main/java/com/baeldung/geode/Customer.java @@ -0,0 +1,78 @@ +package com.baeldung.geode; + +import java.io.Serializable; +import java.util.Objects; + +public class Customer implements Serializable { + + private static final long serialVersionUID = -7482516011038799900L; + + private CustomerKey key; + private String firstName; + private String lastName; + private Integer age; + + public Customer() { + } + + public Customer(String firstName, String lastName, int age) { + this.firstName = firstName; + this.lastName = lastName; + this.age = age; + } + + public Customer(CustomerKey key, String firstName, String lastName, int age) { + this(firstName, lastName, age); + this.key = key; + } + + // setters and getters + + public static long getSerialVersionUID() { + return serialVersionUID; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + @Override + public String toString() { + return "Customer{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", age=" + age + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + Customer customer = (Customer) o; + return Objects.equals(firstName, customer.firstName) && Objects.equals(lastName, customer.lastName) && Objects.equals(age, customer.age); + } + + @Override + public int hashCode() { + return Objects.hash(firstName, lastName, age); + } +} diff --git a/apache-geode/src/main/java/com/baeldung/geode/CustomerKey.java b/apache-geode/src/main/java/com/baeldung/geode/CustomerKey.java new file mode 100644 index 0000000000..bfa64870c0 --- /dev/null +++ b/apache-geode/src/main/java/com/baeldung/geode/CustomerKey.java @@ -0,0 +1,57 @@ +package com.baeldung.geode; + +import java.io.Serializable; + +public class CustomerKey implements Serializable { + + private static final long serialVersionUID = -3529253035303792458L; + private long id; + private String country; + + public CustomerKey(long id) { + this.id = id; + this.country = "USA"; + } + + public CustomerKey(long id, String country) { + this.id = id; + this.country = country; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + + CustomerKey that = (CustomerKey) o; + + if (id != that.id) + return false; + return country != null ? country.equals(that.country) : that.country == null; + } + + @Override + public int hashCode() { + int result = (int) (id ^ (id >>> 32)); + result = 31 * result + (country != null ? country.hashCode() : 0); + return result; + } +} diff --git a/apache-geode/src/main/java/com/baeldung/geode/functions/PrimeNumber.java b/apache-geode/src/main/java/com/baeldung/geode/functions/PrimeNumber.java new file mode 100644 index 0000000000..411816348a --- /dev/null +++ b/apache-geode/src/main/java/com/baeldung/geode/functions/PrimeNumber.java @@ -0,0 +1,49 @@ +package com.baeldung.geode.functions; + +import org.apache.geode.cache.Region; +import org.apache.geode.cache.execute.Function; +import org.apache.geode.cache.execute.FunctionContext; +import org.apache.geode.cache.execute.RegionFunctionContext; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; + +public class PrimeNumber implements Function { + + public static final String ID = PrimeNumber.class.getSimpleName(); + + @Override + public void execute(FunctionContext context) { + RegionFunctionContext regionContext = (RegionFunctionContext) context; + Region region = regionContext.getDataSet(); + + List primes = new ArrayList<>(); + Set keys = region.keySet(); + for (Integer key : keys) { + if (isPrime(key)) { + primes.add(key); + } + } + Collections.sort(primes); + + context.getResultSender() + .lastResult(primes); + } + + @Override + public String getId() { + return ID; + } + + private boolean isPrime(int number) { + int limit = (int) Math.floor(Math.sqrt(number)); + for (int divisor = 2; divisor <= limit; ++divisor) { + if (number % divisor == 0) { + return false; + } + } + return true; + } +} diff --git a/apache-geode/src/test/java/com/baeldung/geode/GeodeSamplesIntegrationTest.java b/apache-geode/src/test/java/com/baeldung/geode/GeodeSamplesIntegrationTest.java new file mode 100644 index 0000000000..5445772259 --- /dev/null +++ b/apache-geode/src/test/java/com/baeldung/geode/GeodeSamplesIntegrationTest.java @@ -0,0 +1,144 @@ +package com.baeldung.geode; + +import com.baeldung.geode.functions.PrimeNumber; +import org.apache.geode.cache.Region; +import org.apache.geode.cache.client.ClientCache; +import org.apache.geode.cache.client.ClientCacheFactory; +import org.apache.geode.cache.client.ClientRegionShortcut; +import org.apache.geode.cache.execute.Execution; +import org.apache.geode.cache.execute.FunctionService; +import org.apache.geode.cache.execute.ResultCollector; +import org.apache.geode.cache.query.*; +import org.junit.Test; + +import java.util.*; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class GeodeSamplesIntegrationTest { + + @Test + public void whenSendMessageToRegion_thenMessageSavedSuccessfully() { + ClientCache cache = new ClientCacheFactory().addPoolLocator("localhost", 10334) + .create(); + Region region = cache. createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY) + .create("baeldung"); + + region.put("1", "Hello"); + region.put("2", "Baeldung"); + + assertEquals("Hello", region.get("1")); + assertEquals("Baeldung", region.get("2")); + + cache.close(); + } + + @Test + public void whenPutMultipleValuesAtOnce_thenValuesSavedSuccessfully() { + ClientCache cache = new ClientCacheFactory().addPoolLocator("localhost", 10334) + .create(); + Region region = cache. createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY) + .create("baeldung"); + + Map values = IntStream.rangeClosed(1, 5) + .mapToObj(String::valueOf) + .collect(Collectors.toMap(Function.identity(), i -> "value" + i)); + + region.putAll(values); + + IntStream.rangeClosed(1, 5) + .mapToObj(String::valueOf) + .forEach(e -> { + assertEquals("value".concat(e), region.get(e)); + }); + cache.close(); + } + + @Test + public void whenSaveCustomerDataOnPartitionedRegion_thenDataSavedCorrectly() { + ClientCache cache = new ClientCacheFactory().addPoolLocator("localhost", 10334) + .create(); + Region region = cache. createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY) + .create("baeldung-partition"); + + Customer customer1 = new Customer(new CustomerKey(1l), "Gheorge", "Manuc", 36); + Customer customer2 = new Customer(new CustomerKey(2l), "Allan", "McDowell", 43); + Customer customer3 = new Customer(new CustomerKey(3l), "Alan", "McClean", 23); + Customer customer4 = new Customer(new CustomerKey(4l), "Allan", "Donald", 46); + + Map customerData = new HashMap<>(); + customerData.put(1, customer1); + customerData.put(2, customer2); + customerData.put(3, customer3); + customerData.put(4, customer4); + + region.putAll(customerData); + // assert the size on the cache server. + assertEquals(4, region.sizeOnServer()); + cache.close(); + } + + @Test + public void whenFindACustomerUsingOQL_thenCorrectCustomerObject() throws NameResolutionException, TypeMismatchException, QueryInvocationTargetException, FunctionDomainException { + ClientCache cache = new ClientCacheFactory().addPoolLocator("localhost", 10334) + .create(); + Region region = cache. createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY) + .create("baeldung-oql"); + + Customer customer1 = new Customer("Gheorge", "Manuc", 36); + Customer customer2 = new Customer("Allan", "McDowell", 43); + Customer customer3 = new Customer("Alan", "McClean", 23); + Customer customer4 = new Customer("Allan", "Donald", 46); + + Map customerData = new HashMap<>(); + customerData.put(1, customer1); + customerData.put(2, customer2); + customerData.put(3, customer3); + customerData.put(4, customer4); + + region.putAll(customerData); + // assert the size on the cache server. + assertEquals(4, region.sizeOnServer()); + + QueryService queryService = cache.getQueryService(); + String query = "select * from /baeldung-oql c where c.firstName = 'Allan'"; + SelectResults queryResults = (SelectResults) queryService.newQuery(query) + .execute(); + assertEquals(2, queryResults.size()); + + cache.close(); + + } + + @Test + public void whenExecutePrimeNumberFunction_thenReturnOnlyPrimeNumbers() { + // connect to the locator using default port 10334 + ClientCache cache = new ClientCacheFactory().addPoolLocator("localhost", 10334) + .create(); + + // create a local region that matches the server region + Region region = cache. createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY) + .create("baeldung-function"); + + Execution execution = FunctionService.onRegion(region); + + IntStream.rangeClosed(1, 5) + .forEach(i -> region.put(i, String.valueOf(i))); + + ResultCollector results = execution.execute(PrimeNumber.ID); + Set primes = new HashSet<>(); + List resultList = results.getResult(); + assertNotNull(resultList); + assertEquals(1, resultList.size()); + + primes.addAll((List) resultList.iterator() + .next()); + assertEquals(4, primes.size()); + cache.close(); + } + +}