Skip to content

Commit 090db59

Browse files
denglimingclaude
andcommitted
Type RedisCommands, resolve FT.SUGGET, name-based INFO decoders, README and examples
- Every RedisCommand constant now declares its reply type; the raw constructor is used only where Redisson types a per-element convertor as Convertor<R>. - FT.SUGGET was decoded with VoidReplayConvertor, so getSuggestion() always returned null. It now builds the command per call with SuggestionDecoder, honouring WITHSCORES / WITHPAYLOADS, and is covered by a test. - RedisBloom *.INFO decoders read fields by name (InfoReply) instead of by position, so reordered or added fields no longer shift every value. They decode field names as plain strings regardless of the configured codec. - Keywords are passed as enums everywhere (Redisson encodes arguments with toString()); RedisGraph's Keywords override toString() to return the alias. - TimeSeriesOptions no longer carries a hidden isAdd flag that RedisTimeSeries.add() mutated on the caller's object; the caller passes the ON_DUPLICATE / DUPLICATE_POLICY choice to build(args, onDuplicate). - RedisAI.setTensorAsync drops a dead null check. - README documents RedisGraph, pipelining and sharing one Redisson instance; the starter README documents the shared redis-module.config. New examples for RedisGraph, RedisBloom pipelining and shared Redisson; the examples module is exempt from checkstyle because demo code prints to stdout. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 8d012a3 commit 090db59

32 files changed

Lines changed: 714 additions & 225 deletions

File tree

‎README.md‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,52 @@ Map<String, Object> actual = redisJSON.get(key, Map.class, new GetArgs().path(".
155155
redisJSONClient.shutdown();
156156
```
157157

158+
RedisGraph
159+
```java
160+
Config config = new Config();
161+
config.useSingleServer().setAddress("redis://127.0.0.1:6379");
162+
RedisGraphClient redisGraphClient = new RedisGraphClient(config);
163+
164+
RedisGraph redisGraph = redisGraphClient.getRedisGraph();
165+
redisGraph.query("social", "CREATE (:person{name:'roi',age:32})-[:knows{since:2000}]->(:person{name:'amit',age:30})", 0L);
166+
ResultSet resultSet = redisGraph.query("social", "MATCH (a:person)-[r:knows]->(b:person) RETURN a, r, b.name", 0L);
167+
for (Record record : resultSet.getResults()) {
168+
Node a = (Node) record.getValue("a");
169+
Edge r = (Edge) record.getValue("r");
170+
// property names and relationship types are resolved, not just indices
171+
System.out.println(a.getProperty("name") + " " + r.getRelationshipType() + " " + record.getString("b.name"));
172+
}
173+
redisGraphClient.shutdown();
174+
```
175+
176+
Pipelining (batch)
177+
178+
Every client has `createXxxBatch()`. Objects obtained from the batch queue their `*Async` calls; `execute()` sends them in one round trip.
179+
```java
180+
RedisBloomBatch batch = redisBloomClient.createRedisBloomBatch();
181+
BloomFilter bloomFilter = batch.getRBloomFilter("bf");
182+
bloomFilter.createAsync(0.01d, 1000);
183+
bloomFilter.maddAsync("a", "b", "c");
184+
bloomFilter.existsAsync("a");
185+
BatchResult<?> result = batch.execute();
186+
// [true, [true, true, true], true]
187+
System.out.println(result.getResponses());
188+
```
189+
See [PipeliningExamples](examples/src/main/java/io/github/dengliming/redismodule/examples/redisbloom/PipeliningExamples.java).
190+
191+
Sharing one Redisson instance
192+
193+
Every client can wrap an existing `RedissonClient` so that several modules share a single connection pool. The wrapping client never shuts the shared instance down.
194+
```java
195+
RedissonClient redisson = Redisson.create(config);
196+
RedisJSONClient redisJSONClient = new RedisJSONClient(redisson);
197+
RediSearchClient rediSearchClient = new RediSearchClient(redisson);
198+
RedisBloomClient redisBloomClient = new RedisBloomClient(redisson);
199+
// ...
200+
redisson.shutdown();
201+
```
202+
See [SharedRedissonExamples](examples/src/main/java/io/github/dengliming/redismodule/examples/SharedRedissonExamples.java).
203+
158204
SpringBoot Starter
159205

160206
see [spring-boot-starter](./spring-boot-starter)

‎examples/pom.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
</parent>
99
<modelVersion>4.0.0</modelVersion>
1010
<artifactId>examples</artifactId>
11+
12+
<properties>
13+
<!-- demo code prints to stdout, which the project checkstyle rules forbid -->
14+
<checkstyle.skip>true</checkstyle.skip>
15+
</properties>
1116
<name>Examples</name>
1217

1318
<dependencies>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2024 dengliming.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.dengliming.redismodule.examples;
18+
19+
import io.github.dengliming.redismodule.redisbloom.BloomFilter;
20+
import io.github.dengliming.redismodule.redisbloom.client.RedisBloomClient;
21+
import io.github.dengliming.redismodule.redisjson.RedisJSON;
22+
import io.github.dengliming.redismodule.redisjson.args.SetArgs;
23+
import io.github.dengliming.redismodule.redisjson.client.RedisJSONClient;
24+
import io.github.dengliming.redismodule.redisearch.RediSearch;
25+
import io.github.dengliming.redismodule.redisearch.client.RediSearchClient;
26+
import org.redisson.Redisson;
27+
import org.redisson.api.RedissonClient;
28+
import org.redisson.config.Config;
29+
30+
/**
31+
* Several module clients can share one Redisson instance (and therefore one connection pool)
32+
* instead of each opening their own.
33+
*/
34+
public class SharedRedissonExamples {
35+
36+
public static void main(String[] args) {
37+
Config config = new Config();
38+
config.useSingleServer().setAddress("redis://127.0.0.1:6379");
39+
RedissonClient redisson = Redisson.create(config);
40+
41+
// Clients built from an existing RedissonClient do not shut it down; the caller owns it
42+
RedisJSONClient redisJSONClient = new RedisJSONClient(redisson);
43+
RediSearchClient rediSearchClient = new RediSearchClient(redisson);
44+
RedisBloomClient redisBloomClient = new RedisBloomClient(redisson);
45+
46+
RedisJSON redisJSON = redisJSONClient.getRedisJSON();
47+
redisJSON.set("doc:1", SetArgs.Builder.create(".", "{\"title\":\"hello\"}"));
48+
49+
RediSearch rediSearch = rediSearchClient.getRediSearch("idx");
50+
System.out.println(rediSearch.listIndexes());
51+
52+
BloomFilter bloomFilter = redisBloomClient.getRBloomFilter("bf");
53+
bloomFilter.add("doc:1");
54+
55+
// No-ops for shared instances
56+
redisJSONClient.shutdown();
57+
rediSearchClient.shutdown();
58+
redisBloomClient.shutdown();
59+
60+
redisson.shutdown();
61+
}
62+
}

‎examples/src/main/java/io/github/dengliming/redismodule/examples/redisbloom/Examples.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public static void main(String[] args) {
3939
bloomFilter.create(0.1d, 100);
4040
bloomFilter.madd(new String[] {"a", "b", "c"});
4141

42-
4342
TopKFilter topKFilter = redisBloomClient.getTopKFilter("topk_add");
4443
topKFilter.reserve(1, 2000, 7, 0.925d);
4544
topKFilter.add("test");
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2024 dengliming.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.dengliming.redismodule.examples.redisbloom;
18+
19+
import io.github.dengliming.redismodule.redisbloom.BloomFilter;
20+
import io.github.dengliming.redismodule.redisbloom.RedisBloomBatch;
21+
import io.github.dengliming.redismodule.redisbloom.client.RedisBloomClient;
22+
import org.redisson.api.BatchResult;
23+
import org.redisson.config.Config;
24+
25+
public class PipeliningExamples {
26+
27+
public static void main(String[] args) {
28+
Config config = new Config();
29+
config.useSingleServer().setAddress("redis://127.0.0.1:6379");
30+
RedisBloomClient redisBloomClient = new RedisBloomClient(config);
31+
32+
// Every module client exposes createXxxBatch(); objects obtained from the batch queue their *Async calls
33+
RedisBloomBatch batch = redisBloomClient.createRedisBloomBatch();
34+
BloomFilter bloomFilter = batch.getRBloomFilter("bf");
35+
bloomFilter.createAsync(0.01d, 1000);
36+
bloomFilter.maddAsync("a", "b", "c");
37+
bloomFilter.existsAsync("a");
38+
bloomFilter.existsAsync("z");
39+
40+
// One round trip for all four commands; responses come back in call order
41+
BatchResult<?> result = batch.execute();
42+
// true
43+
System.out.println(result.getResponses().get(0));
44+
// [true, true, true]
45+
System.out.println(result.getResponses().get(1));
46+
// true
47+
System.out.println(result.getResponses().get(2));
48+
// false
49+
System.out.println(result.getResponses().get(3));
50+
51+
redisBloomClient.shutdown();
52+
}
53+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2024 dengliming.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.dengliming.redismodule.examples.redisgraph;
18+
19+
import io.github.dengliming.redismodule.redisgraph.RedisGraph;
20+
import io.github.dengliming.redismodule.redisgraph.client.RedisGraphClient;
21+
import io.github.dengliming.redismodule.redisgraph.model.Edge;
22+
import io.github.dengliming.redismodule.redisgraph.model.Node;
23+
import io.github.dengliming.redismodule.redisgraph.model.Record;
24+
import io.github.dengliming.redismodule.redisgraph.model.ResultSet;
25+
import org.redisson.config.Config;
26+
27+
public class Examples {
28+
29+
public static void main(String[] args) {
30+
Config config = new Config();
31+
config.useSingleServer().setAddress("redis://127.0.0.1:6379");
32+
RedisGraphClient redisGraphClient = new RedisGraphClient(config);
33+
34+
RedisGraph redisGraph = redisGraphClient.getRedisGraph();
35+
redisGraph.query("social", "CREATE (:person{name:'roi',age:32})", 0L);
36+
redisGraph.query("social", "CREATE (:person{name:'amit',age:30})", 0L);
37+
redisGraph.query("social", "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') "
38+
+ "CREATE (a)-[:knows{since:2000}]->(b)", 0L);
39+
40+
ResultSet resultSet = redisGraph.query("social", "MATCH (a:person)-[r:knows]->(b:person) RETURN a, r, b.name", 0L);
41+
for (Record record : resultSet.getResults()) {
42+
Node a = (Node) record.getValue("a");
43+
Edge r = (Edge) record.getValue("r");
44+
// Property names and relationship types are resolved from the compact protocol indices
45+
System.out.println(a.getProperty("name") + " " + r.getRelationshipType() + " " + record.getString("b.name")
46+
+ " since " + r.getProperty("since"));
47+
}
48+
49+
redisGraph.delete("social");
50+
redisGraphClient.shutdown();
51+
}
52+
}

‎examples/src/main/java/io/github/dengliming/redismodule/examples/redisjson/PipeliningExamples.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import io.github.dengliming.redismodule.redisjson.RedisJSON;
2020
import io.github.dengliming.redismodule.redisjson.RedisJSONBatch;
21-
import io.github.dengliming.redismodule.redisjson.args.GetArgs;
2221
import io.github.dengliming.redismodule.redisjson.args.SetArgs;
2322
import io.github.dengliming.redismodule.redisjson.client.RedisJSONClient;
2423
import io.github.dengliming.redismodule.redisjson.utils.GsonUtils;

‎redisai/src/main/java/io/github/dengliming/redismodule/redisai/RedisAI.java‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,9 @@ public RFuture<Boolean> setTensorAsync(String key, DataType type, int[] dimensio
8282

8383
List<Object> args = new ArrayList<>();
8484
args.add(key);
85-
if (dimensions != null) {
86-
args.add(type);
87-
for (int dimension : dimensions) {
88-
args.add(dimension);
89-
}
85+
args.add(type);
86+
for (int dimension : dimensions) {
87+
args.add(dimension);
9088
}
9189
if (data != null) {
9290
args.add(Keywords.BLOB);
Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 dengliming.
2+
* Copyright 2020-2024 dengliming.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,9 @@
1616

1717
package io.github.dengliming.redismodule.redisai.protocol;
1818

19+
import io.github.dengliming.redismodule.redisai.model.Model;
20+
import io.github.dengliming.redismodule.redisai.model.Script;
21+
import io.github.dengliming.redismodule.redisai.model.Tensor;
1922
import io.github.dengliming.redismodule.redisai.protocol.decoder.ModelDecoder;
2023
import io.github.dengliming.redismodule.redisai.protocol.decoder.ScriptDecoder;
2124
import io.github.dengliming.redismodule.redisai.protocol.decoder.TensorDecoder;
@@ -26,27 +29,30 @@
2629
import org.redisson.client.protocol.decoder.ObjectListReplayDecoder;
2730
import org.redisson.client.protocol.decoder.ObjectMapReplayDecoder;
2831

32+
import java.util.Map;
33+
2934
/**
30-
* @author dengliming
35+
* RedisAI commands.
3136
*/
37+
@SuppressWarnings({"unchecked", "rawtypes"})
3238
public interface RedisCommands {
3339

34-
RedisCommand AI_TENSORSET = new RedisCommand<>("AI.TENSORSET", new BooleanReplayConvertor());
35-
RedisCommand AI_TENSORGET = new RedisCommand<>("AI.TENSORGET", new ListMultiDecoder2<>(new TensorDecoder(), new ObjectListReplayDecoder<>()));
36-
RedisCommand AI_MODELSET = new RedisCommand<>("AI.MODELSET", new BooleanReplayConvertor());
37-
RedisCommand AI_MODELGET = new RedisCommand<>("AI.MODELGET", new ListMultiDecoder2<>(new ModelDecoder(), new ObjectListReplayDecoder<>()));
38-
RedisCommand AI_MODELDEL = new RedisCommand<>("AI.MODELDEL", new BooleanReplayConvertor());
39-
RedisCommand AI_MODELRUN = new RedisCommand<>("AI.MODELRUN", new VoidReplayConvertor());
40-
RedisCommand AI_MODELSCAN = new RedisCommand<>("AI._MODELSCAN", new VoidReplayConvertor());
41-
RedisCommand AI_SCRIPTSET = new RedisCommand<>("AI.SCRIPTSET", new BooleanReplayConvertor());
42-
RedisCommand AI_SCRIPTGET = new RedisCommand<>("AI.SCRIPTGET", new ListMultiDecoder2<>(new ScriptDecoder(), new ObjectListReplayDecoder<>()));
43-
RedisCommand AI_SCRIPTDEL = new RedisCommand<>("AI.SCRIPTDEL", new BooleanReplayConvertor());
44-
RedisCommand AI_SCRIPTRUN = new RedisCommand<>("AI.SCRIPTRUN", new BooleanReplayConvertor());
45-
RedisCommand AI_SCRIPTSCAN = new RedisCommand<>("AI._SCRIPTSCAN", new VoidReplayConvertor());
46-
RedisCommand AI_DAGRUN = new RedisCommand<>("AI.DAGRUN", new VoidReplayConvertor());
47-
RedisCommand AI_DAGRUN_RO = new RedisCommand<>("AI.DAGRUN_RO", new VoidReplayConvertor());
48-
RedisCommand AI_INFO = new RedisCommand<>("AI.INFO", new ObjectMapReplayDecoder());
49-
RedisCommand AI_INFO_RESETSTAT = new RedisCommand<>("AI.INFO", new BooleanReplayConvertor());
50-
RedisCommand AI_CONFIG = new RedisCommand<>("AI.CONFIG", new BooleanReplayConvertor());
51-
RedisCommand AI_SCRIPTSTORE = new RedisCommand<>("AI.SCRIPTSTORE", new BooleanReplayConvertor());
40+
RedisCommand<Boolean> AI_TENSORSET = new RedisCommand<>("AI.TENSORSET", new BooleanReplayConvertor());
41+
RedisCommand<Tensor> AI_TENSORGET = new RedisCommand<>("AI.TENSORGET", new ListMultiDecoder2(new TensorDecoder(), new ObjectListReplayDecoder<>()));
42+
RedisCommand<Boolean> AI_MODELSET = new RedisCommand<>("AI.MODELSET", new BooleanReplayConvertor());
43+
RedisCommand<Model> AI_MODELGET = new RedisCommand<>("AI.MODELGET", new ListMultiDecoder2(new ModelDecoder(), new ObjectListReplayDecoder<>()));
44+
RedisCommand<Boolean> AI_MODELDEL = new RedisCommand<>("AI.MODELDEL", new BooleanReplayConvertor());
45+
RedisCommand<Void> AI_MODELRUN = new RedisCommand<>("AI.MODELRUN", new VoidReplayConvertor());
46+
RedisCommand<Void> AI_MODELSCAN = new RedisCommand<>("AI._MODELSCAN", new VoidReplayConvertor());
47+
RedisCommand<Boolean> AI_SCRIPTSET = new RedisCommand<>("AI.SCRIPTSET", new BooleanReplayConvertor());
48+
RedisCommand<Script> AI_SCRIPTGET = new RedisCommand<>("AI.SCRIPTGET", new ListMultiDecoder2(new ScriptDecoder(), new ObjectListReplayDecoder<>()));
49+
RedisCommand<Boolean> AI_SCRIPTDEL = new RedisCommand<>("AI.SCRIPTDEL", new BooleanReplayConvertor());
50+
RedisCommand<Boolean> AI_SCRIPTRUN = new RedisCommand<>("AI.SCRIPTRUN", new BooleanReplayConvertor());
51+
RedisCommand<Void> AI_SCRIPTSCAN = new RedisCommand<>("AI._SCRIPTSCAN", new VoidReplayConvertor());
52+
RedisCommand<Void> AI_DAGRUN = new RedisCommand<>("AI.DAGRUN", new VoidReplayConvertor());
53+
RedisCommand<Void> AI_DAGRUN_RO = new RedisCommand<>("AI.DAGRUN_RO", new VoidReplayConvertor());
54+
RedisCommand<Map<String, Object>> AI_INFO = new RedisCommand<>("AI.INFO", new ObjectMapReplayDecoder());
55+
RedisCommand<Boolean> AI_INFO_RESETSTAT = new RedisCommand<>("AI.INFO", new BooleanReplayConvertor());
56+
RedisCommand<Boolean> AI_CONFIG = new RedisCommand<>("AI.CONFIG", new BooleanReplayConvertor());
57+
RedisCommand<Boolean> AI_SCRIPTSTORE = new RedisCommand<>("AI.SCRIPTSTORE", new BooleanReplayConvertor());
5258
}

0 commit comments

Comments
 (0)