Server: don't keep file size separately

This commit is contained in:
2025-03-13 23:10:43 +01:00
parent 38ab6de85b
commit 4c90a74fea
3 changed files with 14 additions and 36 deletions

View File

@@ -9,26 +9,22 @@ import java.util.Collection;
import java.util.Set;
public record File(JObjectKey key, long mode, long cTime, long mTime,
boolean symlink, long size
boolean symlink
) implements JDataRemote, JMapHolder<JMapLongKey> {
public File withSymlink(boolean symlink) {
return new File(key, mode, cTime, mTime, symlink, size);
}
public File withSize(long size) {
return new File(key, mode, cTime, mTime, symlink, size);
return new File(key, mode, cTime, mTime, symlink);
}
public File withMode(long mode) {
return new File(key, mode, cTime, mTime, symlink, size);
return new File(key, mode, cTime, mTime, symlink);
}
public File withCTime(long cTime) {
return new File(key, mode, cTime, mTime, symlink, size);
return new File(key, mode, cTime, mTime, symlink);
}
public File withMTime(long mTime) {
return new File(key, mode, cTime, mTime, symlink, size);
return new File(key, mode, cTime, mTime, symlink);
}
@Override

View File

@@ -2,7 +2,6 @@ package com.usatiuk.dhfs.files.service;
import com.google.protobuf.ByteString;
import com.google.protobuf.UnsafeByteOperations;
import com.usatiuk.dhfs.files.objects.File;
import com.usatiuk.dhfs.objects.JObjectKey;
import org.apache.commons.lang3.tuple.Pair;
@@ -29,9 +28,7 @@ public interface DhfsFileService {
Iterable<String> readDir(String name);
void updateFileSize(File file);
Long size(JObjectKey f);
long size(JObjectKey fileUuid);
Optional<ByteString> read(JObjectKey fileUuid, long offset, int length);

View File

@@ -162,7 +162,7 @@ public class DhfsFileServiceImpl implements DhfsFileService {
var fuuid = UUID.randomUUID();
Log.debug("Creating file " + fuuid);
File f = new File(JObjectKey.of(fuuid.toString()), mode, System.currentTimeMillis(), System.currentTimeMillis(), false, 0);
File f = new File(JObjectKey.of(fuuid.toString()), mode, System.currentTimeMillis(), System.currentTimeMillis(), false);
remoteTx.putData(f);
try {
@@ -541,7 +541,6 @@ public class DhfsFileServiceImpl implements DhfsFileService {
remoteTx.putData(file);
cleanupChunks(file, removedChunks.values());
updateFileSize(file);
return (long) data.size();
});
@@ -571,7 +570,6 @@ public class DhfsFileServiceImpl implements DhfsFileService {
// file = file.withChunks(TreePMap.empty()).withMTime(System.currentTimeMillis());
remoteTx.putData(file);
// cleanupChunks(file, oldChunks.values());
updateFileSize(file);
return true;
}
@@ -675,7 +673,6 @@ public class DhfsFileServiceImpl implements DhfsFileService {
remoteTx.putData(file);
cleanupChunks(file, removedChunks.values());
updateFileSize(file);
return true;
});
}
@@ -709,11 +706,10 @@ public class DhfsFileServiceImpl implements DhfsFileService {
Log.debug("Creating file " + fuuid);
ChunkData newChunkData = createChunk(UnsafeByteOperations.unsafeWrap(oldpath.getBytes(StandardCharsets.UTF_8)));
File f = new File(JObjectKey.of(fuuid.toString()), 0, System.currentTimeMillis(), System.currentTimeMillis(), true, 0);
File f = new File(JObjectKey.of(fuuid.toString()), 0, System.currentTimeMillis(), System.currentTimeMillis(), true);
jMapHelper.put(f, JMapLongKey.of(0), newChunkData.key());
updateFileSize(f);
remoteTx.putData(f);
getTree().move(parent.key(), new JKleppmannTreeNodeMetaFile(fname, f.key()), getTree().getNewNodeId());
return f.key();
});
@@ -733,12 +729,13 @@ public class DhfsFileServiceImpl implements DhfsFileService {
}
@Override
public void updateFileSize(File file) {
jObjectTxManager.executeTx(() -> {
public long size(JObjectKey fileUuid) {
return jObjectTxManager.executeTx(() -> {
long realSize = 0;
var file = remoteTx.getData(File.class, fileUuid)
.orElseThrow(() -> new StatusRuntimeException(Status.NOT_FOUND));
Pair<JMapLongKey, JMapEntry<JMapLongKey>> last;
Log.tracev("Getting last");
try (var it = jMapHelper.getIterator(file, IteratorStart.LT, JMapLongKey.max())) {
last = it.hasNext() ? it.next() : null;
}
@@ -747,19 +744,7 @@ public class DhfsFileServiceImpl implements DhfsFileService {
realSize = last.getKey().key() + getChunkSize(last.getValue().ref());
}
if (realSize != file.size()) {
remoteTx.putData(file.withSize(realSize));
}
});
}
@Override
public Long size(JObjectKey uuid) {
return jObjectTxManager.executeTx(() -> {
var read = remoteTx.getData(File.class, uuid)
.orElseThrow(() -> new StatusRuntimeException(Status.NOT_FOUND));
return read.size();
return realSize;
});
}
}