You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2022/04/11 23:19:45 UTC

[GitHub] [beam] pabloem commented on a diff in pull request #15549: [BEAM-11997] Changed RedisIO implementation to SDF

pabloem commented on code in PR #15549:
URL: https://github.com/apache/beam/pull/15549#discussion_r847813348


##########
sdks/java/io/redis/src/main/java/org/apache/beam/sdk/io/redis/RedisCursor.java:
##########
@@ -0,0 +1,149 @@
+/*
+ * 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.sdk.io.redis;
+
+import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkNotNull;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.util.Objects;
+import javax.annotation.Nonnull;
+import org.apache.beam.sdk.coders.BigEndianLongCoder;
+import org.apache.beam.sdk.io.range.ByteKey;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting;
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+public class RedisCursor implements Comparable<RedisCursor>, Serializable {
+
+  public static final RedisCursor ZERO_CURSOR = RedisCursor.of("0", 8);
+
+  private final String cursor;
+  private final ByteKey byteCursor;
+  private final long dbSize;
+  private final int nBits;
+
+  public static RedisCursor of(String cursor, long dbSize) {
+    return new RedisCursor(cursor, dbSize);
+  }
+
+  public static RedisCursor of(ByteKey byteCursor, long dbSize) {
+    return new RedisCursor(byteCursor, dbSize);
+  }
+
+  private RedisCursor(ByteKey byteCursor, long dbSize) {
+    this.byteCursor = byteCursor;
+    this.dbSize = dbSize;
+    this.nBits = getTablePow(dbSize);
+    this.cursor = byteKeyToString(byteCursor, nBits);
+  }
+
+  private RedisCursor(String cursor, long dbSize) {
+    this.cursor = cursor;
+    this.dbSize = dbSize;
+    this.nBits = getTablePow(dbSize);
+    this.byteCursor = stringCursorToByteKey(cursor, this.nBits);
+  }
+
+  /**
+   * {@link RedisCursor} implements {@link Comparable Comparable&lt;RedisCursor&gt;} by transforming
+   * the cursors to an index of the Redis table.
+   */
+  @Override
+  public int compareTo(@Nonnull RedisCursor other) {
+    checkNotNull(other, "other");
+    return Long.compare(Long.parseLong(cursor), Long.parseLong(other.cursor));
+  }
+
+  @Override
+  public boolean equals(@Nullable Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+    RedisCursor that = (RedisCursor) o;
+    return dbSize == that.dbSize
+        && nBits == that.nBits
+        && Objects.equals(cursor, that.cursor)
+        && Objects.equals(byteCursor, that.byteCursor);
+  }
+
+  @Override
+  public int hashCode() {
+    return Objects.hash(cursor, byteCursor, dbSize, nBits);
+  }
+
+  public String getCursor() {
+    return cursor;
+  }
+
+  public ByteKey getByteCursor() {
+    return byteCursor;
+  }
+
+  public long getDbSize() {
+    return dbSize;
+  }
+
+  @VisibleForTesting
+  static ByteKey stringCursorToByteKey(String cursor, int nBits) {
+    long cursorLong = Long.parseLong(cursor);
+    long reversed = shiftBits(cursorLong, nBits);
+    BigEndianLongCoder coder = BigEndianLongCoder.of();
+    ByteArrayOutputStream os = new ByteArrayOutputStream();
+    try {
+      coder.encode(reversed, os);
+    } catch (IOException e) {
+      throw new IllegalArgumentException("invalid redis cursor " + cursor);
+    }
+    byte[] byteArray = os.toByteArray();
+    return ByteKey.copyFrom(byteArray);
+  }
+
+  @VisibleForTesting
+  static long shiftBits(long a, int nBits) {
+    long b = 0;

Review Comment:
   can you please use Long.reverse? 



-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org