From 91694960e4477db912aab396146f6fc0968654af Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Sun, 7 Mar 2021 15:50:11 +0100 Subject: [PATCH] Rewrite --- pom.xml | 18 ++ .../warp/filesponge/BaseMirrorFileSource.java | 35 ---- .../lombok/org/warp/filesponge/DataBlock.java | 35 ++++ .../lombok/org/warp/filesponge/DiskCache.java | 182 ++++++++++++++++++ .../org/warp/filesponge/DiskMetadata.java | 103 ++++++++++ .../warp/filesponge/FileMirrorsManager.java | 77 -------- .../FileAvailability.java => Metadata.java} | 20 +- .../filesponge/MirrorAvailabilityManager.java | 48 ----- .../warp/filesponge/SecureFileAccessor.java | 58 ------ .../FileDataAvailability.java => URL.java} | 14 +- ...eAvailability.java => URLDiskHandler.java} | 15 +- ...AssignedException.java => URLHandler.java} | 28 ++- .../{value/FileType.java => URLWriter.java} | 14 +- ...iAssociation.java => URLsDiskHandler.java} | 44 ++--- .../org/warp/filesponge/URLsHandler.java | 50 +++++ .../FileSpongeClient.java => URLsWriter.java} | 35 ++-- src/main/lombok/org/warp/filesponge/Web.java | 87 +++++++++ .../org/warp/filesponge/api/FileAccessor.java | 57 ------ .../org/warp/filesponge/api/FileActor.java | 72 ------- .../org/warp/filesponge/api/FileSource.java | 62 ------ .../org/warp/filesponge/api/FileStorage.java | 76 -------- .../extra/api/FileURITranslator.java | 44 ----- .../filesponge/extra/api/URIObfuscator.java | 39 ---- .../warp/filesponge/extra/package-info.java | 22 --- .../org/warp/filesponge/reactor/AsyncSet.java | 80 -------- .../reactor/ConcurrentAsyncSet.java | 81 -------- .../warp/filesponge/reactor/HashAsyncSet.java | 88 --------- .../reactor/SynchronizedHashAsyncSet.java | 83 -------- .../org/warp/filesponge/value/FileStatus.java | 40 ---- .../org/warp/filesponge/value/FileURI.java | 21 -- .../org/warp/filesponge/value/MirrorURI.java | 21 -- 31 files changed, 564 insertions(+), 1085 deletions(-) delete mode 100644 src/main/lombok/org/warp/filesponge/BaseMirrorFileSource.java create mode 100644 src/main/lombok/org/warp/filesponge/DataBlock.java create mode 100644 src/main/lombok/org/warp/filesponge/DiskCache.java create mode 100644 src/main/lombok/org/warp/filesponge/DiskMetadata.java delete mode 100644 src/main/lombok/org/warp/filesponge/FileMirrorsManager.java rename src/main/lombok/org/warp/filesponge/{value/FileAvailability.java => Metadata.java} (81%) delete mode 100644 src/main/lombok/org/warp/filesponge/MirrorAvailabilityManager.java delete mode 100644 src/main/lombok/org/warp/filesponge/SecureFileAccessor.java rename src/main/lombok/org/warp/filesponge/{value/FileDataAvailability.java => URL.java} (78%) rename src/main/lombok/org/warp/filesponge/{value/FileSourceAvailability.java => URLDiskHandler.java} (77%) rename src/main/lombok/org/warp/filesponge/{value/AlreadyAssignedException.java => URLHandler.java} (64%) rename src/main/lombok/org/warp/filesponge/{value/FileType.java => URLWriter.java} (75%) rename src/main/lombok/org/warp/filesponge/{reactor/AsyncMultiAssociation.java => URLsDiskHandler.java} (57%) create mode 100644 src/main/lombok/org/warp/filesponge/URLsHandler.java rename src/main/lombok/org/warp/filesponge/{api/FileSpongeClient.java => URLsWriter.java} (57%) create mode 100644 src/main/lombok/org/warp/filesponge/Web.java delete mode 100644 src/main/lombok/org/warp/filesponge/api/FileAccessor.java delete mode 100644 src/main/lombok/org/warp/filesponge/api/FileActor.java delete mode 100644 src/main/lombok/org/warp/filesponge/api/FileSource.java delete mode 100644 src/main/lombok/org/warp/filesponge/api/FileStorage.java delete mode 100644 src/main/lombok/org/warp/filesponge/extra/api/FileURITranslator.java delete mode 100644 src/main/lombok/org/warp/filesponge/extra/api/URIObfuscator.java delete mode 100644 src/main/lombok/org/warp/filesponge/extra/package-info.java delete mode 100644 src/main/lombok/org/warp/filesponge/reactor/AsyncSet.java delete mode 100644 src/main/lombok/org/warp/filesponge/reactor/ConcurrentAsyncSet.java delete mode 100644 src/main/lombok/org/warp/filesponge/reactor/HashAsyncSet.java delete mode 100644 src/main/lombok/org/warp/filesponge/reactor/SynchronizedHashAsyncSet.java delete mode 100644 src/main/lombok/org/warp/filesponge/value/FileStatus.java delete mode 100644 src/main/lombok/org/warp/filesponge/value/FileURI.java delete mode 100644 src/main/lombok/org/warp/filesponge/value/MirrorURI.java diff --git a/pom.xml b/pom.xml index dbd6bbc..89da0a6 100644 --- a/pom.xml +++ b/pom.xml @@ -34,6 +34,19 @@ 11 + + + mchv-release + MCHV Release Apache Maven Packages + https://mvn.mchv.eu/repository/mchv + + + mchv-snapshot + MCHV Snapshot Apache Maven Packages + https://mvn.mchv.eu/repository/mchv-snapshot + + + org.junit.jupiter @@ -67,6 +80,11 @@ 3.4.2 test + + it.cavallium + dbengine + 3.0.0-SNAPSHOT + diff --git a/src/main/lombok/org/warp/filesponge/BaseMirrorFileSource.java b/src/main/lombok/org/warp/filesponge/BaseMirrorFileSource.java deleted file mode 100644 index 3ed08c4..0000000 --- a/src/main/lombok/org/warp/filesponge/BaseMirrorFileSource.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * FileSponge - * Copyright (C) 2020 Andrea Cavalli - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.warp.filesponge; - -import lombok.AccessLevel; -import lombok.AllArgsConstructor; -import org.warp.filesponge.api.FileSource; -import org.warp.filesponge.value.FileType; -import org.warp.filesponge.value.FileURI; -import org.warp.filesponge.value.MirrorURI; - -@AllArgsConstructor(access = AccessLevel.PROTECTED) -public abstract class BaseMirrorFileSource implements - FileSource { - - protected final MirrorAvailabilityManager receiverAvailabilityManager; - protected final MirrorURI mirrorURI; - -} diff --git a/src/main/lombok/org/warp/filesponge/DataBlock.java b/src/main/lombok/org/warp/filesponge/DataBlock.java new file mode 100644 index 0000000..d69d0b8 --- /dev/null +++ b/src/main/lombok/org/warp/filesponge/DataBlock.java @@ -0,0 +1,35 @@ +/* + * FileSponge + * Copyright (C) 2021 Andrea Cavalli + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.warp.filesponge; + +import java.nio.ByteBuffer; +import lombok.Value; + +@Value +public class DataBlock { + + int offset; + int length; + ByteBuffer data; + + + public int getId() { + return offset / Web.BLOCK_SIZE; + } +} diff --git a/src/main/lombok/org/warp/filesponge/DiskCache.java b/src/main/lombok/org/warp/filesponge/DiskCache.java new file mode 100644 index 0000000..65c1934 --- /dev/null +++ b/src/main/lombok/org/warp/filesponge/DiskCache.java @@ -0,0 +1,182 @@ +/* + * FileSponge + * Copyright (C) 2021 Andrea Cavalli + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.warp.filesponge; + +import static org.warp.filesponge.Web.BLOCK_SIZE; + +import com.google.common.primitives.Ints; +import it.cavallium.dbengine.database.Column; +import it.cavallium.dbengine.database.LLDatabaseConnection; +import it.cavallium.dbengine.database.LLDictionary; +import it.cavallium.dbengine.database.LLDictionaryResultType; +import it.cavallium.dbengine.database.LLKeyValueDatabase; +import it.cavallium.dbengine.database.UpdateMode; +import it.unimi.dsi.fastutil.booleans.BooleanArrayList; +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import org.warp.filesponge.DiskMetadata.DiskMetadataSerializer; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; +import reactor.core.scheduler.Schedulers; +import reactor.util.function.Tuple2; +import reactor.util.function.Tuples; + +@AllArgsConstructor(access = AccessLevel.PRIVATE) +public class DiskCache implements URLsDiskHandler, URLsWriter { + + private static final DiskMetadataSerializer diskMetadataSerializer = new DiskMetadataSerializer(); + + private final LLKeyValueDatabase db; + private final LLDictionary fileContent; + private final LLDictionary fileMetadata; + + public static Mono open(LLDatabaseConnection databaseConnection, String dbName, boolean lowMemory) { + return databaseConnection + .getDatabase(dbName, List.of(Column.dictionary("file-content"), Column.dictionary("file-metadata")), lowMemory) + .flatMap(db -> Mono.zip( + Mono.just(db).single(), + db.getDictionary("file-content", UpdateMode.ALLOW).single(), + db.getDictionary("file-metadata", UpdateMode.ALLOW).single() + )) + .map(tuple -> new DiskCache(tuple.getT1(), tuple.getT2(), tuple.getT3())) + .single(); + } + + @Override + public Mono writeMetadata(URL url, Metadata metadata) { + return fileMetadata + .update(url.getSerializer().serialize(url), oldValue -> { + if (oldValue.isPresent()) { + return oldValue; + } else { + return Optional + .of(new DiskMetadata( + metadata.getSize(), + new BooleanArrayList(DiskMetadata.getBlocksCount(metadata.getSize(), BLOCK_SIZE)) + )) + .map(diskMetadataSerializer::serialize); + } + }) + .then(); + } + + @Override + public Mono writeContentBlock(URL url, DataBlock dataBlock) { + return Mono + .fromCallable(() -> { + byte[] bytes = new byte[dataBlock.getLength()]; + dataBlock.getData().get(bytes); + return bytes; + }).subscribeOn(Schedulers.boundedElastic()) + .flatMap(bytes -> fileContent.put(getBlockKey(url, dataBlock.getId()), bytes, LLDictionaryResultType.VOID)) + .then(fileMetadata + .update(url.getSerializer().serialize(url), prevBytes -> prevBytes + .map(diskMetadataSerializer::deserialize) + .map(prevMeta -> { + if (!prevMeta.getDownloadedBlocks().getBoolean(dataBlock.getId())) { + BooleanArrayList bal = prevMeta.getDownloadedBlocks().clone(); + bal.add(dataBlock.getId(), true); + return new DiskMetadata(prevMeta.getSize(), bal); + } else { + return prevMeta; + } + }) + .map(diskMetadataSerializer::serialize) + ) + ) + .then(); + } + + @Override + public Flux requestContent(URL url) { + return requestDiskMetadata(url) + .filter(DiskMetadata::isDownloadedFully) + .flatMapMany(meta -> Flux.fromIterable(meta.getDownloadedBlocks())) + .index() + // Get only downloaded blocks + .filter(Tuple2::getT2) + .flatMapSequential(blockMeta -> { + int blockId = Math.toIntExact(blockMeta.getT1()); + boolean downloaded = blockMeta.getT2(); + if (!downloaded) { + return Mono.empty(); + } + return fileContent.get(null, getBlockKey(url, blockId)).map(data -> { + int blockOffset = getBlockOffset(blockId); + int blockLength = data.length; + if (blockOffset + blockLength >= blockMeta.size()) { + if (blockOffset + blockLength > blockMeta.size()) { + throw new IllegalStateException("Overflowed data size"); + } + } else if (data.length != BLOCK_SIZE) { + throw new IllegalStateException("Block data length != block length"); + } + return new DataBlock(blockOffset, blockLength, ByteBuffer.wrap(data, 0, blockLength)); + }); + }); + } + + private byte[] getBlockKey(URL url, int blockId) { + byte[] urlBytes = url.getSerializer().serialize(url); + byte[] blockIdBytes = Ints.toByteArray(blockId); + byte[] resultBytes = Arrays.copyOf(urlBytes, urlBytes.length); + System.arraycopy(blockIdBytes, 0, resultBytes, urlBytes.length, blockIdBytes.length); + return resultBytes; + } + + private static int getBlockOffset(int blockId) { + return blockId * BLOCK_SIZE; + } + + @Override + public Mono requestDiskMetadata(URL url) { + return fileMetadata + .get(null, url.getSerializer().serialize(url)) + .map(diskMetadataSerializer::deserialize); + } + + @Override + public Mono requestMetadata(URL url) { + return requestDiskMetadata(url) + .map(DiskMetadata::asMetadata); + } + + @Override + public Mono>> request(URL url) { + return fileMetadata + .get(null, url.getSerializer().serialize(url)) + .map(diskMetadataSerializer::deserialize) + .map(diskMeta -> { + var meta = diskMeta.asMetadata(); + if (diskMeta.isDownloadedFully()) { + return Tuples.of(meta, this.requestContent(url)); + } else { + return Tuples.of(meta, Flux.empty()); + } + }); + } + + public Mono close() { + return db.close(); + } +} diff --git a/src/main/lombok/org/warp/filesponge/DiskMetadata.java b/src/main/lombok/org/warp/filesponge/DiskMetadata.java new file mode 100644 index 0000000..317b7b9 --- /dev/null +++ b/src/main/lombok/org/warp/filesponge/DiskMetadata.java @@ -0,0 +1,103 @@ +/* + * FileSponge + * Copyright (C) 2021 Andrea Cavalli + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.warp.filesponge; + +import it.cavallium.dbengine.database.serialization.Serializer; +import it.unimi.dsi.fastutil.booleans.BooleanArrayList; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import lombok.Data; +import lombok.SneakyThrows; +import org.jetbrains.annotations.NotNull; + +@Data +public class DiskMetadata { + + /** + * -1 = unknown size + */ + private final int size; + + private final BooleanArrayList downloadedBlocks; + + private Boolean downloadedFully; + + public boolean isDownloadedFully() { + if (downloadedFully == null) { + // Ensure blocks count is valid by calling getBlocksCount() + getBlocksCount(); + // It's fully downloaded if every block is true + downloadedFully = !this.getDownloadedBlocks().contains(false); + } + return downloadedFully; + } + + private int getBlocksCount() { + var expectedBlocksCount = getBlocksCount(size, Web.BLOCK_SIZE); + if (this.getDownloadedBlocks().size() != expectedBlocksCount) { + throw new IllegalStateException("Blocks array length != expected blocks count"); + } + return expectedBlocksCount; + } + + public static int getBlocksCount(int size, int blockSize) { + return (size + (blockSize - size % blockSize)) / blockSize; + } + + public Metadata asMetadata() { + return new Metadata(size); + } + + public static class DiskMetadataSerializer implements Serializer { + + @SneakyThrows + @Override + public @NotNull DiskMetadata deserialize(byte @NotNull [] serialized) { + var bais = new ByteArrayInputStream(serialized); + var dis = new DataInputStream(bais); + int size = dis.readInt(); + int blocksCount = getBlocksCount(size, Web.BLOCK_SIZE); + boolean[] downloadedBlocks = new boolean[blocksCount]; + for (int i = 0; i < blocksCount; i++) { + downloadedBlocks[i] = dis.readBoolean(); + } + return new DiskMetadata(size, BooleanArrayList.wrap(downloadedBlocks, blocksCount)); + } + + @SneakyThrows + @Override + public byte @NotNull [] serialize(@NotNull DiskMetadata deserialized) { + try (var bos = new ByteArrayOutputStream(Integer.BYTES * 2)) { + try (var dos = new DataOutputStream(bos)) { + dos.writeInt(deserialized.getSize()); + if (deserialized.getDownloadedBlocks().size() != deserialized.getBlocksCount()) { + throw new IllegalStateException("Blocks array length != expected blocks count"); + } + for (boolean downloadedBlock : deserialized.getDownloadedBlocks()) { + dos.writeBoolean(downloadedBlock); + } + } + return bos.toByteArray(); + } + } + + } +} diff --git a/src/main/lombok/org/warp/filesponge/FileMirrorsManager.java b/src/main/lombok/org/warp/filesponge/FileMirrorsManager.java deleted file mode 100644 index 8e7dfac..0000000 --- a/src/main/lombok/org/warp/filesponge/FileMirrorsManager.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * FileSponge - * Copyright (C) 2020 Andrea Cavalli - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.warp.filesponge; - -import java.util.Set; -import java.util.stream.Collectors; -import lombok.AllArgsConstructor; -import org.warp.filesponge.reactor.AsyncMultiAssociation; -import org.warp.filesponge.value.FileURI; -import org.warp.filesponge.value.MirrorURI; -import reactor.core.publisher.Mono; -import reactor.core.scheduler.Scheduler; -import reactor.core.scheduler.Schedulers; - -@AllArgsConstructor() -public class FileMirrorsManager { - - private final Scheduler fileMirrorsManagerScheduler = Schedulers.single(); - - private final MirrorAvailabilityManager mirrorAvailabilityManager; - - /** - * This map must be persistent - */ - private final AsyncMultiAssociation fileMirrors; - - public Mono> getAvailableMirrors(FileURI fileURI) { - return fileMirrors - .getLinks(fileURI) - .filterWhen(mirrorAvailabilityManager::isMirrorAvailable) - .collect(Collectors.toUnmodifiableSet()) - .subscribeOn(fileMirrorsManagerScheduler); - } - - public Mono hasAnyAvailableMirror(FileURI uri) { - return fileMirrors - .getLinks(uri) - .filterWhen(mirrorAvailabilityManager::isMirrorAvailable) - .hasElements() - .subscribeOn(fileMirrorsManagerScheduler); - } - - public Mono addMirror(FileURI uri, MirrorURI mirrorURI) { - return fileMirrors - .link(uri, mirrorURI) - .then() - .subscribeOn(fileMirrorsManagerScheduler); - } - - public Mono removeMirror(FileURI uri, MirrorURI mirrorURI) { - return fileMirrors - .unlink(uri, mirrorURI) - .then() - .subscribeOn(fileMirrorsManagerScheduler); - } - - public Mono unsetAllFiles() { - return fileMirrors.clear() - .subscribeOn(fileMirrorsManagerScheduler); - } -} diff --git a/src/main/lombok/org/warp/filesponge/value/FileAvailability.java b/src/main/lombok/org/warp/filesponge/Metadata.java similarity index 81% rename from src/main/lombok/org/warp/filesponge/value/FileAvailability.java rename to src/main/lombok/org/warp/filesponge/Metadata.java index bd7f062..291c2e7 100644 --- a/src/main/lombok/org/warp/filesponge/value/FileAvailability.java +++ b/src/main/lombok/org/warp/filesponge/Metadata.java @@ -1,6 +1,6 @@ /* * FileSponge - * Copyright (C) 2020 Andrea Cavalli + * Copyright (C) 2021 Andrea Cavalli * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -16,12 +16,16 @@ * along with this program. If not, see . */ -package org.warp.filesponge.value; +package org.warp.filesponge; + +import lombok.Value; + +@Value +public class Metadata { + + /** + * -1 = unknown size + */ + int size; -public enum FileAvailability { - UNKNOWN, - DOWNLOADABLE, - DOWNLOADING, - DOWNLOADED, - FAILED } diff --git a/src/main/lombok/org/warp/filesponge/MirrorAvailabilityManager.java b/src/main/lombok/org/warp/filesponge/MirrorAvailabilityManager.java deleted file mode 100644 index 09f8cff..0000000 --- a/src/main/lombok/org/warp/filesponge/MirrorAvailabilityManager.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * FileSponge - * Copyright (C) 2020 Andrea Cavalli - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.warp.filesponge; - -import lombok.AccessLevel; -import lombok.AllArgsConstructor; -import org.warp.filesponge.value.MirrorURI; -import org.warp.filesponge.reactor.ConcurrentAsyncSet; -import org.warp.filesponge.reactor.AsyncSet; -import reactor.core.publisher.Mono; - -@AllArgsConstructor(access = AccessLevel.PUBLIC) -public class MirrorAvailabilityManager { - - private final AsyncSet availableMirrors = new ConcurrentAsyncSet<>(); - - public Mono setAllMirrorsAsUnavailable() { - return availableMirrors.clear(); - } - - public Mono setMirrorAvailability(MirrorURI mirrorURI, boolean available) { - if (available) { - return availableMirrors.add(mirrorURI).then(); - } else { - return availableMirrors.remove(mirrorURI).then(); - } - } - - public Mono isMirrorAvailable(MirrorURI mirrorURI) { - return this.availableMirrors.contains(mirrorURI); - } -} diff --git a/src/main/lombok/org/warp/filesponge/SecureFileAccessor.java b/src/main/lombok/org/warp/filesponge/SecureFileAccessor.java deleted file mode 100644 index 8c61337..0000000 --- a/src/main/lombok/org/warp/filesponge/SecureFileAccessor.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * FileSponge - * Copyright (C) 2020 Andrea Cavalli - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.warp.filesponge; - -import java.nio.ByteBuffer; -import lombok.AllArgsConstructor; -import lombok.EqualsAndHashCode; -import org.jetbrains.annotations.NotNull; -import org.warp.filesponge.api.FileAccessor; -import org.warp.filesponge.value.FileStatus; -import org.warp.filesponge.value.FileURI; -import reactor.core.publisher.Mono; - -/** - * Prevent access to other methods via casting - */ -@AllArgsConstructor -@EqualsAndHashCode -public class SecureFileAccessor implements FileAccessor { - - private final FileAccessor fileAccessor; - - @Override - public Mono delete(@NotNull FURI fileURI) { - return fileAccessor.delete(fileURI); - } - - @Override - public Mono getContent(@NotNull FURI fileURI, boolean offlineOnly) { - return fileAccessor.getContent(fileURI, offlineOnly); - } - - @Override - public @NotNull Mono getStatus(@NotNull FURI fileURI) { - return fileAccessor.getStatus(fileURI); - } - - @Override - public String toString() { - return fileAccessor.toString(); - } -} diff --git a/src/main/lombok/org/warp/filesponge/value/FileDataAvailability.java b/src/main/lombok/org/warp/filesponge/URL.java similarity index 78% rename from src/main/lombok/org/warp/filesponge/value/FileDataAvailability.java rename to src/main/lombok/org/warp/filesponge/URL.java index 1089ae1..8e254de 100644 --- a/src/main/lombok/org/warp/filesponge/value/FileDataAvailability.java +++ b/src/main/lombok/org/warp/filesponge/URL.java @@ -1,6 +1,6 @@ /* * FileSponge - * Copyright (C) 2020 Andrea Cavalli + * Copyright (C) 2021 Andrea Cavalli * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -16,10 +16,12 @@ * along with this program. If not, see . */ -package org.warp.filesponge.value; +package org.warp.filesponge; + +import it.cavallium.dbengine.database.serialization.Serializer; + +public interface URL { + + Serializer getSerializer(); -public enum FileDataAvailability { - UNAVAILABLE, - PARTIAL, - FULL } diff --git a/src/main/lombok/org/warp/filesponge/value/FileSourceAvailability.java b/src/main/lombok/org/warp/filesponge/URLDiskHandler.java similarity index 77% rename from src/main/lombok/org/warp/filesponge/value/FileSourceAvailability.java rename to src/main/lombok/org/warp/filesponge/URLDiskHandler.java index 1fe338d..9cac5d4 100644 --- a/src/main/lombok/org/warp/filesponge/value/FileSourceAvailability.java +++ b/src/main/lombok/org/warp/filesponge/URLDiskHandler.java @@ -1,6 +1,6 @@ /* * FileSponge - * Copyright (C) 2020 Andrea Cavalli + * Copyright (C) 2021 Andrea Cavalli * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -16,11 +16,12 @@ * along with this program. If not, see . */ -package org.warp.filesponge.value; +package org.warp.filesponge; + +import reactor.core.publisher.Mono; + +public interface URLDiskHandler extends URLHandler { + + Mono requestDiskMetadata(); -public enum FileSourceAvailability { - DOWNLOADABLE, - DOWNLOADING, - DOWNLOADED, - FAILED } diff --git a/src/main/lombok/org/warp/filesponge/value/AlreadyAssignedException.java b/src/main/lombok/org/warp/filesponge/URLHandler.java similarity index 64% rename from src/main/lombok/org/warp/filesponge/value/AlreadyAssignedException.java rename to src/main/lombok/org/warp/filesponge/URLHandler.java index 4d8f0b7..c49d2b2 100644 --- a/src/main/lombok/org/warp/filesponge/value/AlreadyAssignedException.java +++ b/src/main/lombok/org/warp/filesponge/URLHandler.java @@ -16,23 +16,21 @@ * along with this program. If not, see . */ -package org.warp.filesponge.value; +package org.warp.filesponge; -public class AlreadyAssignedException extends Exception { +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; +import reactor.util.function.Tuple2; +import reactor.util.function.Tuples; - public AlreadyAssignedException() { - super(); +public interface URLHandler { + + Flux requestContent(); + + Mono requestMetadata(); + + default Mono>> request() { + return requestMetadata().map(metadata -> Tuples.of(metadata, requestContent())); } - public AlreadyAssignedException(String message) { - super(message); - } - - public AlreadyAssignedException(String message, Throwable cause) { - super(message, cause); - } - - public AlreadyAssignedException(Throwable cause) { - super(cause); - } } diff --git a/src/main/lombok/org/warp/filesponge/value/FileType.java b/src/main/lombok/org/warp/filesponge/URLWriter.java similarity index 75% rename from src/main/lombok/org/warp/filesponge/value/FileType.java rename to src/main/lombok/org/warp/filesponge/URLWriter.java index a43feb6..eeeb1b9 100644 --- a/src/main/lombok/org/warp/filesponge/value/FileType.java +++ b/src/main/lombok/org/warp/filesponge/URLWriter.java @@ -1,6 +1,6 @@ /* * FileSponge - * Copyright (C) 2020 Andrea Cavalli + * Copyright (C) 2021 Andrea Cavalli * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -16,6 +16,14 @@ * along with this program. If not, see . */ -package org.warp.filesponge.value; +package org.warp.filesponge; -public interface FileType {} +import reactor.core.publisher.Mono; + +public interface URLWriter { + + Mono writeMetadata(Metadata metadata); + + Mono writeContentBlock(DataBlock dataBlock); + +} diff --git a/src/main/lombok/org/warp/filesponge/reactor/AsyncMultiAssociation.java b/src/main/lombok/org/warp/filesponge/URLsDiskHandler.java similarity index 57% rename from src/main/lombok/org/warp/filesponge/reactor/AsyncMultiAssociation.java rename to src/main/lombok/org/warp/filesponge/URLsDiskHandler.java index 1c4aff9..b537acb 100644 --- a/src/main/lombok/org/warp/filesponge/reactor/AsyncMultiAssociation.java +++ b/src/main/lombok/org/warp/filesponge/URLsDiskHandler.java @@ -16,40 +16,32 @@ * along with this program. If not, see . */ -package org.warp.filesponge.reactor; +package org.warp.filesponge; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -public interface AsyncMultiAssociation { +public interface URLsDiskHandler extends URLsHandler { - Mono link(T var1, U var2); + Mono requestDiskMetadata(URL url); - Mono unlink(T var1, U var2); + default URLDiskHandler asURLDiskHandler(URL url) { + return new URLDiskHandler() { + @Override + public Mono requestDiskMetadata() { + return URLsDiskHandler.this.requestDiskMetadata(url); + } - Flux unlink(T var1); + @Override + public Flux requestContent() { + return URLsDiskHandler.this.requestContent(url); + } - Flux unlinkFromSource(U var1); - - default Mono hasAnyLink(T src) { - return this.getLinks(src).hasElements(); + @Override + public Mono requestMetadata() { + return URLsDiskHandler.this.requestMetadata(url); + } + }; } - default Mono hasAnyLinkSource(U dest) { - return this.getLinkSources(dest).hasElements(); - } - - Mono hasLink(T var1, U var2); - - Flux getLinks(T var1); - - Flux getLinkSources(U var1); - - Mono clear(); - - Mono size(); - - Flux getSources(); - - Flux getDestinations(); } diff --git a/src/main/lombok/org/warp/filesponge/URLsHandler.java b/src/main/lombok/org/warp/filesponge/URLsHandler.java new file mode 100644 index 0000000..c60916a --- /dev/null +++ b/src/main/lombok/org/warp/filesponge/URLsHandler.java @@ -0,0 +1,50 @@ +/* + * FileSponge + * Copyright (C) 2021 Andrea Cavalli + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.warp.filesponge; + +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; +import reactor.util.function.Tuple2; +import reactor.util.function.Tuples; + +public interface URLsHandler { + + Flux requestContent(URL url); + + Mono requestMetadata(URL url); + + default Mono>> request(URL url) { + return requestMetadata(url).map(metadata -> Tuples.of(metadata, requestContent(url))); + } + + default URLHandler asURLHandler(URL url) { + return new URLHandler() { + @Override + public Flux requestContent() { + return URLsHandler.this.requestContent(url); + } + + @Override + public Mono requestMetadata() { + return URLsHandler.this.requestMetadata(url); + } + }; + } + +} diff --git a/src/main/lombok/org/warp/filesponge/api/FileSpongeClient.java b/src/main/lombok/org/warp/filesponge/URLsWriter.java similarity index 57% rename from src/main/lombok/org/warp/filesponge/api/FileSpongeClient.java rename to src/main/lombok/org/warp/filesponge/URLsWriter.java index a2d0a53..3490609 100644 --- a/src/main/lombok/org/warp/filesponge/api/FileSpongeClient.java +++ b/src/main/lombok/org/warp/filesponge/URLsWriter.java @@ -1,6 +1,6 @@ /* * FileSponge - * Copyright (C) 2020 Andrea Cavalli + * Copyright (C) 2021 Andrea Cavalli * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -16,25 +16,28 @@ * along with this program. If not, see . */ -package org.warp.filesponge.api; +package org.warp.filesponge; -import org.warp.filesponge.SecureFileAccessor; -import org.warp.filesponge.value.FileURI; import reactor.core.publisher.Mono; -/** - * FileAccessor can be used to manage FileSponge and access files from the client side - */ -public interface FileSpongeClient extends FileAccessor { +public interface URLsWriter { - Mono optimizeStorage(); + Mono writeMetadata(URL url, Metadata metadata); - /** - * Get this instance but without special methods - * - * @return limited instance of itself - */ - default FileAccessor asFileAccessor() { - return new SecureFileAccessor<>(this); + Mono writeContentBlock(URL url, DataBlock dataBlock); + + default URLWriter getUrlWriter(URL url) { + return new URLWriter() { + @Override + public Mono writeMetadata(Metadata metadata) { + return URLsWriter.this.writeMetadata(url, metadata); + } + + @Override + public Mono writeContentBlock(DataBlock dataBlock) { + return URLsWriter.this.writeContentBlock(url, dataBlock); + } + }; } + } diff --git a/src/main/lombok/org/warp/filesponge/Web.java b/src/main/lombok/org/warp/filesponge/Web.java new file mode 100644 index 0000000..de8a7d2 --- /dev/null +++ b/src/main/lombok/org/warp/filesponge/Web.java @@ -0,0 +1,87 @@ +/* + * FileSponge + * Copyright (C) 2021 Andrea Cavalli + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.warp.filesponge; + +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +public class Web implements URLsHandler { + + public static final int BLOCK_SIZE = 8 * 1024 * 1024; // 8 MiB + public static final int MAX_BLOCKS = 256; // 2 GiB + + private final Set urlsHandlers = new ConcurrentHashMap().keySet(new Object()); + + private final Set cacheAccess = new ConcurrentHashMap().keySet(new Object()); + private final Set cacheWrite = new ConcurrentHashMap().keySet(new Object()); + + public Web() { + + } + + public Mono registerSource(URLsHandler urLsHandler) { + return Mono.fromRunnable(() -> urlsHandlers.add(urLsHandler)); + } + + public Mono registerCache(T urlsCache) { + return Mono.fromRunnable(() -> { + cacheAccess.add(urlsCache); + cacheWrite.add(urlsCache); + }); + } + + @Override + public Flux requestContent(URL url) { + return Flux + .fromIterable(cacheAccess) + .flatMap(urlsHandler -> urlsHandler.requestContent(url)) + .switchIfEmpty(Flux + .fromIterable(urlsHandlers) + .flatMap(urlsHandler -> urlsHandler + .requestContent(url) + .flatMapSequential(dataBlock -> Flux + .fromIterable(cacheWrite) + .flatMapSequential(cw -> cw.writeContentBlock(url, dataBlock)) + .then(Mono.just(dataBlock)) + ) + ) + ) + .distinct(DataBlock::getId); + } + + @Override + public Mono requestMetadata(URL url) { + return Mono.from(Flux + .fromIterable(cacheAccess) + .flatMap(urlsHandler -> urlsHandler.requestMetadata(url)) + .switchIfEmpty(Flux + .fromIterable(urlsHandlers) + .flatMap(urlsHandler -> urlsHandler + .requestMetadata(url) + .flatMap(dataBlock -> Flux + .fromIterable(cacheWrite) + .flatMapSequential(cw -> cw.writeMetadata(url, dataBlock)) + .then(Mono.just(dataBlock)) + ) + ) + )); + } +} diff --git a/src/main/lombok/org/warp/filesponge/api/FileAccessor.java b/src/main/lombok/org/warp/filesponge/api/FileAccessor.java deleted file mode 100644 index d2a4b82..0000000 --- a/src/main/lombok/org/warp/filesponge/api/FileAccessor.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * FileSponge - * Copyright (C) 2020 Andrea Cavalli - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.warp.filesponge.api; - -import java.nio.ByteBuffer; -import org.jetbrains.annotations.NotNull; -import org.warp.filesponge.value.FileStatus; -import org.warp.filesponge.value.FileURI; -import reactor.core.publisher.Mono; - -/** - * FileAccessor can be used to access files from the client side - */ -public interface FileAccessor { - - /** - * Request file deletion - * - * @param fileURI File URI - * @return Empty. - */ - Mono delete(@NotNull FURI fileURI); - - /** - * Get file content - * - * @param fileURI File URI - * @param offlineOnly true to get the file from cache - * @return content if found. If the request is offline the future will complete instantly. - * Can be empty - */ - Mono getContent(@NotNull FURI fileURI, boolean offlineOnly); - - /** - * Get file status - * - * @param fileURI File URI - * @return status of this file. Cannot be empty. - */ - Mono getStatus(@NotNull FURI fileURI); -} diff --git a/src/main/lombok/org/warp/filesponge/api/FileActor.java b/src/main/lombok/org/warp/filesponge/api/FileActor.java deleted file mode 100644 index 39bfa75..0000000 --- a/src/main/lombok/org/warp/filesponge/api/FileActor.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * FileSponge - * Copyright (C) 2020 Andrea Cavalli - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.warp.filesponge.api; - -import java.time.Duration; -import org.warp.filesponge.value.FileURI; -import reactor.core.publisher.Mono; - -/** - * FileActor sends signals to a mirror - */ -public interface FileActor { - - /** - * Send a "delete file" signal - * - * @param fileURI File URI - * @return true if the signal can be sent. Cannot be empty. - */ - Mono deleteFile(FURI fileURI); - - /** - * Send a "download file" signal - * - * @param fileURI File URI - * @return true if the signal can be sent. Cannot be empty. - */ - Mono downloadFile(FURI fileURI); - - /** - * Check if this actor can handle signals for this file - * - * @param fileURI File URI - * @return true if the actor can send signals related to this file. Cannot be empty. - */ - Mono canHandleFile(FURI fileURI); - - /** - * Send a "download file" signal - * - * @param timeout if it's 0 the method will return immediately, if it's set the method will wait until a file - * download request has been found, or the timeout time elapsed - * @return empty if no pending download requests has been found, true if the signal can be sent, false - * otherwise - */ - Mono downloadNextFile(Duration timeout); - - /** - * Send a "delete file" signal - * - * @param timeout if it's 0 the method will return immediately, if it's set the method will wait until a file - * delete request has been found, or the timeout time elapsed - * @return empty if no pending delete requests has been found, true if the signal can be sent, false otherwise - */ - Mono deleteNextFile(Duration timeout); -} diff --git a/src/main/lombok/org/warp/filesponge/api/FileSource.java b/src/main/lombok/org/warp/filesponge/api/FileSource.java deleted file mode 100644 index 99a9ce8..0000000 --- a/src/main/lombok/org/warp/filesponge/api/FileSource.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * FileSponge - * Copyright (C) 2020 Andrea Cavalli - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.warp.filesponge.api; - -import java.nio.ByteBuffer; -import org.jetbrains.annotations.NotNull; -import org.warp.filesponge.value.FileSourceAvailability; -import org.warp.filesponge.value.FileType; -import org.warp.filesponge.value.FileURI; -import reactor.core.publisher.Mono; - -/** - * FileSource receives responses from a mirror - */ -public interface FileSource { - - /** - * Called when the mirror is online - */ - Mono onAvailable(); - - /** - * Called when the mirror is unreachable - */ - Mono onUnavailable(); - - /** - * Called when the mirror notifies you that a new file exists. Cannot be empty. - */ - Mono onNewFile(@NotNull FURI fileURI, @NotNull FTYPE fileType); - - /** - * Called when the mirror notifies you details about a file. - *

- * {@link FileSource#onNewFile(FURI, FTYPE)} must have been already called - */ - Mono onFile(@NotNull FURI fileURI, @NotNull FileSourceAvailability fileAvailability, long totalSize); - - /** - * Called when the mirror notifies you the bytes of a part of a file. - *

- * {@link FileSource#onNewFile(FURI, FTYPE)} and {@link FileSource#onFile(FURI, FileSourceAvailability, long)} must - * have been already called - */ - Mono onFilePiece(@NotNull FURI fileURI, long offset, long size, @NotNull ByteBuffer piece); -} diff --git a/src/main/lombok/org/warp/filesponge/api/FileStorage.java b/src/main/lombok/org/warp/filesponge/api/FileStorage.java deleted file mode 100644 index 799dd94..0000000 --- a/src/main/lombok/org/warp/filesponge/api/FileStorage.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * FileSponge - * Copyright (C) 2020 Andrea Cavalli - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.warp.filesponge.api; - -import java.nio.ByteBuffer; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.warp.filesponge.value.FileType; -import org.warp.filesponge.value.FileURI; -import org.warp.filesponge.value.MirrorURI; -import reactor.core.publisher.Mono; - -public interface FileStorage { - - Mono newFile(@NotNull FURI fileURI, @NotNull FTYPE fileType); - - /** - * Read file data. - * Fails if not all the file data is available. - * @param fileURI File URI - * @return read-only file data - * @throws java.util.NoSuchElementException if file is not existing, or some requested data is missing - */ - Mono readFileData(@NotNull FURI fileURI); - - /** - * Read a part of file data. - * Fails if not all the requested file data is available. - * @param fileURI File URI - * @param offset offset of the current data segment - * @param size current data segment size - * @return read-only file data part - * @throws java.util.NoSuchElementException if file is not existing, or some requested data is missing - * @throws org.warp.commonutils.error.IndexOutOfBoundsException if requested offset or size is not valid - */ - Mono readFileDataPart(@NotNull FURI fileURI, long offset, long size); - - /** - * Set a part of file data. - * If file size is 0, the file will be deleted. - * @param fileURI File URI - * @param offset offset of the current data segment - * @param size current data segment size - * @param bytes data segment, can be null if totalSize is 0 - * @param totalSize total file size - * @return nothing - */ - Mono setFileData(@NotNull FURI fileURI, long offset, long size, @Nullable ByteBuffer bytes, long totalSize); - - Mono hasAllData(@NotNull FURI fileURI); - - /** - * Delete a file - * @param fileURI File URI - * @return nothing - */ - default Mono deleteFile(@NotNull FURI fileURI) { - return setFileData(fileURI, 0, 0, null, 0); - } -} diff --git a/src/main/lombok/org/warp/filesponge/extra/api/FileURITranslator.java b/src/main/lombok/org/warp/filesponge/extra/api/FileURITranslator.java deleted file mode 100644 index 932a7f5..0000000 --- a/src/main/lombok/org/warp/filesponge/extra/api/FileURITranslator.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * FileSponge - * Copyright (C) 2020 Andrea Cavalli - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.warp.filesponge.extra.api; - -import java.util.Optional; -import org.warp.filesponge.value.AlreadyAssignedException; -import org.warp.filesponge.value.FileURI; - -/** - * Translate File URIs to "fileId" and back - */ -public interface FileURITranslator { - - Optional getURI(FID fileId); - - Optional getFileId(FURI fileURI); - - /** - * @throws AlreadyAssignedException Throw if the uri has another fileId assigned - */ - void setFileId(FURI fileURI, FID fileId) throws AlreadyAssignedException; - - Optional delete(FID fileId); - - Optional delete(FURI fileURI); - - void clear(); -} diff --git a/src/main/lombok/org/warp/filesponge/extra/api/URIObfuscator.java b/src/main/lombok/org/warp/filesponge/extra/api/URIObfuscator.java deleted file mode 100644 index e7d80a2..0000000 --- a/src/main/lombok/org/warp/filesponge/extra/api/URIObfuscator.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * FileSponge - * Copyright (C) 2020 Andrea Cavalli - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.warp.filesponge.extra.api; - -import org.warp.filesponge.value.FileURI; - -/** - * Create an unique "fileId" for each File URI - */ -public interface URIObfuscator { - - FURI deobfuscateFileId(FID fileId); - - void getFileId(FURI fileURI); - - void setURIValue(FURI fileURI, FID fileId); - - FURI delete(FID fileId); - - FID delete(FURI fileURI); - - void clear(); -} diff --git a/src/main/lombok/org/warp/filesponge/extra/package-info.java b/src/main/lombok/org/warp/filesponge/extra/package-info.java deleted file mode 100644 index 5769ec9..0000000 --- a/src/main/lombok/org/warp/filesponge/extra/package-info.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * FileSponge - * Copyright (C) 2020 Andrea Cavalli - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -/** - * Extra functionalities and utilities for FileSponge - */ -package org.warp.filesponge.extra; \ No newline at end of file diff --git a/src/main/lombok/org/warp/filesponge/reactor/AsyncSet.java b/src/main/lombok/org/warp/filesponge/reactor/AsyncSet.java deleted file mode 100644 index ab9415c..0000000 --- a/src/main/lombok/org/warp/filesponge/reactor/AsyncSet.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * FileSponge - * Copyright (C) 2021 Andrea Cavalli - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.warp.filesponge.reactor; - -import java.util.Set; -import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; - -/** - * Asynchronous set - * @param value type - */ -public interface AsyncSet { - - /** - * Clear the set - * - * @return void - */ - Mono clear(); - - /** - * Add element to the set - * - * @param value value to add - * @return true if added, false if it's already present. Can't be empty. - */ - Mono add(T value); - - /** - * Remove element from the set - * - * @param value value to remove - * @return true if removed, false if it's not present. Can't be empty. - */ - Mono remove(T value); - - /** - * Find if element is present in the set - * - * @param value value to find - * @return true if found, false if not. Can't be empty. - */ - Mono contains(T value); - - /** - * Get the set size - * - * @return set size, from 0 to {@value Integer#MAX_VALUE}. Can't be empty. - */ - Mono size(); - - /** - * Get all values - * @return values, in a flux. Can be empty. - */ - Flux toFlux(); - - /** - * Get all values - * @return values, in a set. Can't be empty. - */ - Mono> toSet(); -} diff --git a/src/main/lombok/org/warp/filesponge/reactor/ConcurrentAsyncSet.java b/src/main/lombok/org/warp/filesponge/reactor/ConcurrentAsyncSet.java deleted file mode 100644 index 2273916..0000000 --- a/src/main/lombok/org/warp/filesponge/reactor/ConcurrentAsyncSet.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * FileSponge - * Copyright (C) 2021 Andrea Cavalli - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.warp.filesponge.reactor; - -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentHashMap.KeySetView; -import lombok.EqualsAndHashCode; -import org.warp.commonutils.concurrency.atomicity.Atomic; -import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; - -@Atomic -@EqualsAndHashCode -public class ConcurrentAsyncSet implements AsyncSet { - - private final KeySetView set; - - public ConcurrentAsyncSet() { - this.set = ConcurrentHashMap.newKeySet(); - } - - @Override - public Mono clear() { - return Mono.fromCallable(() -> { - set.clear(); - return null; - }); - } - - @Override - public Mono add(T value) { - return Mono.fromCallable(() -> set.add(value)); - } - - @Override - public Mono remove(T value) { - return Mono.fromCallable(() -> set.remove(value)); - } - - @Override - public Mono contains(T value) { - return Mono.fromCallable(() -> set.contains(value)); - } - - @Override - public Mono size() { - return Mono.fromCallable(set::size); - } - - @Override - public Flux toFlux() { - return Flux.fromStream(set::stream); - } - - @Override - public Mono> toSet() { - return Mono.fromCallable(() -> Set.copyOf(set)); - } - - @Override - public String toString() { - return set.toString(); - } -} diff --git a/src/main/lombok/org/warp/filesponge/reactor/HashAsyncSet.java b/src/main/lombok/org/warp/filesponge/reactor/HashAsyncSet.java deleted file mode 100644 index f228456..0000000 --- a/src/main/lombok/org/warp/filesponge/reactor/HashAsyncSet.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * FileSponge - * Copyright (C) 2021 Andrea Cavalli - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.warp.filesponge.reactor; - -import java.util.HashSet; -import java.util.Set; -import lombok.EqualsAndHashCode; -import org.warp.commonutils.concurrency.atomicity.NotAtomic; -import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; - -@NotAtomic -@EqualsAndHashCode -public class HashAsyncSet implements AsyncSet { - - private final HashSet set; - - public HashAsyncSet() { - this.set = new HashSet<>(); - } - - public HashAsyncSet(HashSet set) { - this.set = set; - } - - @Override - public Mono clear() { - return Mono.fromCallable(() -> { - set.clear(); - return null; - }); - } - - @Override - public Mono add(T value) { - return Mono.fromCallable(() -> set.add(value)); - } - - @Override - public Mono remove(T value) { - return Mono.fromCallable(() -> set.remove(value)); - } - - @Override - public Mono contains(T value) { - return Mono.fromCallable(() -> set.contains(value)); - } - - @Override - public Mono size() { - return Mono.fromCallable(set::size); - } - - @Override - public Flux toFlux() { - return Flux.fromStream(set::stream); - } - - @Override - public Mono> toSet() { - return Mono.fromCallable(() -> Set.copyOf(set)); - } - - @Override - public String toString() { - return set.toString(); - } - - public SynchronizedHashAsyncSet synchronize() { - return new SynchronizedHashAsyncSet<>(this); - } -} diff --git a/src/main/lombok/org/warp/filesponge/reactor/SynchronizedHashAsyncSet.java b/src/main/lombok/org/warp/filesponge/reactor/SynchronizedHashAsyncSet.java deleted file mode 100644 index 7eb44af..0000000 --- a/src/main/lombok/org/warp/filesponge/reactor/SynchronizedHashAsyncSet.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * FileSponge - * Copyright (C) 2021 Andrea Cavalli - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.warp.filesponge.reactor; - -import java.util.Set; -import lombok.EqualsAndHashCode; -import org.warp.commonutils.concurrency.atomicity.NotAtomic; -import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; -import reactor.core.scheduler.Scheduler; -import reactor.core.scheduler.Schedulers; - -@EqualsAndHashCode -@NotAtomic -public class SynchronizedHashAsyncSet implements AsyncSet { - - private transient final Scheduler scheduler = Schedulers.single(); - private final HashAsyncSet set; - - public SynchronizedHashAsyncSet() { - this.set = new HashAsyncSet<>(); - } - - public SynchronizedHashAsyncSet(HashAsyncSet set) { - this.set = set; - } - - @Override - public Mono clear() { - return set.clear().subscribeOn(scheduler); - } - - @Override - public Mono add(T value) { - return set.add(value).subscribeOn(scheduler); - } - - @Override - public Mono remove(T value) { - return set.remove(value).subscribeOn(scheduler); - } - - @Override - public Mono contains(T value) { - return set.contains(value).subscribeOn(scheduler); - } - - @Override - public Mono size() { - return set.size().subscribeOn(scheduler); - } - - @Override - public Flux toFlux() { - return set.toFlux().subscribeOn(scheduler); - } - - @Override - public Mono> toSet() { - return set.toSet().subscribeOn(scheduler); - } - - @Override - public String toString() { - return set.toString(); - } -} diff --git a/src/main/lombok/org/warp/filesponge/value/FileStatus.java b/src/main/lombok/org/warp/filesponge/value/FileStatus.java deleted file mode 100644 index 28027ef..0000000 --- a/src/main/lombok/org/warp/filesponge/value/FileStatus.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * FileSponge - * Copyright (C) 2020 Andrea Cavalli - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.warp.filesponge.value; - -import java.util.Optional; -import lombok.Value; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -@Value -public class FileStatus { - @NotNull FileAvailability availability; - @NotNull FileDataAvailability dataAvailability; - @Nullable Integer totalSize; - @Nullable Integer downloadedSize; - - public Optional getTotalSize() { - return Optional.ofNullable(totalSize); - } - - public Optional getDownloadedSize() { - return Optional.ofNullable(downloadedSize); - } -} diff --git a/src/main/lombok/org/warp/filesponge/value/FileURI.java b/src/main/lombok/org/warp/filesponge/value/FileURI.java deleted file mode 100644 index 80ebb50..0000000 --- a/src/main/lombok/org/warp/filesponge/value/FileURI.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * FileSponge - * Copyright (C) 2020 Andrea Cavalli - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.warp.filesponge.value; - -public interface FileURI {} diff --git a/src/main/lombok/org/warp/filesponge/value/MirrorURI.java b/src/main/lombok/org/warp/filesponge/value/MirrorURI.java deleted file mode 100644 index 288ee5c..0000000 --- a/src/main/lombok/org/warp/filesponge/value/MirrorURI.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * FileSponge - * Copyright (C) 2020 Andrea Cavalli - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.warp.filesponge.value; - -public interface MirrorURI {}