caio.co/de/cerberus

Drop ChronicleMap

o/
Id
6c0bf7025b4800e4c37513e08cb3a37679679eb2
Author
Caio
Commit time
2019-05-20T17:41:43+02:00

Modified pom.xml

@@ -22,7 +22,6
<flatbuffers.version>1.10.0.2</flatbuffers.version>
<flatbuffers.sources>${basedir}/src/main/flatbuffers</flatbuffers.sources>
<flatbuffers.generated.sources>${project.build.directory}/generated-sources/java</flatbuffers.generated.sources>
- <chronicle.version>3.17.2</chronicle.version>
</properties>

<dependencies>
@@ -81,18 +80,6
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
- </dependency>
-
- <dependency>
- <groupId>net.openhft</groupId>
- <artifactId>chronicle-map</artifactId>
- <version>${chronicle.version}</version>
- <exclusions>
- <exclusion>
- <groupId>com.sun.java</groupId>
- <artifactId>tools</artifactId>
- </exclusion>
- </exclusions>
</dependency>

<dependency>

Deleted src/main/java/co/caio/cerberus/db/ChronicleRecipeMetadataDatabase.java

@@ -1,99 +1,0
-package co.caio.cerberus.db;
-
-import java.io.IOException;
-import java.nio.ByteBuffer;
-import java.nio.file.Path;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Optional;
-import net.openhft.chronicle.map.ChronicleMap;
-import net.openhft.chronicle.map.ChronicleMapBuilder;
-
-public class ChronicleRecipeMetadataDatabase implements RecipeMetadataDatabase {
-
- private static final String DATABASE_NAME = "recipe-metadata";
-
- final ChronicleMap<Long, ByteBuffer> backingMap;
-
- private ChronicleRecipeMetadataDatabase(ChronicleMap<Long, ByteBuffer> backingMap) {
- this.backingMap = backingMap;
- }
-
- public static RecipeMetadataDatabase open(Path databasePath) {
- try {
- var map =
- ChronicleMapBuilder.of(Long.class, ByteBuffer.class)
- .name(DATABASE_NAME)
- .createPersistedTo(databasePath.toFile());
- return new ChronicleRecipeMetadataDatabase(map);
- } catch (IOException rethrown) {
- throw new RecipeMetadataDbException(rethrown);
- }
- }
-
- public static RecipeMetadataDatabase create(
- Path databasePath, double averageValueBytesSize, long numberOfEntries) {
- try {
- var map =
- ChronicleMapBuilder.of(Long.class, ByteBuffer.class)
- .name(DATABASE_NAME)
- .constantKeySizeBySample(1L)
- .averageValueSize(averageValueBytesSize)
- .entries(numberOfEntries)
- .createPersistedTo(databasePath.toFile());
- return new WriteableChronicleRecipeMetadataDatabase(map);
- } catch (IOException rethrown) {
- throw new RecipeMetadataDbException(rethrown);
- }
- }
-
- private RecipeMetadata get(long recipeId) {
- var buffer = backingMap.get(recipeId);
- if (buffer != null) {
- return RecipeMetadata.fromFlatRecipe(FlatBufferSerializer.INSTANCE.readRecipe(buffer));
- } else {
- return null;
- }
- }
-
- @Override
- public void close() {
- backingMap.close();
- }
-
- @Override
- public Optional<RecipeMetadata> findById(long recipeId) {
- return Optional.ofNullable(get(recipeId));
- }
-
- @Override
- public List<RecipeMetadata> findAllById(List<Long> recipeIds) {
- var result = new ArrayList<RecipeMetadata>(recipeIds.size());
- recipeIds.forEach(
- id -> {
- var recipe = get(id);
- if (recipe != null) {
- result.add(recipe);
- }
- });
- return result;
- }
-
- @Override
- public void saveAll(List<RecipeMetadata> recipes) {
- throw new RecipeMetadataDbException("Database is open as read-only");
- }
-
- static class WriteableChronicleRecipeMetadataDatabase extends ChronicleRecipeMetadataDatabase {
-
- WriteableChronicleRecipeMetadataDatabase(ChronicleMap<Long, ByteBuffer> backingMap) {
- super(backingMap);
- }
-
- @Override
- public void saveAll(List<RecipeMetadata> recipes) {
- recipes.forEach(
- rm -> backingMap.put(rm.getRecipeId(), FlatBufferSerializer.INSTANCE.flattenRecipe(rm)));
- }
- }
-}

Deleted src/test/java/co/caio/cerberus/db/ChronicleRecipeMetadataDatabaseTest.java

@@ -1,84 +1,0
-package co.caio.cerberus.db;
-
-import static org.junit.jupiter.api.Assertions.*;
-
-import co.caio.cerberus.Util;
-import co.caio.cerberus.db.RecipeMetadataDatabase.RecipeMetadataDbException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.util.List;
-import java.util.stream.Collectors;
-import org.junit.jupiter.api.AfterAll;
-import org.junit.jupiter.api.BeforeAll;
-import org.junit.jupiter.api.Test;
-
-class ChronicleRecipeMetadataDatabaseTest {
-
- private static Path rwdbPath;
- private static RecipeMetadataDatabase testRWDb;
-
- @BeforeAll
- static void createTmpDir() throws Exception {
- rwdbPath = Files.createTempDirectory("chronicle-test").resolve("test.db");
- testRWDb = ChronicleRecipeMetadataDatabase.create(rwdbPath, 2000, Util.expectedIndexSize());
- }
-
- @AfterAll
- static void closeDb() {
- testRWDb.close();
- }
-
- @Test
- void cantOpenADbThatDoesntExist() {
- assertThrows(
- RecipeMetadataDbException.class,
- () -> ChronicleRecipeMetadataDatabase.open(Path.of("/this/doesnt/exist.db")));
- }
-
- @Test
- void canOpenAfterCreation() {
- assertDoesNotThrow(
- () -> {
- var ro = ChronicleRecipeMetadataDatabase.open(rwdbPath);
- ro.close();
- });
- }
-
- @Test
- void cannotWriteToReadOnlyDb() {
- var roDb = ChronicleRecipeMetadataDatabase.open(rwdbPath);
- assertThrows(
- RecipeMetadataDbException.class,
- () -> roDb.saveAll(List.of(RecipeMetadata.fromRecipe(Util.getBasicRecipe()))));
- roDb.close();
- }
-
- @Test
- void canWriteToRwDb() {
- var recipes =
- Util.getSampleRecipes().map(RecipeMetadata::fromRecipe).collect(Collectors.toList());
-
- assertDoesNotThrow(() -> testRWDb.saveAll(recipes));
-
- for (RecipeMetadata rm : recipes) {
- var maybeRm = testRWDb.findById(rm.getRecipeId());
- assertTrue(maybeRm.isPresent());
- assertEquals(rm.getRecipeId(), maybeRm.get().getRecipeId());
- }
-
- var recipeIds =
- recipes.stream().limit(10).map(RecipeMetadata::getRecipeId).collect(Collectors.toList());
- var fetched = testRWDb.findAllById(recipeIds);
-
- assertEquals(recipeIds.size(), fetched.size());
- for (int i = 0; i < recipeIds.size(); i++) {
- var rm = fetched.get(i);
- assertEquals(recipeIds.get(i).longValue(), rm.getRecipeId());
- }
- }
-
- @Test
- void unexistingIdResultIsEmpty() {
- assertTrue(testRWDb.findById(-42).isEmpty());
- }
-}