Skip to content

Commit

Permalink
Polishing #1582
Browse files Browse the repository at this point in the history
Introduce support for exact trimming. Tweak Javadoc. Rename minid to minId.

Original pull request: #1649.
  • Loading branch information
mp911de committed Mar 8, 2021
1 parent 8117a12 commit 0467858
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 31 deletions.
52 changes: 42 additions & 10 deletions src/main/java/io/lettuce/core/XAddArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class XAddArgs {

private boolean approximateTrimming;

private boolean exactTrimming;

private boolean nomkstream;

private String minid;
Expand Down Expand Up @@ -78,17 +80,19 @@ public static XAddArgs nomkstream() {
/**
* Creates new {@link XAddArgs} and setting {@literal MINID}.
*
* @param minid the oldest ID in the stream will be exactly the minimum between its original oldest ID and the specified
* threshold.
* @return new {@link XAddArgs} with {@literal MINID} set.
* @see XAddArgs#minid(String)
* @see XAddArgs#minId(String)
* @since 6.1
*/
public static XAddArgs minid(String minid) {
return new XAddArgs().minid(minid);
public static XAddArgs minId(String minid) {
return new XAddArgs().minId(minid);
}
}

/**
* Limit results to {@code maxlen} entries.
* Specify the message {@code id}.
*
* @param id must not be {@code null}.
* @return {@code this}
Expand Down Expand Up @@ -116,15 +120,16 @@ public XAddArgs maxlen(long maxlen) {
}

/**
* Limit stream to {@code maxlen} entries.
* Limit stream entries by message Id.
*
* @param minid
* @param minid the oldest ID in the stream will be exactly the minimum between its original oldest ID and the specified
* threshold.
* @return {@code this}
* @since 6.1
*/
public XAddArgs minid(String minid) {
public XAddArgs minId(String minid) {

LettuceAssert.notNull(minid, "Minid must not be null");
LettuceAssert.notNull(minid, "minId must not be null");

this.minid = minid;
return this;
Expand All @@ -133,11 +138,11 @@ public XAddArgs minid(String minid) {
/**
* The maximum number of entries to trim.
*
* @param limit has meaning only if `~` was provided.
* @param limit has meaning only if {@link #approximateTrimming `~`} was set.
* @return {@code this}
* @since 6.1
*/
public XAddArgs limit(Long limit) {
public XAddArgs limit(long limit) {

LettuceAssert.isTrue(limit > 0, "Limit must be greater 0");

Expand Down Expand Up @@ -166,6 +171,29 @@ public XAddArgs approximateTrimming(boolean approximateTrimming) {
return this;
}

/**
* Apply exact trimming for capped streams using the {@code =} flag.
*
* @return {@code this}
* @since 6.1
*/
public XAddArgs exactTrimming() {
return exactTrimming(true);
}

/**
* Apply exact trimming for capped streams using the {@code =} flag.
*
* @param exactTrimming {@code true} to apply exact radix node trimming.
* @return {@code this}
* @since 6.1
*/
public XAddArgs exactTrimming(boolean exactTrimming) {

this.exactTrimming = exactTrimming;
return this;
}

/**
* Do add the message if the stream does not already exist.
*
Expand Down Expand Up @@ -196,6 +224,8 @@ public <K, V> void build(CommandArgs<K, V> args) {

if (approximateTrimming) {
args.add("~");
} else if (exactTrimming) {
args.add("=");
}

args.add(maxlen);
Expand All @@ -207,6 +237,8 @@ public <K, V> void build(CommandArgs<K, V> args) {

if (approximateTrimming) {
args.add("~");
} else if (exactTrimming) {
args.add("=");
}

args.add(minid);
Expand Down
63 changes: 46 additions & 17 deletions src/main/java/io/lettuce/core/XTrimArgs.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2021 the original author or authors.
* Copyright 2021 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.
Expand Down Expand Up @@ -28,13 +28,15 @@
* @author dengliming
* @since 6.1
*/
public class XTrimArgs {
public class XTrimArgs implements CompositeArgument {

private Long maxlen;

private boolean approximateTrimming;

private String minid;
private boolean exactTrimming;

private String minId;

private Long limit;

Expand Down Expand Up @@ -62,11 +64,13 @@ public static XTrimArgs maxlen(long count) {
/**
* Creates new {@link XTrimArgs} and setting {@literal MINID}.
*
* @param minid the oldest ID in the stream will be exactly the minimum between its original oldest ID and the specified
* threshold.
* @return new {@link XTrimArgs} with {@literal MINID} set.
* @see XTrimArgs#minid(String)
* @see XTrimArgs#minId(String)
*/
public static XTrimArgs minid(String minid) {
return new XTrimArgs().minid(minid);
public static XTrimArgs minId(String minid) {
return new XTrimArgs().minId(minid);
}
}

Expand All @@ -85,26 +89,27 @@ public XTrimArgs maxlen(long maxlen) {
}

/**
* Limit stream to {@code maxlen} entries.
* Limit stream entries by message Id.
*
* @param minid
* @param minid the oldest ID in the stream will be exactly the minimum between its original oldest ID and the specified
* threshold.
* @return {@code this}
*/
public XTrimArgs minid(String minid) {
public XTrimArgs minId(String minid) {

LettuceAssert.notNull(minid, "Minid must not be null");
LettuceAssert.notNull(minid, "minId must not be null");

this.minid = minid;
this.minId = minid;
return this;
}

/**
* The maximum number of entries to trim.
*
* @param limit has meaning only if `~` was provided.
* @param limit has meaning only if {@link #approximateTrimming `~`} was set.
* @return {@code this}
*/
public XTrimArgs limit(Long limit) {
public XTrimArgs limit(long limit) {

LettuceAssert.isTrue(limit > 0, "Limit must be greater 0");

Expand Down Expand Up @@ -133,27 +138,51 @@ public XTrimArgs approximateTrimming(boolean approximateTrimming) {
return this;
}

/**
* Apply exact trimming for capped streams using the {@code =} flag.
*
* @return {@code this}
*/
public XTrimArgs exactTrimming() {
return exactTrimming(true);
}

/**
* Apply exact trimming for capped streams using the {@code =} flag.
*
* @param exactTrimming {@code true} to apply exact radix node trimming.
* @return {@code this}
*/
public XTrimArgs exactTrimming(boolean exactTrimming) {

this.exactTrimming = exactTrimming;
return this;
}

@Override
public <K, V> void build(CommandArgs<K, V> args) {

if (maxlen != null) {
args.add(CommandKeyword.MAXLEN);

if (approximateTrimming) {
args.add("~");
} else if (exactTrimming) {
args.add("=");
}

args.add(maxlen);
}

if (minid != null) {
} else if (minId != null) {

args.add(CommandKeyword.MINID);

if (approximateTrimming) {
args.add("~");
} else if (exactTrimming) {
args.add("=");
}

args.add(minid);
args.add(minId);
}

if (limit != null && approximateTrimming) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ void xaddMaxLenEfficientTrimming() {
@Test
@EnabledOnCommand("XAUTOCLAIM") // Redis 6.2
void xaddMinidLimit() {
redis.xadd(key, XAddArgs.Builder.minid("2").id("3"), "foo", "bar");
redis.xadd(key, XAddArgs.Builder.minid("2").id("4"), "foo", "bar");
redis.xadd(key, XAddArgs.Builder.minId("2").id("3"), "foo", "bar");
redis.xadd(key, XAddArgs.Builder.minId("2").id("4"), "foo", "bar");
assertThat(redis.xlen(key)).isEqualTo(2);
redis.xadd(key, XAddArgs.Builder.minid("4").id("5"), "foo", "bar");
redis.xadd(key, XAddArgs.Builder.minId("4").id("5"), "foo", "bar");
assertThat(redis.xlen(key)).isEqualTo(2);
redis.del(key);

Expand Down Expand Up @@ -161,7 +161,7 @@ void xtrim() {
@EnabledOnCommand("XAUTOCLAIM") // Redis 6.2
void xtrimMinidLimit() {
redis.xadd(key, XAddArgs.Builder.maxlen(3).id("3"), "foo", "bar");
redis.xtrim(key, XTrimArgs.Builder.minid("4"));
redis.xtrim(key, XTrimArgs.Builder.minId("4"));
assertThat(redis.xlen(key)).isZero();

List<String> ids = new ArrayList<>();
Expand Down

0 comments on commit 0467858

Please sign in to comment.