Skip to content
This repository has been archived by the owner on Apr 24, 2022. It is now read-only.

Commit

Permalink
fixed null pointer exception in sugget
Browse files Browse the repository at this point in the history
  • Loading branch information
jruaux committed Feb 19, 2021
1 parent fb4854f commit ec45f95
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected <A, B, T> Command<A, B, T> createCommand(CommandType type, CommandOutp

public Command<K, V, String> create(K index, CreateOptions<K, V> options, Field<K>... fields) {
assertNotNull(index, "index");
LettuceAssert.isTrue(fields.length>0, "At least one field is required.");
LettuceAssert.isTrue(fields.length > 0, "At least one field is required.");
RediSearchCommandArgs<K, V> args = createArgs(index);
if (options != null) {
options.build(args);
Expand Down Expand Up @@ -167,18 +167,26 @@ public Command<K, V, Long> sugadd(K key, Suggestion<V> suggestion, boolean incre
}

public Command<K, V, List<Suggestion<V>>> sugget(K key, V prefix) {
return sugget(key, prefix, SuggetOptions.builder().build());
return sugget(key, prefix, null);
}

public Command<K, V, List<Suggestion<V>>> sugget(K key, V prefix, SuggetOptions options) {
assertNotNull(key, "key");
assertNotNull(prefix, "prefix");
assertNotNull(options, "options");
RediSearchCommandArgs<K, V> args = new RediSearchCommandArgs<>(codec);
args.addKey(key);
args.addValue(prefix);
options.build(args);
return createCommand(SUGGET, new SuggestOutput<>(codec, options), args);
if (options != null) {
options.build(args);
}
return createCommand(SUGGET, suggestOutput(options), args);
}

private SuggestOutput<K, V> suggestOutput(SuggetOptions options) {
if (options == null) {
return new SuggestOutput<>(codec);
}
return new SuggestOutput<>(codec, options.isWithScores(), options.isWithPayloads());
}

public Command<K, V, Boolean> sugdel(K key, V string) {
Expand Down
36 changes: 23 additions & 13 deletions src/main/java/com/redislabs/lettusearch/output/SuggestOutput.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,61 @@
package com.redislabs.lettusearch.output;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

import com.redislabs.lettusearch.Suggestion;
import com.redislabs.lettusearch.SuggetOptions;

import io.lettuce.core.codec.RedisCodec;
import io.lettuce.core.output.CommandOutput;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

public class SuggestOutput<K, V> extends CommandOutput<K, V, List<Suggestion<V>>> {

private final boolean withScores;
private final boolean withPayloads;
private Suggestion<V> current;
private final SuggetOptions options;
private boolean payloadSet = false;
private boolean scoreSet = false;

public SuggestOutput(RedisCodec<K, V> codec) {
this(codec, false, false);
}

public SuggestOutput(RedisCodec<K, V> codec, SuggetOptions options) {
public SuggestOutput(RedisCodec<K, V> codec, boolean withScores, boolean withPayloads) {
super(codec, new ArrayList<>());
this.options = options;
this.withScores = withScores;
this.withPayloads = withPayloads;
}

@Override
public void set(ByteBuffer bytes) {
if (current == null) {
current = new Suggestion<>();
payloadSet = false;
scoreSet = false;
if (bytes != null) {
current.setString(codec.decodeValue(bytes));
}
output.add(current);
if (!options.isWithScores() && !options.isWithPayloads()) {
if (!withScores && !withPayloads) {
current = null;
}
} else {
if (current.getPayload() == null && options.isWithPayloads()) {
if (withPayloads && !payloadSet) {
if (bytes != null) {
current.setPayload(codec.decodeValue(bytes));
}
payloadSet = true;
current = null;
}
}
}

@Override
public void set(double number) {
if (current.getScore() == null && options.isWithScores()) {
if (withScores && !scoreSet) {
current.setScore(number);
if (!options.isWithPayloads()) {
scoreSet = true;
if (!withPayloads) {
current = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TestSuggestions extends AbstractBaseTest {
public class TestSuggest extends AbstractBaseTest {

@Test
public void testSugget() throws IOException {
createBeerSuggestions();
List<Suggestion<String>> results = sync.sugget(SUGINDEX, "Ame");
assertEquals(5, results.size());
}

@Test
public void testSuggetOptions() throws IOException {
createBeerSuggestions();
List<Suggestion<String>> results = sync.sugget(SUGINDEX, "Ame", SuggetOptions.builder().max(1000L).build());
assertEquals(8, results.size());
Expand Down

0 comments on commit ec45f95

Please sign in to comment.