Update database

This commit is contained in:
Andrea Cavalli 2022-04-01 01:31:50 +02:00
parent 4922de022f
commit f9a9b42114

View File

@ -89,11 +89,14 @@ public class DiskCache implements URLsDiskHandler, URLsWriter {
Mono<Send<Buffer>> keyMono = Mono.fromCallable(() -> serializeUrl(url)); Mono<Send<Buffer>> keyMono = Mono.fromCallable(() -> serializeUrl(url));
return fileMetadata return fileMetadata
.update(keyMono, oldValue -> Objects.requireNonNullElseGet(oldValue, .update(keyMono,
oldValue -> Objects.requireNonNullElseGet(oldValue,
() -> serializeMetadata(new DiskMetadata(metadata.size(), () -> serializeMetadata(new DiskMetadata(metadata.size(),
BooleanArrayList.wrap(new boolean[DiskMetadata.getBlocksCount(metadata.size(), BLOCK_SIZE)]) BooleanArrayList.wrap(new boolean[DiskMetadata.getBlocksCount(metadata.size(), BLOCK_SIZE)])
)) )).receive()
).receive(), UpdateReturnMode.NOTHING) ),
UpdateReturnMode.NOTHING
)
.then(); .then();
} }
@ -126,9 +129,9 @@ public class DiskCache implements URLsDiskHandler, URLsWriter {
} }
} }
private DiskMetadata deserializeMetadata(Send<Buffer> prevBytes) { private DiskMetadata deserializeMetadata(Buffer prevBytes) {
try (var prevBytesBuf = prevBytes.receive()) { try {
return diskMetadataSerializer.deserialize(prevBytesBuf); return diskMetadataSerializer.deserialize(prevBytes);
} catch (SerializationException ex) { } catch (SerializationException ex) {
throw new IllegalStateException("Failed to deserialize metadata", ex); throw new IllegalStateException("Failed to deserialize metadata", ex);
} }
@ -244,7 +247,11 @@ public class DiskCache implements URLsDiskHandler, URLsWriter {
Mono<Send<Buffer>> urlKeyMono = Mono.fromCallable(() -> serializeUrl(url)); Mono<Send<Buffer>> urlKeyMono = Mono.fromCallable(() -> serializeUrl(url));
return fileMetadata return fileMetadata
.get(null, urlKeyMono) .get(null, urlKeyMono)
.map(this::deserializeMetadata); .map(prevBytesSend -> {
try (var prevBytes = prevBytesSend.receive()) {
return deserializeMetadata(prevBytes);
}
});
} }
@Override @Override
@ -263,7 +270,10 @@ public class DiskCache implements URLsDiskHandler, URLsWriter {
Send::close Send::close
) )
.map(serialized -> { .map(serialized -> {
var diskMeta = deserializeMetadata(serialized); DiskMetadata diskMeta;
try (var serializedBuf = serialized.receive()) {
diskMeta = deserializeMetadata(serializedBuf);
}
var meta = diskMeta.asMetadata(); var meta = diskMeta.asMetadata();
if (diskMeta.isDownloadedFully()) { if (diskMeta.isDownloadedFully()) {
return Tuples.of(meta, this.requestContent(url)); return Tuples.of(meta, this.requestContent(url));