From e452ddee722f4b64516f98338dbdb33cf2998619 Mon Sep 17 00:00:00 2001 From: jruaux Date: Thu, 15 Feb 2024 21:54:22 -0800 Subject: [PATCH] refactor: Introduced LongScoredValue to replace key/long pairs --- .../RedisModulesAsyncCommandsImpl.java | 19 +- .../RedisModulesReactiveCommandsImpl.java | 19 +- .../api/async/RedisBloomAsyncCommands.java | 7 +- .../reactive/RedisBloomReactiveCommands.java | 9 +- .../api/sync/RedisBloomCommands.java | 8 +- .../lettucemod/bloom/BloomCommandBuilder.java | 42 +++-- .../lettucemod/bloom/LongScoredValue.java | 173 ++++++++++++++++++ ...dulesAdvancedClusterAsyncCommandsImpl.java | 13 +- ...esAdvancedClusterReactiveCommandsImpl.java | 17 +- .../redis/lettucemod/output/GetOutput.java | 6 +- .../lettucemod/search/BaseSearchOptions.java | 16 +- .../redis/lettucemod/search/Parameter.java | 31 ---- .../lettucemod/timeseries/BaseOptions.java | 23 +-- .../lettucemod/timeseries/GetResult.java | 8 +- .../redis/lettucemod/timeseries/Label.java | 50 ----- .../com/redis/lettucemod/ModulesTests.java | 25 +-- .../java/com/redis/lettucemod/StackTests.java | 10 +- 17 files changed, 287 insertions(+), 189 deletions(-) create mode 100644 subprojects/lettucemod/src/main/java/com/redis/lettucemod/bloom/LongScoredValue.java delete mode 100644 subprojects/lettucemod/src/main/java/com/redis/lettucemod/search/Parameter.java delete mode 100644 subprojects/lettucemod/src/main/java/com/redis/lettucemod/timeseries/Label.java diff --git a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/RedisModulesAsyncCommandsImpl.java b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/RedisModulesAsyncCommandsImpl.java index b6e9dac..c0ea06d 100644 --- a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/RedisModulesAsyncCommandsImpl.java +++ b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/RedisModulesAsyncCommandsImpl.java @@ -13,6 +13,7 @@ import com.redis.lettucemod.bloom.CuckooFilter; import com.redis.lettucemod.bloom.CuckooFilterInsertOptions; import com.redis.lettucemod.bloom.CuckooFilterReserveOptions; +import com.redis.lettucemod.bloom.LongScoredValue; import com.redis.lettucemod.bloom.TDigestInfo; import com.redis.lettucemod.bloom.TDigestMergeOptions; import com.redis.lettucemod.bloom.TopKInfo; @@ -713,10 +714,10 @@ public RedisFuture cfReserve(K key, long capacity, CuckooFilterReserveOp public RedisFuture cmsIncrBy(K key, V item, long increment) { return dispatch(bloomCommandBuilder.cmsIncrBy(key, item, increment)); } - + @Override - public RedisFuture> cmsIncrBy(K key, Map increments) { - return dispatch(bloomCommandBuilder.cmsIncrBy(key, increments)); + public RedisFuture> cmsIncrBy(K key, LongScoredValue... itemIncrements) { + return dispatch(bloomCommandBuilder.cmsIncrBy(key, itemIncrements)); } @Override @@ -738,10 +739,10 @@ public RedisFuture> cmsQuery(K key, V... items) { public RedisFuture cmsMerge(K destKey, K... keys) { return dispatch(bloomCommandBuilder.cmsMerge(destKey, keys)); } - + @Override - public RedisFuture cmsMerge(K destKey, Map keyWeightMap) { - return dispatch(bloomCommandBuilder.cmsMerge(destKey, keyWeightMap)); + public RedisFuture cmsMerge(K destKey, LongScoredValue... sourceKeyWeights) { + return dispatch(bloomCommandBuilder.cmsMerge(destKey, sourceKeyWeights)); } @Override @@ -753,10 +754,10 @@ public RedisFuture cmsInfo(K key) { public RedisFuture>> topKAdd(K key, V... items) { return dispatch(bloomCommandBuilder.topKAdd(key, items)); } - + @Override - public RedisFuture>> topKIncrBy(K key, Map increments) { - return dispatch(bloomCommandBuilder.topKIncrBy(key, increments)); + public RedisFuture>> topKIncrBy(K key, LongScoredValue... itemIncrements) { + return dispatch(bloomCommandBuilder.topKIncrBy(key, itemIncrements)); } @Override diff --git a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/RedisModulesReactiveCommandsImpl.java b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/RedisModulesReactiveCommandsImpl.java index c99e34d..4c5f734 100644 --- a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/RedisModulesReactiveCommandsImpl.java +++ b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/RedisModulesReactiveCommandsImpl.java @@ -12,6 +12,7 @@ import com.redis.lettucemod.bloom.CuckooFilter; import com.redis.lettucemod.bloom.CuckooFilterInsertOptions; import com.redis.lettucemod.bloom.CuckooFilterReserveOptions; +import com.redis.lettucemod.bloom.LongScoredValue; import com.redis.lettucemod.bloom.TDigestInfo; import com.redis.lettucemod.bloom.TDigestMergeOptions; import com.redis.lettucemod.bloom.TopKInfo; @@ -440,10 +441,10 @@ public Flux ftDictdump(K dict) { public Flux> topKAdd(K key, V... items) { return createDissolvingFlux(() -> bloomCommandBuilder.topKAdd(key, items)); } - + @Override - public Flux> topKIncrBy(K key, Map increments) { - return createDissolvingFlux(() -> bloomCommandBuilder.topKIncrBy(key, increments)); + public Flux> topKIncrBy(K key, LongScoredValue... itemIncrements) { + return createDissolvingFlux(() -> bloomCommandBuilder.topKIncrBy(key, itemIncrements)); } @Override @@ -824,10 +825,10 @@ public Mono cfReserve(K key, long capacity, CuckooFilterReserveOptions o public Mono cmsIncrBy(K key, V item, long increment) { return createMono(() -> bloomCommandBuilder.cmsIncrBy(key, item, increment)); } - + @Override - public Flux cmsIncrBy(K key, Map increments) { - return createDissolvingFlux(() -> bloomCommandBuilder.cmsIncrBy(key, increments)); + public Flux cmsIncrBy(K key, LongScoredValue... itemIncrements) { + return createDissolvingFlux(() -> bloomCommandBuilder.cmsIncrBy(key, itemIncrements)); } @Override @@ -849,10 +850,10 @@ public Flux cmsQuery(K key, V... items) { public Mono cmsMerge(K destKey, K... keys) { return createMono(() -> bloomCommandBuilder.cmsMerge(destKey, keys)); } - + @Override - public Mono cmsMerge(K destKey, Map keyWeightMap) { - return createMono(() -> bloomCommandBuilder.cmsMerge(destKey, keyWeightMap)); + public Mono cmsMerge(K destKey, LongScoredValue... sourceKeyWeights) { + return createMono(() -> bloomCommandBuilder.cmsMerge(destKey, sourceKeyWeights)); } @Override diff --git a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/api/async/RedisBloomAsyncCommands.java b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/api/async/RedisBloomAsyncCommands.java index 15589d8..036d471 100644 --- a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/api/async/RedisBloomAsyncCommands.java +++ b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/api/async/RedisBloomAsyncCommands.java @@ -10,6 +10,7 @@ import com.redis.lettucemod.bloom.CuckooFilter; import com.redis.lettucemod.bloom.CuckooFilterInsertOptions; import com.redis.lettucemod.bloom.CuckooFilterReserveOptions; +import com.redis.lettucemod.bloom.LongScoredValue; import com.redis.lettucemod.bloom.TDigestInfo; import com.redis.lettucemod.bloom.TDigestMergeOptions; import com.redis.lettucemod.bloom.TopKInfo; @@ -72,7 +73,7 @@ public interface RedisBloomAsyncCommands { RedisFuture cmsIncrBy(K key, V item, long increment); - RedisFuture> cmsIncrBy(K key, Map increments); + RedisFuture> cmsIncrBy(K key, LongScoredValue... itemIncrements); RedisFuture cmsInitByProb(K key, double error, double probability); @@ -82,13 +83,13 @@ public interface RedisBloomAsyncCommands { RedisFuture cmsMerge(K destKey, K... keys); - RedisFuture cmsMerge(K destKey, Map keyWeightMap); + RedisFuture cmsMerge(K destKey, LongScoredValue... sourceKeyWeights); RedisFuture cmsInfo(K key); RedisFuture>> topKAdd(K key, V... items); - RedisFuture>> topKIncrBy(K key, Map increments); + RedisFuture>> topKIncrBy(K key, LongScoredValue... itemIncrements); RedisFuture topKInfo(K key); diff --git a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/api/reactive/RedisBloomReactiveCommands.java b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/api/reactive/RedisBloomReactiveCommands.java index 917794c..4ce060f 100644 --- a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/api/reactive/RedisBloomReactiveCommands.java +++ b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/api/reactive/RedisBloomReactiveCommands.java @@ -1,7 +1,5 @@ package com.redis.lettucemod.api.reactive; -import java.util.Map; - import com.redis.lettucemod.bloom.BloomFilterInfo; import com.redis.lettucemod.bloom.BloomFilterInfoType; import com.redis.lettucemod.bloom.BloomFilterInsertOptions; @@ -9,6 +7,7 @@ import com.redis.lettucemod.bloom.CuckooFilter; import com.redis.lettucemod.bloom.CuckooFilterInsertOptions; import com.redis.lettucemod.bloom.CuckooFilterReserveOptions; +import com.redis.lettucemod.bloom.LongScoredValue; import com.redis.lettucemod.bloom.TDigestInfo; import com.redis.lettucemod.bloom.TDigestMergeOptions; import com.redis.lettucemod.bloom.TopKInfo; @@ -72,7 +71,7 @@ public interface RedisBloomReactiveCommands { Mono cmsIncrBy(K key, V item, long increment); - Flux cmsIncrBy(K key, Map increments); + Flux cmsIncrBy(K key, LongScoredValue... itemIncrements); Mono cmsInitByProb(K key, double error, double probability); @@ -82,13 +81,13 @@ public interface RedisBloomReactiveCommands { Mono cmsMerge(K destKey, K... keys); - Mono cmsMerge(K destKey, Map keyWeightMap); + Mono cmsMerge(K destKey, LongScoredValue... sourceKeyWeights); Mono cmsInfo(K key); Flux> topKAdd(K key, V... items); - Flux> topKIncrBy(K key, Map increments); + Flux> topKIncrBy(K key, LongScoredValue... itemIncrements); Mono topKInfo(K key); diff --git a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/api/sync/RedisBloomCommands.java b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/api/sync/RedisBloomCommands.java index 164a492..9594815 100644 --- a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/api/sync/RedisBloomCommands.java +++ b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/api/sync/RedisBloomCommands.java @@ -1,7 +1,6 @@ package com.redis.lettucemod.api.sync; import java.util.List; -import java.util.Map; import com.redis.lettucemod.bloom.BloomFilterInfo; import com.redis.lettucemod.bloom.BloomFilterInfoType; @@ -10,6 +9,7 @@ import com.redis.lettucemod.bloom.CuckooFilter; import com.redis.lettucemod.bloom.CuckooFilterInsertOptions; import com.redis.lettucemod.bloom.CuckooFilterReserveOptions; +import com.redis.lettucemod.bloom.LongScoredValue; import com.redis.lettucemod.bloom.TDigestInfo; import com.redis.lettucemod.bloom.TDigestMergeOptions; import com.redis.lettucemod.bloom.TopKInfo; @@ -71,7 +71,7 @@ public interface RedisBloomCommands { Long cmsIncrBy(K key, V item, long increment); - List cmsIncrBy(K key, Map increments); + List cmsIncrBy(K key, LongScoredValue... itemIncrements); String cmsInitByProb(K key, double error, double probability); @@ -81,13 +81,13 @@ public interface RedisBloomCommands { String cmsMerge(K destKey, K... keys); - String cmsMerge(K destKey, Map keyWeightMap); + String cmsMerge(K destKey, LongScoredValue... sourceKeyWeights); CmsInfo cmsInfo(K key); List> topKAdd(K key, V... items); - List> topKIncrBy(K key, Map increments); + List> topKIncrBy(K key, LongScoredValue... itemIncrements); TopKInfo topKInfo(K key); diff --git a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/bloom/BloomCommandBuilder.java b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/bloom/BloomCommandBuilder.java index 66d1b17..278a79f 100644 --- a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/bloom/BloomCommandBuilder.java +++ b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/bloom/BloomCommandBuilder.java @@ -1,7 +1,8 @@ package com.redis.lettucemod.bloom; import java.util.List; -import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; import com.redis.lettucemod.RedisModulesCommandBuilder; import com.redis.lettucemod.cms.CmsInfo; @@ -234,11 +235,12 @@ public Command cmsIncrBy(K key, V item, long increment) { return createCommand(CountMinSketchCommandType.INCRBY, new IntegerOutput<>(codec), args); } - public Command> cmsIncrBy(K key, Map increments) { + @SuppressWarnings("unchecked") + public Command> cmsIncrBy(K key, LongScoredValue... increments) { CommandArgs args = args(key); - for (Map.Entry entry : increments.entrySet()) { - args.addValue(entry.getKey()); - args.add(entry.getValue()); + for (LongScoredValue increment : increments) { + args.addValue(increment.getValue()); + args.add(increment.getScore()); } return createCommand(CountMinSketchCommandType.INCRBY, new IntegerListOutput<>(codec), args); } @@ -265,22 +267,22 @@ public Command> cmsQuery(K key, V... items) { } @SuppressWarnings("unchecked") - public Command cmsMerge(K desKey, K... keys) { + public Command cmsMerge(K desKey, K... sourceKeys) { + LettuceAssert.isTrue(sourceKeys.length > 0, "At least one source is required"); CommandArgs args = args(desKey); - args.add(keys.length); - args.addKeys(keys); + args.add(sourceKeys.length); + args.addKeys(sourceKeys); return createCommand(CountMinSketchCommandType.MERGE, new StatusOutput<>(codec), args); } - public Command cmsMerge(K destKey, Map keyWeightMap) { + @SuppressWarnings("unchecked") + public Command cmsMerge(K destKey, LongScoredValue... weights) { + LettuceAssert.isTrue(weights.length > 0, "At least one weight is required"); CommandArgs args = args(destKey); - args.add(keyWeightMap.size()); - args.addKeys(keyWeightMap.keySet()); + args.add(weights.length); + args.addKeys(Stream.of(weights).map(LongScoredValue::getValue).collect(Collectors.toList())); args.add(BloomCommandKeyword.WEIGHTS); - for (Long weight : keyWeightMap.values()) { - args.add(weight); - } - + Stream.of(weights).map(LongScoredValue::getScore).forEach(args::add); return createCommand(CountMinSketchCommandType.MERGE, new StatusOutput<>(codec), args); } @@ -296,11 +298,13 @@ public Command>> topKAdd(K key, V... items) { return createCommand(TopKCommandType.ADD, new ValueValueListOutput<>(codec), args); } - public Command>> topKIncrBy(K key, Map increments) { + @SuppressWarnings("unchecked") + public Command>> topKIncrBy(K key, LongScoredValue... increments) { + LettuceAssert.isTrue(increments.length > 0, "At least one increment is required"); CommandArgs args = args(key); - for (Map.Entry entry : increments.entrySet()) { - args.addValue(entry.getKey()); - args.add(entry.getValue()); + for (LongScoredValue entry : increments) { + args.addValue(entry.getValue()); + args.add(entry.getScore()); } return createCommand(TopKCommandType.ADD, new ValueValueListOutput<>(codec), args); diff --git a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/bloom/LongScoredValue.java b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/bloom/LongScoredValue.java new file mode 100644 index 0000000..d5484b8 --- /dev/null +++ b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/bloom/LongScoredValue.java @@ -0,0 +1,173 @@ +package com.redis.lettucemod.bloom; + +import java.util.Optional; +import java.util.function.Function; + +import io.lettuce.core.Value; +import io.lettuce.core.internal.LettuceAssert; + +/** + * A long scored-value extension to {@link Value}. + * + * @param Value type. + * @author Will Glozer + * @author Mark Paluch + * @author Julien Ruaux + */ +public class LongScoredValue extends Value { + + private static final long serialVersionUID = 1L; + + private static final LongScoredValue EMPTY = new LongScoredValue<>(0, null); + + private final long score; + + /** + * Serializable constructor. + */ + protected LongScoredValue() { + super(null); + this.score = 0; + } + + private LongScoredValue(long score, V value) { + super(value); + this.score = score; + } + + /** + * Creates a {@link Value} from a {@code key} and an {@link Optional}. The + * resulting value contains the value from the {@link Optional} if a value is + * present. Value is empty if the {@link Optional} is empty. + * + * @param score the score. + * @param optional the optional. May be empty but never {@code null}. + * @return the {@link Value}. + */ + public static LongScoredValue from(long score, Optional optional) { + + LettuceAssert.notNull(optional, "Optional must not be null"); + + if (optional.isPresent()) { + return new LongScoredValue<>(score, optional.get()); + } + + return empty(); + } + + /** + * Creates a {@link Value} from a {@code score} and {@code value}. The resulting + * value contains the value if the {@code value} is not null. + * + * @param score the score. + * @param value the value. May be {@code null}. + * @return the {@link Value}. + */ + public static LongScoredValue fromNullable(long score, T value) { + + if (value == null) { + return empty(); + } + + return new LongScoredValue<>(score, value); + } + + /** + * Returns an empty {@code ScoredValue} instance. No value is present for this + * instance. + * + * @return the {@link LongScoredValue} + */ + @SuppressWarnings("unchecked") + public static LongScoredValue empty() { + return (LongScoredValue) EMPTY; + } + + /** + * Creates a {@link LongScoredValue} from a {@code key} and {@code value}. The + * resulting value contains the value. + * + * @param score the score. + * @param value the value. Must not be {@code null}. + * @return the {@link LongScoredValue}. + */ + public static LongScoredValue just(long score, T value) { + + LettuceAssert.notNull(value, "Value must not be null"); + + return new LongScoredValue<>(score, value); + } + + public long getScore() { + return score; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (!(o instanceof LongScoredValue)) + return false; + if (!super.equals(o)) + return false; + + LongScoredValue that = (LongScoredValue) o; + + return Long.compare(that.score, score) == 0; + } + + @Override + public int hashCode() { + int result = (int) (score ^ (score >>> 32)); + result = 31 * result + (hasValue() ? getValue().hashCode() : 0); + return result; + } + + @Override + public String toString() { + return hasValue() ? String.format("LongScoredValue[%f, %s]", score, getValue()) + : String.format("LongScoredValue[%f].empty", score); + } + + /** + * Returns a {@link LongScoredValue} consisting of the results of applying the + * given function to the value of this element. Mapping is performed only if a + * {@link #hasValue() value is present}. + * + * @param element type of the new {@link LongScoredValue}. + * @param mapper a stateless function to apply to each element. + * @return the new {@link LongScoredValue}. + */ + @Override + @SuppressWarnings("unchecked") + public LongScoredValue map(Function mapper) { + + LettuceAssert.notNull(mapper, "Mapper function must not be null"); + + if (hasValue()) { + return new LongScoredValue<>(score, mapper.apply(getValue())); + } + + return (LongScoredValue) this; + } + + /** + * Returns a {@link LongScoredValue} consisting of the results of applying the + * given function to the score of this element. Mapping is performed only if a + * {@link #hasValue() value is present}. + * + * @param mapper a stateless function to apply to each element. + * @return the new {@link LongScoredValue} . + */ + public LongScoredValue mapScore(Function mapper) { + + LettuceAssert.notNull(mapper, "Mapper function must not be null"); + + if (hasValue()) { + return new LongScoredValue<>(mapper.apply(score).longValue(), getValue()); + } + + return this; + } + +} diff --git a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/cluster/RedisModulesAdvancedClusterAsyncCommandsImpl.java b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/cluster/RedisModulesAdvancedClusterAsyncCommandsImpl.java index b65a46e..81d7b94 100644 --- a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/cluster/RedisModulesAdvancedClusterAsyncCommandsImpl.java +++ b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/cluster/RedisModulesAdvancedClusterAsyncCommandsImpl.java @@ -15,6 +15,7 @@ import com.redis.lettucemod.bloom.CuckooFilter; import com.redis.lettucemod.bloom.CuckooFilterInsertOptions; import com.redis.lettucemod.bloom.CuckooFilterReserveOptions; +import com.redis.lettucemod.bloom.LongScoredValue; import com.redis.lettucemod.bloom.TDigestInfo; import com.redis.lettucemod.bloom.TDigestMergeOptions; import com.redis.lettucemod.bloom.TopKInfo; @@ -771,8 +772,8 @@ public RedisFuture cmsIncrBy(K key, V item, long increment) { } @Override - public RedisFuture> cmsIncrBy(K key, Map increments) { - return delegate.cmsIncrBy(key, increments); + public RedisFuture> cmsIncrBy(K key, LongScoredValue... itemIncrements) { + return delegate.cmsIncrBy(key, itemIncrements); } @Override @@ -796,8 +797,8 @@ public RedisFuture cmsMerge(K destKey, K... keys) { } @Override - public RedisFuture cmsMerge(K destKey, Map keyWeightMap) { - return delegate.cmsMerge(destKey, keyWeightMap); + public RedisFuture cmsMerge(K destKey, LongScoredValue... sourceKeyWeights) { + return delegate.cmsMerge(destKey, sourceKeyWeights); } @Override @@ -811,8 +812,8 @@ public RedisFuture>> topKAdd(K key, V... items) { } @Override - public RedisFuture>> topKIncrBy(K key, Map increments) { - return delegate.topKIncrBy(key, increments); + public RedisFuture>> topKIncrBy(K key, LongScoredValue... itemIncrements) { + return delegate.topKIncrBy(key, itemIncrements); } @Override diff --git a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/cluster/RedisModulesAdvancedClusterReactiveCommandsImpl.java b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/cluster/RedisModulesAdvancedClusterReactiveCommandsImpl.java index 05daa5d..e86bb99 100644 --- a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/cluster/RedisModulesAdvancedClusterReactiveCommandsImpl.java +++ b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/cluster/RedisModulesAdvancedClusterReactiveCommandsImpl.java @@ -16,6 +16,7 @@ import com.redis.lettucemod.bloom.CuckooFilter; import com.redis.lettucemod.bloom.CuckooFilterInsertOptions; import com.redis.lettucemod.bloom.CuckooFilterReserveOptions; +import com.redis.lettucemod.bloom.LongScoredValue; import com.redis.lettucemod.bloom.TDigestInfo; import com.redis.lettucemod.bloom.TDigestMergeOptions; import com.redis.lettucemod.bloom.TopKInfo; @@ -332,8 +333,8 @@ public Flux> topKAdd(K key, V... items) { } @Override - public Flux> topKIncrBy(K key, Map increments) { - return delegate.topKIncrBy(key, increments); + public Flux> topKIncrBy(K key, LongScoredValue... itemIncrements) { + return delegate.topKIncrBy(key, itemIncrements); } @Override @@ -887,10 +888,10 @@ public Mono cfReserve(K key, long capacity, CuckooFilterReserveOptions o public Mono cmsIncrBy(K key, V item, long increment) { return delegate.cmsIncrBy(key, item, increment); } - + @Override - public Flux cmsIncrBy(K key, Map increments) { - return delegate.cmsIncrBy(key, increments); + public Flux cmsIncrBy(K key, LongScoredValue... itemIncrements) { + return delegate.cmsIncrBy(key, itemIncrements); } @Override @@ -912,10 +913,10 @@ public Flux cmsQuery(K key, V... items) { public Mono cmsMerge(K destKey, K... keys) { return delegate.cmsMerge(destKey, keys); } - + @Override - public Mono cmsMerge(K destKey, Map keyWeightMap) { - return delegate.cmsMerge(destKey, keyWeightMap); + public Mono cmsMerge(K destKey, LongScoredValue... sourceKeyWeights) { + return delegate.cmsMerge(destKey, sourceKeyWeights); } @Override diff --git a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/output/GetOutput.java b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/output/GetOutput.java index bdd44cc..ae38485 100644 --- a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/output/GetOutput.java +++ b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/output/GetOutput.java @@ -6,9 +6,9 @@ import java.util.List; import com.redis.lettucemod.timeseries.GetResult; -import com.redis.lettucemod.timeseries.Label; import com.redis.lettucemod.timeseries.Sample; +import io.lettuce.core.KeyValue; import io.lettuce.core.codec.RedisCodec; import io.lettuce.core.internal.LettuceAssert; import io.lettuce.core.internal.LettuceStrings; @@ -24,7 +24,7 @@ public class GetOutput extends CommandOutput>> private boolean skipKeyReset; private K key; private K labelKey; - private List> labels; + private List> labels; private Sample sample; private boolean labelsComplete; @@ -54,7 +54,7 @@ public void set(ByteBuffer bytes) { labelKey = codec.decodeKey(bytes); return; } - labels.add(Label.of(labelKey, bytes == null ? null : codec.decodeValue(bytes))); + labels.add(KeyValue.just(labelKey, bytes == null ? null : codec.decodeValue(bytes))); labelKey = null; } diff --git a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/search/BaseSearchOptions.java b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/search/BaseSearchOptions.java index e3caf25..67ec9bf 100644 --- a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/search/BaseSearchOptions.java +++ b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/search/BaseSearchOptions.java @@ -10,12 +10,14 @@ import com.redis.lettucemod.protocol.SearchCommandKeyword; +import io.lettuce.core.KeyValue; + public class BaseSearchOptions implements RediSearchArgument { private boolean verbatim; private Optional timeout = Optional.empty(); private Optional limit = Optional.empty(); - private List> params = new ArrayList<>(); + private List> params = new ArrayList<>(); private OptionalInt dialect = OptionalInt.empty(); protected BaseSearchOptions() { @@ -53,11 +55,11 @@ public void setLimit(Limit limit) { this.limit = Optional.of(limit); } - public List> getParams() { + public List> getParams() { return params; } - public void setParams(List> params) { + public void setParams(List> params) { this.params = params; } @@ -79,7 +81,7 @@ public void build(SearchCommandArgs args) { if (!params.isEmpty()) { args.add(SearchCommandKeyword.PARAMS); args.add(params.size() * 2l); - params.forEach(p -> args.addKey(p.getName()).addValue(p.getValue())); + params.forEach(p -> args.addKey(p.getKey()).addValue(p.getValue())); } dialect.ifPresent(d -> args.add(SearchCommandKeyword.DIALECT).add(d)); } @@ -88,7 +90,7 @@ public static class Builder> { private boolean verbatim; private Optional timeout = Optional.empty(); - private final List> params = new ArrayList<>(); + private final List> params = new ArrayList<>(); private Optional limit = Optional.empty(); private OptionalInt dialect = OptionalInt.empty(); @@ -117,12 +119,12 @@ public B timeout(Duration timeout) { @SuppressWarnings("unchecked") public B param(K name, V value) { - this.params.add(Parameter.of(name, value)); + this.params.add(KeyValue.just(name, value)); return (B) this; } @SuppressWarnings("unchecked") - public B params(Parameter... params) { + public B params(KeyValue... params) { this.params.addAll(Arrays.asList(params)); return (B) this; } diff --git a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/search/Parameter.java b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/search/Parameter.java deleted file mode 100644 index cd73224..0000000 --- a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/search/Parameter.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.redis.lettucemod.search; - -public class Parameter { - - private K name; - private V value; - - public K getName() { - return name; - } - - public void setName(K name) { - this.name = name; - } - - public V getValue() { - return value; - } - - public void setValue(V value) { - this.value = value; - } - - public static Parameter of(K name, V value) { - Parameter l = new Parameter<>(); - l.setName(name); - l.setValue(value); - return l; - } - -} diff --git a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/timeseries/BaseOptions.java b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/timeseries/BaseOptions.java index 8a53733..51a5371 100644 --- a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/timeseries/BaseOptions.java +++ b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/timeseries/BaseOptions.java @@ -13,13 +13,14 @@ import com.redis.lettucemod.protocol.TimeSeriesCommandKeyword; import io.lettuce.core.CompositeArgument; +import io.lettuce.core.KeyValue; import io.lettuce.core.protocol.CommandArgs; public class BaseOptions implements CompositeArgument { private Optional retentionPeriod = Optional.empty(); private OptionalLong chunkSize = OptionalLong.empty(); - private List> labels = new ArrayList<>(); + private List> labels = new ArrayList<>(); public BaseOptions() { } @@ -30,11 +31,11 @@ protected BaseOptions(Builder builder) { this.labels = builder.labels; } - public List> getLabels() { + public List> getLabels() { return labels; } - public void setLabels(Iterable> labels) { + public void setLabels(Iterable> labels) { this.labels = StreamSupport.stream(labels.spliterator(), false).collect(Collectors.toList()); } @@ -61,7 +62,7 @@ public void build(CommandArgs args) { chunkSize.ifPresent(s -> args.add(TimeSeriesCommandKeyword.CHUNK_SIZE).add(s)); if (!labels.isEmpty()) { args.add(TimeSeriesCommandKeyword.LABELS); - labels.forEach(l -> args.addKey((K) l.getLabel()).addValue((V) l.getValue())); + labels.forEach(l -> args.addKey((K) l.getKey()).addValue((V) l.getValue())); } } @@ -86,7 +87,7 @@ public static class Builder> { private Optional retentionPeriod = Optional.empty(); private OptionalLong chunkSize = OptionalLong.empty(); - private final List> labels = new ArrayList<>(); + private final List> labels = new ArrayList<>(); public B retentionPeriod(long millis) { return retentionPeriod(Duration.ofMillis(millis)); @@ -102,8 +103,8 @@ public B chunkSize(long chunkSize) { return (B) this; } - public B labels(Iterable> labels) { - for (Label label : labels) { + public B labels(Iterable> labels) { + for (KeyValue label : labels) { this.labels.add(label); } return (B) this; @@ -123,16 +124,16 @@ public B labels(Object... keyValues) { throw new IllegalArgumentException("size must be even, it is a set of key=value pairs"); } for (int i = 0; i < keyValues.length; i += 2) { - label(Label.of((K) keyValues[i], (V) keyValues[i + 1])); + label(KeyValue.just((K) keyValues[i], (V) keyValues[i + 1])); } return (B) this; } - public B label(Label label) { + public B label(KeyValue label) { return labels(label); } - public B labels(Label... labels) { + public B labels(KeyValue... labels) { return labels(Arrays.asList(labels)); } @@ -142,7 +143,7 @@ public B labels(Map map) { } public B label(K label, V value) { - this.labels.add(Label.of(label, value)); + this.labels.add(KeyValue.just(label, value)); return (B) this; } diff --git a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/timeseries/GetResult.java b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/timeseries/GetResult.java index 9cf064a..d60fd4a 100644 --- a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/timeseries/GetResult.java +++ b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/timeseries/GetResult.java @@ -2,10 +2,12 @@ import java.util.List; +import io.lettuce.core.KeyValue; + public class GetResult { private K key; - private List> labels; + private List> labels; private Sample sample; public K getKey() { @@ -16,11 +18,11 @@ public void setKey(K key) { this.key = key; } - public List> getLabels() { + public List> getLabels() { return labels; } - public void setLabels(List> labels) { + public void setLabels(List> labels) { this.labels = labels; } diff --git a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/timeseries/Label.java b/subprojects/lettucemod/src/main/java/com/redis/lettucemod/timeseries/Label.java deleted file mode 100644 index 94fc8d5..0000000 --- a/subprojects/lettucemod/src/main/java/com/redis/lettucemod/timeseries/Label.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.redis.lettucemod.timeseries; - -import java.util.Objects; - -public class Label { - - private K name; - private V value; - - public K getLabel() { - return name; - } - - public void setLabel(K label) { - this.name = label; - } - - public V getValue() { - return value; - } - - public void setValue(V value) { - this.value = value; - } - - public static Label of(K label, V value) { - Label l = new Label<>(); - l.setLabel(label); - l.setValue(value); - return l; - } - - @Override - public int hashCode() { - return Objects.hash(name, value); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - Label other = (Label) obj; - return Objects.equals(name, other.name) && Objects.equals(value, other.value); - } - -} diff --git a/subprojects/lettucemod/src/test/java/com/redis/lettucemod/ModulesTests.java b/subprojects/lettucemod/src/test/java/com/redis/lettucemod/ModulesTests.java index 9063bc1..167b525 100644 --- a/subprojects/lettucemod/src/test/java/com/redis/lettucemod/ModulesTests.java +++ b/subprojects/lettucemod/src/test/java/com/redis/lettucemod/ModulesTests.java @@ -63,6 +63,7 @@ import com.redis.lettucemod.bloom.BloomFilterInsertOptions; import com.redis.lettucemod.bloom.CuckooFilter; import com.redis.lettucemod.bloom.CuckooFilterInsertOptions; +import com.redis.lettucemod.bloom.LongScoredValue; import com.redis.lettucemod.bloom.TDigestInfo; import com.redis.lettucemod.bloom.TopKInfo; import com.redis.lettucemod.cluster.RedisModulesClusterClient; @@ -102,7 +103,6 @@ import com.redis.lettucemod.timeseries.AddOptions; import com.redis.lettucemod.timeseries.Aggregation; import com.redis.lettucemod.timeseries.Aggregator; -import com.redis.lettucemod.timeseries.Label; import com.redis.lettucemod.timeseries.RangeOptions; import com.redis.lettucemod.timeseries.Sample; import com.redis.lettucemod.timeseries.TimeRange; @@ -1020,7 +1020,8 @@ void tsAdd() { // TS.ADD temperature:3:11 1548149181 30 Long add1 = ts.tsAdd(TS_KEY, Sample.of(TIMESTAMP_1, VALUE_1), AddOptions.builder().retentionPeriod(6000) - .labels(Label.of(LABEL_SENSOR_ID, SENSOR_ID), Label.of(LABEL_AREA_ID, AREA_ID)).build()); + .labels(KeyValue.just(LABEL_SENSOR_ID, SENSOR_ID), KeyValue.just(LABEL_AREA_ID, AREA_ID)) + .build()); assertEquals(TIMESTAMP_1, add1); Sample sample = ts.tsGet(TS_KEY); assertEquals(TIMESTAMP_1, sample.getTimestamp()); @@ -1068,12 +1069,12 @@ protected void populate(RedisTimeSeriesCommands ts) { // TS.CREATE temperature:3:11 RETENTION 6000 LABELS sensor_id 2 area_id 32 // TS.ADD temperature:3:11 1548149181 30 ts.tsAdd(TS_KEY, Sample.of(TIMESTAMP_1, VALUE_1), AddOptions.builder().retentionPeriod(6000) - .labels(Label.of(LABEL_SENSOR_ID, SENSOR_ID), Label.of(LABEL_AREA_ID, AREA_ID)).build()); + .labels(KeyValue.just(LABEL_SENSOR_ID, SENSOR_ID), KeyValue.just(LABEL_AREA_ID, AREA_ID)).build()); // TS.ADD temperature:3:11 1548149191 42 ts.tsAdd(TS_KEY, Sample.of(TIMESTAMP_2, VALUE_2)); ts.tsAdd(TS_KEY_2, Sample.of(TIMESTAMP_1, VALUE_1), AddOptions.builder().retentionPeriod(6000) - .labels(Label.of(LABEL_SENSOR_ID, SENSOR_ID), Label.of(LABEL_AREA_ID, AREA_ID_2)).build()); + .labels(KeyValue.just(LABEL_SENSOR_ID, SENSOR_ID), KeyValue.just(LABEL_AREA_ID, AREA_ID_2)).build()); ts.tsAdd(TS_KEY_2, Sample.of(TIMESTAMP_2, VALUE_2)); } @@ -1231,12 +1232,8 @@ void cms() { assertEquals("OK", cms.cmsInitByProb(key1, .001, .01)); assertEquals(2, cms.cmsIncrBy(key1, "test", 2)); - Map increments = new HashMap<>(); - increments.put("one", 1L); - increments.put("two", 2L); - increments.put("three", 3L); - - List result = cms.cmsIncrBy(key1, increments); + List result = cms.cmsIncrBy(key1, LongScoredValue.just(1, "one"), LongScoredValue.just(2, "two"), + LongScoredValue.just(3, "three")); assertEquals(1, result.get(0)); assertEquals(2, result.get(1)); assertEquals(3, result.get(2)); @@ -1274,12 +1271,8 @@ void cmsReactive() { assertEquals("OK", cms.cmsInitByProb(key1, .001, .01).block()); assertEquals(2, cms.cmsIncrBy(key1, "test", 2).block()); - Map increments = new HashMap<>(); - increments.put("one", 1L); - increments.put("two", 2L); - increments.put("three", 3L); - - List result = cms.cmsIncrBy(key1, increments).collectList().block(); + List result = cms.cmsIncrBy(key1, LongScoredValue.just(1, "one"), LongScoredValue.just(2, "two"), + LongScoredValue.just(3, "three")).collectList().block(); assertNotNull(result); assertEquals(1, result.get(0)); assertEquals(2, result.get(1)); diff --git a/subprojects/lettucemod/src/test/java/com/redis/lettucemod/StackTests.java b/subprojects/lettucemod/src/test/java/com/redis/lettucemod/StackTests.java index 040bedd..8fb24fa 100644 --- a/subprojects/lettucemod/src/test/java/com/redis/lettucemod/StackTests.java +++ b/subprojects/lettucemod/src/test/java/com/redis/lettucemod/StackTests.java @@ -20,7 +20,6 @@ import com.redis.lettucemod.timeseries.CreateOptions; import com.redis.lettucemod.timeseries.DuplicatePolicy; import com.redis.lettucemod.timeseries.GetResult; -import com.redis.lettucemod.timeseries.Label; import com.redis.lettucemod.timeseries.MRangeOptions; import com.redis.lettucemod.timeseries.RangeResult; import com.redis.lettucemod.timeseries.Sample; @@ -31,6 +30,7 @@ import io.lettuce.core.AbstractRedisClient; import io.lettuce.core.AclSetuserArgs; +import io.lettuce.core.KeyValue; import io.lettuce.core.RedisURI; import io.lettuce.core.codec.StringCodec; import io.lettuce.core.resource.DefaultClientResources; @@ -45,7 +45,7 @@ class StackTests extends ModulesTests { protected AbstractRedisContainer getRedisContainer() { return container; } - + @Test void jsonMerge() throws JsonProcessingException { String key = "jsonMerge:test"; @@ -107,12 +107,12 @@ void tsCreate() { assertEquals("OK", connection.sync().tsCreate(TS_KEY, com.redis.lettucemod.timeseries.CreateOptions.builder().retentionPeriod(6000).build())); String key = "virag"; - List> labels = Arrays.asList(Label.of("name", "value")); + List> labels = Arrays.asList(KeyValue.just("name", "value")); assertEquals("OK", connection.sync().tsCreate(key, com.redis.lettucemod.timeseries.CreateOptions.builder() .retentionPeriod(100000L).labels(labels).policy(DuplicatePolicy.LAST).build())); List> results = connection.sync().tsMgetWithLabels("name=value"); - Label expectedLabel = labels.get(0); + KeyValue expectedLabel = labels.get(0); assertEquals(expectedLabel, results.get(0).getLabels().get(0)); } @@ -122,7 +122,7 @@ void tsQueryIndex() { String key2 = "tsQueryIndex:key2"; connection.sync().del(key1, key2); String id = "tsQueryIndex"; - List> labels = Collections.singletonList(Label.of("id", id)); + List> labels = Collections.singletonList(KeyValue.just("id", id)); assertEquals("OK", connection.sync().tsCreate(key1, CreateOptions.builder().labels(labels).build())); assertEquals("OK",