Implement referencing other nodes in graph

This commit is contained in:
ChronosX88 2023-11-17 22:20:15 +03:00
parent bcda85a8b1
commit 08912a6f67
3 changed files with 43 additions and 4 deletions

View File

@ -27,6 +27,10 @@ public class PathReference {
this.storageManager = storageManager;
}
public String[] getPath() {
return path.toArray(new String[0]);
}
public PathReference get(String key) {
path.add(key);
return this;
@ -120,4 +124,27 @@ public class PathReference {
storageManager.addMapChangeListener(pathData.get(pathData.size()-1), mapListener);
});
}
public CompletableFuture<Result> put(PathReference nodeReference) {
String[] pathToAnotherNode = nodeReference.getPath();
return CompletableFuture.supplyAsync(() -> {
try {
return storageManager.getPathData(pathToAnotherNode);
} catch (TimeoutException | ExecutionException | InterruptedException e) {
throw new CompletionException(e);
}
}).thenComposeAsync(pathData -> {
if (pathData.size() < pathToAnotherNode.length) {
return CompletableFuture.failedFuture(new IllegalArgumentException("target node not found"));
}
String nodeId = pathData.get(pathData.size()-1);
MemoryGraph graph = new NodeBuilder()
.add(path.get(path.size() - 1), NodeLinkValue.builder()
.link(nodeId)
.build())
.build();
if (path.size() > 1) this.path.remove(path.size()-1);
return this.put(graph);
});
}
}

View File

@ -91,6 +91,10 @@ public class NodeBuilder {
return this;
}
public NodeBuilder add(String name, NodeLinkValue link) {
return addScalar(name, link);
}
public MemoryGraph build() {
return this.graph;
}

View File

@ -18,14 +18,22 @@ public class MainClient {
Storage storage = new MemoryStorage();
NetworkNode peer = new NetworkNode(Inet4Address.getByAddress(new byte[]{127, 0, 0, 1}), 5054, storage);
Gun gun = new Gun(storage, peer);
Result result = gun.get("person").put(new NodeBuilder()
.add("firstName", "ABCD")
.add("firstName", "Test")
.build()).get();
System.out.println(result);
result = gun.get("person").get("address").put(new NodeBuilder()
.add("city", "HUY")
.add("ZIP", new NodeBuilder()
.add("post", "pochta rossii"))
.add("city", "Dallas")
.build()).get();
System.out.println(result);
result = gun.get("person").get("homeAddress").put(gun.get("person").get("address")).get();
System.out.println(result);
result = gun.get("person").get("homeAddress").put(new NodeBuilder()
.add("city", "New YORK CITY")
.build()).get();
System.out.println(result);
}