mirror of
https://github.com/usatiuk/dhfs.git
synced 2025-10-28 20:47:49 +01:00
refactor object persistence out of repository
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package com.usatiuk.dhfs.storage.objects.repository;
|
||||
|
||||
import com.usatiuk.dhfs.storage.objects.data.Object;
|
||||
import com.usatiuk.dhfs.storage.objects.repository.persistence.ObjectPersistentStore;
|
||||
import io.smallrye.mutiny.Multi;
|
||||
import io.smallrye.mutiny.Uni;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
@ApplicationScoped
|
||||
public class SimplePersistentObjectRepository implements ObjectRepository {
|
||||
@Inject
|
||||
ObjectPersistentStore objectPersistentStore;
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Multi<String> findObjects(String namespace, String prefix) {
|
||||
return objectPersistentStore.findObjects(namespace, prefix);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Uni<Boolean> existsObject(String namespace, String name) {
|
||||
return objectPersistentStore.existsObject(namespace, name);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Uni<Object> readObject(String namespace, String name) {
|
||||
return objectPersistentStore.readObject(namespace, name);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Uni<Void> writeObject(String namespace, Object object) {
|
||||
return objectPersistentStore.writeObject(namespace, object);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Uni<Void> deleteObject(String namespace, String name) {
|
||||
return objectPersistentStore.deleteObject(namespace, name);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Uni<Void> createNamespace(String namespace) {
|
||||
return objectPersistentStore.createNamespace(namespace);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Uni<Void> deleteNamespace(String namespace) {
|
||||
return objectPersistentStore.deleteNamespace(namespace);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package com.usatiuk.dhfs.storage.objects.repository.persistence;
|
||||
|
||||
import com.usatiuk.dhfs.storage.objects.data.Namespace;
|
||||
import com.usatiuk.dhfs.storage.objects.data.Object;
|
||||
import io.grpc.Status;
|
||||
import io.grpc.StatusRuntimeException;
|
||||
import io.quarkus.logging.Log;
|
||||
import io.quarkus.runtime.Shutdown;
|
||||
import io.quarkus.runtime.StartupEvent;
|
||||
import io.smallrye.mutiny.Multi;
|
||||
import io.smallrye.mutiny.Uni;
|
||||
import io.vertx.mutiny.core.Vertx;
|
||||
import io.vertx.mutiny.core.buffer.Buffer;
|
||||
import jakarta.annotation.Priority;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.event.Observes;
|
||||
import jakarta.inject.Inject;
|
||||
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
@ApplicationScoped
|
||||
public class FileObjectPersistentStore implements ObjectPersistentStore {
|
||||
@ConfigProperty(name = "dhfs.objects.persistence.files.root")
|
||||
String root;
|
||||
|
||||
@Inject
|
||||
Vertx vertx;
|
||||
|
||||
void init(@Observes @Priority(200) StartupEvent event) {
|
||||
Paths.get(root).toFile().mkdirs();
|
||||
Log.info("Initializing with root " + root);
|
||||
}
|
||||
|
||||
@Shutdown
|
||||
void shutdown() {
|
||||
Log.info("Shutdown");
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Multi<String> findObjects(String namespace, String prefix) {
|
||||
Path nsRoot = Paths.get(root, namespace);
|
||||
|
||||
if (!nsRoot.toFile().isDirectory())
|
||||
throw new StatusRuntimeException(Status.NOT_FOUND);
|
||||
|
||||
return vertx.fileSystem().readDir(nsRoot.toString()).onItem()
|
||||
.transformToMulti(v -> Multi.createFrom().iterable(v))
|
||||
.select().where(n -> n.startsWith(prefix))
|
||||
.map(f -> nsRoot.relativize(Paths.get(f)).toString());
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Uni<Boolean> existsObject(String namespace, String name) {
|
||||
Path obj = Paths.get(root, namespace, name);
|
||||
|
||||
if (!obj.toFile().isFile())
|
||||
return Uni.createFrom().item(false);
|
||||
|
||||
return Uni.createFrom().item(true);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Uni<Object> readObject(String namespace, String name) {
|
||||
var file = Path.of(root, namespace, name);
|
||||
|
||||
if (!file.toFile().exists())
|
||||
throw new StatusRuntimeException(Status.NOT_FOUND);
|
||||
|
||||
return vertx.fileSystem().readFile(file.toString()).map(r -> new Object(new Namespace(namespace), name, r.getBytes()));
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Uni<Void> writeObject(String namespace, Object object) {
|
||||
var file = Path.of(root, namespace, object.getName());
|
||||
|
||||
return vertx.fileSystem().writeFile(file.toString(), Buffer.buffer(object.getData()));
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Uni<Void> deleteObject(String namespace, String name) {
|
||||
var file = Path.of(root, namespace, name);
|
||||
|
||||
if (!file.toFile().exists())
|
||||
throw new StatusRuntimeException(Status.NOT_FOUND);
|
||||
|
||||
return vertx.fileSystem().delete(file.toString());
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Uni<Void> createNamespace(String namespace) {
|
||||
if (Paths.get(root, namespace).toFile().exists())
|
||||
return Uni.createFrom().voidItem();
|
||||
if (!Paths.get(root, namespace).toFile().mkdirs())
|
||||
throw new StatusRuntimeException(Status.INTERNAL);
|
||||
return Uni.createFrom().voidItem();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Uni<Void> deleteNamespace(String namespace) {
|
||||
if (!Paths.get(root, namespace).toFile().exists())
|
||||
throw new StatusRuntimeException(Status.NOT_FOUND);
|
||||
if (!Paths.get(root, namespace).toFile().delete())
|
||||
throw new StatusRuntimeException(Status.INTERNAL);
|
||||
return Uni.createFrom().voidItem();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.usatiuk.dhfs.storage.objects.repository.persistence;
|
||||
|
||||
import com.usatiuk.dhfs.storage.objects.data.Object;
|
||||
import io.smallrye.mutiny.Multi;
|
||||
import io.smallrye.mutiny.Uni;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public interface ObjectPersistentStore {
|
||||
@Nonnull
|
||||
Multi<String> findObjects(String namespace, String prefix);
|
||||
@Nonnull
|
||||
Uni<Boolean> existsObject(String namespace, String name);
|
||||
|
||||
@Nonnull
|
||||
Uni<Object> readObject(String namespace, String name);
|
||||
@Nonnull
|
||||
Uni<Void> writeObject(String namespace, Object object);
|
||||
@Nonnull
|
||||
Uni<Void> deleteObject(String namespace, String name);
|
||||
|
||||
@Nonnull
|
||||
Uni<Void> createNamespace(String namespace);
|
||||
@Nonnull
|
||||
Uni<Void> deleteNamespace(String namespace);
|
||||
}
|
||||
@@ -1,3 +1,3 @@
|
||||
quarkus.grpc.server.use-separate-server=false
|
||||
dhfs.filerepo.root=${HOME}/dhfs_data/dhfs_root
|
||||
dhfs.objects.persistence.files.root=${HOME}/dhfs_data/dhfs_root
|
||||
dhfs.fuse.root=${HOME}/dhfs_data/dhfs_fuse_root
|
||||
@@ -10,7 +10,7 @@ import java.util.Objects;
|
||||
|
||||
@QuarkusTest
|
||||
public abstract class SimpleFileRepoTest {
|
||||
@ConfigProperty(name = "dhfs.filerepo.root")
|
||||
@ConfigProperty(name = "dhfs.objects.persistence.files.root")
|
||||
String tempDirectory;
|
||||
|
||||
void purgeDirectory(File dir) {
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
dhfs.filerepo.root=${HOME}/dhfs_data/dhfs_root_test
|
||||
dhfs.objects.persistence.files.root=${HOME}/dhfs_data/dhfs_root_test
|
||||
dhfs.fuse.root=${HOME}/dhfs_data/dhfs_fuse_root_test
|
||||
|
||||
Reference in New Issue
Block a user