From 5c61411fd3eba6aeb67485fb793dbbe951ecab13 Mon Sep 17 00:00:00 2001 From: Darren Ackers Date: Fri, 31 Jan 2025 18:33:06 +0000 Subject: [PATCH 1/6] docs(memorystore): added valkey leaderboard snippets --- .../valkey/leaderboard/snippets/README.md | 0 .../valkey/leaderboard/snippets/pom.xml | 117 ++++++++++++++++++ .../java/samples/MemorystoreAddScore.java | 76 ++++++++++++ .../java/samples/MemorystoreFilterByAsc.java | 79 ++++++++++++ .../java/samples/MemorystoreFilterByDesc.java | 87 +++++++++++++ .../MemorystoreLeaderboardPagination.java | 115 +++++++++++++++++ 6 files changed, 474 insertions(+) create mode 100644 memorystore/valkey/leaderboard/snippets/README.md create mode 100644 memorystore/valkey/leaderboard/snippets/pom.xml create mode 100644 memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreAddScore.java create mode 100644 memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByAsc.java create mode 100644 memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByDesc.java create mode 100644 memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreLeaderboardPagination.java diff --git a/memorystore/valkey/leaderboard/snippets/README.md b/memorystore/valkey/leaderboard/snippets/README.md new file mode 100644 index 00000000000..e69de29bb2d diff --git a/memorystore/valkey/leaderboard/snippets/pom.xml b/memorystore/valkey/leaderboard/snippets/pom.xml new file mode 100644 index 00000000000..f4be44d7ba4 --- /dev/null +++ b/memorystore/valkey/leaderboard/snippets/pom.xml @@ -0,0 +1,117 @@ + + + + + + 4.0.0 + + com.example.memorystore + caching + 1.0-SNAPSHOT + + Google Cloud Memorystore Sample + http://www.example.com + + + UTF-8 + 11 + + + + + + org.junit + junit-bom + 5.11.0 + pom + import + + + + + + + + redis.clients + jedis + 4.3.1 + + + + + com.google.cloud + google-cloud-core + 2.16.0 + + + + + org.junit.jupiter + junit-jupiter-api + test + + + org.junit.jupiter + junit-jupiter-params + test + + + + + + + + maven-clean-plugin + 3.4.0 + + + maven-resources-plugin + 3.3.1 + + + maven-compiler-plugin + 3.13.0 + + + maven-surefire-plugin + 3.3.0 + + + maven-jar-plugin + 3.4.2 + + + maven-install-plugin + 3.1.2 + + + maven-deploy-plugin + 3.1.2 + + + maven-site-plugin + 3.12.1 + + + maven-project-info-reports-plugin + 3.6.1 + + + + + \ No newline at end of file diff --git a/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreAddScore.java b/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreAddScore.java new file mode 100644 index 00000000000..38d9402789a --- /dev/null +++ b/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreAddScore.java @@ -0,0 +1,76 @@ +/* + * Copyright 2025 Google LLC + * + * 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 + * + * http://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. + */ + +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; + +import java.util.AbstractMap.SimpleEntry; +import java.util.List; + +public final class MemorystoreAddScore { + + /** Replace the Memorystore instance id. */ + private static final String INSTANCE_ID = "INSTANCE_ID"; + + /** Replace the Memorystore port, if not the default port. */ + private static final int PORT = 6379; + + /** Set the name for the Leaderboard */ + private static final String LEADERBOARD_KEY = "leaderboard"; + + /** Replace the names and scores to write to Memorystore. */ + private static final List> USER_SCORES = + List.of( + new SimpleEntry<>("User1", 100.0), + new SimpleEntry<>("User2", 80.0), + new SimpleEntry<>("User3", 95.0), + new SimpleEntry<>("User4", 70.0)); + + private MemorystoreAddScore() { + // No-op; won't be called + } + + /** + * Writes to Memorystore before verifying the item exists. + * + * @param args command-line arguments + */ + public static void main(final String[] args) { + // Connect to the Memorystore instance + JedisPool pool = new JedisPool(INSTANCE_ID, PORT); + + try (Jedis jedis = pool.getResource()) { + // Add the scores to the leaderboard + for (SimpleEntry entry : USER_SCORES) { + jedis.zadd(LEADERBOARD_KEY, entry.getValue(), entry.getKey()); + System.out.printf( + "Added/Updated %s with score %s%n", entry.getKey(), entry.getValue()); + } + + // Verify that scores have been added + for (String userKey : USER_SCORES.keySet()) { + // Find the user score based on the user key + Double userScore = jedis.zscore(LEADERBOARD_KEY, userKey); + + // Print out the user score + System.out.printf("User %s has a score of %s%n", userKey, userScore); + } + } catch (Exception e) { + // Print any errors found in the exception + System.err.printf("Error connecting to Redis: %s%n", e.getMessage()); + } + } +} diff --git a/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByAsc.java b/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByAsc.java new file mode 100644 index 00000000000..090096c3aa3 --- /dev/null +++ b/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByAsc.java @@ -0,0 +1,79 @@ +/* + * Copyright 2025 Google LLC + * + * 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 + * + * http://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. + */ + +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; + +import java.util.AbstractMap.SimpleEntry; +import java.util.List; +import java.util.Set; + +public final class MemorystoreFilterByAsc { + + /** Replace the Memorystore instance id. */ + private static final String INSTANCE_ID = "INSTANCE_ID"; + + /** Replace the Memorystore port, if not the default port. */ + private static final int PORT = 6379; + + /** Set the name for the Leaderboard */ + private static final String LEADERBOARD_KEY = "leaderboard"; + + /** Replace the names and scores to write to Memorystore. */ + private static final List> USER_SCORES = + List.of( + new SimpleEntry<>("User1", 100.0), + new SimpleEntry<>("User2", 80.0), + new SimpleEntry<>("User3", 95.0), + new SimpleEntry<>("User4", 70.0)); + + private MemorystoreFilterByAsc() { + // No-op; won't be called + } + + /** + * Writes to Memorystore and retrieves the leaderboard sorted in ascending order. + * + * @param args command-line arguments + */ + public static void main(final String[] args) { + // Connect to the Memorystore instance + JedisPool pool = new JedisPool(INSTANCE_ID, PORT); + + try (Jedis jedis = pool.getResource()) { + // Add the scores to the leaderboard + for (SimpleEntry entry : USER_SCORES) { + jedis.zadd(LEADERBOARD_KEY, entry.getValue(), entry.getKey()); + System.out.printf( + "Added/Updated %s with score %s%n", entry.getKey(), entry.getValue()); + } + + // Retrieve and print all users sorted by score in ascending order + Set sortedUsers = jedis.zrange(LEADERBOARD_KEY, 0, -1); + + // Print the leaderboard in ascending order + System.out.printf("Leaderboad (Ascending)"); + + // For each user, print the score + for (String user : sortedUsers) { + System.out.printf( + "User: %s, Score: %s%n", user, jedis.zscore(LEADERBOARD_KEY, user)); + } + } catch (Exception e) { + System.err.printf("Error connecting to Redis: %s%n", e.getMessage()); + } + } +} diff --git a/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByDesc.java b/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByDesc.java new file mode 100644 index 00000000000..aa086d7e49d --- /dev/null +++ b/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByDesc.java @@ -0,0 +1,87 @@ +/* + * Copyright 2025 Google LLC + * + * 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 + * + * http://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. + */ + +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; + +import java.util.AbstractMap.SimpleEntry; +import java.util.List; +import java.util.Set; + +public final class MemorystoreFilterByDesc { + + /** Replace the Memorystore instance id. */ + private static final String INSTANCE_ID = "INSTANCE_ID"; + + /** Replace the Memorystore port, if not the default port. */ + private static final int PORT = 6379; + + /** Set the name for the Leaderboard */ + private static final String LEADERBOARD_KEY = "leaderboard"; + + /** Replace the names and scores to write to Memorystore. */ + private static final List> USER_SCORES = + List.of( + new SimpleEntry<>("User1", 100.0), + new SimpleEntry<>("User2", 80.0), + new SimpleEntry<>("User3", 95.0), + new SimpleEntry<>("User4", 70.0)); + + private MemorystoreFilterByDesc() { + // No-op; won't be called + } + + /** + * Writes to Memorystore and retrieves the leaderboard sorted in descending order. + * + * @param args command-line arguments + */ + public static void main(final String[] args) { + // Connect to the Memorystore instance + JedisPool pool = new JedisPool(INSTANCE_ID, PORT); + + try (Jedis jedis = pool.getResource()) { + // Add the scores to the leaderboard + for (SimpleEntry entry : USER_SCORES) { + jedis.zadd(LEADERBOARD_KEY, entry.getKey(), user.value()); + System.out.printf("Added/Updated %s with score %s%n", user, score); + } + + // Retrieve and print all users sorted by score in descending order + System.out.println("\nLeaderboard (Sorted by Descending Scores):"); + Set sortedUsers = jedis.zrevrange(LEADERBOARD_KEY, 0, -1); + + // Print the leaderboard in descending order + for (String user : sortedUsers) { + System.out.printf( + "User: %s, Score: %s%n", user, jedis.zscore(LEADERBOARD_KEY, user)); + } + + // Get the highest-ranked user + System.out.println("\nTop Ranked User:"); + Set topUser = jedis.zrevrange(LEADERBOARD_KEY, 0, 0); + if (!topUser.isEmpty()) { + String user = topUser.iterator().next(); + System.out.printf( + "User: %s, Score: %s%n", user, jedis.zscore(LEADERBOARD_KEY, user)); + } else { + System.out.println("Leaderboard is empty."); + } + } catch (Exception e) { + System.err.printf("Error connecting to Redis: %s%n", e.getMessage()); + } + } +} diff --git a/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreLeaderboardPagination.java b/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreLeaderboardPagination.java new file mode 100644 index 00000000000..ad3b0f9d80b --- /dev/null +++ b/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreLeaderboardPagination.java @@ -0,0 +1,115 @@ +/* + * Copyright 2025 Google LLC + * + * 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 + * + * http://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. + */ + +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; + +import java.util.AbstractMap.SimpleEntry; +import java.util.List; +import java.util.Set; + +public final class MemorystoreLeaderboardPagination { + + /** Replace the Memorystore instance id. */ + private static final String INSTANCE_ID = "INSTANCE_ID"; + + /** Replace the Memorystore port, if not the default port. */ + private static final int PORT = 6379; + + /** Set the name for the Leaderboard */ + private static final String LEADERBOARD_KEY = "leaderboard"; + + /** Number of users per page */ + private static final int PAGE_SIZE = 2; + + /** Sample user scores */ + private static final List> USER_SCORES = + List.of( + new SimpleEntry<>("User1", 100.0), + new SimpleEntry<>("User2", 80.0), + new SimpleEntry<>("User3", 95.0), + new SimpleEntry<>("User4", 70.0), + new SimpleEntry<>("User5", 60.0), + new SimpleEntry<>("User6", 50.0)); + + private MemorystoreLeaderboardPagination() { + // No-op; won't be called + } + + /** + * Writes to Memorystore and retrieves the leaderboard using pagination. + * + * @param args command-line arguments (page number) + */ + public static void main(final String[] args) { + // Validate input arguments + int pageNumber = args.length > 0 ? Integer.parseInt(args[0]) : 1; + if (pageNumber < 1) { + System.out.println("Invalid page number. Defaulting to page 1."); + pageNumber = 1; + } + + // Connect to the Memorystore instance + JedisPool pool = new JedisPool(INSTANCE_ID, PORT); + + try (Jedis jedis = pool.getResource()) { + // Add the scores to the leaderboard + for (SimpleEntry entry : USER_SCORES) { + jedis.zadd(LEADERBOARD_KEY, entry.getValue(), entry.getKey()); + System.out.printf( + "Added/Updated %s with score %s%n", entry.getKey(), entry.getValue()); + } + + // Display Users on page 1 + System.out.printf("\nLeaderboard - Page %d:\n", 1); + displayPaginatedLeaderboard(jedis, 1); + + // Display Users on page 2 + System.out.printf("\nLeaderboard - Page %d:\n", 2); + displayPaginatedLeaderboard(jedis, 2); + } catch (Exception e) { + System.err.printf("Error connecting to Redis: %s%n", e.getMessage()); + } + } + + /** + * Fetches and displays leaderboard users based on the given page number. + * + * @param jedis Redis client instance + * @param page The page number to fetch + */ + private static void displayPaginatedLeaderboard(Jedis jedis, int page) { + // Caulcate the start and end index for each page + int start = (page - 1) * PAGE_SIZE; + int end = start + PAGE_SIZE - 1; + + // Use zrevrange to find users between the start and end index + Set paginatedUsers = jedis.zrevrange(LEADERBOARD_KEY, start, end); + + // If no users are found, print a message and return + if (paginatedUsers.isEmpty()) { + System.out.println("No users found on this page."); + return; + } + + int ranking = start + 1; + for (String user : paginatedUsers) { + System.out.printf( + "Rank %d: %s, Score: %s%n", + ranking++, user, jedis.zscore(LEADERBOARD_KEY, user)); + } + } +} From 3b30bca2d27316d142df2843a01e732ed5713817 Mon Sep 17 00:00:00 2001 From: Jacob Cable Date: Thu, 6 Feb 2025 17:55:00 +0000 Subject: [PATCH 2/6] refactor(memorystore-leaderboard-snippets): lint --- .../valkey/leaderboard/snippets/pom.xml | 10 + .../java/samples/MemorystoreAddScore.java | 88 +++++---- .../java/samples/MemorystoreFilterByAsc.java | 119 ++++++------ .../java/samples/MemorystoreFilterByDesc.java | 107 ++++++----- .../MemorystoreLeaderboardPagination.java | 172 +++++++++--------- 5 files changed, 250 insertions(+), 246 deletions(-) diff --git a/memorystore/valkey/leaderboard/snippets/pom.xml b/memorystore/valkey/leaderboard/snippets/pom.xml index f4be44d7ba4..57ffbb5dd23 100644 --- a/memorystore/valkey/leaderboard/snippets/pom.xml +++ b/memorystore/valkey/leaderboard/snippets/pom.xml @@ -27,6 +27,16 @@ Google Cloud Memorystore Sample http://www.example.com + + + com.google.cloud.samples + shared-configuration + 1.2.0 + + UTF-8 11 diff --git a/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreAddScore.java b/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreAddScore.java index 38d9402789a..0f8278fa00b 100644 --- a/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreAddScore.java +++ b/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreAddScore.java @@ -14,63 +14,61 @@ * limitations under the License. */ -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisPool; - import java.util.AbstractMap.SimpleEntry; import java.util.List; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; public final class MemorystoreAddScore { - /** Replace the Memorystore instance id. */ - private static final String INSTANCE_ID = "INSTANCE_ID"; + /** Replace the Memorystore instance id. */ + private static final String INSTANCE_ID = "INSTANCE_ID"; - /** Replace the Memorystore port, if not the default port. */ - private static final int PORT = 6379; + /** Replace the Memorystore port, if not the default port. */ + private static final int PORT = 6379; - /** Set the name for the Leaderboard */ - private static final String LEADERBOARD_KEY = "leaderboard"; + /** Set the name for the Leaderboard */ + private static final String LEADERBOARD_KEY = "leaderboard"; - /** Replace the names and scores to write to Memorystore. */ - private static final List> USER_SCORES = - List.of( - new SimpleEntry<>("User1", 100.0), - new SimpleEntry<>("User2", 80.0), - new SimpleEntry<>("User3", 95.0), - new SimpleEntry<>("User4", 70.0)); + /** Replace the names and scores to write to Memorystore. */ + private static final List> USER_SCORES = List.of( + new SimpleEntry<>("User1", 100.0), + new SimpleEntry<>("User2", 80.0), + new SimpleEntry<>("User3", 95.0), + new SimpleEntry<>("User4", 70.0)); - private MemorystoreAddScore() { - // No-op; won't be called - } + private MemorystoreAddScore() { + // No-op; won't be called + } - /** - * Writes to Memorystore before verifying the item exists. - * - * @param args command-line arguments - */ - public static void main(final String[] args) { - // Connect to the Memorystore instance - JedisPool pool = new JedisPool(INSTANCE_ID, PORT); + /** + * Writes to Memorystore before verifying the item exists. + * + * @param args command-line arguments + */ + public static void main(final String[] args) { + // Connect to the Memorystore instance + JedisPool pool = new JedisPool(INSTANCE_ID, PORT); - try (Jedis jedis = pool.getResource()) { - // Add the scores to the leaderboard - for (SimpleEntry entry : USER_SCORES) { - jedis.zadd(LEADERBOARD_KEY, entry.getValue(), entry.getKey()); - System.out.printf( - "Added/Updated %s with score %s%n", entry.getKey(), entry.getValue()); - } + try (Jedis jedis = pool.getResource()) { + // Add the scores to the leaderboard + for (SimpleEntry entry : USER_SCORES) { + jedis.zadd(LEADERBOARD_KEY, entry.getValue(), entry.getKey()); + System.out.printf( + "Added/Updated %s with score %s%n", entry.getKey(), entry.getValue()); + } - // Verify that scores have been added - for (String userKey : USER_SCORES.keySet()) { - // Find the user score based on the user key - Double userScore = jedis.zscore(LEADERBOARD_KEY, userKey); + // Verify that scores have been added + for (String userKey : USER_SCORES.keySet()) { + // Find the user score based on the user key + Double userScore = jedis.zscore(LEADERBOARD_KEY, userKey); - // Print out the user score - System.out.printf("User %s has a score of %s%n", userKey, userScore); - } - } catch (Exception e) { - // Print any errors found in the exception - System.err.printf("Error connecting to Redis: %s%n", e.getMessage()); - } + // Print out the user score + System.out.printf("User %s has a score of %s%n", userKey, userScore); + } + } catch (Exception e) { + // Print any errors found in the exception + System.err.printf("Error connecting to Redis: %s%n", e.getMessage()); } + } } diff --git a/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByAsc.java b/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByAsc.java index 090096c3aa3..9094955ec9b 100644 --- a/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByAsc.java +++ b/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByAsc.java @@ -1,79 +1,78 @@ /* - * Copyright 2025 Google LLC - * - * 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 - * - * http://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. - */ - -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisPool; +* Copyright 2025 Google LLC +* +* 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 +* +* http://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. +*/ import java.util.AbstractMap.SimpleEntry; import java.util.List; import java.util.Set; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; public final class MemorystoreFilterByAsc { - /** Replace the Memorystore instance id. */ - private static final String INSTANCE_ID = "INSTANCE_ID"; + /** Replace the Memorystore instance id. */ + private static final String INSTANCE_ID = "INSTANCE_ID"; - /** Replace the Memorystore port, if not the default port. */ - private static final int PORT = 6379; + /** Replace the Memorystore port, if not the default port. */ + private static final int PORT = 6379; - /** Set the name for the Leaderboard */ - private static final String LEADERBOARD_KEY = "leaderboard"; + /** Set the name for the Leaderboard */ + private static final String LEADERBOARD_KEY = "leaderboard"; - /** Replace the names and scores to write to Memorystore. */ - private static final List> USER_SCORES = - List.of( - new SimpleEntry<>("User1", 100.0), - new SimpleEntry<>("User2", 80.0), - new SimpleEntry<>("User3", 95.0), - new SimpleEntry<>("User4", 70.0)); + /** Replace the names and scores to write to Memorystore. */ + private static final List> USER_SCORES = List.of( + new SimpleEntry<>("User1", 100.0), + new SimpleEntry<>("User2", 80.0), + new SimpleEntry<>("User3", 95.0), + new SimpleEntry<>("User4", 70.0)); - private MemorystoreFilterByAsc() { - // No-op; won't be called - } + private MemorystoreFilterByAsc() { + // No-op; won't be called + } - /** - * Writes to Memorystore and retrieves the leaderboard sorted in ascending order. - * - * @param args command-line arguments - */ - public static void main(final String[] args) { - // Connect to the Memorystore instance - JedisPool pool = new JedisPool(INSTANCE_ID, PORT); + /** + * Writes to Memorystore and retrieves the leaderboard sorted in ascending + * order. + * + * @param args command-line arguments + */ + public static void main(final String[] args) { + // Connect to the Memorystore instance + JedisPool pool = new JedisPool(INSTANCE_ID, PORT); - try (Jedis jedis = pool.getResource()) { - // Add the scores to the leaderboard - for (SimpleEntry entry : USER_SCORES) { - jedis.zadd(LEADERBOARD_KEY, entry.getValue(), entry.getKey()); - System.out.printf( - "Added/Updated %s with score %s%n", entry.getKey(), entry.getValue()); - } + try (Jedis jedis = pool.getResource()) { + // Add the scores to the leaderboard + for (SimpleEntry entry : USER_SCORES) { + jedis.zadd(LEADERBOARD_KEY, entry.getValue(), entry.getKey()); + System.out.printf( + "Added/Updated %s with score %s%n", entry.getKey(), entry.getValue()); + } - // Retrieve and print all users sorted by score in ascending order - Set sortedUsers = jedis.zrange(LEADERBOARD_KEY, 0, -1); + // Retrieve and print all users sorted by score in ascending order + Set sortedUsers = jedis.zrange(LEADERBOARD_KEY, 0, -1); - // Print the leaderboard in ascending order - System.out.printf("Leaderboad (Ascending)"); + // Print the leaderboard in ascending order + System.out.printf("Leaderboad (Ascending)"); - // For each user, print the score - for (String user : sortedUsers) { - System.out.printf( - "User: %s, Score: %s%n", user, jedis.zscore(LEADERBOARD_KEY, user)); - } - } catch (Exception e) { - System.err.printf("Error connecting to Redis: %s%n", e.getMessage()); - } + // For each user, print the score + for (String user : sortedUsers) { + System.out.printf( + "User: %s, Score: %s%n", user, jedis.zscore(LEADERBOARD_KEY, user)); + } + } catch (Exception e) { + System.err.printf("Error connecting to Redis: %s%n", e.getMessage()); } + } } diff --git a/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByDesc.java b/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByDesc.java index aa086d7e49d..77f29ffe54b 100644 --- a/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByDesc.java +++ b/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByDesc.java @@ -14,74 +14,73 @@ * limitations under the License. */ -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisPool; - import java.util.AbstractMap.SimpleEntry; import java.util.List; import java.util.Set; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; public final class MemorystoreFilterByDesc { - /** Replace the Memorystore instance id. */ - private static final String INSTANCE_ID = "INSTANCE_ID"; + /** Replace the Memorystore instance id. */ + private static final String INSTANCE_ID = "INSTANCE_ID"; - /** Replace the Memorystore port, if not the default port. */ - private static final int PORT = 6379; + /** Replace the Memorystore port, if not the default port. */ + private static final int PORT = 6379; - /** Set the name for the Leaderboard */ - private static final String LEADERBOARD_KEY = "leaderboard"; + /** Set the name for the Leaderboard */ + private static final String LEADERBOARD_KEY = "leaderboard"; - /** Replace the names and scores to write to Memorystore. */ - private static final List> USER_SCORES = - List.of( - new SimpleEntry<>("User1", 100.0), - new SimpleEntry<>("User2", 80.0), - new SimpleEntry<>("User3", 95.0), - new SimpleEntry<>("User4", 70.0)); + /** Replace the names and scores to write to Memorystore. */ + private static final List> USER_SCORES = List.of( + new SimpleEntry<>("User1", 100.0), + new SimpleEntry<>("User2", 80.0), + new SimpleEntry<>("User3", 95.0), + new SimpleEntry<>("User4", 70.0)); - private MemorystoreFilterByDesc() { - // No-op; won't be called - } + private MemorystoreFilterByDesc() { + // No-op; won't be called + } - /** - * Writes to Memorystore and retrieves the leaderboard sorted in descending order. - * - * @param args command-line arguments - */ - public static void main(final String[] args) { - // Connect to the Memorystore instance - JedisPool pool = new JedisPool(INSTANCE_ID, PORT); + /** + * Writes to Memorystore and retrieves the leaderboard sorted in descending + * order. + * + * @param args command-line arguments + */ + public static void main(final String[] args) { + // Connect to the Memorystore instance + JedisPool pool = new JedisPool(INSTANCE_ID, PORT); - try (Jedis jedis = pool.getResource()) { - // Add the scores to the leaderboard - for (SimpleEntry entry : USER_SCORES) { - jedis.zadd(LEADERBOARD_KEY, entry.getKey(), user.value()); - System.out.printf("Added/Updated %s with score %s%n", user, score); - } + try (Jedis jedis = pool.getResource()) { + // Add the scores to the leaderboard + for (SimpleEntry entry : USER_SCORES) { + jedis.zadd(LEADERBOARD_KEY, entry.getKey(), user.value()); + System.out.printf("Added/Updated %s with score %s%n", user, score); + } - // Retrieve and print all users sorted by score in descending order - System.out.println("\nLeaderboard (Sorted by Descending Scores):"); - Set sortedUsers = jedis.zrevrange(LEADERBOARD_KEY, 0, -1); + // Retrieve and print all users sorted by score in descending order + System.out.println("\nLeaderboard (Sorted by Descending Scores):"); + Set sortedUsers = jedis.zrevrange(LEADERBOARD_KEY, 0, -1); - // Print the leaderboard in descending order - for (String user : sortedUsers) { - System.out.printf( - "User: %s, Score: %s%n", user, jedis.zscore(LEADERBOARD_KEY, user)); - } + // Print the leaderboard in descending order + for (String user : sortedUsers) { + System.out.printf( + "User: %s, Score: %s%n", user, jedis.zscore(LEADERBOARD_KEY, user)); + } - // Get the highest-ranked user - System.out.println("\nTop Ranked User:"); - Set topUser = jedis.zrevrange(LEADERBOARD_KEY, 0, 0); - if (!topUser.isEmpty()) { - String user = topUser.iterator().next(); - System.out.printf( - "User: %s, Score: %s%n", user, jedis.zscore(LEADERBOARD_KEY, user)); - } else { - System.out.println("Leaderboard is empty."); - } - } catch (Exception e) { - System.err.printf("Error connecting to Redis: %s%n", e.getMessage()); - } + // Get the highest-ranked user + System.out.println("\nTop Ranked User:"); + Set topUser = jedis.zrevrange(LEADERBOARD_KEY, 0, 0); + if (!topUser.isEmpty()) { + String user = topUser.iterator().next(); + System.out.printf( + "User: %s, Score: %s%n", user, jedis.zscore(LEADERBOARD_KEY, user)); + } else { + System.out.println("Leaderboard is empty."); + } + } catch (Exception e) { + System.err.printf("Error connecting to Redis: %s%n", e.getMessage()); } + } } diff --git a/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreLeaderboardPagination.java b/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreLeaderboardPagination.java index ad3b0f9d80b..2a0f279b0d4 100644 --- a/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreLeaderboardPagination.java +++ b/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreLeaderboardPagination.java @@ -14,102 +14,100 @@ * limitations under the License. */ -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisPool; - import java.util.AbstractMap.SimpleEntry; import java.util.List; import java.util.Set; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; public final class MemorystoreLeaderboardPagination { - /** Replace the Memorystore instance id. */ - private static final String INSTANCE_ID = "INSTANCE_ID"; - - /** Replace the Memorystore port, if not the default port. */ - private static final int PORT = 6379; - - /** Set the name for the Leaderboard */ - private static final String LEADERBOARD_KEY = "leaderboard"; - - /** Number of users per page */ - private static final int PAGE_SIZE = 2; - - /** Sample user scores */ - private static final List> USER_SCORES = - List.of( - new SimpleEntry<>("User1", 100.0), - new SimpleEntry<>("User2", 80.0), - new SimpleEntry<>("User3", 95.0), - new SimpleEntry<>("User4", 70.0), - new SimpleEntry<>("User5", 60.0), - new SimpleEntry<>("User6", 50.0)); - - private MemorystoreLeaderboardPagination() { - // No-op; won't be called + /** Replace the Memorystore instance id. */ + private static final String INSTANCE_ID = "INSTANCE_ID"; + + /** Replace the Memorystore port, if not the default port. */ + private static final int PORT = 6379; + + /** Set the name for the Leaderboard */ + private static final String LEADERBOARD_KEY = "leaderboard"; + + /** Number of users per page */ + private static final int PAGE_SIZE = 2; + + /** Sample user scores */ + private static final List> USER_SCORES = List.of( + new SimpleEntry<>("User1", 100.0), + new SimpleEntry<>("User2", 80.0), + new SimpleEntry<>("User3", 95.0), + new SimpleEntry<>("User4", 70.0), + new SimpleEntry<>("User5", 60.0), + new SimpleEntry<>("User6", 50.0)); + + private MemorystoreLeaderboardPagination() { + // No-op; won't be called + } + + /** + * Writes to Memorystore and retrieves the leaderboard using pagination. + * + * @param args command-line arguments (page number) + */ + public static void main(final String[] args) { + // Validate input arguments + int pageNumber = args.length > 0 ? Integer.parseInt(args[0]) : 1; + if (pageNumber < 1) { + System.out.println("Invalid page number. Defaulting to page 1."); + pageNumber = 1; } - /** - * Writes to Memorystore and retrieves the leaderboard using pagination. - * - * @param args command-line arguments (page number) - */ - public static void main(final String[] args) { - // Validate input arguments - int pageNumber = args.length > 0 ? Integer.parseInt(args[0]) : 1; - if (pageNumber < 1) { - System.out.println("Invalid page number. Defaulting to page 1."); - pageNumber = 1; - } - - // Connect to the Memorystore instance - JedisPool pool = new JedisPool(INSTANCE_ID, PORT); - - try (Jedis jedis = pool.getResource()) { - // Add the scores to the leaderboard - for (SimpleEntry entry : USER_SCORES) { - jedis.zadd(LEADERBOARD_KEY, entry.getValue(), entry.getKey()); - System.out.printf( - "Added/Updated %s with score %s%n", entry.getKey(), entry.getValue()); - } - - // Display Users on page 1 - System.out.printf("\nLeaderboard - Page %d:\n", 1); - displayPaginatedLeaderboard(jedis, 1); - - // Display Users on page 2 - System.out.printf("\nLeaderboard - Page %d:\n", 2); - displayPaginatedLeaderboard(jedis, 2); - } catch (Exception e) { - System.err.printf("Error connecting to Redis: %s%n", e.getMessage()); - } + // Connect to the Memorystore instance + JedisPool pool = new JedisPool(INSTANCE_ID, PORT); + + try (Jedis jedis = pool.getResource()) { + // Add the scores to the leaderboard + for (SimpleEntry entry : USER_SCORES) { + jedis.zadd(LEADERBOARD_KEY, entry.getValue(), entry.getKey()); + System.out.printf( + "Added/Updated %s with score %s%n", entry.getKey(), entry.getValue()); + } + + // Display Users on page 1 + System.out.printf("\nLeaderboard - Page %d:\n", 1); + displayPaginatedLeaderboard(jedis, 1); + + // Display Users on page 2 + System.out.printf("\nLeaderboard - Page %d:\n", 2); + displayPaginatedLeaderboard(jedis, 2); + } catch (Exception e) { + System.err.printf("Error connecting to Redis: %s%n", e.getMessage()); + } + } + + /** + * Fetches and displays leaderboard users based on the given page number. + * + * @param jedis Redis client instance + * @param page The page number to fetch + */ + private static void displayPaginatedLeaderboard(Jedis jedis, int page) { + // Caulcate the start and end index for each page + int start = (page - 1) * PAGE_SIZE; + int end = start + PAGE_SIZE - 1; + + // Use zrevrange to find users between the start and end index + Set paginatedUsers = jedis.zrevrange(LEADERBOARD_KEY, start, end); + + // If no users are found, print a message and return + if (paginatedUsers.isEmpty()) { + System.out.println("No users found on this page."); + return; } - /** - * Fetches and displays leaderboard users based on the given page number. - * - * @param jedis Redis client instance - * @param page The page number to fetch - */ - private static void displayPaginatedLeaderboard(Jedis jedis, int page) { - // Caulcate the start and end index for each page - int start = (page - 1) * PAGE_SIZE; - int end = start + PAGE_SIZE - 1; - - // Use zrevrange to find users between the start and end index - Set paginatedUsers = jedis.zrevrange(LEADERBOARD_KEY, start, end); - - // If no users are found, print a message and return - if (paginatedUsers.isEmpty()) { - System.out.println("No users found on this page."); - return; - } - - int ranking = start + 1; - for (String user : paginatedUsers) { - System.out.printf( - "Rank %d: %s, Score: %s%n", - ranking++, user, jedis.zscore(LEADERBOARD_KEY, user)); - } + int ranking = start + 1; + for (String user : paginatedUsers) { + System.out.printf( + "Rank %d: %s, Score: %s%n", + ranking++, user, jedis.zscore(LEADERBOARD_KEY, user)); } + } } From 018ccf84e600952edeb4be88cee008db8aa61fd4 Mon Sep 17 00:00:00 2001 From: Darren Ackers Date: Thu, 6 Feb 2025 18:56:59 +0000 Subject: [PATCH 3/6] docs(leaderboard): updated snippet function naming --- .../src/main/java/samples/MemorystoreFilterByAsc.java | 4 ++-- .../src/main/java/samples/MemorystoreFilterByDesc.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByAsc.java b/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByAsc.java index 9094955ec9b..7ee289af567 100644 --- a/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByAsc.java +++ b/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByAsc.java @@ -20,7 +20,7 @@ import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; -public final class MemorystoreFilterByAsc { +public final class MemorystoreSortByScoreAsc { /** Replace the Memorystore instance id. */ private static final String INSTANCE_ID = "INSTANCE_ID"; @@ -38,7 +38,7 @@ public final class MemorystoreFilterByAsc { new SimpleEntry<>("User3", 95.0), new SimpleEntry<>("User4", 70.0)); - private MemorystoreFilterByAsc() { + private MemorystoreSortByScoreAsc() { // No-op; won't be called } diff --git a/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByDesc.java b/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByDesc.java index 77f29ffe54b..5098a34f758 100644 --- a/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByDesc.java +++ b/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByDesc.java @@ -20,7 +20,7 @@ import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; -public final class MemorystoreFilterByDesc { +public final class MemorystoreSortByScoreDesc { /** Replace the Memorystore instance id. */ private static final String INSTANCE_ID = "INSTANCE_ID"; @@ -38,7 +38,7 @@ public final class MemorystoreFilterByDesc { new SimpleEntry<>("User3", 95.0), new SimpleEntry<>("User4", 70.0)); - private MemorystoreFilterByDesc() { + private MemorystoreSortByScoreDesc() { // No-op; won't be called } From 03587ab7778b47307d73c5eed4ca57130e8e5483 Mon Sep 17 00:00:00 2001 From: Darren Ackers Date: Thu, 6 Feb 2025 23:22:44 +0000 Subject: [PATCH 4/6] docs(leaderboard): updated files names for asc and desc examples --- ...MemorystoreFilterByAsc.java => MemorystoreSortByScoreAsc.java} | 0 ...morystoreFilterByDesc.java => MemorystoreSortByScoreDesc.java} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename memorystore/valkey/leaderboard/snippets/src/main/java/samples/{MemorystoreFilterByAsc.java => MemorystoreSortByScoreAsc.java} (100%) rename memorystore/valkey/leaderboard/snippets/src/main/java/samples/{MemorystoreFilterByDesc.java => MemorystoreSortByScoreDesc.java} (100%) diff --git a/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByAsc.java b/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreSortByScoreAsc.java similarity index 100% rename from memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByAsc.java rename to memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreSortByScoreAsc.java diff --git a/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByDesc.java b/memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreSortByScoreDesc.java similarity index 100% rename from memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreFilterByDesc.java rename to memorystore/valkey/leaderboard/snippets/src/main/java/samples/MemorystoreSortByScoreDesc.java From 9f9647677a655e0e99f6a190e23adebc0405eb8d Mon Sep 17 00:00:00 2001 From: Corie Watson Date: Fri, 7 Feb 2025 13:42:29 +0000 Subject: [PATCH 5/6] chore(leaderboard): fix license --- memorystore/valkey/leaderboard/snippets/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/memorystore/valkey/leaderboard/snippets/pom.xml b/memorystore/valkey/leaderboard/snippets/pom.xml index 57ffbb5dd23..9ac9e5dfbb4 100644 --- a/memorystore/valkey/leaderboard/snippets/pom.xml +++ b/memorystore/valkey/leaderboard/snippets/pom.xml @@ -1,7 +1,7 @@