From 0933326ea9bf9e9b945dd7cfa9ca8b8802df9f6d Mon Sep 17 00:00:00 2001 From: TINO Date: Sun, 9 Jun 2019 23:10:28 +0300 Subject: [PATCH 1/3] BAEL-2956 Initial Commit --- libraries-2/pom.xml | 12 ++ .../chroniclemap/ChronicleMapUnitTest.java | 132 ++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 libraries-2/src/test/java/com/baeldung/chroniclemap/ChronicleMapUnitTest.java diff --git a/libraries-2/pom.xml b/libraries-2/pom.xml index 6303c0cab5..2e23e0433c 100644 --- a/libraries-2/pom.xml +++ b/libraries-2/pom.xml @@ -55,6 +55,17 @@ spring-boot-starter ${spring-boot-starter.version} + + net.openhft + chronicle-map + ${chronicle.map.version} + + + com.sun.java + tools + + + @@ -62,6 +73,7 @@ 4.8.28 6.0.0.Final 3.9.6 + 3.17.2 2.1.4.RELEASE diff --git a/libraries-2/src/test/java/com/baeldung/chroniclemap/ChronicleMapUnitTest.java b/libraries-2/src/test/java/com/baeldung/chroniclemap/ChronicleMapUnitTest.java new file mode 100644 index 0000000000..f396fbb443 --- /dev/null +++ b/libraries-2/src/test/java/com/baeldung/chroniclemap/ChronicleMapUnitTest.java @@ -0,0 +1,132 @@ +package com.baeldung.chroniclemap; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.io.File; +import java.util.HashSet; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import net.openhft.chronicle.core.values.LongValue; +import net.openhft.chronicle.map.ChronicleMap; +import net.openhft.chronicle.map.ExternalMapQueryContext; +import net.openhft.chronicle.map.MapEntry; +import net.openhft.chronicle.values.Values; + +public class ChronicleMapUnitTest { + + static ChronicleMap persistedCountryMap = null; + + static ChronicleMap inMemoryCountryMap = null; + + static ChronicleMap> multiMap = null; + + @SuppressWarnings({ "unchecked", "rawtypes" }) + @BeforeClass + public static void init() { + try { + inMemoryCountryMap = ChronicleMap.of(LongValue.class, CharSequence.class) + .name("country-map") + .entries(50) + .averageValue("America") + .create(); + + persistedCountryMap = ChronicleMap.of(LongValue.class, CharSequence.class) + .name("country-map") + .entries(50) + .averageValue("America") + .createPersistedTo(new File(System.getProperty("java.io.tmpdir") + "country-details.dat")); + + Set averageValue = IntStream.of(1, 2) + .boxed() + .collect(Collectors.toSet()); + multiMap = ChronicleMap.of(Integer.class, (Class>) (Class) Set.class) + .name("multi-map") + .entries(50) + .averageValue(averageValue) + .create(); + + LongValue qatarKey = Values.newHeapInstance(LongValue.class); + qatarKey.setValue(1); + inMemoryCountryMap.put(qatarKey, "Qatar"); + + LongValue key = Values.newHeapInstance(LongValue.class); + key.setValue(1); + persistedCountryMap.put(key, "Romania"); + key.setValue(2); + persistedCountryMap.put(key, "India"); + + Set set1 = new HashSet<>(); + set1.add(1); + set1.add(2); + multiMap.put(1, set1); + + Set set2 = new HashSet<>(); + set2.add(3); + multiMap.put(2, set2); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void shouldReturnResult_whenQuery_withGet() { + LongValue key = Values.newHeapInstance(LongValue.class); + key.setValue(1); + CharSequence country = inMemoryCountryMap.get(key); + assertThat(country.toString(), is(equalTo("Qatar"))); + } + + @Test + public void shouldReturnResult_whenQuery_withGetUsing() { + LongValue key = Values.newHeapInstance(LongValue.class); + StringBuilder country = new StringBuilder(); + key.setValue(1); + persistedCountryMap.getUsing(key, country); + assertThat(country.toString(), is(equalTo("Romania"))); + key.setValue(2); + persistedCountryMap.getUsing(key, country); + assertThat(country.toString(), is(equalTo("India"))); + } + + @Test + public void shouldChangeTheValue_whenManage_withMultipleKeys() { + try (ExternalMapQueryContext, ?> fistContext = multiMap.queryContext(1)) { + try (ExternalMapQueryContext, ?> secondContext = multiMap.queryContext(2)) { + fistContext.updateLock() + .lock(); + secondContext.updateLock() + .lock(); + MapEntry> firstEntry = fistContext.entry(); + Set firstSet = firstEntry.value() + .get(); + firstSet.remove(2); + MapEntry> secondEntry = secondContext.entry(); + Set secondSet = secondEntry.value() + .get(); + secondSet.add(4); + firstEntry.doReplaceValue(fistContext.wrapValueAsData(firstSet)); + secondEntry.doReplaceValue(secondContext.wrapValueAsData(secondSet)); + } + } finally { + assertThat(multiMap.get(1) + .size(), is(equalTo(1))); + assertThat(multiMap.get(2) + .size(), is(equalTo(2))); + } + } + + @AfterClass + public static void finish() { + persistedCountryMap.close(); + inMemoryCountryMap.close(); + multiMap.close(); + } +} From f64b7c027b79a5d5a36c30a1859f826125ea4068 Mon Sep 17 00:00:00 2001 From: TINO Date: Wed, 12 Jun 2019 13:30:58 +0300 Subject: [PATCH 2/3] BAEL - 2956 Changed the test methods name --- .../com/baeldung/chroniclemap/ChronicleMapUnitTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries-2/src/test/java/com/baeldung/chroniclemap/ChronicleMapUnitTest.java b/libraries-2/src/test/java/com/baeldung/chroniclemap/ChronicleMapUnitTest.java index f396fbb443..067e3c9ef5 100644 --- a/libraries-2/src/test/java/com/baeldung/chroniclemap/ChronicleMapUnitTest.java +++ b/libraries-2/src/test/java/com/baeldung/chroniclemap/ChronicleMapUnitTest.java @@ -77,7 +77,7 @@ public class ChronicleMapUnitTest { } @Test - public void shouldReturnResult_whenQuery_withGet() { + public void givenGetQuery_whenCalled_shouldReturnResult() { LongValue key = Values.newHeapInstance(LongValue.class); key.setValue(1); CharSequence country = inMemoryCountryMap.get(key); @@ -85,7 +85,7 @@ public class ChronicleMapUnitTest { } @Test - public void shouldReturnResult_whenQuery_withGetUsing() { + public void givenGetUsingQuery_whenCalled_shouldReturnResult() { LongValue key = Values.newHeapInstance(LongValue.class); StringBuilder country = new StringBuilder(); key.setValue(1); @@ -97,7 +97,7 @@ public class ChronicleMapUnitTest { } @Test - public void shouldChangeTheValue_whenManage_withMultipleKeys() { + public void givenMultipleKeyQuery_whenProcessed_shouldChangeTheValue() { try (ExternalMapQueryContext, ?> fistContext = multiMap.queryContext(1)) { try (ExternalMapQueryContext, ?> secondContext = multiMap.queryContext(2)) { fistContext.updateLock() From bd80953ac42e0035735a7376a9fb98ff049f7dc3 Mon Sep 17 00:00:00 2001 From: TINO Date: Sat, 15 Jun 2019 12:29:59 +0300 Subject: [PATCH 3/3] BAEL - 2956 Fixed permission denied exception --- .../java/com/baeldung/chroniclemap/ChronicleMapUnitTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries-2/src/test/java/com/baeldung/chroniclemap/ChronicleMapUnitTest.java b/libraries-2/src/test/java/com/baeldung/chroniclemap/ChronicleMapUnitTest.java index 067e3c9ef5..7f36a9abdb 100644 --- a/libraries-2/src/test/java/com/baeldung/chroniclemap/ChronicleMapUnitTest.java +++ b/libraries-2/src/test/java/com/baeldung/chroniclemap/ChronicleMapUnitTest.java @@ -42,7 +42,7 @@ public class ChronicleMapUnitTest { .name("country-map") .entries(50) .averageValue("America") - .createPersistedTo(new File(System.getProperty("java.io.tmpdir") + "country-details.dat")); + .createPersistedTo(new File(System.getProperty("user.home") + "/country-details.dat")); Set averageValue = IntStream.of(1, 2) .boxed()