commit
2ca31efc70
|
@ -55,6 +55,17 @@
|
||||||
<artifactId>spring-boot-starter</artifactId>
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
<version>${spring-boot-starter.version}</version>
|
<version>${spring-boot-starter.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.openhft</groupId>
|
||||||
|
<artifactId>chronicle-map</artifactId>
|
||||||
|
<version>${chronicle.map.version}</version>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>com.sun.java</groupId>
|
||||||
|
<artifactId>tools</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- Dependencies for response decoder with okhttp -->
|
<!-- Dependencies for response decoder with okhttp -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -95,6 +106,7 @@
|
||||||
<classgraph.version>4.8.28</classgraph.version>
|
<classgraph.version>4.8.28</classgraph.version>
|
||||||
<jbpm.version>6.0.0.Final</jbpm.version>
|
<jbpm.version>6.0.0.Final</jbpm.version>
|
||||||
<picocli.version>3.9.6</picocli.version>
|
<picocli.version>3.9.6</picocli.version>
|
||||||
|
<chronicle.map.version>3.17.2</chronicle.map.version>
|
||||||
<crawler4j.version>4.4.0</crawler4j.version>
|
<crawler4j.version>4.4.0</crawler4j.version>
|
||||||
<spring-boot-starter.version>2.1.4.RELEASE</spring-boot-starter.version>
|
<spring-boot-starter.version>2.1.4.RELEASE</spring-boot-starter.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
|
@ -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<LongValue, CharSequence> persistedCountryMap = null;
|
||||||
|
|
||||||
|
static ChronicleMap<LongValue, CharSequence> inMemoryCountryMap = null;
|
||||||
|
|
||||||
|
static ChronicleMap<Integer, Set<Integer>> 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("user.home") + "/country-details.dat"));
|
||||||
|
|
||||||
|
Set<Integer> averageValue = IntStream.of(1, 2)
|
||||||
|
.boxed()
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
multiMap = ChronicleMap.of(Integer.class, (Class<Set<Integer>>) (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<Integer> set1 = new HashSet<>();
|
||||||
|
set1.add(1);
|
||||||
|
set1.add(2);
|
||||||
|
multiMap.put(1, set1);
|
||||||
|
|
||||||
|
Set<Integer> set2 = new HashSet<>();
|
||||||
|
set2.add(3);
|
||||||
|
multiMap.put(2, set2);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenGetQuery_whenCalled_shouldReturnResult() {
|
||||||
|
LongValue key = Values.newHeapInstance(LongValue.class);
|
||||||
|
key.setValue(1);
|
||||||
|
CharSequence country = inMemoryCountryMap.get(key);
|
||||||
|
assertThat(country.toString(), is(equalTo("Qatar")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenGetUsingQuery_whenCalled_shouldReturnResult() {
|
||||||
|
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 givenMultipleKeyQuery_whenProcessed_shouldChangeTheValue() {
|
||||||
|
try (ExternalMapQueryContext<Integer, Set<Integer>, ?> fistContext = multiMap.queryContext(1)) {
|
||||||
|
try (ExternalMapQueryContext<Integer, Set<Integer>, ?> secondContext = multiMap.queryContext(2)) {
|
||||||
|
fistContext.updateLock()
|
||||||
|
.lock();
|
||||||
|
secondContext.updateLock()
|
||||||
|
.lock();
|
||||||
|
MapEntry<Integer, Set<Integer>> firstEntry = fistContext.entry();
|
||||||
|
Set<Integer> firstSet = firstEntry.value()
|
||||||
|
.get();
|
||||||
|
firstSet.remove(2);
|
||||||
|
MapEntry<Integer, Set<Integer>> secondEntry = secondContext.entry();
|
||||||
|
Set<Integer> 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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue