You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@accumulo.apache.org by "cshannon (via GitHub)" <gi...@apache.org> on 2023/09/22 11:46:34 UTC

[GitHub] [accumulo] cshannon commented on a diff in pull request #3761: No chop merge and delete

cshannon commented on code in PR #3761:
URL: https://github.com/apache/accumulo/pull/3761#discussion_r1334274040


##########
core/src/main/java/org/apache/accumulo/core/metadata/StoredTabletFile.java:
##########
@@ -118,11 +148,115 @@ public String toString() {
    * Validates that the provided metadata string for the StoredTabletFile is valid.
    */
   public static void validate(String metadataEntry) {
-    ReferencedTabletFile.parsePath(new Path(URI.create(metadataEntry)));
+    final TabletFileCq tabletFileCq = deserialize(metadataEntry);
+    // Validate the path
+    ReferencedTabletFile.parsePath(deserialize(metadataEntry).path);
+    // Validate the range
+    requireRowRange(tabletFileCq.range);
+  }
+
+  public static StoredTabletFile of(final Text metadataEntry) {
+    return new StoredTabletFile(Objects.requireNonNull(metadataEntry).toString());
   }
 
   public static StoredTabletFile of(final String metadataEntry) {
     return new StoredTabletFile(metadataEntry);
   }
 
+  public static StoredTabletFile of(final URI path, Range range) {
+    return of(new Path(Objects.requireNonNull(path)), range);
+  }
+
+  public static StoredTabletFile of(final Path path, Range range) {
+    return new StoredTabletFile(new TabletFileCq(Objects.requireNonNull(path), range));
+  }
+
+  private static final Gson gson = ByteArrayToBase64TypeAdapter.createBase64Gson();
+
+  private static TabletFileCq deserialize(String json) {
+    final TabletFileCqMetadataGson metadata =
+        gson.fromJson(Objects.requireNonNull(json), TabletFileCqMetadataGson.class);
+    // Recreate the exact Range that was originally stored in Metadata. Stored ranges are originally
+    // constructed with inclusive/exclusive for the start and end key inclusivity settings.
+    // (Except for Ranges with no start/endkey as then the inclusivity flags do not matter)
+    //
+    // With this particular constructor, when setting the startRowInclusive to true and
+    // endRowInclusive to false, both the start and end row values will be taken as is
+    // and not modified and will recreate the original Range.
+    //
+    // This constructor will always set the resulting inclusivity of the Range to be true for the
+    // start row and false for end row regardless of what the startRowInclusive and endRowInclusive
+    // flags are set to.
+    return new TabletFileCq(new Path(URI.create(metadata.path)),
+        new Range(decodeRow(metadata.startRow), true, decodeRow(metadata.endRow), false));
+  }
+
+  public static String serialize(String path) {
+    return serialize(path, new Range());
+  }
+
+  public static String serialize(String path, Range range) {
+    requireRowRange(range);
+    final TabletFileCqMetadataGson metadata = new TabletFileCqMetadataGson();
+    metadata.path = Objects.requireNonNull(path);
+    metadata.startRow = encodeRow(range.getStartKey());
+    metadata.endRow = encodeRow(range.getEndKey());
+
+    return gson.toJson(metadata);
+  }
+
+  private static String serialize(TabletFileCq tabletFileCq) {
+    return serialize(Objects.requireNonNull(tabletFileCq).path.toString(), tabletFileCq.range);
+  }
+
+  /**
+   * Helper methods to encode and decode rows in a range to/from byte arrays. Null rows will just be
+   * returned as an empty byte array
+   **/
+
+  private static byte[] encodeRow(final Key key) {
+    final Text row = key != null ? key.getRow() : null;
+    if (row != null) {
+      try (DataOutputBuffer buffer = new DataOutputBuffer()) {
+        row.write(buffer);
+        return buffer.getData();
+      } catch (IOException e) {
+        throw new UncheckedIOException(e);
+      }

Review Comment:
   @keith-turner - Thanks for the suggestion, I made the changes with the serialization to shrink how much is written and also the html escaping. I had also notice the long out put as well a while back and meant to go back and figure out why but I forgot because the last few weeks I've been testing with deserializing back into the Range object so it was human readable (as I noticed elsehwere) .



-- 
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: notifications-unsubscribe@accumulo.apache.org

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