write objects directly

This commit is contained in:
2024-06-30 13:44:20 +02:00
parent e726986081
commit 4dc3cfa481
5 changed files with 22 additions and 32 deletions

View File

@@ -1,8 +1,6 @@
package com.usatiuk.dhfs.storage.objects.jrepository;
import com.google.common.collect.Streams;
import com.google.protobuf.ByteString;
import com.usatiuk.dhfs.storage.SerializationHelper;
import com.usatiuk.dhfs.storage.objects.repository.distributed.ObjectMetadata;
import com.usatiuk.dhfs.storage.objects.repository.distributed.PersistentRemoteHostsService;
import com.usatiuk.dhfs.storage.objects.repository.persistence.ObjectPersistentStore;
@@ -101,7 +99,7 @@ public class JObjectManagerImpl implements JObjectManager {
if (inMap != null) return Optional.of(inMap);
}
ByteString readMd;
Object readMd;
try {
readMd = objectPersistentStore.readObject("meta_" + name);
} catch (StatusRuntimeException ex) {
@@ -109,8 +107,7 @@ public class JObjectManagerImpl implements JObjectManager {
return Optional.empty();
throw ex;
}
var meta = SerializationHelper.deserialize(readMd);
if (!(meta instanceof ObjectMetadata))
if (!(readMd instanceof ObjectMetadata meta))
throw new StatusRuntimeException(Status.DATA_LOSS.withDescription("Unexpected metadata type for " + name));
if (((ObjectMetadata) meta).isDeleted()) {

View File

@@ -94,7 +94,7 @@ public class JObjectResolver {
public <T extends JObjectData> Optional<T> resolveDataLocal(JObject<T> jObject) {
jObject.assertRWLock();
if (objectPersistentStore.existsObject(jObject.getName()))
return Optional.of(SerializationHelper.deserialize(objectPersistentStore.readObject(jObject.getName())));
return Optional.of((T) objectPersistentStore.readObject(jObject.getName())); //FIXME:
return Optional.empty();
}

View File

@@ -181,9 +181,9 @@ public class JObjectWriteback {
return;
}
m.markWritten();
objectPersistentStore.writeObject("meta_" + m.getName(), SerializationHelper.serialize(m));
objectPersistentStore.writeObject("meta_" + m.getName(), m);
if (data != null)
objectPersistentStore.writeObject(m.getName(), SerializationHelper.serialize(data));
objectPersistentStore.writeObject(m.getName(), data);
}
public void remove(JObject<?> object) {

View File

@@ -1,7 +1,6 @@
package com.usatiuk.dhfs.storage.objects.repository.persistence;
import com.google.protobuf.ByteString;
import com.google.protobuf.UnsafeByteOperations;
import com.usatiuk.dhfs.storage.SerializationHelper;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.quarkus.logging.Log;
@@ -10,11 +9,11 @@ import io.quarkus.runtime.StartupEvent;
import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import org.apache.commons.lang3.SerializationException;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import javax.annotation.Nonnull;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
@@ -65,14 +64,13 @@ public class FileObjectPersistentStore implements ObjectPersistentStore {
@Nonnull
@Override
public ByteString readObject(String name) {
public Object readObject(String name) {
var file = Path.of(root, name);
if (!file.toFile().exists())
throw new StatusRuntimeException(Status.NOT_FOUND);
try {
return UnsafeByteOperations.unsafeWrap(Files.readAllBytes(file));
try (FileInputStream in = new FileInputStream(file.toFile())) {
return SerializationHelper.deserialize(in);
} catch (FileNotFoundException f) {
throw new StatusRuntimeException(Status.NOT_FOUND.withDescription("Not found: " + name));
} catch (IOException e) {
Log.error("Error reading file " + file, e);
throw new StatusRuntimeException(Status.INTERNAL);
@@ -80,20 +78,17 @@ public class FileObjectPersistentStore implements ObjectPersistentStore {
}
@Override
public void writeObject(String name, ByteString data) {
public void writeObject(String name, Object data) {
var file = Path.of(root, name);
if (!Paths.get(root).toFile().isDirectory()
&& !Paths.get(root).toFile().mkdirs())
throw new StatusRuntimeException(Status.INTERNAL);
try {
file.toFile().createNewFile();
try (var fc = new FileOutputStream(file.toFile())) {
if (fc.getChannel().write(data.asReadOnlyByteBuffer()) != data.size())
throw new StatusRuntimeException(Status.INTERNAL.withDescription("Could not write all bytes to file"));
try (var fc = new FileOutputStream(file.toFile(), false);
ObjectOutputStream out = new ObjectOutputStream(fc)) {
out.writeObject(data);
} catch (final IOException ex) {
throw new SerializationException(ex);
}
} catch (IOException e) {
} catch (Exception e) {
Log.error("Error writing file " + file, e);
throw new StatusRuntimeException(Status.INTERNAL);
}

View File

@@ -1,7 +1,5 @@
package com.usatiuk.dhfs.storage.objects.repository.persistence;
import com.google.protobuf.ByteString;
import javax.annotation.Nonnull;
import java.util.List;
@@ -12,7 +10,7 @@ public interface ObjectPersistentStore {
Boolean existsObject(String name);
@Nonnull
ByteString readObject(String name);
void writeObject(String name, ByteString data);
Object readObject(String name);
void writeObject(String name, Object data);
void deleteObject(String name);
}