[BAEL-2959] A Guide to MapDB code (#7042)
* [BAEL-2959] A Guide to MapDB code * [BAEL-2959] fixes * [BAEL-2959] made VOLUME_LOCATION static in SortedTableMapUnitTest * moved mapdb files from libraries to libraries-2 * [BAEL-2959] formatting fixes * updated pom.xml
This commit is contained in:
parent
e82221416d
commit
7c4019e384
|
@ -1,70 +1,71 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>libraries2</artifactId>
|
||||
<name>libraries2</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>jboss-public-repository-group</id>
|
||||
<name>JBoss Public Repository Group</name>
|
||||
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
<updatePolicy>never</updatePolicy>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
<updatePolicy>daily</updatePolicy>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.pivovarit</groupId>
|
||||
<artifactId>parallel-collectors</artifactId>
|
||||
<version>1.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.classgraph</groupId>
|
||||
<artifactId>classgraph</artifactId>
|
||||
<version>${classgraph.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jbpm</groupId>
|
||||
<artifactId>jbpm-test</artifactId>
|
||||
<version>${jbpm.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>info.picocli</groupId>
|
||||
<artifactId>picocli</artifactId>
|
||||
<version>${picocli.version}</version>
|
||||
</dependency>
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>libraries2</artifactId>
|
||||
<name>libraries2</name>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>jboss-public-repository-group</id>
|
||||
<name>JBoss Public Repository Group</name>
|
||||
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
<updatePolicy>never</updatePolicy>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
<updatePolicy>daily</updatePolicy>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.mapdb</groupId>
|
||||
<artifactId>mapdb</artifactId>
|
||||
<version>${mapdb.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.pivovarit</groupId>
|
||||
<artifactId>parallel-collectors</artifactId>
|
||||
<version>1.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.classgraph</groupId>
|
||||
<artifactId>classgraph</artifactId>
|
||||
<version>${classgraph.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jbpm</groupId>
|
||||
<artifactId>jbpm-test</artifactId>
|
||||
<version>${jbpm.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>info.picocli</groupId>
|
||||
<artifactId>picocli</artifactId>
|
||||
<version>${picocli.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.ejml</groupId>
|
||||
<artifactId>ejml-all</artifactId>
|
||||
<version>${ejml.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.nd4j</groupId>
|
||||
<artifactId>nd4j-native</artifactId>
|
||||
<version>${nd4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.nd4j</groupId>
|
||||
<artifactId>nd4j-native</artifactId>
|
||||
<version>${nd4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.la4j</groupId>
|
||||
<artifactId>la4j</artifactId>
|
||||
|
@ -75,87 +76,79 @@
|
|||
<artifactId>colt</artifactId>
|
||||
<version>${colt.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<version>${spring-boot-starter.version}</version>
|
||||
</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 -->
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>3.14.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.9.9</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.8.5</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>mockwebserver</artifactId>
|
||||
<version>3.14.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<version>${spring-boot-starter.version}</version>
|
||||
</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 -->
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>3.14.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.9.9</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.8.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>mockwebserver</artifactId>
|
||||
<version>3.14.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>edu.uci.ics</groupId>
|
||||
<artifactId>crawler4j</artifactId>
|
||||
<version>${crawler4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.jknack</groupId>
|
||||
<artifactId>handlebars</artifactId>
|
||||
<version>4.1.2</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Benchmarking -->
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<assertj.version>3.6.2</assertj.version>
|
||||
<classgraph.version>4.8.28</classgraph.version>
|
||||
<jbpm.version>6.0.0.Final</jbpm.version>
|
||||
<picocli.version>3.9.6</picocli.version>
|
||||
<chronicle.map.version>3.17.2</chronicle.map.version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.jknack</groupId>
|
||||
<artifactId>handlebars</artifactId>
|
||||
<version>4.1.2</version>
|
||||
</dependency>
|
||||
<!-- Benchmarking -->
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<properties>
|
||||
<mapdb.version>3.0.7</mapdb.version>
|
||||
<assertj.version>3.6.2</assertj.version>
|
||||
<classgraph.version>4.8.28</classgraph.version>
|
||||
<jbpm.version>6.0.0.Final</jbpm.version>
|
||||
<picocli.version>3.9.6</picocli.version>
|
||||
<chronicle.map.version>3.17.2</chronicle.map.version>
|
||||
<crawler4j.version>4.4.0</crawler4j.version>
|
||||
<spring-boot-starter.version>2.1.4.RELEASE</spring-boot-starter.version>
|
||||
<ejml.version>0.38</ejml.version>
|
||||
<nd4j.version>1.0.0-beta4</nd4j.version>
|
||||
<colt.version>1.2.0</colt.version>
|
||||
<la4j.version>0.6.0</la4j.version>
|
||||
<jmh.version>1.19</jmh.version>
|
||||
</properties>
|
||||
<spring-boot-starter.version>2.1.4.RELEASE</spring-boot-starter.version>
|
||||
<ejml.version>0.38</ejml.version>
|
||||
<nd4j.version>1.0.0-beta4</nd4j.version>
|
||||
<colt.version>1.2.0</colt.version>
|
||||
<la4j.version>0.6.0</la4j.version>
|
||||
<jmh.version>1.19</jmh.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.mapdb;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mapdb.DB;
|
||||
import org.mapdb.DBMaker;
|
||||
import org.mapdb.Serializer;
|
||||
|
||||
import java.util.NavigableSet;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
public class CollectionsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSetCreatedInDB_whenMultipleElementsAdded_checkOnlyOneExists() {
|
||||
|
||||
DB db = DBMaker.memoryDB().make();
|
||||
|
||||
NavigableSet<String> set = db.
|
||||
treeSet("mySet")
|
||||
.serializer(Serializer.STRING)
|
||||
.createOrOpen();
|
||||
|
||||
String myString = "Baeldung!";
|
||||
|
||||
set.add(myString);
|
||||
set.add(myString);
|
||||
|
||||
assertEquals(1, set.size());
|
||||
|
||||
db.close();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.mapdb;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.Test;
|
||||
import org.mapdb.*;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
public class HTreeMapUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenValidDB_whenHTreeMapAddedToAndRetrieved_CheckedRetrievalCorrect() {
|
||||
|
||||
DB db = DBMaker.memoryDB().make();
|
||||
|
||||
HTreeMap<String, String> hTreeMap = db
|
||||
.hashMap("myTreMap")
|
||||
.keySerializer(Serializer.STRING)
|
||||
.valueSerializer(Serializer.STRING)
|
||||
.create();
|
||||
|
||||
hTreeMap.put("key1", "value1");
|
||||
hTreeMap.put("key2", "value2");
|
||||
|
||||
assertEquals(2, hTreeMap.size());
|
||||
|
||||
//add another value with the same key
|
||||
|
||||
hTreeMap.put("key1", "value3");
|
||||
|
||||
assertEquals(2, hTreeMap.size());
|
||||
assertEquals("value3", hTreeMap.get("key1"));
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package com.baeldung.mapdb;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mapdb.DB;
|
||||
import org.mapdb.DBMaker;
|
||||
import org.mapdb.HTreeMap;
|
||||
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
public class HelloBaeldungUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenInMemoryDBInstantiateCorrectly_whenDataSavedAndRetrieved_checkRetrievalCorrect() {
|
||||
|
||||
DB db = DBMaker.memoryDB().make();
|
||||
|
||||
String welcomeMessageKey = "Welcome Message";
|
||||
String welcomeMessageString = "Hello Baeldung!";
|
||||
|
||||
HTreeMap myMap = db.hashMap("myMap").createOrOpen();
|
||||
myMap.put(welcomeMessageKey, welcomeMessageString);
|
||||
|
||||
String welcomeMessageFromDB = (String) myMap.get(welcomeMessageKey);
|
||||
|
||||
db.close();
|
||||
|
||||
assertEquals(welcomeMessageString, welcomeMessageFromDB);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInFileDBInstantiateCorrectly_whenDataSavedAndRetrieved_checkRetrievalCorrect() {
|
||||
|
||||
DB db = DBMaker.fileDB("file.db").make();
|
||||
|
||||
String welcomeMessageKey = "Welcome Message";
|
||||
String welcomeMessageString = "Hello Baeldung!";
|
||||
|
||||
HTreeMap myMap = db.hashMap("myMap").createOrOpen();
|
||||
myMap.put(welcomeMessageKey, welcomeMessageString);
|
||||
|
||||
String welcomeMessageFromDB = (String) myMap.get(welcomeMessageKey);
|
||||
|
||||
db.close();
|
||||
|
||||
assertEquals(welcomeMessageString, welcomeMessageFromDB);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package com.baeldung.mapdb;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mapdb.DB;
|
||||
import org.mapdb.DBMaker;
|
||||
import org.mapdb.HTreeMap;
|
||||
import org.mapdb.Serializer;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
public class InMemoryModesUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDBCreatedOnHeap_whenUsed_checkUsageCorrect() {
|
||||
|
||||
DB heapDB = DBMaker.heapDB().make();
|
||||
|
||||
HTreeMap<Integer, String> map = heapDB
|
||||
.hashMap("myMap")
|
||||
.keySerializer(Serializer.INTEGER)
|
||||
.valueSerializer(Serializer.STRING)
|
||||
.createOrOpen();
|
||||
|
||||
map.put(1, "ONE");
|
||||
|
||||
assertEquals("ONE", map.get(1));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDBCreatedBaseOnByteArray_whenUsed_checkUsageCorrect() {
|
||||
|
||||
DB heapDB = DBMaker.memoryDB().make();
|
||||
|
||||
HTreeMap<Integer, String> map = heapDB
|
||||
.hashMap("myMap")
|
||||
.keySerializer(Serializer.INTEGER)
|
||||
.valueSerializer(Serializer.STRING)
|
||||
.createOrOpen();
|
||||
|
||||
map.put(1, "ONE");
|
||||
|
||||
assertEquals("ONE", map.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDBCreatedBaseOnDirectByteBuffer_whenUsed_checkUsageCorrect() {
|
||||
|
||||
DB heapDB = DBMaker.memoryDirectDB().make();
|
||||
|
||||
HTreeMap<Integer, String> map = heapDB
|
||||
.hashMap("myMap")
|
||||
.keySerializer(Serializer.INTEGER)
|
||||
.valueSerializer(Serializer.STRING)
|
||||
.createOrOpen();
|
||||
|
||||
map.put(1, "ONE");
|
||||
|
||||
assertEquals("ONE", map.get(1));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package com.baeldung.mapdb;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mapdb.Serializer;
|
||||
import org.mapdb.SortedTableMap;
|
||||
import org.mapdb.volume.MappedFileVol;
|
||||
import org.mapdb.volume.Volume;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
public class SortedTableMapUnitTest {
|
||||
|
||||
private static final String VOLUME_LOCATION = "sortedTableMapVol.db";
|
||||
|
||||
@Test
|
||||
public void givenValidSortedTableMapSetup_whenQueried_checkValuesCorrect() {
|
||||
|
||||
//create memory mapped volume, readonly false
|
||||
Volume vol = MappedFileVol.FACTORY.makeVolume(VOLUME_LOCATION, false);
|
||||
|
||||
//create sink to feed the map with data
|
||||
SortedTableMap.Sink<Integer, String> sink =
|
||||
SortedTableMap.create(
|
||||
vol,
|
||||
Serializer.INTEGER,
|
||||
Serializer.STRING
|
||||
).createFromSink();
|
||||
|
||||
//add content
|
||||
for(int i = 0; i < 100; i++){
|
||||
sink.put(i, "Value " + Integer.toString(i));
|
||||
}
|
||||
|
||||
sink.create();
|
||||
|
||||
//now open in read-only mode
|
||||
Volume openVol = MappedFileVol.FACTORY.makeVolume(VOLUME_LOCATION, true);
|
||||
|
||||
SortedTableMap<Integer, String> sortedTableMap = SortedTableMap.open(
|
||||
openVol,
|
||||
Serializer.INTEGER,
|
||||
Serializer.STRING
|
||||
);
|
||||
|
||||
assertEquals(100, sortedTableMap.size());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.mapdb;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mapdb.DB;
|
||||
import org.mapdb.DBMaker;
|
||||
import org.mapdb.Serializer;
|
||||
|
||||
import java.util.NavigableSet;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
public class TransactionsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenValidDBSetup_whenTransactionCommittedAndRolledBack_checkPreviousStateAchieved() {
|
||||
|
||||
DB db = DBMaker.memoryDB().transactionEnable().make();
|
||||
|
||||
NavigableSet<String> set = db
|
||||
.treeSet("mySet")
|
||||
.serializer(Serializer.STRING)
|
||||
.createOrOpen();
|
||||
|
||||
set.add("One");
|
||||
set.add("Two");
|
||||
|
||||
db.commit();
|
||||
|
||||
assertEquals(2, set.size());
|
||||
|
||||
set.add("Three");
|
||||
|
||||
assertEquals(3, set.size());
|
||||
|
||||
db.rollback();
|
||||
|
||||
assertEquals(2, set.size());
|
||||
|
||||
db.close();
|
||||
}
|
||||
}
|
|
@ -19,6 +19,9 @@
|
|||
<version>${typesafe-akka.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.typesafe.akka</groupId>
|
||||
<artifactId>akka-testkit_2.12</artifactId>
|
||||
|
@ -837,6 +840,7 @@
|
|||
<h2.version>1.4.196</h2.version>
|
||||
<jnats.version>1.0</jnats.version>
|
||||
|
||||
|
||||
<httpclient.version>4.5.3</httpclient.version>
|
||||
<jackson.version>2.9.7</jackson.version>
|
||||
<neuroph.version>2.92</neuroph.version>
|
||||
|
|
Loading…
Reference in New Issue