Skip to content

Commit 19d9d2e

Browse files
committed
Reviews the review, updates README
1 parent 944d248 commit 19d9d2e

File tree

6 files changed

+79
-112
lines changed

6 files changed

+79
-112
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
*
22
!/**/
33
!**/*.java
4-
!**/*.jar
54
!**/*.md
65
!.gitignore
76
!pom.xml

README.md

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,41 @@ A Java Client Library for [ReJSON](https://github.com/redislabsmodules/rejson)
66

77
This client provides access to ReJSON's Redis API, and provides back-and-forth serialization between Java's and its objects.
88

9-
## Usage example
9+
## Installation
1010

11-
Firstly, the client's initialization:
11+
1. Install Jedis (once v3 is released this step will be obsolete)
12+
1. Clone it: `git clone --depth 1 [email protected]:xetorthio/jedis.git`
13+
2. `cd jedis`
14+
3. `mvn -Dmaven.test.skip=true install`
15+
2. Install JReJSON (todo: add to maven)
16+
1. Clone it: `git clone [email protected]:RedisLabs/JReJSON.git`
17+
2. `cd JReJSON`
18+
3. `mvn -Dmaven.test.skip=true install`
1219

13-
```java
14-
Client c = new Client("localhost", 6379);
15-
```
20+
## Usage example
1621

17-
Setting a Redis key name _foo_ to the string _"bar"_, and reading it back:
1822
```java
19-
c.set("foo", "bar");
20-
String s0 = (String) c.get("foo");
21-
```
23+
import redis.clients.jedis.Jedis;
24+
import io.rejson.JReJSON;
2225

23-
Omitting the path (usually) defaults to the root path, so the call above to `get()` and the following ones are basically interchangeable:
26+
// First get a connection
27+
Jedis jedis = new Jedis("localhost", 6379);
2428

25-
```java
26-
String s1 = (String) c.get("foo", new Path("."));
27-
String s2 = (String) c.get("foo", Path.RootPath());
28-
```
29+
// Setting a Redis key name _foo_ to the string _"bar"_, and reading it back
30+
JReJSON.set(jedis,"foo", "bar");
31+
String s0 = (String) JReJSON.get(jedis,"foo");
2932

30-
Any Gson-able object can be set and updated:
31-
```java
32-
c.set("obj", new Object()); // just an empty object
33-
c.set("obj", null, new Path(".zilch"));
33+
// Omitting the path (usually) defaults to the root path, so the call above to
34+
// `get()` and the following ones // are basically interchangeable
35+
String s1 = (String) JReJSON.get(jedis,"foo", new Path("."));
36+
String s2 = (String) JReJSON.get(jedis, "foo", Path.RootPath());
37+
38+
// Any Gson-able object can be set and updated
39+
JReJSON.set(jedis,"obj", new Object()); // just an empty object
40+
JReJSON.set(jedis,"obj", null, new Path(".zilch"));
3441
Path p = new Path(".whatevs");
35-
c.set("obj", true, p);
36-
c.set("obj", 42, p);
37-
c.del("obj", p); // back to almost nothing
42+
JReJSON.set(jedis,"obj", true, p);
43+
JReJSON.set(jedis,"obj", 42, p);
44+
JReJSON.del(jedis,"obj", p); // back to almost nothing
45+
3846
```

src/main/java/io/rejson/IJReJSON.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/main/java/io/rejson/JReJSON.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,11 @@ else if (1 == path.length)
8484
else
8585
// throw out the baby with the water
8686
throw new RuntimeException("Only a single optional path is allowed");
87-
8887
}
8988

90-
91-
9289
/**
9390
* Deletes a path
91+
* @param conn the Jedis connection
9492
* @param key the key name
9593
* @param path optional single path in the object, defaults to root
9694
* @return the number of paths deleted (0 or 1)
@@ -112,11 +110,12 @@ public static Long del(Jedis conn, String key, Path... path) {
112110

113111
/**
114112
* Gets an object
113+
* @param conn the Jedis connection
115114
* @param key the key name
116115
* @param paths optional one ore more paths in the object, defaults to root
117116
* @return the requested object
118117
*/
119-
public static Object get(Jedis conn,String key, Path... paths) {
118+
public static Object get(Jedis conn, String key, Path... paths) {
120119

121120
ArrayList<byte[]> args = new ArrayList(2);
122121

@@ -134,16 +133,15 @@ public static Object get(Jedis conn,String key, Path... paths) {
134133
return gson.fromJson(rep, Object.class);
135134
}
136135

137-
// TODO: add support for JSON.MGET
138-
139136
/**
140137
* Sets an object
138+
* @param conn the Jedis connection
141139
* @param key the key name
142140
* @param object the Java object to store
143141
* @param flag an existential modifier
144142
* @param path optional single path in the object, defaults to root
145143
*/
146-
public static void set(Jedis conn ,String key, Object object, ExistenceModifier flag, Path... path) {
144+
public static void set(Jedis conn, String key, Object object, ExistenceModifier flag, Path... path) {
147145

148146
ArrayList<byte[]> args = new ArrayList(4);
149147

@@ -164,21 +162,23 @@ public static void set(Jedis conn ,String key, Object object, ExistenceModifier
164162

165163
/**
166164
* Sets an object without caring about target path existing
165+
* @param conn the Jedis connection
167166
* @param key the key name
168167
* @param object the Java object to store
169168
* @param path optional single path in the object, defaults to root
170169
*/
171-
public static void set(Jedis conn,String key, Object object, Path... path) {
170+
public static void set(Jedis conn, String key, Object object, Path... path) {
172171
set(conn,key, object, ExistenceModifier.DEFAULT, path);
173172
}
174173

175174
/**
176175
* Gets the class of an object
176+
* @param conn the Jedis connection
177177
* @param key the key name
178178
* @param path optional single path in the object, defaults to root
179179
* @return the Java class of the requested object
180180
*/
181-
public static Class<? extends Object> type(Jedis conn,String key, Path... path) {
181+
public static Class<? extends Object> type(Jedis conn, String key, Path... path) {
182182

183183
ArrayList<byte[]> args = new ArrayList(2);
184184

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

Lines changed: 38 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -32,94 +32,90 @@ public IRLObject() {
3232

3333
public class ClientTest {
3434

35-
private JReJSON c;
3635
private Gson g;
37-
private String host="52.7.3.128";
36+
private String host="localhost";
3837
private int port=6379;
39-
Jedis jedis = new Jedis(host,port);
38+
Jedis jedis = new Jedis(host,port);
4039

4140
@Before
4241
public void initialize() {
4342
g = new Gson();
44-
4543
}
4644

4745
@Test
4846
public void basicSetGetShouldSucceed() throws Exception {
4947

50-
51-
5248
// naive set with a path
5349
JReJSON.set(jedis, "null", null, Path.RootPath());
54-
assertNull(JReJSON.get(jedis,"null", Path.RootPath()));
50+
assertNull(JReJSON.get(jedis, "null", Path.RootPath()));
5551

5652
// real scalar value and no path
57-
JReJSON.set(jedis,"str", "strong");
58-
assertEquals("strong", JReJSON.get(jedis,"str"));
53+
JReJSON.set(jedis, "str", "strong");
54+
assertEquals("strong", JReJSON.get(jedis, "str"));
5955

6056
// A slightly more complex object
6157
IRLObject obj = new IRLObject();
62-
JReJSON.set(jedis,"obj", obj);
58+
JReJSON.set(jedis, "obj", obj);
6359
Object expected = g.fromJson(g.toJson(obj), Object.class);
64-
assertTrue(expected.equals(JReJSON.get(jedis,"obj")));
60+
assertTrue(expected.equals(JReJSON.get(jedis, "obj")));
6561

6662
// check an update
6763
Path p = new Path(".str");
68-
JReJSON.set(jedis,"obj", "strung", p);
69-
assertEquals("strung", JReJSON.get(jedis,"obj", p));
64+
JReJSON.set(jedis, "obj", "strung", p);
65+
assertEquals("strung", JReJSON.get(jedis, "obj", p));
7066
}
7167

7268
@Test
7369
public void setExistingPathOnlyIfExistsShouldSucceed() throws Exception {
7470
jedis.flushDB();
7571

76-
JReJSON.set(jedis,"obj", new IRLObject());
72+
JReJSON.set(jedis, "obj", new IRLObject());
7773
Path p = new Path(".str");
78-
JReJSON.set(jedis,"obj", "strangle", JReJSON.ExistenceModifier.MUST_EXIST, p);
79-
assertEquals("strangle", JReJSON.get(jedis,"obj", p));
74+
JReJSON.set(jedis, "obj", "strangle", JReJSON.ExistenceModifier.MUST_EXIST, p);
75+
assertEquals("strangle", JReJSON.get(jedis, "obj", p));
8076
}
8177

8278
@Test
8379
public void setNonExistingOnlyIfNotExistsShouldSucceed() throws Exception {
8480
jedis.flushDB();
8581

86-
JReJSON.set(jedis,"obj", new IRLObject());
82+
JReJSON.set(jedis, "obj", new IRLObject());
8783
Path p = new Path(".none");
88-
JReJSON.set(jedis,"obj", "strangle", JReJSON.ExistenceModifier.NOT_EXISTS, p);
89-
assertEquals("strangle", JReJSON.get(jedis,"obj", p));
84+
JReJSON.set(jedis, "obj", "strangle", JReJSON.ExistenceModifier.NOT_EXISTS, p);
85+
assertEquals("strangle", JReJSON.get(jedis, "obj", p));
9086
}
9187

9288
@Test(expected = Exception.class)
9389
public void setExistingPathOnlyIfNotExistsShouldFail() throws Exception {
9490
jedis.flushDB();
9591

96-
JReJSON.set(jedis,"obj", new IRLObject());
92+
JReJSON.set(jedis, "obj", new IRLObject());
9793
Path p = new Path(".str");
98-
JReJSON.set(jedis,"obj", "strangle", JReJSON.ExistenceModifier.NOT_EXISTS, p);
94+
JReJSON.set(jedis, "obj", "strangle", JReJSON.ExistenceModifier.NOT_EXISTS, p);
9995
}
10096

10197
@Test(expected = Exception.class)
10298
public void setNonExistingPathOnlyIfExistsShouldFail() throws Exception {
10399
jedis.flushDB();
104100

105-
JReJSON.set(jedis,"obj", new IRLObject());
101+
JReJSON.set(jedis, "obj", new IRLObject());
106102
Path p = new Path(".none");
107-
JReJSON.set(jedis,"obj", "strangle", JReJSON.ExistenceModifier.MUST_EXIST, p);
103+
JReJSON.set(jedis, "obj", "strangle", JReJSON.ExistenceModifier.MUST_EXIST, p);
108104
}
109105

110106
@Test(expected = Exception.class)
111107
public void setException() throws Exception {
112108
jedis.flushDB();
113109

114110
// should error on non root path for new key
115-
JReJSON.set(jedis,"test", "bar", new Path(".foo"));
111+
JReJSON.set(jedis, "test", "bar", new Path(".foo"));
116112
}
117113

118114
@Test(expected = Exception.class)
119115
public void setMultiplePathsShouldFail() throws Exception {
120116
jedis.flushDB();
121-
JReJSON.set(jedis,"obj", new IRLObject());
122-
JReJSON.set(jedis,"obj", "strange", new Path(".str"), new Path(".str"));
117+
JReJSON.set(jedis, "obj", new IRLObject());
118+
JReJSON.set(jedis, "obj", "strange", new Path(".str"), new Path(".str"));
123119
}
124120

125121
@Test
@@ -128,89 +124,65 @@ public void getMultiplePathsShouldSucceed() throws Exception {
128124

129125
// check multiple paths
130126
IRLObject obj = new IRLObject();
131-
JReJSON.set(jedis,"obj", obj);
127+
JReJSON.set(jedis, "obj", obj);
132128
Object expected = g.fromJson(g.toJson(obj), Object.class);
133-
assertTrue(expected.equals(JReJSON.get(jedis,"obj", new Path("bTrue"), new Path("str"))));
129+
assertTrue(expected.equals(JReJSON.get(jedis, "obj", new Path("bTrue"), new Path("str"))));
134130

135131
}
136132

137133
@Test(expected = Exception.class)
138134
public void getException() throws Exception {
139135
jedis.flushDB();
140-
JReJSON.set(jedis,"test", "foo", Path.RootPath());
141-
JReJSON.get(jedis,"test", new Path(".bar"));
136+
JReJSON.set(jedis, "test", "foo", Path.RootPath());
137+
JReJSON.get(jedis, "test", new Path(".bar"));
142138
}
143139

144140
@Test
145141
public void delValidShouldSucceed() throws Exception {
146142
jedis.flushDB();
147143

148144
// check deletion of a single path
149-
JReJSON.set(jedis,"obj", new IRLObject(), Path.RootPath());
150-
JReJSON.del(jedis,"obj", new Path(".str"));
145+
JReJSON.set(jedis, "obj", new IRLObject(), Path.RootPath());
146+
JReJSON.del(jedis, "obj", new Path(".str"));
151147
assertTrue(jedis.exists("obj"));
152148

153149
// check deletion root using default root -> key is removed
154-
JReJSON.del(jedis,"obj");
150+
JReJSON.del(jedis, "obj");
155151
assertFalse(jedis.exists("obj"));
156152
}
157153

158154
@Test(expected = Exception.class)
159155
public void delException() throws Exception {
160156
jedis.flushDB();
161-
JReJSON.set(jedis,"foobar", new FooBarObject(), Path.RootPath());
162-
JReJSON.del(jedis,"foobar", new Path(".foo[1]"));
157+
JReJSON.set(jedis, "foobar", new FooBarObject(), Path.RootPath());
158+
JReJSON.del(jedis, "foobar", new Path(".foo[1]"));
163159
}
164160

165161
@Test(expected = Exception.class)
166162
public void delMultiplePathsShoudFail() throws Exception {
167163
jedis.flushDB();
168-
JReJSON.del(jedis,"foobar", new Path(".foo"), new Path(".bar"));
164+
JReJSON.del(jedis, "foobar", new Path(".foo"), new Path(".bar"));
169165
}
170166

171167
@Test
172168
public void typeChecksShouldSucceed() throws Exception {
173169
jedis.flushDB();
174-
JReJSON.set(jedis,"foobar", new FooBarObject(), Path.RootPath());
175-
assertSame(Object.class, c.type(jedis,"foobar", Path.RootPath()));
176-
assertSame(String.class, c.type(jedis,"foobar", new Path(".foo")));
170+
JReJSON.set(jedis, "foobar", new FooBarObject(), Path.RootPath());
171+
assertSame(Object.class, JReJSON.type(jedis, "foobar", Path.RootPath()));
172+
assertSame(String.class, JReJSON.type(jedis, "foobar", new Path(".foo")));
177173
}
178174

179175
@Test(expected = Exception.class)
180176
public void typeException() throws Exception {
181177
jedis.flushDB();
182-
JReJSON.set(jedis,"foobar", new FooBarObject(), Path.RootPath());
178+
JReJSON.set(jedis, "foobar", new FooBarObject(), Path.RootPath());
183179
JReJSON.type(jedis, "foobar", new Path(".foo[1]"));
184180
}
185181

186182
@Test(expected = Exception.class)
187183
public void type1Exception() throws Exception {
188184
jedis.flushDB();
189-
JReJSON.set(jedis,"foobar", new FooBarObject(), Path.RootPath());
190-
JReJSON.type(jedis,"foobar", new Path(".foo[1]"));
191-
192-
// JedisPoolConfig conf = new JedisPoolConfig();
193-
// conf.setMaxTotal(55);
194-
// conf.setTestOnBorrow(false);
195-
// conf.setTestOnReturn(false);
196-
// conf.setTestOnCreate(false);
197-
// conf.setTestWhileIdle(false);
198-
// conf.setMinEvictableIdleTimeMillis(60000);
199-
// conf.setTimeBetweenEvictionRunsMillis(30000);
200-
// conf.setNumTestsPerEvictionRun(-1);
201-
// conf.setFairness(true);
202-
//
203-
// JedisPool pool = new JedisPool(conf, "localhost", 6379,1000);
204-
//
205-
// JReJSON jReJSON = (JReJSON) pool.getResource();
206-
207-
208-
209-
210-
//Pipeline pipe = jReJSON.pipelined();
211-
212-
185+
JReJSON.set(jedis, "foobar", new FooBarObject(), Path.RootPath());
186+
JReJSON.type(jedis, "foobar", new Path(".foo[1]"));
213187
}
214-
215-
216188
}

0 commit comments

Comments
 (0)