damondouglas commented on code in PR #29081:
URL: https://github.com/apache/beam/pull/29081#discussion_r1367257675


##########
sdks/java/io/rrio/src/main/java/org/apache/beam/io/requestresponse/RedisClient.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.beam.io.requestresponse;
+
+import static org.apache.beam.sdk.util.Preconditions.checkStateNotNull;
+
+import java.net.URI;
+import java.nio.charset.StandardCharsets;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.joda.time.Duration;
+import redis.clients.jedis.JedisPooled;
+import redis.clients.jedis.exceptions.JedisException;
+
+/**
+ * {@link RedisClient} is a convenience class that supports operations needed 
for caching by various
+ * transforms in this package. It implements the {@link SetupTeardown} 
interface for ease-of-use
+ * within a {@link DoFn} context. Unlike the underlying {@link JedisPooled} 
client, {@link
+ * RedisClient} is {@link java.io.Serializable}.
+ */
+class RedisClient implements SetupTeardown {
+
+  private final URI uri;
+
+  private transient @MonotonicNonNull JedisPooled jedis;
+
+  /**
+   * Instantiates a {@link RedisClient}. {@link URI} expected of the form: 
{@code
+   * redis://<host>:<port>}.
+   */
+  RedisClient(URI uri) {
+    this.uri = uri;
+  }
+
+  /**
+   * Decrement a value stored by the key, returning the resulting decremented 
value. Per Redis
+   * convention, sets the value to -1 for keys that do not exist. Naming of 
this method preserves
+   * that of the underlying {@link JedisPooled} client and performs a null 
check prior to execution.
+   */
+  long decr(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().decr(key);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /**
+   * Get the long value stored by the key. Yields zero when key does not 
exist, keeping consistency
+   * with Redis convention. Consider using {@link #exists} to query key 
existance.
+   */
+  long getLong(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().decrBy(key, 0L);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /** Query whether the key exists. */
+  boolean exists(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().exists(key);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /**
+   * Increment a value stored by the key, returning the resulting decremented 
value. Sets the value
+   * to 1, key does not exist, per Redis convention. Naming of this method 
preserves that of the
+   * underlying {@link JedisPooled} client and performs a null check prior to 
execution.
+   */
+  long incr(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().incr(key);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /**
+   * Query the size of a list identified by the key. Returns 0 if key does not 
exist, per Redis
+   * convention. Naming of this method preserves that of the underlying {@link 
JedisPooled} client
+   * and performs a null check prior to execution.
+   */
+  long llen(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().llen(key);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /**
+   * Pushes items to the back ('right') of the list. Naming of this method 
preserves that of the
+   * underlying {@link JedisPooled} client and performs a null check prior to 
execution.
+   */
+  void rpush(String key, byte[]... items) throws UserCodeExecutionException {
+    try {
+      getSafeClient().lpush(key.getBytes(StandardCharsets.UTF_8), items);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /**
+   * Pops items from the front ('left') of the list. Naming of this method 
preserves that of the
+   * underlying {@link JedisPooled} client and performs a null check prior to 
execution.
+   */
+  byte[] lpop(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().rpop(key.getBytes(StandardCharsets.UTF_8));

Review Comment:
   Thank you!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to