@@ -24,8 +24,10 @@ Jedis _conn() {
2424
2525 private enum Command implements ProtocolCommand {
2626
27+ DEL ("JSON.DEL" ),
28+ GET ("JSON.GET" ),
2729 SET ("JSON.SET" ),
28- GET ("JSON.GET " );
30+ TYPE ("JSON.TYPE " );
2931 private final byte [] raw ;
3032
3133 Command (String alt ) {
@@ -37,22 +39,24 @@ public byte[] getRaw() {
3739 }
3840 }
3941
40- private void assertReplyNotError (String str ) throws Exception {
42+ private void assertReplyNotError (final String str ) throws Exception {
4143 if (str .startsWith ("-ERR" ))
4244 throw new Exception (str .substring (5 ));
4345 }
4446
45- private void assertReplyOK (String str ) throws Exception {
47+ private void assertReplyOK (final String str ) throws Exception {
4648 if (!str .equals ("OK" ))
4749 throw new Exception (str );
4850 }
4951
5052 /**
5153 * Create a new client
52- * @param host the redis host
53- * @param port the redis port
54+ * @param host the Redis host
55+ * @param port the Redis port
56+ * @param timeout the timeout
57+ * @param poolSize the pool's size
5458 */
55- public Client (String host , int port , int timeout , int poolSize ) {
59+ public Client (final String host , final int port , final int timeout , final int poolSize ) {
5660 JedisPoolConfig conf = new JedisPoolConfig ();
5761 conf .setMaxTotal (poolSize );
5862 conf .setTestOnBorrow (false );
@@ -69,23 +73,70 @@ public Client(String host, int port, int timeout, int poolSize) {
6973
7074 }
7175
72- public Client (String host , int port ) {
76+ public Client (final String host , final int port ) {
7377 this (host , port , 500 , 100 );
7478 }
7579
80+ /**
81+ * Deletes a path
82+ * @param key the key name
83+ * @param path a path in the object
84+ * @return the number of paths deleted (0 or 1)
85+ */
86+ public Long del (final String key , final String path ) throws Exception {
87+ Jedis conn = _conn ();
88+ ArrayList <byte []> args = new ArrayList (3 );
89+
90+ args .add (SafeEncoder .encode (key ));
91+ args .add (SafeEncoder .encode (path ));
92+
93+ Long rep = conn .getClient ()
94+ .sendCommand (Command .DEL , args .toArray (new byte [args .size ()][]))
95+ .getIntegerReply ();
96+ conn .close ();
97+
98+ return rep ;
99+ }
100+
101+ /**
102+ * Gets an object
103+ * @param key the key name
104+ * @param path a path in the object
105+ * @return the requested object
106+ */
107+ public Object get (final String key , final String path ) throws Exception {
108+ // TODO: need the variadic paths variant?
109+ Jedis conn = _conn ();
110+ ArrayList <byte []> args = new ArrayList (2 );
111+
112+ args .add (SafeEncoder .encode (key ));
113+ args .add (SafeEncoder .encode (path ));
114+
115+ String rep = conn .getClient ()
116+ .sendCommand (Command .GET , args .toArray (new byte [args .size ()][]))
117+ .getBulkReply ();
118+ conn .close ();
119+
120+ assertReplyNotError (rep );
121+ return gson .fromJson (rep , Object .class );
122+ }
123+
124+ // TODO: add support for JSON.MGET
125+
76126 /**
77127 * Sets an object
78128 * @param key the key name
79129 * @param path a path in the object
80130 * @param object the Java object to store
81131 */
82- public void set (String key , String path , Object object ) throws Exception {
132+ public void set (final String key , final String path , final Object object ) throws Exception {
133+ // TODO: support NX|XX flags
83134 Jedis conn = _conn ();
84135 ArrayList <byte []> args = new ArrayList (3 );
85136
86- args .add (key . getBytes ( ));
87- args .add (path . getBytes ( ));
88- args .add (gson .toJson (object ). getBytes ( ));
137+ args .add (SafeEncoder . encode ( key ));
138+ args .add (SafeEncoder . encode ( path ));
139+ args .add (SafeEncoder . encode ( gson .toJson (object )));
89140
90141 String status = conn .getClient ()
91142 .sendCommand (Command .SET , args .toArray (new byte [args .size ()][]))
@@ -96,25 +147,43 @@ public void set(String key, String path, Object object) throws Exception {
96147 }
97148
98149 /**
99- * Gets an object
150+ * Gets the class of an object
100151 * @param key the key name
101152 * @param path a path in the object
102- * @return the requested object
153+ * @return the Java class of the requested object
103154 */
104- public Object get ( String key , String path ) throws Exception {
155+ public Class <? extends Object > type ( final String key , final String path ) throws Exception {
105156 Jedis conn = _conn ();
106157 ArrayList <byte []> args = new ArrayList (2 );
107158
108- args .add (key . getBytes ( ));
109- args .add (path . getBytes ( ));
159+ args .add (SafeEncoder . encode ( key ));
160+ args .add (SafeEncoder . encode ( path ));
110161
111162 String rep = conn .getClient ()
112- .sendCommand (Command .GET , args .toArray (new byte [args .size ()][]))
163+ .sendCommand (Command .TYPE , args .toArray (new byte [args .size ()][]))
113164 .getBulkReply ();
114165 conn .close ();
115166
116167 assertReplyNotError (rep );
117- return gson .fromJson (rep , Object .class );
168+
169+ switch (rep ) {
170+ case "null" :
171+ return null ;
172+ case "boolean" :
173+ return boolean .class ;
174+ case "integer" :
175+ return int .class ;
176+ case "number" :
177+ return float .class ;
178+ case "string" :
179+ return String .class ;
180+ case "object" :
181+ return Object .class ;
182+ case "array" :
183+ return List .class ;
184+ default :
185+ throw new Exception (rep );
186+ }
118187 }
119188
120189}
0 commit comments