mirror of
https://github.com/usatiuk/dhfs.git
synced 2025-10-28 20:47:49 +01:00
find files
This commit is contained in:
@@ -1,16 +0,0 @@
|
||||
package com.usatiuk.dhfs.storage;
|
||||
|
||||
import io.quarkus.grpc.GrpcService;
|
||||
|
||||
import io.smallrye.mutiny.Uni;
|
||||
|
||||
@GrpcService
|
||||
public class HelloGrpcService implements HelloGrpc {
|
||||
|
||||
@Override
|
||||
public Uni<HelloReply> sayHello(HelloRequest request) {
|
||||
return Uni.createFrom().item("Hello " + request.getName() + "!")
|
||||
.map(msg -> HelloReply.newBuilder().setMessage(msg).build());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.usatiuk.dhfs.storage.api;
|
||||
|
||||
import com.usatiuk.dhfs.storage.repository.ObjectRepository;
|
||||
import io.quarkus.grpc.GrpcService;
|
||||
import io.smallrye.mutiny.Uni;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
@GrpcService
|
||||
public class DhfsObjectGrpcService implements DhfsObjectGrpc {
|
||||
@Inject
|
||||
ObjectRepository objectRepository;
|
||||
|
||||
@Override
|
||||
public Uni<FindObjectsReply> findObjects(FindObjectsRequest request) {
|
||||
return objectRepository.findObjects(request.getNamespace(), request.getPrefix())
|
||||
.map(m -> FindObjectsReply.FindObjectsEntry.newBuilder().setName(m).build())
|
||||
.collect().in(FindObjectsReply::newBuilder, FindObjectsReply.Builder::addFound)
|
||||
.map(FindObjectsReply.Builder::build);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Uni<ReadObjectReply> readObject(ReadObjectRequest request) {
|
||||
return Uni.createFrom().item("Hello ")
|
||||
.map(msg -> ReadObjectReply.newBuilder().build());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Uni<WriteObjectReply> writeObject(WriteObjectRequest request) {
|
||||
return Uni.createFrom().item("Hello ")
|
||||
.map(msg -> WriteObjectReply.newBuilder().build());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.usatiuk.dhfs.storage.data;
|
||||
|
||||
public class Namespace {
|
||||
String name;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.usatiuk.dhfs.storage.data;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class Object {
|
||||
Namespace namespace;
|
||||
|
||||
String name;
|
||||
ByteBuffer data;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.usatiuk.dhfs.storage.repository;
|
||||
|
||||
import com.usatiuk.dhfs.storage.data.Object;
|
||||
import io.smallrye.mutiny.Multi;
|
||||
import io.smallrye.mutiny.Uni;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public interface ObjectRepository {
|
||||
@Nonnull
|
||||
public Multi<String> findObjects(String namespace, String prefix);
|
||||
|
||||
@Nonnull
|
||||
public Uni<Object> readObject(String namespace, String name);
|
||||
@Nonnull
|
||||
public Uni<Void> writeObject(String namespace, String name, Object data);
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.usatiuk.dhfs.storage.repository;
|
||||
|
||||
import com.usatiuk.dhfs.storage.data.Object;
|
||||
import io.quarkus.logging.Log;
|
||||
import io.quarkus.runtime.Shutdown;
|
||||
import io.quarkus.runtime.Startup;
|
||||
import io.smallrye.mutiny.Multi;
|
||||
import io.smallrye.mutiny.Uni;
|
||||
import io.vertx.mutiny.core.Vertx;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
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;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ApplicationScoped
|
||||
public class SimpleFileObjectRepository implements ObjectRepository {
|
||||
@ConfigProperty(name = "dhfs.filerepo.root")
|
||||
String root;
|
||||
|
||||
@Inject
|
||||
Vertx vertx;
|
||||
|
||||
@Startup
|
||||
void init() {
|
||||
if (!Paths.get(root).toFile().exists()) {
|
||||
Paths.get(root).toFile().mkdirs();
|
||||
Log.info("Creted root " + root);
|
||||
}
|
||||
|
||||
Log.info("Initializing with root " + root);
|
||||
}
|
||||
|
||||
@Shutdown
|
||||
void shutdown() {
|
||||
Log.info("Shutdown");
|
||||
}
|
||||
|
||||
private Multi<String> TraverseDir(String dir) {
|
||||
return vertx.fileSystem().readDir(dir).onItem().transformToMulti(
|
||||
t -> {
|
||||
List<Multi<String>> results = new ArrayList<>();
|
||||
t.forEach(entry -> {
|
||||
if (Paths.get(entry).toFile().isDirectory()) {
|
||||
results.add(TraverseDir(Paths.get(entry).toString()));
|
||||
} else {
|
||||
results.add(Multi.createFrom().item(entry));
|
||||
}
|
||||
});
|
||||
return Multi.createBy().merging().streams(results);
|
||||
}
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Multi<String> findObjects(String namespace, String prefix) {
|
||||
Path path = Paths.get(root, namespace, prefix);
|
||||
Path rootDir = path.toFile().isDirectory() ? path : path.getParent();
|
||||
String prefixInRoot = path.toFile().isDirectory() ? "" : path.getFileName().toString();
|
||||
return vertx.fileSystem().readDir(rootDir.toString()).onItem().transformToMulti(
|
||||
t -> {
|
||||
List<Multi<String>> results = new ArrayList<>();
|
||||
t.forEach(entry -> {
|
||||
if (entry.startsWith(prefixInRoot)) {
|
||||
if (Paths.get(entry).toFile().isDirectory()) {
|
||||
results.add(TraverseDir(Paths.get(entry).toString()));
|
||||
} else {
|
||||
results.add(Multi.createFrom().item(entry));
|
||||
}
|
||||
}
|
||||
});
|
||||
return Multi.createBy().merging().streams(results);
|
||||
}
|
||||
).map(f -> rootDir.relativize(Paths.get(f)).toString());
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Uni<Object> readObject(String namespace, String name) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Uni<Void> writeObject(String namespace, String name, Object data) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
55
server/src/main/proto/dhfs_objects.proto
Normal file
55
server/src/main/proto/dhfs_objects.proto
Normal file
@@ -0,0 +1,55 @@
|
||||
syntax = "proto3";
|
||||
|
||||
option java_multiple_files = true;
|
||||
option java_package = "com.usatiuk.dhfs.storage.api";
|
||||
option java_outer_classname = "DhfsStorageApi";
|
||||
|
||||
package hello;
|
||||
|
||||
service DhfsObjectGrpc {
|
||||
rpc FindObjects (FindObjectsRequest) returns (FindObjectsReply) {}
|
||||
rpc ReadObject (ReadObjectRequest) returns (ReadObjectReply) {}
|
||||
rpc WriteObject (WriteObjectRequest) returns (WriteObjectReply) {}
|
||||
rpc DeleteObject (DeleteObjectRequest) returns (DeleteObjectReply) {}
|
||||
}
|
||||
|
||||
message FindObjectsRequest {
|
||||
string namespace = 1;
|
||||
string prefix = 2;
|
||||
}
|
||||
|
||||
message FindObjectsReply {
|
||||
message FindObjectsEntry {
|
||||
string name = 1;
|
||||
}
|
||||
repeated FindObjectsEntry found = 1;
|
||||
}
|
||||
|
||||
message ReadObjectRequest {
|
||||
string namespace = 1;
|
||||
string name = 2;
|
||||
}
|
||||
|
||||
message ReadObjectReply {
|
||||
string message = 1;
|
||||
bytes data = 10;
|
||||
}
|
||||
|
||||
message WriteObjectRequest {
|
||||
string namespace = 1;
|
||||
string name = 2;
|
||||
bytes data = 10;
|
||||
}
|
||||
|
||||
message WriteObjectReply {
|
||||
bool ok = 1;
|
||||
}
|
||||
|
||||
message DeleteObjectRequest {
|
||||
string namespace = 1;
|
||||
string name = 2;
|
||||
}
|
||||
|
||||
message DeleteObjectReply {
|
||||
bool ok = 1;
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
syntax = "proto3";
|
||||
|
||||
option java_multiple_files = true;
|
||||
option java_package = "com.usatiuk.dhfs.storage";
|
||||
option java_outer_classname = "HelloGrpcProto";
|
||||
|
||||
package hello;
|
||||
|
||||
service HelloGrpc {
|
||||
rpc SayHello (HelloRequest) returns (HelloReply) {}
|
||||
}
|
||||
|
||||
message HelloRequest {
|
||||
string name = 1;
|
||||
}
|
||||
|
||||
message HelloReply {
|
||||
string message = 1;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
quarkus.grpc.server.use-separate-server=false
|
||||
dhfs.filerepo.root=${HOME}/dhfs_root
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.usatiuk.dhfs.storage;
|
||||
|
||||
import com.usatiuk.dhfs.storage.api.DhfsObjectGrpc;
|
||||
import com.usatiuk.dhfs.storage.api.FindObjectsReply;
|
||||
import com.usatiuk.dhfs.storage.api.FindObjectsRequest;
|
||||
import io.quarkus.grpc.GrpcClient;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
@QuarkusTest
|
||||
class DhfsObjectGrpcService {
|
||||
@GrpcClient
|
||||
DhfsObjectGrpc dhfsObjectGrpc;
|
||||
|
||||
@ConfigProperty(name = "dhfs.filerepo.root")
|
||||
String tempDirectory;
|
||||
|
||||
@Test
|
||||
void testFind() {
|
||||
FindObjectsReply reply = dhfsObjectGrpc
|
||||
.findObjects(FindObjectsRequest.newBuilder().setNamespace("TestNs").build()).await().atMost(Duration.ofSeconds(5));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.usatiuk.dhfs.storage;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import io.quarkus.grpc.GrpcClient;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@QuarkusTest
|
||||
class HelloGrpcServiceTest {
|
||||
@GrpcClient
|
||||
HelloGrpc helloGrpc;
|
||||
|
||||
@Test
|
||||
void testHello() {
|
||||
HelloReply reply = helloGrpc
|
||||
.sayHello(HelloRequest.newBuilder().setName("Neo").build()).await().atMost(Duration.ofSeconds(5));
|
||||
assertEquals("Hello Neo!", reply.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.usatiuk.dhfs.storage;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
|
||||
@QuarkusTest
|
||||
public abstract class SimpleFileRepoTest {
|
||||
@ConfigProperty(name = "dhfs.filerepo.root")
|
||||
String tempDirectory;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void teardown() {
|
||||
|
||||
}
|
||||
}
|
||||
1
server/src/test/resources/application.properties
Normal file
1
server/src/test/resources/application.properties
Normal file
@@ -0,0 +1 @@
|
||||
dhfs.filerepo.root=${HOME}/dhfs_root_test
|
||||
Reference in New Issue
Block a user