Feature/bael 5653 microstream (#12660)
* BAEL-5653: Microstream storage * BAEL-5653: Eager loading * BAEL-5653: Lazy loading * BAEL-5653: Temp dir * BAEL-5653: Refactoring * BAEL-5653: Refactoring * BAEL-5653: Refactoring * BAEL-5653: PR comments
This commit is contained in:
parent
be5b79756b
commit
8993a3064b
|
@ -0,0 +1,3 @@
|
|||
.idea
|
||||
storage
|
||||
target
|
|
@ -51,6 +51,22 @@
|
|||
<artifactId>json</artifactId>
|
||||
<version>${json.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>one.microstream</groupId>
|
||||
<artifactId>microstream-storage-embedded</artifactId>
|
||||
<version>${microstream.storage.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>one.microstream</groupId>
|
||||
<artifactId>microstream-storage-embedded-configuration</artifactId>
|
||||
<version>${microstream.storage.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
@ -59,6 +75,7 @@
|
|||
<mysql.driver.version>8.0.22</mysql.driver.version>
|
||||
<jooq.version>3.11.11</jooq.version>
|
||||
<json.version>20220320</json.version>
|
||||
<microstream.storage.version>07.00.00-MS-GA</microstream.storage.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.microstream;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Author {
|
||||
|
||||
private final String name;
|
||||
private final String surname;
|
||||
|
||||
public Author(String name, String surname) {
|
||||
this.name = name;
|
||||
this.surname = surname;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getSurname() {
|
||||
return surname;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Author author = (Author) o;
|
||||
return Objects.equals(name, author.name) && Objects.equals(surname, author.surname);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(name, surname);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.microstream;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Book {
|
||||
|
||||
private final String title;
|
||||
private final Author author;
|
||||
private final int year;
|
||||
|
||||
public Book(String title, Author author, int year) {
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
this.year = year;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public Author getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public int getYear() {
|
||||
return year;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Book book = (Book) o;
|
||||
return year == book.year && Objects.equals(title, book.title) && Objects.equals(author, book.author);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(title, author, year);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.microstream;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class RootInstance {
|
||||
|
||||
private final String name;
|
||||
private final List<Book> books;
|
||||
|
||||
public RootInstance(String name) {
|
||||
this.name = name;
|
||||
books = new ArrayList<>();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public List<Book> getBooks() {
|
||||
return books;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.microstream;
|
||||
|
||||
import one.microstream.reference.Lazy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class RootInstanceLazy {
|
||||
|
||||
private final String name;
|
||||
private final Lazy<List<Book>> books;
|
||||
|
||||
public RootInstanceLazy(String name) {
|
||||
this.name = name;
|
||||
books = Lazy.Reference(new ArrayList<>());
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public List<Book> getBooks() {
|
||||
return Lazy.get(books);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package com.baeldung.microstream;
|
||||
|
||||
import one.microstream.storage.embedded.types.EmbeddedStorage;
|
||||
import one.microstream.storage.embedded.types.EmbeddedStorageManager;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
public class StorageManager {
|
||||
|
||||
public static final String STORAGE_ROOT_FOLDER = "storage";
|
||||
|
||||
public static EmbeddedStorageManager initializeStorageWithStringAsRoot(Path directory, String root) {
|
||||
EmbeddedStorageManager storageManager = EmbeddedStorage.start(directory);
|
||||
storageManager.setRoot(root);
|
||||
storageManager.storeRoot();
|
||||
return storageManager;
|
||||
}
|
||||
|
||||
public static EmbeddedStorageManager initializeStorageWithCustomTypeAsRoot(Path directory, String root) {
|
||||
EmbeddedStorageManager storageManager = EmbeddedStorage.start(directory);
|
||||
storageManager.setRoot(new RootInstance(root));
|
||||
storageManager.storeRoot();
|
||||
return storageManager;
|
||||
}
|
||||
|
||||
public static EmbeddedStorageManager initializeStorageWithCustomTypeAsRoot(Path directory, String root, List<Book> booksToStore) {
|
||||
EmbeddedStorageManager storageManager = EmbeddedStorage.start(directory);
|
||||
RootInstance rootInstance = new RootInstance(root);
|
||||
storageManager.setRoot(rootInstance);
|
||||
storageManager.storeRoot();
|
||||
|
||||
List<Book> books = rootInstance.getBooks();
|
||||
books.addAll(booksToStore);
|
||||
storageManager.store(books);
|
||||
return storageManager;
|
||||
}
|
||||
|
||||
public static EmbeddedStorageManager loadOrCreateStorageWithCustomTypeAsRoot(Path directory, String root) {
|
||||
EmbeddedStorageManager storageManager = EmbeddedStorage.start(directory);
|
||||
if (storageManager.root() == null) {
|
||||
RootInstance rootInstance = new RootInstance(root);
|
||||
storageManager.setRoot(rootInstance);
|
||||
storageManager.storeRoot();
|
||||
}
|
||||
return storageManager;
|
||||
}
|
||||
|
||||
public static EmbeddedStorageManager lazyLoadOrCreateStorageWithCustomTypeAsRoot(Path directory, String root, List<Book> booksToStore) {
|
||||
EmbeddedStorageManager storageManager = EmbeddedStorage.start(directory);
|
||||
if (storageManager.root() == null) {
|
||||
RootInstanceLazy rootInstance = new RootInstanceLazy(root);
|
||||
rootInstance.getBooks().addAll(booksToStore);
|
||||
storageManager.setRoot(rootInstance);
|
||||
storageManager.storeRoot();
|
||||
}
|
||||
return storageManager;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
package com.baeldung.microstream;
|
||||
|
||||
import one.microstream.storage.embedded.types.EmbeddedStorageManager;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
class StorageManagerUnitTest {
|
||||
|
||||
private static final Author AUTHOR = new Author("Joanne", "Rowling");
|
||||
private static final Book BOOK_ONE = new Book("Harry Potter and the Philosopher's Stone", AUTHOR, 1997);
|
||||
private static final Book BOOK_TWO = new Book("Harry Potter and the Chamber of Secrets", AUTHOR, 1998);
|
||||
|
||||
@Test
|
||||
void givenStorageWithStringAsRoot_whenFetchingRoot_thenExpectedStringIsReturned(@TempDir Path tempDir) {
|
||||
EmbeddedStorageManager storageManager = StorageManager.initializeStorageWithStringAsRoot(tempDir, "baeldung-demo-1");
|
||||
assertThat(storageManager.root()).isEqualTo("baeldung-demo-1");
|
||||
storageManager.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStorageWithCustomTypeAsRoot_whenFetchingRoot_thenCustomTypeWithExpectedValuesIsReturned(@TempDir Path tempDir) {
|
||||
EmbeddedStorageManager storageManager = StorageManager.initializeStorageWithCustomTypeAsRoot(tempDir, "baeldung-demo-2");
|
||||
RootInstance rootInstance = (RootInstance) storageManager.root();
|
||||
assertThat(rootInstance.getName()).isEqualTo("baeldung-demo-2");
|
||||
assertThat(rootInstance.getBooks()).isEmpty();
|
||||
storageManager.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStorageWithAdditionalObjects_whenLoadingRoot_thenAdditionalObjectsAreSuccessfullyStored(@TempDir Path tempDir) {
|
||||
EmbeddedStorageManager storageManager = StorageManager.initializeStorageWithCustomTypeAsRoot(tempDir, "baeldung-demo-3", Arrays.asList(BOOK_ONE, BOOK_TWO));
|
||||
RootInstance rootInstance = (RootInstance) storageManager.root();
|
||||
assertThat(rootInstance.getName()).isEqualTo("baeldung-demo-3");
|
||||
assertThat(rootInstance.getBooks()).hasSize(2);
|
||||
assertThat(rootInstance.getBooks().get(0)).isEqualTo(BOOK_ONE);
|
||||
assertThat(rootInstance.getBooks().get(1)).isEqualTo(BOOK_TWO);
|
||||
storageManager.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStorageWithAdditionalObjects_whenLazyLoadingRoot_thenAdditionalObjectsAreSuccessfullyStored(@TempDir Path tempDir) {
|
||||
EmbeddedStorageManager storageManager = StorageManager.lazyLoadOrCreateStorageWithCustomTypeAsRoot(tempDir, "baeldung-demo-4", Arrays.asList(BOOK_ONE, BOOK_TWO));
|
||||
RootInstanceLazy rootInstance = (RootInstanceLazy) storageManager.root();
|
||||
assertThat(rootInstance.getName()).isEqualTo("baeldung-demo-4");
|
||||
assertThat(rootInstance.getBooks()).hasSize(2);
|
||||
assertThat(rootInstance.getBooks().get(0)).isEqualTo(BOOK_ONE);
|
||||
assertThat(rootInstance.getBooks().get(1)).isEqualTo(BOOK_TWO);
|
||||
storageManager.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStorageWithAdditionalObjects_whenRemovingObjectsFromGraph_thenObjectsAreSuccessfullyRemoved(@TempDir Path tempDir) {
|
||||
EmbeddedStorageManager storageManager = StorageManager.lazyLoadOrCreateStorageWithCustomTypeAsRoot(tempDir, "baeldung-demo-5", Arrays.asList(BOOK_ONE, BOOK_TWO));
|
||||
RootInstanceLazy rootInstance = (RootInstanceLazy) storageManager.root();
|
||||
List<Book> books = rootInstance.getBooks();
|
||||
books.remove(1);
|
||||
storageManager.store(books);
|
||||
assertThat(rootInstance.getName()).isEqualTo("baeldung-demo-5");
|
||||
assertThat(books).hasSize(1);
|
||||
storageManager.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStorageWithAdditionalObjects_whenFilteringCollectionFromGraph_thenStreamsCanBeUsed(@TempDir Path tempDir) {
|
||||
EmbeddedStorageManager storageManager = StorageManager.lazyLoadOrCreateStorageWithCustomTypeAsRoot(tempDir, "baeldung-demo-6", Arrays.asList(BOOK_ONE, BOOK_TWO));
|
||||
RootInstanceLazy rootInstance = (RootInstanceLazy) storageManager.root();
|
||||
List<Book> booksFrom1998 = rootInstance.getBooks().stream()
|
||||
.filter(book -> book.getYear() == 1998)
|
||||
.collect(Collectors.toList());
|
||||
assertThat(rootInstance.getName()).isEqualTo("baeldung-demo-6");
|
||||
assertThat(booksFrom1998).hasSize(1);
|
||||
assertThat(booksFrom1998.get(0).getYear()).isEqualTo(1998);
|
||||
storageManager.shutdown();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue