From 26b833310d5a3be4d67f83402d1a0bf2ab06b332 Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Thu, 11 Nov 2021 20:27:57 +0100 Subject: [PATCH] Optionally cache urls, stringify urls --- .../java/org/warp/filesponge/DiskCache.java | 19 ++++++++++++++++--- src/main/java/org/warp/filesponge/URL.java | 4 ++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/warp/filesponge/DiskCache.java b/src/main/java/org/warp/filesponge/DiskCache.java index 5706eb9..a2dcf35 100644 --- a/src/main/java/org/warp/filesponge/DiskCache.java +++ b/src/main/java/org/warp/filesponge/DiskCache.java @@ -35,6 +35,7 @@ import it.cavallium.dbengine.database.serialization.SerializationException; import it.unimi.dsi.fastutil.booleans.BooleanArrayList; import java.util.List; import java.util.Objects; +import java.util.function.Predicate; import org.jetbrains.annotations.Nullable; import org.warp.filesponge.DiskMetadata.DiskMetadataSerializer; import reactor.core.publisher.Flux; @@ -50,17 +51,23 @@ public class DiskCache implements URLsDiskHandler, URLsWriter { private final LLKeyValueDatabase db; private final LLDictionary fileContent; private final LLDictionary fileMetadata; + private final Predicate shouldCache; - public DiskCache(LLKeyValueDatabase db, LLDictionary fileContent, LLDictionary fileMetadata) { + public DiskCache(LLKeyValueDatabase db, + LLDictionary fileContent, + LLDictionary fileMetadata, + Predicate shouldCache) { this.db = db; this.fileContent = fileContent; this.fileMetadata = fileMetadata; this.diskMetadataSerializer = new DiskMetadataSerializer(); + this.shouldCache = shouldCache; } public static Mono open(LLDatabaseConnection databaseConnection, String dbName, - DatabaseOptions databaseOptions) { + DatabaseOptions databaseOptions, + Predicate shouldCache) { return databaseConnection .getDatabase(dbName, List.of(Column.dictionary("file-content"), Column.dictionary("file-metadata")), @@ -71,12 +78,15 @@ public class DiskCache implements URLsDiskHandler, URLsWriter { db.getDictionary("file-content", UpdateMode.ALLOW).single(), db.getDictionary("file-metadata", UpdateMode.ALLOW).single() )) - .map(tuple -> new DiskCache(tuple.getT1(), tuple.getT2(), tuple.getT3())) + .map(tuple -> new DiskCache(tuple.getT1(), tuple.getT2(), tuple.getT3(), shouldCache)) .single(); } @Override public Mono writeMetadata(URL url, Metadata metadata) { + // Check if this cache should cache the url, otherwise do nothing + if (!shouldCache.test(url)) return Mono.empty(); + Mono> keyMono = Mono.fromCallable(() -> serializeUrl(url)); return fileMetadata .update(keyMono, oldValue -> Objects.requireNonNullElseGet(oldValue, @@ -124,6 +134,9 @@ public class DiskCache implements URLsDiskHandler, URLsWriter { @Override public Mono writeContentBlock(URL url, DataBlock dataBlock) { + // Check if this cache should cache the url, otherwise do nothing + if (!shouldCache.test(url)) return Mono.empty(); + Mono> urlKeyMono = Mono.fromCallable(() -> serializeUrl(url)); Mono> blockKeyMono = Mono.fromCallable(() -> getBlockKey(url, dataBlock.getId())); return Mono diff --git a/src/main/java/org/warp/filesponge/URL.java b/src/main/java/org/warp/filesponge/URL.java index 7b18f54..b1feee1 100644 --- a/src/main/java/org/warp/filesponge/URL.java +++ b/src/main/java/org/warp/filesponge/URL.java @@ -25,4 +25,8 @@ public interface URL { Serializer getSerializer(); + /** + * @return String representation of this URL + */ + String toString(); }