diff --git a/src/main/java/io/github/chronosx88/JGUN/api/PathReference.java b/src/main/java/io/github/chronosx88/JGUN/api/PathReference.java index 96dbd7f..791d635 100644 --- a/src/main/java/io/github/chronosx88/JGUN/api/PathReference.java +++ b/src/main/java/io/github/chronosx88/JGUN/api/PathReference.java @@ -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 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); + }); + } } diff --git a/src/main/java/io/github/chronosx88/JGUN/api/graph/NodeBuilder.java b/src/main/java/io/github/chronosx88/JGUN/api/graph/NodeBuilder.java index 05496b3..b7a2181 100644 --- a/src/main/java/io/github/chronosx88/JGUN/api/graph/NodeBuilder.java +++ b/src/main/java/io/github/chronosx88/JGUN/api/graph/NodeBuilder.java @@ -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; } diff --git a/src/main/java/io/github/chronosx88/JGUN/examples/MainClient.java b/src/main/java/io/github/chronosx88/JGUN/examples/MainClient.java index 8021cf9..749f4b6 100644 --- a/src/main/java/io/github/chronosx88/JGUN/examples/MainClient.java +++ b/src/main/java/io/github/chronosx88/JGUN/examples/MainClient.java @@ -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); }