Added template to extending storage

This commit is contained in:
ChronosX88 2019-05-03 21:18:04 +04:00
parent 791f29a177
commit d085b9f80d
8 changed files with 54 additions and 26 deletions

View File

@ -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("#"))

View File

@ -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<String, Node> 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)) {

View File

@ -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;

View File

@ -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<Node> {
@ -44,7 +45,7 @@ public class Node implements Comparable<Node> {
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);
}

View File

@ -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

View File

@ -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;
}

View File

@ -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<String, Node> 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<>();
}

View File

@ -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<Map.Entry<String, Node>> entries();
Collection<Node> nodes();
String toString();
String toPrettyString();
JSONObject toJSONObject();
boolean isEmpty();
}