Skip to content

Commit

Permalink
fix: Lcs response parse
Browse files Browse the repository at this point in the history
* Handle parsing `len` and `matches` in any order;
  • Loading branch information
jabolina committed Aug 20, 2024
1 parent 9495478 commit 287d2c5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/main/java/io/lettuce/core/output/StringMatchResultOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static io.lettuce.core.StringMatchResult.Position;

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

Expand All @@ -37,6 +38,8 @@
*/
public class StringMatchResultOutput<K, V> extends CommandOutput<K, V, StringMatchResult> {

private static final ByteBuffer LEN = StandardCharsets.US_ASCII.encode("len");

private final boolean withIdx;

private String matchString;
Expand All @@ -45,6 +48,8 @@ public class StringMatchResultOutput<K, V> extends CommandOutput<K, V, StringMat

private List<Long> positions;

private boolean readingLen = true;

private final List<MatchedPosition> matchedPositions = new ArrayList<>();

public StringMatchResultOutput(RedisCodec<K, V> codec, boolean withIdx) {
Expand All @@ -57,18 +62,23 @@ public void set(ByteBuffer bytes) {

if (!withIdx && matchString == null) {
matchString = (String) codec.decodeKey(bytes);
} else {
readingLen = LEN.equals(bytes);
}
}

@Override
public void set(long integer) {

this.len = (int) integer;

if (positions == null) {
positions = new ArrayList<>();
if (readingLen) {
this.len = (int) integer;
} else {
if (positions == null) {
positions = new ArrayList<>();
}
positions.add(integer);
}
positions.add(integer);

}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.lettuce.core.output;


import static org.assertj.core.api.Assertions.assertThat;

import java.nio.ByteBuffer;

import org.junit.jupiter.api.Test;

import io.lettuce.core.StringMatchResult;
import io.lettuce.core.codec.StringCodec;

public class StringMatchResultOutputUnitTests {

@Test
void parseLenAndMatches() {
StringMatchResultOutput<String, String> output = new StringMatchResultOutput<>(new StringCodec(), false);

output.set(ByteBuffer.wrap("len".getBytes()));
output.set(42);

output.set(ByteBuffer.wrap("matches".getBytes()));
output.set(0);
output.set(5);
output.set(10);
output.set(15);

output.complete(2);
output.complete(0);

StringMatchResult result = output.get();

assertThat(result.getLen()).isEqualTo(42);

assertThat(result.getMatches()).hasSize(1)
.satisfies(m -> assertMatchedPositions(m.get(0), 0, 5, 10, 15));
}

private void assertMatchedPositions(StringMatchResult.MatchedPosition match, int ... expected) {
assertThat(match.getA().getStart()).isEqualTo(expected[0]);
assertThat(match.getA().getEnd()).isEqualTo(expected[1]);
assertThat(match.getB().getStart()).isEqualTo(expected[2]);
assertThat(match.getB().getEnd()).isEqualTo(expected[3]);
}
}

0 comments on commit 287d2c5

Please sign in to comment.