Skip to content

Commit

Permalink
RediSearch commands implementation (continued) and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 committed Nov 14, 2021
1 parent 7cc507c commit fe7d92c
Show file tree
Hide file tree
Showing 21 changed files with 2,610 additions and 64 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@
<artifactId>gson</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>

<dependency>
<groupId>junit</groupId>
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/redis/clients/jedis/BuilderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ public String toString() {
}
};

public static final Builder<Map<String, Object>> ENCODED_OBJECT_MAP_FROM_PLAIN_LIST = new Builder<Map<String, Object>>() {
@Override
public Map<String, Object> build(Object data) {
final List list = (List) data;
final Map<String, Object> map = new HashMap<>(list.size() / 2, 1);
final Iterator iterator = list.iterator();
while (iterator.hasNext()) {
map.put(STRING.build(iterator.next()), ENCODED_OBJECT.build(iterator.next()));
}
return map;
}
};

public static final Builder<Long> LONG = new Builder<Long>() {
@Override
public Long build(Object data) {
Expand Down Expand Up @@ -1222,6 +1235,27 @@ public LCSMatchResult build(Object data) {
}
};

public static final Builder<Map<String, String>> STRING_MAP_FROM_PAIR_ARRAY = new Builder<Map<String, String>>() {
@Override
@SuppressWarnings("unchecked")
public Map<String, String> build(Object data) {
final List<Object> list = (List<Object>) data;
final Map<String, String> map = new HashMap<>(list.size());
for (Object object : list) {
final List<byte[]> flat = (List<byte[]>) object;
map.put(SafeEncoder.encode(flat.get(0)), flat.get(1) != null ? SafeEncoder.encode(flat.get(1)) : null);
}

return map;
}

@Override
public String toString() {
return "Map<String, String>";
}

};

public static final Builder<Class<?>> JSON_TYPE = new Builder<Class<?>>() {
@Override
public Class<?> build(Object data) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/redis/clients/jedis/CommandArguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public CommandArguments addObjects(Object... args) {
return this;
}

public CommandArguments addObjects(Collection<Object> args) {
public CommandArguments addObjects(Collection args) {
for (Object arg : args) {
add(arg);
}
Expand Down
109 changes: 100 additions & 9 deletions src/main/java/redis/clients/jedis/RedisClusterCommandObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

import static redis.clients.jedis.Protocol.Command.KEYS;
import static redis.clients.jedis.Protocol.Command.SCAN;
import static redis.clients.jedis.Protocol.Command.WAIT;
import static redis.clients.jedis.Protocol.Keyword.TYPE;

import java.util.List;
import java.util.Map;
import java.util.Set;

import redis.clients.jedis.commands.ProtocolCommand;
import redis.clients.jedis.params.ScanParams;
import redis.clients.jedis.resps.ScanResult;
import redis.clients.jedis.search.IndexOptions;
import redis.clients.jedis.search.Query;
import redis.clients.jedis.search.Schema;
import redis.clients.jedis.search.SearchProtocol;
import redis.clients.jedis.search.SearchResult;
import redis.clients.jedis.search.aggr.AggregationBuilder;
import redis.clients.jedis.search.aggr.AggregationResult;
import redis.clients.jedis.util.JedisClusterHashTag;

public class RedisClusterCommandObjects extends RedisCommandObjects {
Expand All @@ -25,6 +28,11 @@ protected ClusterCommandArguments commandArguments(ProtocolCommand command) {

private static final String CLUSTER_UNSUPPORTED_MESSAGE = "Not supported in cluster mode.";

@Override
public CommandObject<Long> dbSize() {
throw new UnsupportedOperationException(CLUSTER_UNSUPPORTED_MESSAGE);
}

private static final String KEYS_PATTERN_MESSAGE = "Cluster mode only supports KEYS command"
+ " with pattern containing hash-tag ( curly-brackets enclosed string )";

Expand Down Expand Up @@ -105,23 +113,106 @@ private <T> CommandObject<T> processSearchCommand(String indexName, CommandObjec
return command;
}

private <T> CommandObject<T> processSearchCommand(byte[] indexName, CommandObject<T> command) {
@Override
public final CommandObject<String> ftCreate(String indexName, IndexOptions indexOptions, Schema schema) {
return processSearchCommand(indexName, super.ftCreate(indexName, indexOptions, schema));
}

@Override
public final CommandObject<String> ftAlter(String indexName, Schema schema) {
return processSearchCommand(indexName, super.ftAlter(indexName, schema));
}

@Override
public final CommandObject<SearchResult> ftSearch(String indexName, Query query) {
return processSearchCommand(indexName, super.ftSearch(indexName, query));
}

@Override
public final CommandObject<SearchResult> ftSearch(byte[] indexName, Query query) {
CommandObject<SearchResult> command = super.ftSearch(indexName, query);
if (searchLite) command.getArguments().processKey(indexName);
return command;
}

@Override
public CommandObject<String> ftCreate(String indexName, IndexOptions indexOptions, Schema schema) {
return processSearchCommand(indexName, super.ftCreate(indexName, indexOptions, schema));
public CommandObject<String> ftExplain(String indexName, Query query) {
return processSearchCommand(indexName, super.ftExplain(indexName, query));
}

@Override
public CommandObject<SearchResult> ftSearch(String indexName, Query query) {
return processSearchCommand(indexName, super.ftSearch(indexName, query));
public CommandObject<List<String>> ftExplainCLI(String indexName, Query query) {
return processSearchCommand(indexName, super.ftExplainCLI(indexName, query));
}

@Override
public CommandObject<SearchResult> ftSearch(byte[] indexName, Query query) {
return processSearchCommand(indexName, super.ftSearch(indexName, query));
public CommandObject<AggregationResult> ftAggregate(String indexName, AggregationBuilder aggr) {
return processSearchCommand(indexName, super.ftAggregate(indexName, aggr));
}

@Override
public CommandObject<AggregationResult> ftCursorRead(String indexName, long cursorId, int count) {
return processSearchCommand(indexName, super.ftCursorRead(indexName, cursorId, count));
}

@Override
public CommandObject<String> ftCursorDel(String indexName, long cursorId) {
return processSearchCommand(indexName, super.ftCursorDel(indexName, cursorId));
}

@Override
public CommandObject<String> ftDropIndex(String indexName) {
return processSearchCommand(indexName, super.ftDropIndex(indexName));
}

@Override
public CommandObject<String> ftDropIndexDD(String indexName) {
return processSearchCommand(indexName, super.ftDropIndexDD(indexName));
}

@Override
public CommandObject<String> ftSynUpdate(String indexName, String synonymGroupId, String... terms) {
return processSearchCommand(indexName, super.ftSynUpdate(indexName, synonymGroupId, terms));
}

@Override
public CommandObject<Map<String, List<Object>>> ftSynDump(String indexName) {
return processSearchCommand(indexName, super.ftSynDump(indexName));
}

@Override
public CommandObject<Map<String, Object>> ftInfo(String indexName) {
return processSearchCommand(indexName, super.ftInfo(indexName));
}

@Override
public CommandObject<String> ftAliasAdd(String aliasName, String indexName) {
CommandObject<String> command = super.ftAliasAdd(aliasName, indexName);
if (searchLite) command.getArguments().processKey(aliasName).processKey(indexName);
return command;
}

@Override
public CommandObject<String> ftAliasUpdate(String aliasName, String indexName) {
CommandObject<String> command = super.ftAliasUpdate(aliasName, indexName);
if (searchLite) command.getArguments().processKey(aliasName).processKey(indexName);
return command;
}

@Override
public CommandObject<String> ftAliasDel(String aliasName) {
CommandObject<String> command = super.ftAliasDel(aliasName);
if (searchLite) command.getArguments().processKey(aliasName);
return command;
}

@Override
public CommandObject<Map<String, String>> ftConfigGet(String indexName, String option) {
return processSearchCommand(indexName, super.ftConfigGet(indexName, option));
}

@Override
public CommandObject<String> ftConfigSet(String indexName, String option, String value) {
return processSearchCommand(indexName, super.ftConfigSet(indexName, option, value));
}
}
70 changes: 64 additions & 6 deletions src/main/java/redis/clients/jedis/RedisCommandObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ public final CommandObject<Long> renamenx(byte[] oldkey, byte[] newkey) {
return new CommandObject<>(commandArguments(RENAMENX).key(oldkey).key(newkey), BuilderFactory.LONG);
}

public CommandObject<Long> dbSize() {
return new CommandObject<>(commandArguments(DBSIZE), BuilderFactory.LONG);
}

public CommandObject<Set<String>> keys(String pattern) {
CommandArguments args = commandArguments(Command.KEYS).key(pattern);
return new CommandObject<>(args, BuilderFactory.STRING_SET);
Expand Down Expand Up @@ -2445,7 +2449,7 @@ public CommandObject<String> ftCreate(String indexName, IndexOptions indexOption
return new CommandObject<>(args, BuilderFactory.STRING);
}

public final CommandObject<String> ftAlter(String indexName, Schema schema) {
public CommandObject<String> ftAlter(String indexName, Schema schema) {
CommandArguments args = commandArguments(SearchCommand.ALTER).add(indexName)
.add(SearchKeyword.SCHEMA).add(SearchKeyword.ADD);
schema.fields.forEach(field -> field.addParams(args));
Expand All @@ -2462,28 +2466,82 @@ public CommandObject<SearchResult> ftSearch(byte[] indexName, Query query) {
new SearchResultBuilder(!query.getNoContent(), query.getWithScores(), query.getWithPayloads(), false));
}

public final CommandObject<AggregationResult> ftAggregate(String indexName, AggregationBuilder aggr) {
public CommandObject<String> ftExplain(String indexName, Query query) {
return new CommandObject<>(commandArguments(SearchCommand.EXPLAIN).add(indexName).addParams(query), BuilderFactory.STRING);
}

public CommandObject<List<String>> ftExplainCLI(String indexName, Query query) {
return new CommandObject<>(commandArguments(SearchCommand.EXPLAINCLI).add(indexName).addParams(query), BuilderFactory.STRING_LIST);
}

public CommandObject<AggregationResult> ftAggregate(String indexName, AggregationBuilder aggr) {
return new CommandObject<>(commandArguments(SearchCommand.AGGREGATE).add(indexName).addObjects(aggr.getArgs()),
!aggr.isWithCursor() ? BuilderFactory.SEARCH_AGGREGATION_RESULT : BuilderFactory.SEARCH_AGGREGATION_RESULT_WITH_CURSOR);
}

public final CommandObject<String> ftDropIndex(String indexName) {
public CommandObject<AggregationResult> ftCursorRead(String indexName, long cursorId, int count) {
return new CommandObject<>(commandArguments(SearchCommand.CURSOR).add(SearchKeyword.READ)
.add(indexName).add(cursorId).add(count), BuilderFactory.SEARCH_AGGREGATION_RESULT_WITH_CURSOR);
}

public CommandObject<String> ftCursorDel(String indexName, long cursorId) {
return new CommandObject<>(commandArguments(SearchCommand.CURSOR).add(SearchKeyword.DEL)
.add(indexName).add(cursorId), BuilderFactory.STRING);
}

public CommandObject<String> ftDropIndex(String indexName) {
return new CommandObject<>(commandArguments(SearchCommand.DROPINDEX).add(indexName), BuilderFactory.STRING);
}

public final CommandObject<String> ftDropIndexDD(String indexName) {
public CommandObject<String> ftDropIndexDD(String indexName) {
return new CommandObject<>(commandArguments(SearchCommand.DROPINDEX).add(indexName).add(SearchKeyword.DD), BuilderFactory.STRING);
}

public final CommandObject<String> ftSynUpdate(String indexName, String synonymGroupId, String... terms) {
public CommandObject<String> ftSynUpdate(String indexName, long synonymGroupId, String... terms) {
return new CommandObject<>(commandArguments(SearchCommand.SYNUPDATE).add(indexName)
.add(synonymGroupId).addObjects((Object[]) terms), BuilderFactory.STRING);
}

public final CommandObject<Map<String, List<Object>>> ftSynDump(String indexName) {
public CommandObject<String> ftSynUpdate(String indexName, String synonymGroupId, String... terms) {
return new CommandObject<>(commandArguments(SearchCommand.SYNUPDATE).add(indexName)
.add(synonymGroupId).addObjects((Object[]) terms), BuilderFactory.STRING);
}

public CommandObject<Map<String, List<Object>>> ftSynDump(String indexName) {
return new CommandObject<>(commandArguments(SearchCommand.SYNDUMP).add(indexName), BuilderFactory.SEARCH_SYNONYM_GROUPS);
}

public CommandObject<Map<String, Object>> ftInfo(String indexName) {
return new CommandObject<>(commandArguments(SearchCommand.INFO).add(indexName), BuilderFactory.ENCODED_OBJECT_MAP_FROM_PLAIN_LIST);
}

public CommandObject<String> ftAliasAdd(String aliasName, String indexName) {
return new CommandObject<>(commandArguments(SearchCommand.ALIASADD).add(aliasName).add(indexName), BuilderFactory.STRING);
}

public CommandObject<String> ftAliasUpdate(String aliasName, String indexName) {
return new CommandObject<>(commandArguments(SearchCommand.ALIASUPDATE).add(aliasName).add(indexName), BuilderFactory.STRING);
}

public CommandObject<String> ftAliasDel(String aliasName) {
return new CommandObject<>(commandArguments(SearchCommand.ALIASDEL).add(aliasName), BuilderFactory.STRING);
}

public final CommandObject<Map<String, String>> ftConfigGet(String option) {
return new CommandObject<>(commandArguments(SearchCommand.CONFIG).add(SearchKeyword.GET).add(option), BuilderFactory.STRING_MAP_FROM_PAIR_ARRAY);
}

public CommandObject<Map<String, String>> ftConfigGet(String indexName, String option) {
return ftConfigGet(option);
}

public final CommandObject<String> ftConfigSet(String option, String value) {
return new CommandObject<>(commandArguments(SearchCommand.CONFIG).add(SearchKeyword.SET).add(option).add(value), BuilderFactory.STRING);
}

public CommandObject<String> ftConfigSet(String indexName, String option, String value) {
return ftConfigSet(option, value);
}
// RediSearch commands

// RedisJSON commands
Expand Down
Loading

0 comments on commit fe7d92c

Please sign in to comment.