mirror of
https://github.com/ChronosX88/JGUN.git
synced 2024-11-21 22:12:18 +00:00
Implement referencing other nodes in graph
This commit is contained in:
parent
bcda85a8b1
commit
08912a6f67
@ -27,6 +27,10 @@ public class PathReference {
|
|||||||
this.storageManager = storageManager;
|
this.storageManager = storageManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String[] getPath() {
|
||||||
|
return path.toArray(new String[0]);
|
||||||
|
}
|
||||||
|
|
||||||
public PathReference get(String key) {
|
public PathReference get(String key) {
|
||||||
path.add(key);
|
path.add(key);
|
||||||
return this;
|
return this;
|
||||||
@ -120,4 +124,27 @@ public class PathReference {
|
|||||||
storageManager.addMapChangeListener(pathData.get(pathData.size()-1), mapListener);
|
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);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -91,6 +91,10 @@ public class NodeBuilder {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public NodeBuilder add(String name, NodeLinkValue link) {
|
||||||
|
return addScalar(name, link);
|
||||||
|
}
|
||||||
|
|
||||||
public MemoryGraph build() {
|
public MemoryGraph build() {
|
||||||
return this.graph;
|
return this.graph;
|
||||||
}
|
}
|
||||||
|
@ -18,14 +18,22 @@ public class MainClient {
|
|||||||
Storage storage = new MemoryStorage();
|
Storage storage = new MemoryStorage();
|
||||||
NetworkNode peer = new NetworkNode(Inet4Address.getByAddress(new byte[]{127, 0, 0, 1}), 5054, storage);
|
NetworkNode peer = new NetworkNode(Inet4Address.getByAddress(new byte[]{127, 0, 0, 1}), 5054, storage);
|
||||||
Gun gun = new Gun(storage, peer);
|
Gun gun = new Gun(storage, peer);
|
||||||
|
|
||||||
Result result = gun.get("person").put(new NodeBuilder()
|
Result result = gun.get("person").put(new NodeBuilder()
|
||||||
.add("firstName", "ABCD")
|
.add("firstName", "Test")
|
||||||
.build()).get();
|
.build()).get();
|
||||||
System.out.println(result);
|
System.out.println(result);
|
||||||
|
|
||||||
result = gun.get("person").get("address").put(new NodeBuilder()
|
result = gun.get("person").get("address").put(new NodeBuilder()
|
||||||
.add("city", "HUY")
|
.add("city", "Dallas")
|
||||||
.add("ZIP", new NodeBuilder()
|
.build()).get();
|
||||||
.add("post", "pochta rossii"))
|
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();
|
.build()).get();
|
||||||
System.out.println(result);
|
System.out.println(result);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user