Skip to content

Commit 19b4878

Browse files
committed
Initial commit.
Based on from https://github.com/RedisLabs/JRediSearch
0 parents  commit 19b4878

10 files changed

Lines changed: 278 additions & 0 deletions

File tree

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*
2+
!/**/
3+
!**/*.java
4+
!**/*.jar
5+
!**/*.md
6+
!.gitignore
7+
!pom.xml

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# JReJSON
2+
3+
A Java Client Library for [ReJSON](https://https://github.com/redislabsmodules/rejson)
4+
5+
## Overview
6+
7+
8+
## Usage example
9+
10+
Initializing the client:
11+
12+
```java
13+
14+
import io.rejson.Client;
15+
16+
...
17+
18+
Client client = new Client("localhost", 6379);
19+
20+
```
21+
22+
Setting a value:
23+
24+
...

lib/commons-pool2-2.4.2.jar

109 KB
Binary file not shown.

lib/hamcrest-core-1.3.jar

44 KB
Binary file not shown.

lib/jedis-3.0.0-sources.jar

158 KB
Binary file not shown.

lib/jedis-3.0.0.jar

544 KB
Binary file not shown.

lib/junit-4.12.jar

308 KB
Binary file not shown.

pom.xml

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.redislabs</groupId>
8+
<artifactId>jrejson</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<dependencies>
11+
<dependency>
12+
<groupId>redis.clients</groupId>
13+
<artifactId>jedis</artifactId>
14+
<version>3.0.0-SNAPSHOT</version>
15+
<scope>system</scope>
16+
<systemPath>${project.basedir}/lib/jedis-3.0.0.jar</systemPath>
17+
</dependency>
18+
<dependency>
19+
<groupId>com.google.code.gson</groupId>
20+
<artifactId>gson</artifactId>
21+
<version>2.8.0</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.apache.commons</groupId>
25+
<artifactId>commons-pool2</artifactId>
26+
<version>2.4.2</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>junit</groupId>
30+
<artifactId>junit</artifactId>
31+
<version>4.12</version>
32+
</dependency>
33+
</dependencies>
34+
<properties>
35+
<maven.compiler.source>8</maven.compiler.source>
36+
<maven.compiler.target>8</maven.compiler.target>
37+
<maven.test.source>8</maven.test.source>
38+
<maven.test.target>8</maven.test.target>
39+
</properties>
40+
41+
42+
<build>
43+
<plugins>
44+
<plugin>
45+
<artifactId>maven-assembly-plugin</artifactId>
46+
<configuration>
47+
<archive>
48+
<manifest>
49+
<addClasspath>true</addClasspath>
50+
<!--<mainClass></mainClass>-->
51+
</manifest>
52+
</archive>
53+
<descriptorRefs>
54+
<descriptorRef>jar-with-dependencies</descriptorRef>
55+
</descriptorRefs>
56+
</configuration>
57+
<executions>
58+
<execution>
59+
<id>make-my-jar-with-dependencies</id>
60+
<phase>package</phase>
61+
<goals>
62+
<goal>single</goal>
63+
</goals>
64+
</execution>
65+
</executions>
66+
</plugin>
67+
<plugin>
68+
<artifactId>maven-compiler-plugin</artifactId>
69+
<version>3.0</version>
70+
<configuration>
71+
<source>1.8</source>
72+
<target>1.8</target>
73+
</configuration>
74+
</plugin>
75+
</plugins>
76+
</build>
77+
78+
79+
80+
81+
</project>
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package io.rejson;
2+
3+
import redis.clients.jedis.Jedis;
4+
import redis.clients.jedis.JedisPool;
5+
import redis.clients.jedis.JedisPoolConfig;
6+
7+
import com.google.gson.Gson;
8+
import redis.clients.jedis.commands.ProtocolCommand;
9+
import redis.clients.util.SafeEncoder;
10+
11+
import java.util.*;
12+
13+
/**
14+
* Client is the main ReJSON client class, wrapping connection management and all ReJSON commands
15+
*/
16+
public class Client {
17+
18+
private JedisPool pool;
19+
private Gson gson;
20+
21+
Jedis _conn() {
22+
return pool.getResource();
23+
}
24+
25+
private enum Command implements ProtocolCommand {
26+
27+
SET("JSON.SET"),
28+
GET("JSON.GET");
29+
private final byte[] raw;
30+
31+
Command(String alt) {
32+
raw = SafeEncoder.encode(alt);
33+
}
34+
35+
public byte[] getRaw() {
36+
return raw;
37+
}
38+
}
39+
40+
private void assertReplyNotError(String str) throws Exception {
41+
if (str.startsWith("-ERR"))
42+
throw new Exception(str.substring(5));
43+
}
44+
45+
private void assertReplyOK(String str) throws Exception {
46+
if (!str.equals("OK"))
47+
throw new Exception(str);
48+
}
49+
50+
/**
51+
* Create a new client
52+
* @param host the redis host
53+
* @param port the redis port
54+
*/
55+
public Client(String host, int port, int timeout, int poolSize) {
56+
JedisPoolConfig conf = new JedisPoolConfig();
57+
conf.setMaxTotal(poolSize);
58+
conf.setTestOnBorrow(false);
59+
conf.setTestOnReturn(false);
60+
conf.setTestOnCreate(false);
61+
conf.setTestWhileIdle(false);
62+
conf.setMinEvictableIdleTimeMillis(60000);
63+
conf.setTimeBetweenEvictionRunsMillis(30000);
64+
conf.setNumTestsPerEvictionRun(-1);
65+
conf.setFairness(true);
66+
67+
pool = new JedisPool(conf, host, port, timeout);
68+
gson = new Gson();
69+
70+
}
71+
72+
public Client(String host, int port) {
73+
this(host, port, 500, 100);
74+
}
75+
76+
/**
77+
* Sets an object
78+
* @param key the key name
79+
* @param path a path in the object
80+
* @param object the Java object to store
81+
*/
82+
public void set(String key, String path, Object object) throws Exception {
83+
Jedis conn = _conn();
84+
ArrayList<byte[]> args = new ArrayList(3);
85+
86+
args.add(key.getBytes());
87+
args.add(path.getBytes());
88+
args.add(gson.toJson(object).getBytes());
89+
90+
String status = conn.getClient()
91+
.sendCommand(Command.SET, args.toArray(new byte[args.size()][]))
92+
.getStatusCodeReply();
93+
conn.close();
94+
95+
assertReplyOK(status);
96+
}
97+
98+
/**
99+
* Gets an object
100+
* @param key the key name
101+
* @param path a path in the object
102+
* @return the requested object
103+
*/
104+
public Object get(String key, String path) throws Exception {
105+
Jedis conn = _conn();
106+
ArrayList<byte[]> args = new ArrayList(2);
107+
108+
args.add(key.getBytes());
109+
args.add(path.getBytes());
110+
111+
String rep = conn.getClient()
112+
.sendCommand(Command.GET, args.toArray(new byte[args.size()][]))
113+
.getBulkReply();
114+
conn.close();
115+
116+
assertReplyNotError(rep);
117+
return gson.fromJson(rep, Object.class);
118+
}
119+
120+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.rejson;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import static junit.framework.TestCase.*;
7+
8+
public class ClientTest {
9+
10+
private Client c;
11+
12+
@Before
13+
public void initialize() {
14+
c = new Client("localhost", 6379);
15+
}
16+
17+
@Test
18+
public void set() throws Exception {
19+
c._conn().flushDB();
20+
21+
// basic set - should succeed
22+
c.set("test", ".", null);
23+
}
24+
25+
@Test(expected = Exception.class)
26+
public void setException() throws Exception {
27+
c._conn().flushDB();
28+
29+
// should error on non root path for new key
30+
c.set("test", ".foo", "bar");
31+
}
32+
33+
@Test
34+
public void get() throws Exception {
35+
c._conn().flushDB();
36+
c.set("test", ".", "foo");
37+
assertEquals("foo", c.get("test", "."));
38+
}
39+
40+
@Test(expected = Exception.class)
41+
public void getException() throws Exception {
42+
c._conn().flushDB();
43+
c.set("test", ".", "foo");
44+
c.get("test", ".bar");
45+
}
46+
}

0 commit comments

Comments
 (0)