Skip to content

Commit cbf1474

Browse files
committed
Adds foundations for Path
1 parent 5dac1e2 commit cbf1474

3 files changed

Lines changed: 44 additions & 24 deletions

File tree

src/main/java/io/rejson/Client.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.rejson;
22

3+
import io.rejson.Path;
4+
35
import redis.clients.jedis.Jedis;
46
import redis.clients.jedis.JedisPool;
57
import redis.clients.jedis.JedisPoolConfig;
@@ -83,12 +85,12 @@ public Client(final String host, final int port) {
8385
* @param path a path in the object
8486
* @return the number of paths deleted (0 or 1)
8587
*/
86-
public Long del(final String key, final String path) throws Exception {
88+
public Long del(final String key, final Path path) throws Exception {
8789
Jedis conn = _conn();
8890
ArrayList<byte[]> args = new ArrayList(3);
8991

9092
args.add(SafeEncoder.encode(key));
91-
args.add(SafeEncoder.encode(path));
93+
args.add(SafeEncoder.encode(path.toString()));
9294

9395
Long rep = conn.getClient()
9496
.sendCommand(Command.DEL, args.toArray(new byte[args.size()][]))
@@ -104,13 +106,13 @@ public Long del(final String key, final String path) throws Exception {
104106
* @param path a path in the object
105107
* @return the requested object
106108
*/
107-
public Object get(final String key, final String path) throws Exception {
109+
public Object get(final String key, final Path path) throws Exception {
108110
// TODO: need the variadic paths variant?
109111
Jedis conn = _conn();
110112
ArrayList<byte[]> args = new ArrayList(2);
111113

112114
args.add(SafeEncoder.encode(key));
113-
args.add(SafeEncoder.encode(path));
115+
args.add(SafeEncoder.encode(path.toString()));
114116

115117
String rep = conn.getClient()
116118
.sendCommand(Command.GET, args.toArray(new byte[args.size()][]))
@@ -129,13 +131,13 @@ public Object get(final String key, final String path) throws Exception {
129131
* @param path a path in the object
130132
* @param object the Java object to store
131133
*/
132-
public void set(final String key, final String path, final Object object) throws Exception {
134+
public void set(final String key, final Path path, final Object object) throws Exception {
133135
// TODO: support NX|XX flags
134136
Jedis conn = _conn();
135137
ArrayList<byte[]> args = new ArrayList(3);
136138

137139
args.add(SafeEncoder.encode(key));
138-
args.add(SafeEncoder.encode(path));
140+
args.add(SafeEncoder.encode(path.toString()));
139141
args.add(SafeEncoder.encode(gson.toJson(object)));
140142

141143
String status = conn.getClient()
@@ -152,12 +154,12 @@ public void set(final String key, final String path, final Object object) throws
152154
* @param path a path in the object
153155
* @return the Java class of the requested object
154156
*/
155-
public Class<? extends Object> type(final String key, final String path) throws Exception {
157+
public Class<? extends Object> type(final String key, final Path path) throws Exception {
156158
Jedis conn = _conn();
157159
ArrayList<byte[]> args = new ArrayList(2);
158160

159161
args.add(SafeEncoder.encode(key));
160-
args.add(SafeEncoder.encode(path));
162+
args.add(SafeEncoder.encode(path.toString()));
161163

162164
String rep = conn.getClient()
163165
.sendCommand(Command.TYPE, args.toArray(new byte[args.size()][]))

src/main/java/io/rejson/Path.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.rejson;
2+
3+
public class Path {
4+
private final String strPath;
5+
6+
public Path(final String strPath) {
7+
this.strPath = strPath;
8+
}
9+
10+
public static Path RootPath() {
11+
return new Path(".");
12+
}
13+
14+
@Override
15+
public String toString() {
16+
return strPath;
17+
}
18+
}

src/test/java/io/rejson/ClientTest.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,59 +26,59 @@ public void initialize() {
2626
public void set() throws Exception {
2727
c._conn().flushDB();
2828

29-
c.set("null", ".", null);
30-
c.set("foobar", ".", new FooBarObject());
29+
c.set("null", Path.RootPath(), null);
30+
c.set("foobar", Path.RootPath(), new FooBarObject());
3131
}
3232

3333
@Test(expected = Exception.class)
3434
public void setException() throws Exception {
3535
c._conn().flushDB();
3636

3737
// should error on non root path for new key
38-
c.set("test", ".foo", "bar");
38+
c.set("test", new Path(".foo"), "bar");
3939
}
4040

4141
@Test
4242
public void get() throws Exception {
4343
c._conn().flushDB();
44-
c.set("test", ".", "foo");
45-
assertEquals("foo", c.get("test", "."));
44+
c.set("test", Path.RootPath(), "foo");
45+
assertEquals("foo", c.get("test", Path.RootPath()));
4646
}
4747

4848
@Test(expected = Exception.class)
4949
public void getException() throws Exception {
5050
c._conn().flushDB();
51-
c.set("test", ".", "foo");
52-
c.get("test", ".bar");
51+
c.set("test", Path.RootPath(), "foo");
52+
c.get("test", new Path(".bar"));
5353
}
5454

5555
@Test
5656
public void del() throws Exception {
5757
c._conn().flushDB();
58-
c.set("foobar", ".", new FooBarObject());
59-
c.del("foobar", ".foo");
58+
c.set("foobar", Path.RootPath(), new FooBarObject());
59+
c.del("foobar", new Path(".foo"));
6060
}
6161

6262
@Test(expected = Exception.class)
6363
public void delException() throws Exception {
6464
c._conn().flushDB();
65-
c.set("foobar", ".", new FooBarObject());
66-
c.del("foobar", ".foo[1]");
65+
c.set("foobar", Path.RootPath(), new FooBarObject());
66+
c.del("foobar", new Path(".foo[1]"));
6767
}
6868

6969
@Test
7070
public void type() throws Exception {
7171
c._conn().flushDB();
72-
c.set("foobar", ".", new FooBarObject());
73-
assertSame(Object.class, c.type("foobar", "."));
74-
assertSame(String.class, c.type("foobar", ".foo"));
72+
c.set("foobar", Path.RootPath(), new FooBarObject());
73+
assertSame(Object.class, c.type("foobar", Path.RootPath()));
74+
assertSame(String.class, c.type("foobar", new Path(".foo")));
7575
}
7676

7777
@Test(expected = Exception.class)
7878
public void typeException() throws Exception {
7979
c._conn().flushDB();
80-
c.set("foobar", ".", new FooBarObject());
81-
c.type("foobar", ".foo[1]");
80+
c.set("foobar", Path.RootPath(), new FooBarObject());
81+
c.type("foobar", new Path(".foo[1]"));
8282
}
8383

8484
}

0 commit comments

Comments
 (0)