truncate fix

This commit is contained in:
2024-06-22 18:35:07 +02:00
parent 5f46a81f1d
commit abfaf579cb
2 changed files with 55 additions and 15 deletions

View File

@@ -470,29 +470,25 @@ public class DhfsFileServiceImpl implements DhfsFileService {
try {
file.runWriteLocked((m, fData, bump) -> {
var chunksAll = fData.getChunks();
var newChunks = chunksAll.subMap(0L, length - 1);
var lastChunk = newChunks.lastEntry();
var lastChunk = chunksAll.lastEntry();
if (lastChunk != null) {
var chunkUuid = lastChunk.getValue();
var chunkRead = jObjectManager.get(ChunkData.getNameFromHash(chunkUuid), ChunkData.class);
var size = getChunkSize(lastChunk.getValue());
var chunkEnd = size + lastChunk.getKey();
if (chunkRead.isEmpty()) {
Log.error("Chunk requested not found: " + chunkUuid);
return false;
}
if (chunkEnd == length) return null;
var chunkBytes = chunkRead.get().runReadLocked((m2, d) -> d.getBytes());
if (chunkEnd > length) {
var chunkData = readChunk(lastChunk.getValue());
if (lastChunk.getKey() + chunkBytes.size() > 0) {
int start = (int) (length - lastChunk.getKey());
ChunkData newChunkData = new ChunkData(chunkBytes.substring(0, (int) (length - start)));
ChunkData newChunkData = new ChunkData(chunkData.substring(0, (int) (length - lastChunk.getKey())));
ChunkInfo newChunkInfo = new ChunkInfo(newChunkData.getHash(), newChunkData.getBytes().size());
jObjectManager.put(newChunkData);
jObjectManager.put(newChunkInfo);
newChunks.put(lastChunk.getKey(), newChunkData.getHash());
chunksAll.put(lastChunk.getKey(), newChunkData.getHash());
} else {
write(fileUuid, chunkEnd, new byte[(int) (length - chunkEnd)]);
}
}

View File

@@ -69,7 +69,7 @@ public class DhfsFileServiceSimpleTest {
@Test
void writeTest() {
var ret = fileService.create("/a", 777);
var ret = fileService.create("/writeTest", 777);
Assertions.assertTrue(ret.isPresent());
var uuid = ret.get();
@@ -85,4 +85,48 @@ public class DhfsFileServiceSimpleTest {
fileService.write(uuid, 3, new byte[]{17, 18});
Assertions.assertArrayEquals(new byte[]{0, 1, 2, 17, 18, 11, 15, 16, 8, 9, 13, 14}, fileService.read(uuid, 0, 12).get().toByteArray());
}
@Test
void truncateTest1() {
var ret = fileService.create("/truncateTest1", 777);
Assertions.assertTrue(ret.isPresent());
var uuid = ret.get();
fileService.write(uuid, 0, new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
Assertions.assertArrayEquals(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, fileService.read(uuid, 0, 10).get().toByteArray());
fileService.truncate(uuid, 20);
fileService.write(uuid, 5, new byte[]{10, 11, 12, 13, 14, 15, 16, 17});
Assertions.assertArrayEquals(new byte[]{0, 1, 2, 3, 4, 10, 11, 12, 13, 14, 15, 16, 17, 0, 0, 0, 0, 0, 0, 0}, fileService.read(uuid, 0, 20).get().toByteArray());
}
@Test
void truncateTest2() {
var ret = fileService.create("/truncateTest2", 777);
Assertions.assertTrue(ret.isPresent());
var uuid = ret.get();
fileService.write(uuid, 0, new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
Assertions.assertArrayEquals(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, fileService.read(uuid, 0, 10).get().toByteArray());
fileService.truncate(uuid, 20);
fileService.write(uuid, 10, new byte[]{11, 12, 13, 14, 15, 16, 17, 18, 19, 20});
Assertions.assertArrayEquals(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, fileService.read(uuid, 0, 20).get().toByteArray());
}
@Test
void truncateTest3() {
var ret = fileService.create("/truncateTest3", 777);
Assertions.assertTrue(ret.isPresent());
var uuid = ret.get();
fileService.write(uuid, 0, new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
Assertions.assertArrayEquals(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, fileService.read(uuid, 0, 10).get().toByteArray());
fileService.truncate(uuid, 7);
Assertions.assertArrayEquals(new byte[]{0, 1, 2, 3, 4, 5, 6,}, fileService.read(uuid, 0, 20).get().toByteArray());
}
}