From eb8050a7abc46677d6887ceb56927e9924a68320 Mon Sep 17 00:00:00 2001 From: George Madi Date: Mon, 20 Jul 2026 07:36:59 -0500 Subject: [PATCH] Add option to limit number of keys returned when listing keys. Ref: RIAK-3044 --- .../riak/client/api/commands/kv/ListKeys.java | 31 +++++++++++++++++++ .../core/operations/ListKeysOperation.java | 24 ++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/main/java/com/basho/riak/client/api/commands/kv/ListKeys.java b/src/main/java/com/basho/riak/client/api/commands/kv/ListKeys.java index 5e01be6..4739045 100644 --- a/src/main/java/com/basho/riak/client/api/commands/kv/ListKeys.java +++ b/src/main/java/com/basho/riak/client/api/commands/kv/ListKeys.java @@ -1,5 +1,6 @@ /* * Copyright 2013 Basho Technologies Inc + * Copyright 2022-2026 Workday, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -78,11 +79,13 @@ public final class ListKeys extends StreamableRiakCommand.StreamableRiakCommandW { private final Namespace namespace; private final int timeout; + private final int keysLimit; ListKeys(Builder builder) throws ListException { this.namespace = builder.namespace; this.timeout = builder.timeout; + this.keysLimit = builder.keysLimit; if (!builder.allowListing) { @@ -113,6 +116,11 @@ protected ListKeysOperation buildCoreOperation(boolean streamResults) builder.withTimeout(timeout); } + if (keysLimit > 0) + { + builder.withKeysLimit(keysLimit); + } + builder.streamResults(streamResults); return builder.build(); @@ -169,6 +177,7 @@ public static class Builder { private final Namespace namespace; private int timeout; + private int keysLimit; private boolean allowListing; /** @@ -214,6 +223,23 @@ public Builder withTimeout(int timeout) return this; } + /** + * Limit the number of keys returned by the server. + *

+ * When set, Riak returns at most {@code keysLimit} keys. Any keys up + * to the limit may be returned, in no particular order, and two + * identical requests may return different key sets. + * A non-positive value means unlimited (the field is not sent). + *

+ * @param keysLimit the maximum number of keys to return. + * @return a reference to this object. + */ + public Builder withKeysLimit(int keysLimit) + { + this.keysLimit = keysLimit; + return this; + } + /** * Construct the ListKeys command. * @return A ListKeys command. @@ -231,6 +257,7 @@ public int hashCode() int result = 1; result = prime * result + (namespace != null ? namespace.hashCode() : 0); result = prime * result + timeout; + result = prime * result + keysLimit; return result; } @@ -259,6 +286,10 @@ public boolean equals(Object obj) { return false; } + if (this.keysLimit != other.keysLimit) + { + return false; + } return true; } diff --git a/src/main/java/com/basho/riak/client/core/operations/ListKeysOperation.java b/src/main/java/com/basho/riak/client/core/operations/ListKeysOperation.java index 929d5e6..deab8fe 100644 --- a/src/main/java/com/basho/riak/client/core/operations/ListKeysOperation.java +++ b/src/main/java/com/basho/riak/client/core/operations/ListKeysOperation.java @@ -1,5 +1,6 @@ /* * Copyright 2013 Basho Technologies Inc + * Copyright 2022-2026 Workday, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -114,6 +115,29 @@ public Builder withTimeout(int timeout) return this; } + /** + * Limit the number of keys returned by the server. + *

+ * When set, Riak returns at most {@code keysLimit} keys. Any keys up + * to the limit may be returned, in no particular order, and two + * identical requests may return different key sets. + * A value of 0 means unlimited so is not a legal value here. + * Builder rejects non-positive values. + * Omit the call for unlimited. + *

+ * @param keysLimit the maximum number of keys to return (must be > 0). + * @return a reference to this object. + */ + public Builder withKeysLimit(int keysLimit) + { + if (keysLimit <= 0) + { + throw new IllegalArgumentException("Keys limit can not be zero or less"); + } + reqBuilder.setKeysLimit(keysLimit); + return this; + } + /** * Set the streamResults flag. *