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

132 lines
4.2 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;
2021-05-03 00:33:04 +02:00
import io.netty.buffer.ByteBuf;
2021-05-05 00:08:08 +02:00
import io.netty.buffer.ByteBufAllocator;
2021-05-03 00:33:04 +02:00
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
import io.netty.buffer.PooledByteBufAllocator;
2021-09-03 02:23:19 +02:00
import io.netty.buffer.api.Buffer;
import io.netty.buffer.api.BufferAllocator;
import io.netty.buffer.api.Send;
import it.cavallium.dbengine.database.serialization.BufferDataInput;
import it.cavallium.dbengine.database.serialization.BufferDataOutput;
2021-03-07 15:50:11 +01:00
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;
2021-05-21 00:18:39 +02:00
import java.io.IOException;
import org.apache.commons.lang3.SerializationException;
2021-03-07 15:50:11 +01:00
import org.jetbrains.annotations.NotNull;
2021-05-21 00:18:39 +02:00
/**
* size -1 = unknown size
*/
public record DiskMetadata(int size, BooleanArrayList downloadedBlocks) {
2021-03-07 15:50:11 +01:00
public boolean isDownloadedFully() {
2021-05-21 00:18:39 +02:00
boolean downloadedFullyVal;
// Ensure blocks count is valid by calling getBlocksCount()
getBlocksCount();
// It's fully downloaded if every block is true
downloadedFullyVal = !this.downloadedBlocks().contains(false);
return downloadedFullyVal;
2021-03-07 15:50:11 +01:00
}
2021-06-02 13:21:34 +02:00
@SuppressWarnings("UnusedReturnValue")
2021-03-07 15:50:11 +01:00
private int getBlocksCount() {
2021-06-02 13:21:34 +02:00
if (size == -1) {
return downloadedBlocks().size();
}
2021-03-07 15:51:54 +01:00
var expectedBlocksCount = getBlocksCount(size, FileSponge.BLOCK_SIZE);
2021-06-02 13:21:34 +02:00
if (this.downloadedBlocks().size() != expectedBlocksCount) {
2021-03-09 00:35:24 +01:00
throw new IllegalStateException(
2021-05-21 00:18:39 +02:00
"Blocks array length (" + this.downloadedBlocks().size() + ") != expected blocks count ("
+ expectedBlocksCount + ")");
2021-03-07 15:50:11 +01:00
}
return expectedBlocksCount;
}
public static int getBlocksCount(int size, int blockSize) {
2021-06-02 13:21:34 +02:00
if (size == -1) {
return 0;
}
2021-03-07 15:50:11 +01:00
return (size + (blockSize - size % blockSize)) / blockSize;
}
public Metadata asMetadata() {
return new Metadata(size);
}
2021-06-02 13:21:34 +02:00
public boolean isDownloadedBlock(int id) {
if (size == -1 && downloadedBlocks().size() <= id) {
return false;
} else {
return downloadedBlocks().getBoolean(id);
}
}
2021-09-03 02:23:19 +02:00
public static class DiskMetadataSerializer implements Serializer<DiskMetadata> {
2021-03-07 15:50:11 +01:00
2021-09-03 02:23:19 +02:00
private final BufferAllocator allocator;
2021-05-05 00:08:08 +02:00
2021-09-03 02:23:19 +02:00
public DiskMetadataSerializer(BufferAllocator allocator) {
2021-05-05 00:08:08 +02:00
this.allocator = allocator;
}
2021-05-21 00:18:39 +02:00
2021-03-07 15:50:11 +01:00
@Override
2021-09-03 02:23:19 +02:00
public @NotNull DeserializationResult<DiskMetadata> deserialize(@NotNull Send<Buffer> serialized) {
var dis = new BufferDataInput(serialized);
int size = dis.readInt();
int blocksCount;
if (size == -1) {
blocksCount = dis.readShort();
} else {
blocksCount = getBlocksCount(size, FileSponge.BLOCK_SIZE);
2021-03-07 15:50:11 +01:00
}
2021-09-03 02:23:19 +02:00
var downloadedBlocks = new BooleanArrayList(blocksCount);
for (int i = 0; i < blocksCount; i++) {
downloadedBlocks.add(dis.readBoolean());
}
return new DeserializationResult<>(new DiskMetadata(size, downloadedBlocks), dis.getReadBytesCount());
2021-03-07 15:50:11 +01:00
}
@Override
2021-09-03 02:23:19 +02:00
public @NotNull Send<Buffer> serialize(@NotNull DiskMetadata deserialized) {
try (var buffer = allocator.allocate(64)) {
var dos = new BufferDataOutput(buffer);
dos.writeInt(deserialized.size());
if (deserialized.size == -1) {
dos.writeShort(deserialized.getBlocksCount());
} else {
deserialized.getBlocksCount();
}
for (boolean downloadedBlock : deserialized.downloadedBlocks()) {
dos.writeBoolean(downloadedBlock);
2021-03-07 15:50:11 +01:00
}
2021-09-03 02:23:19 +02:00
return buffer.send();
2021-03-07 15:50:11 +01:00
}
}
}
}