From d085b9f80dbb3e0e0229846828daeb00908eaec4 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Fri, 3 May 2019 21:18:04 +0400 Subject: [PATCH] Added template to extending storage --- .../io/github/chronosx88/GunJava/Client.java | 8 ++++--- .../io/github/chronosx88/GunJava/HAM.java | 22 ++++++++++--------- .../chronosx88/GunJava/MainClientServer.java | 1 - .../io/github/chronosx88/GunJava/Node.java | 3 ++- .../io/github/chronosx88/GunJava/Server.java | 7 +++--- .../io/github/chronosx88/GunJava/Utils.java | 10 +++++---- .../MemoryBackend.java} | 9 ++++---- .../storageBackends/StorageBackend.java | 20 +++++++++++++++++ 8 files changed, 54 insertions(+), 26 deletions(-) rename src/main/java/io/github/chronosx88/GunJava/{Graph.java => storageBackends/MemoryBackend.java} (88%) create mode 100644 src/main/java/io/github/chronosx88/GunJava/storageBackends/StorageBackend.java diff --git a/src/main/java/io/github/chronosx88/GunJava/Client.java b/src/main/java/io/github/chronosx88/GunJava/Client.java index 62debd0..e353580 100644 --- a/src/main/java/io/github/chronosx88/GunJava/Client.java +++ b/src/main/java/io/github/chronosx88/GunJava/Client.java @@ -1,5 +1,7 @@ package io.github.chronosx88.GunJava; +import io.github.chronosx88.GunJava.storageBackends.MemoryBackend; +import io.github.chronosx88.GunJava.storageBackends.StorageBackend; import org.java_websocket.client.WebSocketClient; import org.java_websocket.handshake.ServerHandshake; import org.json.JSONObject; @@ -10,7 +12,7 @@ import java.net.URISyntaxException; public class Client extends WebSocketClient { private Dup dup = new Dup(); - private Graph graph = new Graph(); + private StorageBackend graph = new MemoryBackend(); public Client(InetAddress address, int port) throws URISyntaxException { super(new URI("ws://" + address.getHostAddress() + ":" + port)); @@ -56,10 +58,10 @@ public class Client extends WebSocketClient { if(dup.check(msg.getString("#"))){ return; } dup.track(msg.getString("#")); if(msg.opt("put") != null) { - HAM.mix(new Graph(msg.getJSONObject("put")), graph); + HAM.mix(new MemoryBackend(msg.getJSONObject("put")), graph); } if(msg.opt("get") != null) { - Graph getResults = Utils.getRequest(msg.getJSONObject("get"), graph); + MemoryBackend getResults = Utils.getRequest(msg.getJSONObject("get"), graph); JSONObject ack = new JSONObject() .put("#", dup.track(Dup.random())) .put("@", msg.getString("#")) diff --git a/src/main/java/io/github/chronosx88/GunJava/HAM.java b/src/main/java/io/github/chronosx88/GunJava/HAM.java index 04786b3..0e9d022 100644 --- a/src/main/java/io/github/chronosx88/GunJava/HAM.java +++ b/src/main/java/io/github/chronosx88/GunJava/HAM.java @@ -1,5 +1,7 @@ package io.github.chronosx88.GunJava; +import io.github.chronosx88.GunJava.storageBackends.MemoryBackend; +import io.github.chronosx88.GunJava.storageBackends.StorageBackend; import org.json.JSONObject; import java.util.Map; @@ -53,9 +55,9 @@ public class HAM { return result; } - public static Graph mix(Graph change, Graph data) { + public static MemoryBackend mix(MemoryBackend change, StorageBackend data) { long machine = System.currentTimeMillis(); - Graph diff = null; + MemoryBackend diff = null; for(Map.Entry entry : change.entries()) { Node node = entry.getValue(); for(String key : node.values.keySet()) { @@ -65,7 +67,7 @@ public class HAM { long was = -1; Object known = null; if(data == null) { - data = new Graph(); + data = new MemoryBackend(); } if(data.hasNode(node.soul)) { if(data.getNode(node.soul).states.opt(key) != null) { @@ -79,14 +81,14 @@ public class HAM { if(ham.defer) { System.out.println("DEFER: " + key + " " + value); // Hack for accessing value in lambda without making the variable final - Graph[] graph = new Graph[] {data}; + StorageBackend[] graph = new StorageBackend[] {data}; Utils.setTimeout(() -> mix(node, graph[0]), (int) (state - System.currentTimeMillis())); } continue; } if(diff == null) { - diff = new Graph(); + diff = new MemoryBackend(); } if(!diff.hasNode(node.soul)) { @@ -108,9 +110,9 @@ public class HAM { return diff; } - public static Graph mix(Node incomingNode, Graph data) { + public static MemoryBackend mix(Node incomingNode, StorageBackend data) { long machine = System.currentTimeMillis(); - Graph diff = null; + MemoryBackend diff = null; for(String key : incomingNode.values.keySet()) { Object value = incomingNode.values.get(key); @@ -119,7 +121,7 @@ public class HAM { long was = -1; Object known = null; if(data == null) { - data = new Graph(); + data = new MemoryBackend(); } if(data.hasNode(incomingNode.soul)) { if(data.getNode(incomingNode.soul).states.opt(key) != null) { @@ -133,14 +135,14 @@ public class HAM { if(ham.defer) { System.out.println("DEFER: " + key + " " + value); // Hack for accessing value in lambda without making the variable final - Graph[] graph = new Graph[] {data}; + StorageBackend[] graph = new StorageBackend[] {data}; Utils.setTimeout(() -> mix(incomingNode, graph[0]), (int) (state - System.currentTimeMillis())); } continue; } if(diff == null) { - diff = new Graph(); + diff = new MemoryBackend(); } if(!diff.hasNode(incomingNode.soul)) { diff --git a/src/main/java/io/github/chronosx88/GunJava/MainClientServer.java b/src/main/java/io/github/chronosx88/GunJava/MainClientServer.java index fdf014f..9c683e8 100644 --- a/src/main/java/io/github/chronosx88/GunJava/MainClientServer.java +++ b/src/main/java/io/github/chronosx88/GunJava/MainClientServer.java @@ -1,7 +1,6 @@ package io.github.chronosx88.GunJava; import java.net.Inet4Address; -import java.net.InetAddress; import java.net.URISyntaxException; import java.net.UnknownHostException; diff --git a/src/main/java/io/github/chronosx88/GunJava/Node.java b/src/main/java/io/github/chronosx88/GunJava/Node.java index 3d4c576..5c15a02 100644 --- a/src/main/java/io/github/chronosx88/GunJava/Node.java +++ b/src/main/java/io/github/chronosx88/GunJava/Node.java @@ -1,5 +1,6 @@ package io.github.chronosx88.GunJava; +import io.github.chronosx88.GunJava.storageBackends.MemoryBackend; import org.json.JSONObject; public class Node implements Comparable { @@ -44,7 +45,7 @@ public class Node implements Comparable { return values.optJSONObject(key) != null; } - public Node getNode(String key, Graph g) { + public Node getNode(String key, MemoryBackend g) { String soulRef = values.getJSONObject(key).getString("#"); return g.getNode(soulRef); } diff --git a/src/main/java/io/github/chronosx88/GunJava/Server.java b/src/main/java/io/github/chronosx88/GunJava/Server.java index a9ee1a4..e06da0c 100644 --- a/src/main/java/io/github/chronosx88/GunJava/Server.java +++ b/src/main/java/io/github/chronosx88/GunJava/Server.java @@ -1,5 +1,6 @@ package io.github.chronosx88.GunJava; +import io.github.chronosx88.GunJava.storageBackends.MemoryBackend; import org.java_websocket.WebSocket; import org.java_websocket.handshake.ClientHandshake; import org.java_websocket.server.WebSocketServer; @@ -11,7 +12,7 @@ import java.util.Timer; public class Server extends WebSocketServer { private Timer timer = new Timer(true); private Dup dup = new Dup(); - private Graph graph = new Graph(); + private MemoryBackend graph = new MemoryBackend(); public Server(int port) { super(new InetSocketAddress(port)); @@ -34,10 +35,10 @@ public class Server extends WebSocketServer { if(dup.check(msg.getString("#"))) { return; } dup.track(msg.getString("#")); if(msg.opt("put") != null) { - HAM.mix(new Graph(msg.getJSONObject("put")), graph); + HAM.mix(new MemoryBackend(msg.getJSONObject("put")), graph); } if(msg.opt("get") != null) { - Graph result = Utils.getRequest(msg.optJSONObject("get"), graph); + MemoryBackend result = Utils.getRequest(msg.optJSONObject("get"), graph); if(!result.isEmpty()) { JSONObject ack = new JSONObject(); emit(ack diff --git a/src/main/java/io/github/chronosx88/GunJava/Utils.java b/src/main/java/io/github/chronosx88/GunJava/Utils.java index 13133d0..8a549d3 100644 --- a/src/main/java/io/github/chronosx88/GunJava/Utils.java +++ b/src/main/java/io/github/chronosx88/GunJava/Utils.java @@ -1,5 +1,7 @@ package io.github.chronosx88.GunJava; +import io.github.chronosx88.GunJava.storageBackends.MemoryBackend; +import io.github.chronosx88.GunJava.storageBackends.StorageBackend; import org.json.JSONObject; public class Utils { @@ -26,18 +28,18 @@ public class Utils { return new Node(data); } - public static Graph getRequest(JSONObject lex, Graph graph) { + public static MemoryBackend getRequest(JSONObject lex, StorageBackend graph) { String soul = lex.getString("#"); String key = lex.optString(".", null); Node node = graph.getNode(soul); Object tmp; if(node == null) { - return new Graph(); + return new MemoryBackend(); } if(key != null) { tmp = node.values.opt(key); if(tmp == null) { - return new Graph(); + return new MemoryBackend(); } Node node1 = new Node(node.toJSONObject()); node = Utils.newNode(node.soul, new JSONObject()); @@ -46,7 +48,7 @@ public class Utils { JSONObject tmpStates = node1.states; node.states.put(key, tmpStates.get(key)); } - Graph ack = new Graph(); + MemoryBackend ack = new MemoryBackend(); ack.addNode(soul, node); return ack; } diff --git a/src/main/java/io/github/chronosx88/GunJava/Graph.java b/src/main/java/io/github/chronosx88/GunJava/storageBackends/MemoryBackend.java similarity index 88% rename from src/main/java/io/github/chronosx88/GunJava/Graph.java rename to src/main/java/io/github/chronosx88/GunJava/storageBackends/MemoryBackend.java index 34f2742..e55172f 100644 --- a/src/main/java/io/github/chronosx88/GunJava/Graph.java +++ b/src/main/java/io/github/chronosx88/GunJava/storageBackends/MemoryBackend.java @@ -1,21 +1,22 @@ -package io.github.chronosx88.GunJava; +package io.github.chronosx88.GunJava.storageBackends; +import io.github.chronosx88.GunJava.Node; import org.json.JSONObject; import java.util.*; -public class Graph { +public class MemoryBackend implements StorageBackend { private final HashMap nodes; - public Graph(JSONObject source) { + public MemoryBackend(JSONObject source) { nodes = new LinkedHashMap<>(); for (String soul : source.keySet()) nodes.put(soul, new Node(source.getJSONObject(soul))); } - public Graph() { + public MemoryBackend() { nodes = new LinkedHashMap<>(); } diff --git a/src/main/java/io/github/chronosx88/GunJava/storageBackends/StorageBackend.java b/src/main/java/io/github/chronosx88/GunJava/storageBackends/StorageBackend.java new file mode 100644 index 0000000..73c35b5 --- /dev/null +++ b/src/main/java/io/github/chronosx88/GunJava/storageBackends/StorageBackend.java @@ -0,0 +1,20 @@ +package io.github.chronosx88.GunJava.storageBackends; + +import io.github.chronosx88.GunJava.Node; +import org.json.JSONObject; + +import java.util.Collection; +import java.util.Map; +import java.util.Set; + +public interface StorageBackend { + Node getNode(String soul); + void addNode(String soul, Node node); + boolean hasNode(String soul); + Set> entries(); + Collection nodes(); + String toString(); + String toPrettyString(); + JSONObject toJSONObject(); + boolean isEmpty(); +}