
io.lettuce.core.output.StringMatchResultOutput Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lettuce-core Show documentation
Show all versions of lettuce-core Show documentation
Advanced and thread-safe Java Redis client for synchronous, asynchronous, and
reactive usage. Supports Cluster, Sentinel, Pipelining, Auto-Reconnect, Codecs
and much more.
The newest version!
/*
* Copyright 2018-Present, Redis Ltd. and Contributors
* All rights reserved.
*
* Licensed under the MIT License.
*
* This file contains contributions from third-party contributors
* licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.lettuce.core.output;
import io.lettuce.core.StringMatchResult;
import io.lettuce.core.codec.RedisCodec;
import io.lettuce.core.protocol.CommandKeyword;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import static io.lettuce.core.StringMatchResult.MatchedPosition;
import static io.lettuce.core.StringMatchResult.Position;
/**
* Command output for {@code STRALGO} returning {@link StringMatchResult}.
*
* @author dengliming
* @since 6.0
*/
public class StringMatchResultOutput extends CommandOutput {
private static final ByteBuffer LEN = StandardCharsets.US_ASCII.encode(CommandKeyword.LEN.toString().toLowerCase());
private String matchString;
private int len;
private List positions;
private boolean readingLen = true;
private final List matchedPositions = new ArrayList<>();
public StringMatchResultOutput(RedisCodec codec) {
super(codec, null);
}
@Override
public void set(ByteBuffer bytes) {
matchString = (String) codec.decodeKey(bytes);
readingLen = LEN.equals(bytes);
}
@Override
public void set(long integer) {
if (readingLen) {
this.len = (int) integer;
} else {
if (positions == null) {
positions = new ArrayList<>();
}
positions.add(integer);
}
matchString = null;
}
@Override
public void complete(int depth) {
if (depth == 2) {
matchedPositions.add(buildMatchedString(positions));
positions = null;
}
if (depth == 0) {
output = new StringMatchResult(matchString, matchedPositions, len);
}
}
private static MatchedPosition buildMatchedString(List positions) {
if (positions == null) {
throw new IllegalStateException("No matched positions");
}
int size = positions.size();
// not WITHMATCHLEN
long matchLen = size % 2 == 0 ? 0L : positions.get(size - 1);
return new MatchedPosition(new Position(positions.get(0), positions.get(1)),
new Position(positions.get(2), positions.get(3)), matchLen);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy