mirror of
https://github.com/usatiuk/dhfs.git
synced 2025-10-29 04:57:48 +01:00
write objects directly
This commit is contained in:
@@ -1,8 +1,6 @@
|
|||||||
package com.usatiuk.dhfs.storage.objects.jrepository;
|
package com.usatiuk.dhfs.storage.objects.jrepository;
|
||||||
|
|
||||||
import com.google.common.collect.Streams;
|
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.ObjectMetadata;
|
||||||
import com.usatiuk.dhfs.storage.objects.repository.distributed.PersistentRemoteHostsService;
|
import com.usatiuk.dhfs.storage.objects.repository.distributed.PersistentRemoteHostsService;
|
||||||
import com.usatiuk.dhfs.storage.objects.repository.persistence.ObjectPersistentStore;
|
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);
|
if (inMap != null) return Optional.of(inMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteString readMd;
|
Object readMd;
|
||||||
try {
|
try {
|
||||||
readMd = objectPersistentStore.readObject("meta_" + name);
|
readMd = objectPersistentStore.readObject("meta_" + name);
|
||||||
} catch (StatusRuntimeException ex) {
|
} catch (StatusRuntimeException ex) {
|
||||||
@@ -109,8 +107,7 @@ public class JObjectManagerImpl implements JObjectManager {
|
|||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
throw ex;
|
throw ex;
|
||||||
}
|
}
|
||||||
var meta = SerializationHelper.deserialize(readMd);
|
if (!(readMd instanceof ObjectMetadata meta))
|
||||||
if (!(meta instanceof ObjectMetadata))
|
|
||||||
throw new StatusRuntimeException(Status.DATA_LOSS.withDescription("Unexpected metadata type for " + name));
|
throw new StatusRuntimeException(Status.DATA_LOSS.withDescription("Unexpected metadata type for " + name));
|
||||||
|
|
||||||
if (((ObjectMetadata) meta).isDeleted()) {
|
if (((ObjectMetadata) meta).isDeleted()) {
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ public class JObjectResolver {
|
|||||||
public <T extends JObjectData> Optional<T> resolveDataLocal(JObject<T> jObject) {
|
public <T extends JObjectData> Optional<T> resolveDataLocal(JObject<T> jObject) {
|
||||||
jObject.assertRWLock();
|
jObject.assertRWLock();
|
||||||
if (objectPersistentStore.existsObject(jObject.getName()))
|
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();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -181,9 +181,9 @@ public class JObjectWriteback {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
m.markWritten();
|
m.markWritten();
|
||||||
objectPersistentStore.writeObject("meta_" + m.getName(), SerializationHelper.serialize(m));
|
objectPersistentStore.writeObject("meta_" + m.getName(), m);
|
||||||
if (data != null)
|
if (data != null)
|
||||||
objectPersistentStore.writeObject(m.getName(), SerializationHelper.serialize(data));
|
objectPersistentStore.writeObject(m.getName(), data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void remove(JObject<?> object) {
|
public void remove(JObject<?> object) {
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package com.usatiuk.dhfs.storage.objects.repository.persistence;
|
package com.usatiuk.dhfs.storage.objects.repository.persistence;
|
||||||
|
|
||||||
import com.google.protobuf.ByteString;
|
import com.usatiuk.dhfs.storage.SerializationHelper;
|
||||||
import com.google.protobuf.UnsafeByteOperations;
|
|
||||||
import io.grpc.Status;
|
import io.grpc.Status;
|
||||||
import io.grpc.StatusRuntimeException;
|
import io.grpc.StatusRuntimeException;
|
||||||
import io.quarkus.logging.Log;
|
import io.quarkus.logging.Log;
|
||||||
@@ -10,11 +9,11 @@ import io.quarkus.runtime.StartupEvent;
|
|||||||
import jakarta.annotation.Priority;
|
import jakarta.annotation.Priority;
|
||||||
import jakarta.enterprise.context.ApplicationScoped;
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
import jakarta.enterprise.event.Observes;
|
import jakarta.enterprise.event.Observes;
|
||||||
|
import org.apache.commons.lang3.SerializationException;
|
||||||
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import java.io.FileOutputStream;
|
import java.io.*;
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.NoSuchFileException;
|
import java.nio.file.NoSuchFileException;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
@@ -65,14 +64,13 @@ public class FileObjectPersistentStore implements ObjectPersistentStore {
|
|||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public ByteString readObject(String name) {
|
public Object readObject(String name) {
|
||||||
var file = Path.of(root, name);
|
var file = Path.of(root, name);
|
||||||
|
|
||||||
if (!file.toFile().exists())
|
try (FileInputStream in = new FileInputStream(file.toFile())) {
|
||||||
throw new StatusRuntimeException(Status.NOT_FOUND);
|
return SerializationHelper.deserialize(in);
|
||||||
|
} catch (FileNotFoundException f) {
|
||||||
try {
|
throw new StatusRuntimeException(Status.NOT_FOUND.withDescription("Not found: " + name));
|
||||||
return UnsafeByteOperations.unsafeWrap(Files.readAllBytes(file));
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.error("Error reading file " + file, e);
|
Log.error("Error reading file " + file, e);
|
||||||
throw new StatusRuntimeException(Status.INTERNAL);
|
throw new StatusRuntimeException(Status.INTERNAL);
|
||||||
@@ -80,20 +78,17 @@ public class FileObjectPersistentStore implements ObjectPersistentStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeObject(String name, ByteString data) {
|
public void writeObject(String name, Object data) {
|
||||||
var file = Path.of(root, name);
|
var file = Path.of(root, name);
|
||||||
|
|
||||||
if (!Paths.get(root).toFile().isDirectory()
|
|
||||||
&& !Paths.get(root).toFile().mkdirs())
|
|
||||||
throw new StatusRuntimeException(Status.INTERNAL);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
file.toFile().createNewFile();
|
try (var fc = new FileOutputStream(file.toFile(), false);
|
||||||
try (var fc = new FileOutputStream(file.toFile())) {
|
ObjectOutputStream out = new ObjectOutputStream(fc)) {
|
||||||
if (fc.getChannel().write(data.asReadOnlyByteBuffer()) != data.size())
|
out.writeObject(data);
|
||||||
throw new StatusRuntimeException(Status.INTERNAL.withDescription("Could not write all bytes to file"));
|
} catch (final IOException ex) {
|
||||||
|
throw new SerializationException(ex);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (Exception e) {
|
||||||
Log.error("Error writing file " + file, e);
|
Log.error("Error writing file " + file, e);
|
||||||
throw new StatusRuntimeException(Status.INTERNAL);
|
throw new StatusRuntimeException(Status.INTERNAL);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package com.usatiuk.dhfs.storage.objects.repository.persistence;
|
package com.usatiuk.dhfs.storage.objects.repository.persistence;
|
||||||
|
|
||||||
import com.google.protobuf.ByteString;
|
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -12,7 +10,7 @@ public interface ObjectPersistentStore {
|
|||||||
Boolean existsObject(String name);
|
Boolean existsObject(String name);
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
ByteString readObject(String name);
|
Object readObject(String name);
|
||||||
void writeObject(String name, ByteString data);
|
void writeObject(String name, Object data);
|
||||||
void deleteObject(String name);
|
void deleteObject(String name);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user