FileSponge/src/main/java/org/warp/filesponge/DiskCache.java

298 lines
9.7 KiB
Java
Raw Normal View History

2021-03-07 15:50:11 +01:00
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package org.warp.filesponge;
2022-06-22 12:04:38 +02:00
import static java.lang.Math.toIntExact;
2021-03-07 15:51:54 +01:00
import static org.warp.filesponge.FileSponge.BLOCK_SIZE;
2021-03-07 15:50:11 +01:00
2022-03-16 13:52:48 +01:00
import io.netty5.buffer.api.Buffer;
2022-05-20 10:25:59 +02:00
import io.netty5.buffer.api.Resource;
2022-03-16 13:52:48 +01:00
import io.netty5.buffer.api.Send;
2022-05-20 10:25:59 +02:00
import it.cavallium.dbengine.database.BufSupplier;
2022-03-02 12:38:25 +01:00
import it.cavallium.dbengine.database.ColumnUtils;
2021-03-07 15:50:11 +01:00
import it.cavallium.dbengine.database.LLDatabaseConnection;
import it.cavallium.dbengine.database.LLDictionary;
import it.cavallium.dbengine.database.LLDictionaryResultType;
import it.cavallium.dbengine.database.LLKeyValueDatabase;
2021-09-03 02:23:19 +02:00
import it.cavallium.dbengine.database.LLUtils;
2021-03-07 15:50:11 +01:00
import it.cavallium.dbengine.database.UpdateMode;
2021-05-08 03:09:49 +02:00
import it.cavallium.dbengine.database.UpdateReturnMode;
2021-08-22 23:52:51 +02:00
import it.cavallium.dbengine.database.serialization.SerializationException;
2022-03-02 12:38:25 +01:00
import it.cavallium.dbengine.rpc.current.data.DatabaseOptions;
2021-03-07 15:50:11 +01:00
import it.unimi.dsi.fastutil.booleans.BooleanArrayList;
import java.util.List;
2021-08-22 18:20:52 +02:00
import java.util.Objects;
2021-11-11 20:27:57 +01:00
import java.util.function.Predicate;
2021-05-03 00:33:04 +02:00
import org.jetbrains.annotations.Nullable;
2021-03-07 15:50:11 +01:00
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;
public class DiskCache implements URLsDiskHandler, URLsWriter {
2021-05-05 00:08:08 +02:00
private final DiskMetadataSerializer diskMetadataSerializer;
2021-03-07 15:50:11 +01:00
private final LLKeyValueDatabase db;
private final LLDictionary fileContent;
private final LLDictionary fileMetadata;
2021-11-11 20:27:57 +01:00
private final Predicate<URL> shouldCache;
2021-03-07 15:50:11 +01:00
2021-11-11 20:27:57 +01:00
public DiskCache(LLKeyValueDatabase db,
LLDictionary fileContent,
LLDictionary fileMetadata,
Predicate<URL> shouldCache) {
2021-05-05 00:08:08 +02:00
this.db = db;
this.fileContent = fileContent;
this.fileMetadata = fileMetadata;
2021-10-19 00:28:30 +02:00
this.diskMetadataSerializer = new DiskMetadataSerializer();
2021-11-11 20:27:57 +01:00
this.shouldCache = shouldCache;
2021-05-05 00:08:08 +02:00
}
2021-06-27 17:04:48 +02:00
public static Mono<DiskCache> open(LLDatabaseConnection databaseConnection,
String dbName,
2021-11-11 20:27:57 +01:00
DatabaseOptions databaseOptions,
Predicate<URL> shouldCache) {
2021-03-07 15:50:11 +01:00
return databaseConnection
2021-06-20 01:07:21 +02:00
.getDatabase(dbName,
2022-03-02 12:38:25 +01:00
List.of(ColumnUtils.dictionary("file-content"), ColumnUtils.dictionary("file-metadata")),
2021-06-27 17:04:48 +02:00
databaseOptions
2021-06-20 01:07:21 +02:00
)
2021-03-07 15:50:11 +01:00
.flatMap(db -> Mono.zip(
Mono.just(db).single(),
db.getDictionary("file-content", UpdateMode.ALLOW).single(),
db.getDictionary("file-metadata", UpdateMode.ALLOW).single()
))
2021-11-11 20:27:57 +01:00
.map(tuple -> new DiskCache(tuple.getT1(), tuple.getT2(), tuple.getT3(), shouldCache))
2021-03-07 15:50:11 +01:00
.single();
}
@Override
public Mono<Void> writeMetadata(URL url, Metadata metadata) {
2021-11-11 20:27:57 +01:00
// Check if this cache should cache the url, otherwise do nothing
if (!shouldCache.test(url)) return Mono.empty();
2022-05-20 10:25:59 +02:00
Mono<Buffer> keyMono = Mono.fromCallable(() -> serializeUrl(url));
2021-08-22 18:20:52 +02:00
return fileMetadata
2022-04-01 01:31:50 +02:00
.update(keyMono,
oldValue -> Objects.requireNonNullElseGet(oldValue,
() -> serializeMetadata(new DiskMetadata(metadata.size(),
BooleanArrayList.wrap(new boolean[DiskMetadata.getBlocksCount(metadata.size(), BLOCK_SIZE)])
2022-05-20 10:25:59 +02:00
))
2022-04-01 01:31:50 +02:00
),
UpdateReturnMode.NOTHING
)
2021-03-07 15:50:11 +01:00
.then();
}
2022-05-20 10:25:59 +02:00
private <T extends URL> Buffer serializeUrl(T url) {
2021-11-12 02:05:18 +01:00
@SuppressWarnings("unchecked")
URLSerializer<T> urlSerializer = (URLSerializer<T>) url.getSerializer();
2021-10-19 00:28:30 +02:00
int sizeHint = urlSerializer.getSerializedSizeHint();
if (sizeHint == -1) sizeHint = 64;
2022-05-20 10:25:59 +02:00
var buffer = db.getAllocator().allocate(sizeHint);
try {
2021-10-19 00:28:30 +02:00
try {
urlSerializer.serialize(url, buffer);
} catch (SerializationException ex) {
throw new IllegalStateException("Failed to serialize url", ex);
}
2022-05-20 10:25:59 +02:00
return buffer;
} catch (Throwable ex) {
buffer.close();
throw ex;
2021-10-19 00:28:30 +02:00
}
}
2022-05-20 10:25:59 +02:00
private Buffer serializeMetadata(DiskMetadata diskMetadata) {
2021-10-19 00:28:30 +02:00
int sizeHint = diskMetadataSerializer.getSerializedSizeHint();
if (sizeHint == -1) sizeHint = 64;
2022-05-20 10:25:59 +02:00
var buffer = db.getAllocator().allocate(sizeHint);
try {
2021-10-19 00:28:30 +02:00
try {
diskMetadataSerializer.serialize(diskMetadata, buffer);
} catch (SerializationException ex) {
throw new IllegalStateException("Failed to serialize metadata", ex);
}
2022-05-20 10:25:59 +02:00
return buffer;
} catch (Throwable ex) {
buffer.close();
throw ex;
2021-10-19 00:28:30 +02:00
}
}
2022-04-01 01:31:50 +02:00
private DiskMetadata deserializeMetadata(Buffer prevBytes) {
try {
return diskMetadataSerializer.deserialize(prevBytes);
2021-10-19 00:28:30 +02:00
} catch (SerializationException ex) {
throw new IllegalStateException("Failed to deserialize metadata", ex);
}
}
2021-03-07 15:50:11 +01:00
@Override
public Mono<Void> writeContentBlock(URL url, DataBlock dataBlock) {
2021-11-11 20:27:57 +01:00
// Check if this cache should cache the url, otherwise do nothing
if (!shouldCache.test(url)) return Mono.empty();
2022-05-20 10:25:59 +02:00
Mono<Buffer> urlKeyMono = Mono.fromCallable(() -> serializeUrl(url));
Mono<Buffer> blockKeyMono = Mono.fromCallable(() -> getBlockKey(url, dataBlock.getId()));
return Mono.using(
() -> BufSupplier.of(dataBlock::getDataCopy),
bufSupplier -> fileContent
.put(blockKeyMono, Mono.fromSupplier(bufSupplier::get), LLDictionaryResultType.VOID)
.doOnNext(Resource::close)
2021-09-03 02:23:19 +02:00
.then(),
2022-05-20 10:25:59 +02:00
BufSupplier::close
)
2021-08-22 18:20:52 +02:00
.then(fileMetadata.update(urlKeyMono, prevBytes -> {
@Nullable DiskMetadata result;
if (prevBytes != null) {
2021-10-19 00:28:30 +02:00
DiskMetadata prevMeta = deserializeMetadata(prevBytes);
2021-08-22 18:20:52 +02:00
if (!prevMeta.isDownloadedBlock(dataBlock.getId())) {
BooleanArrayList bal = prevMeta.downloadedBlocks().clone();
if (prevMeta.size() == -1) {
if (bal.size() > dataBlock.getId()) {
bal.set(dataBlock.getId(), true);
} else if (bal.size() == dataBlock.getId()) {
bal.add(true);
2021-05-16 14:59:12 +02:00
} else {
2021-08-22 18:20:52 +02:00
throw new IndexOutOfBoundsException(
"Trying to write a block too much far from the last block. Previous total blocks: "
+ bal.size() + " Current block id: " + dataBlock.getId());
2021-05-16 14:59:12 +02:00
}
} else {
2021-08-22 18:20:52 +02:00
bal.set(dataBlock.getId(), true);
2021-05-16 14:59:12 +02:00
}
2021-08-22 18:20:52 +02:00
result = new DiskMetadata(prevMeta.size(), bal);
} else {
result = prevMeta;
}
} else {
result = null;
}
if (result != null) {
2022-05-20 10:25:59 +02:00
return serializeMetadata(result);
2021-08-22 18:20:52 +02:00
} else {
return null;
}
}, UpdateReturnMode.NOTHING)
2021-05-16 14:59:12 +02:00
)
2021-03-07 15:50:11 +01:00
.then();
}
@Override
public Flux<DataBlock> requestContent(URL url) {
2021-08-22 18:20:52 +02:00
return this
.requestDiskMetadata(url)
2021-03-07 15:50:11 +01:00
.filter(DiskMetadata::isDownloadedFully)
2021-06-06 02:24:21 +02:00
.flatMapMany(meta -> Flux.fromStream(meta.downloadedBlocks()::stream)
2021-03-09 00:35:24 +01:00
.index()
// Get only downloaded blocks
.filter(Tuple2::getT2)
.flatMapSequential(blockMeta -> {
2022-06-22 12:04:38 +02:00
int blockId = toIntExact(blockMeta.getT1());
2021-03-09 00:35:24 +01:00
boolean downloaded = blockMeta.getT2();
if (!downloaded) {
2021-08-22 18:20:52 +02:00
return Mono.empty();
2021-03-07 15:50:11 +01:00
}
2021-09-03 02:23:19 +02:00
var blockKeyMono = Mono.fromCallable(() -> getBlockKey(url, blockId));
2021-08-22 18:20:52 +02:00
return fileContent
.get(null, blockKeyMono)
2022-05-21 15:29:48 +02:00
.map(data -> {
try (data) {
2022-06-22 12:04:38 +02:00
long blockOffset = getBlockOffset(blockId);
2021-05-16 14:59:12 +02:00
int blockLength = data.readableBytes();
2021-06-02 13:21:34 +02:00
if (meta.size() != -1) {
if (blockOffset + blockLength >= meta.size()) {
if (blockOffset + blockLength > meta.size()) {
throw new IllegalStateException("Overflowed data size");
}
} else {
// Intermediate blocks must be of max size
assert data.readableBytes() == BLOCK_SIZE;
2021-05-16 14:59:12 +02:00
}
}
2021-09-03 02:23:19 +02:00
return DataBlock.of(blockOffset, blockLength, data.send());
2021-05-05 17:31:50 +02:00
}
2021-05-16 14:59:12 +02:00
});
2021-08-22 18:20:52 +02:00
})
);
2021-03-07 15:50:11 +01:00
}
2022-05-20 10:25:59 +02:00
private Buffer getBlockKey(URL url, int blockId) {
try (var urlBytes = serializeUrl(url)) {
2021-09-03 02:23:19 +02:00
Buffer blockIdBytes = this.db.getAllocator().allocate(Integer.BYTES);
blockIdBytes.writeInt(blockId);
2022-05-20 10:25:59 +02:00
return LLUtils.compositeBuffer(db.getAllocator(), urlBytes.send(), blockIdBytes.send());
2021-09-03 02:23:19 +02:00
}
2021-03-07 15:50:11 +01:00
}
2022-06-22 12:04:38 +02:00
private static long getBlockOffset(int blockId) {
return blockId * (long) BLOCK_SIZE;
2021-03-07 15:50:11 +01:00
}
@Override
public Mono<DiskMetadata> requestDiskMetadata(URL url) {
2022-05-20 10:25:59 +02:00
Mono<Buffer> urlKeyMono = Mono.fromCallable(() -> serializeUrl(url));
2021-08-22 18:20:52 +02:00
return fileMetadata
.get(null, urlKeyMono)
2022-05-21 15:29:48 +02:00
.map(prevBytes -> {
try (prevBytes) {
2022-04-01 01:31:50 +02:00
return deserializeMetadata(prevBytes);
}
});
2021-03-07 15:50:11 +01:00
}
@Override
public Mono<Metadata> requestMetadata(URL url) {
return requestDiskMetadata(url)
.map(DiskMetadata::asMetadata);
}
@Override
public Mono<Tuple2<Metadata, Flux<DataBlock>>> request(URL url) {
2022-05-20 10:25:59 +02:00
Mono<Buffer> urlKeyMono = Mono.fromCallable(() -> serializeUrl(url));
2021-05-16 14:59:12 +02:00
return Mono
.using(
2021-10-19 00:28:30 +02:00
() -> serializeUrl(url),
2021-08-22 18:20:52 +02:00
key -> fileMetadata.get(null, urlKeyMono),
2022-05-20 10:25:59 +02:00
Resource::close
2021-05-16 14:59:12 +02:00
)
2021-09-04 16:45:21 +02:00
.map(serialized -> {
2022-04-01 01:31:50 +02:00
DiskMetadata diskMeta;
2022-05-20 10:25:59 +02:00
try (serialized) {
diskMeta = deserializeMetadata(serialized);
2022-04-01 01:31:50 +02:00
}
2021-03-07 15:50:11 +01:00
var meta = diskMeta.asMetadata();
if (diskMeta.isDownloadedFully()) {
return Tuples.of(meta, this.requestContent(url));
} else {
return Tuples.of(meta, Flux.empty());
}
});
}
public Mono<Void> close() {
return db.close();
}
}