|
| 1 | +package com.redislabs.redisgraph; |
| 2 | + |
| 3 | +import com.sun.org.apache.xerces.internal.xs.datatypes.ObjectList; |
| 4 | +import redis.clients.jedis.Jedis; |
| 5 | +import redis.clients.jedis.JedisPool; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +public class Client { |
| 12 | + private JedisPool pool; |
| 13 | + |
| 14 | + Jedis _conn() { |
| 15 | + return pool.getResource(); |
| 16 | + } |
| 17 | + |
| 18 | + public Client(String host, int port) { |
| 19 | + pool = new JedisPool(host, port); |
| 20 | + } |
| 21 | + |
| 22 | + public String createNode(String graph, String label, Object... attributes) { |
| 23 | + Jedis conn = _conn(); |
| 24 | + |
| 25 | + List<String> args = new ArrayList<String>(2 + attributes.length); |
| 26 | + args.add(graph); |
| 27 | + args.add(label); |
| 28 | + |
| 29 | + for(Object attr: attributes) { |
| 30 | + args.add(attr.toString()); |
| 31 | + } |
| 32 | + |
| 33 | + String[] stringArgs = args.toArray(new String[args.size()]); |
| 34 | + |
| 35 | + String nodeId = conn.getClient() |
| 36 | + .sendCommand(Commands.Command.CREATENODE, stringArgs) |
| 37 | + .getBulkReply(); |
| 38 | + |
| 39 | + conn.close(); |
| 40 | + return nodeId; |
| 41 | + } |
| 42 | + |
| 43 | + public String createNode(String graph, Object... attributes) { |
| 44 | + Jedis conn = _conn(); |
| 45 | + |
| 46 | + List<String> args = new ArrayList<String>(1 + attributes.length); |
| 47 | + args.add(graph); |
| 48 | + |
| 49 | + for(Object attr: attributes) { |
| 50 | + args.add(attr.toString()); |
| 51 | + } |
| 52 | + |
| 53 | + String[] stringArgs = args.toArray(new String[args.size()]); |
| 54 | + |
| 55 | + String nodeId = conn.getClient() |
| 56 | + .sendCommand(Commands.Command.CREATENODE, stringArgs) |
| 57 | + .getBulkReply(); |
| 58 | + |
| 59 | + conn.close(); |
| 60 | + return nodeId; |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + public String connectNodes(String graph, String srcNodeID, String relation, String destNodeID, Object... attributes) { |
| 65 | + Jedis conn = _conn(); |
| 66 | + |
| 67 | + List<Object> args = new ArrayList<Object>(4 + attributes.length); |
| 68 | + args.add(graph); |
| 69 | + args.add(srcNodeID); |
| 70 | + args.add(relation); |
| 71 | + args.add(destNodeID); |
| 72 | + |
| 73 | + for(Object attr: attributes) { |
| 74 | + args.add(attr); |
| 75 | + } |
| 76 | + |
| 77 | + String[] stringArgs = args.toArray(new String[args.size()]); |
| 78 | + |
| 79 | + String edgeId = conn.getClient() |
| 80 | + .sendCommand(Commands.Command.ADDEDGE, stringArgs) |
| 81 | + .getBulkReply(); |
| 82 | + conn.close(); |
| 83 | + return edgeId; |
| 84 | + } |
| 85 | + |
| 86 | + private Map<String, String> getGraphEntity(String id) { |
| 87 | + Jedis conn = _conn(); |
| 88 | + Map<String, String> properties = conn.hgetAll(id); |
| 89 | + conn.close(); |
| 90 | + return properties; |
| 91 | + } |
| 92 | + |
| 93 | + public Map<String, String> getNode(String id) { |
| 94 | + return getGraphEntity(id); |
| 95 | + } |
| 96 | + |
| 97 | + public Map<String, String> getEdge(String id) { |
| 98 | + return getGraphEntity(id); |
| 99 | + } |
| 100 | + |
| 101 | + public ResultSet query(String graphID, String query) { |
| 102 | + Jedis conn = _conn(); |
| 103 | + |
| 104 | + List<Object> resp = conn.getClient() |
| 105 | + .sendCommand(Commands.Command.QUERY, graphID, query) |
| 106 | + .getObjectMultiBulkReply(); |
| 107 | + |
| 108 | + return new ResultSet(resp); |
| 109 | + } |
| 110 | +} |
0 commit comments