Skip to content

Commit

Permalink
Feature: Triggers and functions commands
Browse files Browse the repository at this point in the history
  • Loading branch information
bsbodden committed Sep 15, 2023
1 parent 2cd2601 commit b9de0e3
Show file tree
Hide file tree
Showing 20 changed files with 1,519 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/main/java/redis/clients/jedis/BuilderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.stream.Collectors;

import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.gears.resps.GearsLibraryInfo;
import redis.clients.jedis.resps.*;
import redis.clients.jedis.resps.LCSMatchResult.MatchedPosition;
import redis.clients.jedis.resps.LCSMatchResult.Position;
Expand Down Expand Up @@ -1730,6 +1731,14 @@ public List<LibraryInfo> build(Object data) {
}
};

public static final Builder<List<GearsLibraryInfo>> GEARS_LIBRARY_LIST = new Builder<List<GearsLibraryInfo>>() {
@Override
public List<GearsLibraryInfo> build(Object data) {
List<Object> list = (List<Object>) data;
return list.stream().map(o -> GearsLibraryInfo.LIBRARY_BUILDER.build(o)).collect(Collectors.toList());
}
};

public static final Builder<List<List<String>>> STRING_LIST_LIST = new Builder<List<List<String>>>() {
@Override
@SuppressWarnings("unchecked")
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/redis/clients/jedis/CommandObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static redis.clients.jedis.Protocol.Command.*;
import static redis.clients.jedis.Protocol.Keyword.*;
import static redis.clients.jedis.gears.RedisGearsProtocol.GearsCommand.TFCALL;
import static redis.clients.jedis.gears.RedisGearsProtocol.GearsCommand.TFCALLASYNC;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -15,6 +17,11 @@
import redis.clients.jedis.bloom.*;
import redis.clients.jedis.bloom.RedisBloomProtocol.*;
import redis.clients.jedis.commands.ProtocolCommand;
import redis.clients.jedis.gears.RedisGearsProtocol.GearsKeyword;
import redis.clients.jedis.gears.RedisGearsProtocol.GearsCommand;
import redis.clients.jedis.gears.TFunctionListParams;
import redis.clients.jedis.gears.TFunctionLoadParams;
import redis.clients.jedis.gears.resps.GearsLibraryInfo;
import redis.clients.jedis.graph.GraphProtocol.*;
import redis.clients.jedis.json.*;
import redis.clients.jedis.json.JsonProtocol.JsonCommand;
Expand Down Expand Up @@ -4163,6 +4170,51 @@ public final CommandObject<Map<String, Object>> graphConfigGet(String configName
}
// RedisGraph commands

// RedisGears commands

public final CommandObject<String> tFunctionLoad(String libraryCode, TFunctionLoadParams params) {
CommandArguments args = commandArguments(GearsCommand.TFUNCTION);
args.add(GearsKeyword.LOAD.getValue());
params.addParams(args);
args.add(libraryCode);

return new CommandObject<>(args, BuilderFactory.STRING);
}

public final CommandObject<String> tFunctionDelete(String libraryName) {
CommandArguments args = commandArguments(GearsCommand.TFUNCTION);
args.add(GearsKeyword.DELETE.getValue());
args.add(libraryName);

return new CommandObject<>(args, BuilderFactory.STRING);
}

public final CommandObject<List<GearsLibraryInfo>> tFunctionList(TFunctionListParams params) {
CommandArguments args = commandArguments(GearsCommand.TFUNCTION);
args.add(GearsKeyword.LIST.getValue());
params.addParams(args);

return new CommandObject<>(args, BuilderFactory.GEARS_LIBRARY_LIST);
}

public final CommandObject<Object> tFunctionCall(String library, String function, List<String> keys, List<String> args) {
String[] keysArray = keys.toArray(new String[keys.size()]);
String[] argsArray = args.toArray(new String[args.size()]);
return new CommandObject<>(commandArguments(TFCALL).add(library+"."+function).add(keysArray.length)
.keys((Object[]) keysArray).addObjects((Object[]) argsArray),
BuilderFactory.ENCODED_OBJECT);
}

public final CommandObject<Object> tFunctionCallAsync(String library, String function, List<String> keys, List<String> args) {
String[] keysArray = keys.toArray(new String[keys.size()]);
String[] argsArray = args.toArray(new String[args.size()]);
return new CommandObject<>(commandArguments(TFCALLASYNC).add(library+"."+function).add(keysArray.length)
.keys((Object[]) keysArray).addObjects((Object[]) argsArray),
BuilderFactory.ENCODED_OBJECT);
}

// RedisGears commands

/**
* Get the instance for JsonObjectMapper if not null, otherwise a new instance reference with
* default implementation will be created and returned.
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/redis/clients/jedis/UnifiedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import redis.clients.jedis.commands.RedisModuleCommands;
import redis.clients.jedis.exceptions.JedisException;
import redis.clients.jedis.executors.*;
import redis.clients.jedis.gears.TFunctionListParams;
import redis.clients.jedis.gears.TFunctionLoadParams;
import redis.clients.jedis.gears.resps.GearsLibraryInfo;
import redis.clients.jedis.graph.GraphCommandObjects;
import redis.clients.jedis.graph.ResultSet;
import redis.clients.jedis.json.JsonSetParams;
Expand Down Expand Up @@ -4807,4 +4810,43 @@ public void setJsonObjectMapper(JsonObjectMapper jsonObjectMapper) {
public void setDefaultSearchDialect(int dialect) {
this.commandObjects.setDefaultSearchDialect(dialect);
}

// RedisGears commands

@Override
public String tFunctionLoad(String libraryCode) {
return executeCommand(commandObjects.tFunctionLoad(libraryCode, TFunctionLoadParams.loadParams()));
}

@Override
public String tFunctionLoad(String libraryCode, TFunctionLoadParams params) {
return executeCommand(commandObjects.tFunctionLoad(libraryCode, params));
}

@Override
public String tFunctionDelete(String libraryName) {
return executeCommand(commandObjects.tFunctionDelete(libraryName));
}

@Override
public List<GearsLibraryInfo> tFunctionList() {
return executeCommand(commandObjects.tFunctionList(TFunctionListParams.listParams()));
}

@Override
public List<GearsLibraryInfo> tFunctionList(TFunctionListParams params) {
return executeCommand(commandObjects.tFunctionList(params));
}

@Override
public Object tFunctionCall(String library, String function, List<String> keys, List<String> args) {
return executeCommand(commandObjects.tFunctionCall(library, function, keys, args));
}

@Override
public Object tFunctionCallAsync(String library, String function, List<String> keys, List<String> args) {
return executeCommand(commandObjects.tFunctionCallAsync(library, function, keys, args));
}

// RedisGears commands
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package redis.clients.jedis.commands;

import redis.clients.jedis.bloom.commands.RedisBloomCommands;
import redis.clients.jedis.gears.RedisGearsCommands;
import redis.clients.jedis.graph.RedisGraphCommands;
import redis.clients.jedis.json.commands.RedisJsonCommands;
import redis.clients.jedis.search.RediSearchCommands;
Expand All @@ -11,6 +12,7 @@ public interface RedisModuleCommands extends
RedisJsonCommands,
RedisTimeSeriesCommands,
RedisBloomCommands,
RedisGraphCommands {
RedisGraphCommands,
RedisGearsCommands {

}
15 changes: 15 additions & 0 deletions src/main/java/redis/clients/jedis/gears/RedisGearsCommands.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package redis.clients.jedis.gears;

import redis.clients.jedis.gears.resps.GearsLibraryInfo;

import java.util.List;

public interface RedisGearsCommands {
String tFunctionLoad(String libraryCode);
String tFunctionLoad(String libraryCode, TFunctionLoadParams params);
List<GearsLibraryInfo> tFunctionList(TFunctionListParams params);
List<GearsLibraryInfo> tFunctionList();
String tFunctionDelete(String libraryName);
Object tFunctionCall(String library, String function, List<String> keys, List<String> args);
Object tFunctionCallAsync(String library, String function, List<String> keys, List<String> args);
}
44 changes: 44 additions & 0 deletions src/main/java/redis/clients/jedis/gears/RedisGearsProtocol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package redis.clients.jedis.gears;

import redis.clients.jedis.commands.ProtocolCommand;
import redis.clients.jedis.util.SafeEncoder;

public class RedisGearsProtocol {
public enum GearsCommand implements ProtocolCommand {
TFUNCTION("TFUNCTION"),
TFCALL("TFCALL"),
TFCALLASYNC("TFCALLASYNC");

private final byte[] raw;

GearsCommand(String alt) {
raw = SafeEncoder.encode(alt);
}

@Override
public byte[] getRaw() {
return raw;
}
}

public enum GearsKeyword {
CONFIG("CONFIG"),
REPLACE("REPLACE"),
LOAD("LOAD"),
DELETE("DELETE"),
LIST("LIST"),
WITHCODE("WITHCODE"),
LIBRARY("LIBRARY"),
VERBOSE("VERBOSE");

private final String value;

GearsKeyword(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}
}
50 changes: 50 additions & 0 deletions src/main/java/redis/clients/jedis/gears/TFunctionListParams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package redis.clients.jedis.gears;

import redis.clients.jedis.CommandArguments;
import redis.clients.jedis.gears.RedisGearsProtocol.GearsKeyword;
import redis.clients.jedis.params.IParams;

import java.util.Collections;

public class TFunctionListParams implements IParams {
private boolean withCode = false;
private int verbose;
private String libraryName;

public static TFunctionListParams listParams() {
return new TFunctionListParams();
}

@Override
public void addParams(CommandArguments args) {
if (withCode) {
args.add(GearsKeyword.WITHCODE.getValue());
}

if (verbose > 0 && verbose < 4) {
args.add(String.join("", Collections.nCopies(verbose, "v")));
} else if (verbose != 0) { // verbose == 0 is the default, so we don't need to throw an error
throw new IllegalArgumentException("verbose must be between 1 and 3");
}

if (libraryName != null) {
args.add(GearsKeyword.LIBRARY);
args.add(libraryName);
}
}

public TFunctionListParams withCode() {
this.withCode = true;
return this;
}

public TFunctionListParams verbose(int verbose) {
this.verbose = verbose;
return this;
}

public TFunctionListParams library(String libraryName) {
this.libraryName = libraryName;
return this;
}
}
36 changes: 36 additions & 0 deletions src/main/java/redis/clients/jedis/gears/TFunctionLoadParams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package redis.clients.jedis.gears;

import redis.clients.jedis.CommandArguments;
import redis.clients.jedis.gears.RedisGearsProtocol.GearsKeyword;
import redis.clients.jedis.params.IParams;

public class TFunctionLoadParams implements IParams {
private boolean replace = false;
private String config;

public static TFunctionLoadParams loadParams() {
return new TFunctionLoadParams();
}

@Override
public void addParams(CommandArguments args) {
if (replace) {
args.add(GearsKeyword.REPLACE.getValue());
}

if (config != null && !config.isEmpty()) {
args.add(GearsKeyword.CONFIG.getValue());
args.add(config);
}
}

public TFunctionLoadParams replace() {
this.replace = true;
return this;
}

public TFunctionLoadParams withConfig(String config) {
this.config = config;
return this;
}
}
Loading

0 comments on commit b9de0e3

Please sign in to comment.