org.springframework.data.redis.connection.jedis.StreamConverters Maven / Gradle / Ivy
/*
* Copyright 2021-2022 the original author or authors.
*
* 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 org.springframework.data.redis.connection.jedis;
import redis.clients.jedis.StreamEntry;
import redis.clients.jedis.StreamEntryID;
import redis.clients.jedis.StreamPendingEntry;
import redis.clients.jedis.util.SafeEncoder;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.data.domain.Range;
import org.springframework.data.redis.connection.stream.ByteRecord;
import org.springframework.data.redis.connection.stream.Consumer;
import org.springframework.data.redis.connection.stream.PendingMessage;
import org.springframework.data.redis.connection.stream.PendingMessages;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.connection.stream.StreamOffset;
import org.springframework.data.redis.connection.stream.StreamRecords;
/**
* Converters for Redis Stream-specific types.
*
* Converters typically convert between value objects/argument objects retaining the actual types of values (i.e. no
* serialization/deserialization happens here).
*
* @author dengliming
* @author Mark Paluch
* @since 2.3
*/
class StreamConverters {
static byte[][] entryIdsToBytes(List recordIds) {
byte[][] target = new byte[recordIds.size()][];
for (int i = 0; i < recordIds.size(); ++i) {
RecordId id = recordIds.get(i);
target[i] = JedisConverters.toBytes(id.getValue());
}
return target;
}
static String getLowerValue(Range range) {
return getValue(range.getLowerBound(), "-");
}
static String getUpperValue(Range range) {
return getValue(range.getUpperBound(), "+");
}
private static String getValue(Range.Bound bound, String fallbackValue) {
if (bound.equals(Range.Bound.unbounded())) {
return fallbackValue;
}
return bound.getValue().map(it -> bound.isInclusive() ? it : "(" + it).orElse(fallbackValue);
}
static List