You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2021/09/17 05:10:00 UTC

[GitHub] [iceberg] szehon-ho commented on a change in pull request #3104: Core: add snapshot annotation to table metadata (WIP)

szehon-ho commented on a change in pull request #3104:
URL: https://github.com/apache/iceberg/pull/3104#discussion_r710753216



##########
File path: core/src/main/java/org/apache/iceberg/SnapshotAnnotationParser.java
##########
@@ -0,0 +1,90 @@
+/*
+ * 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.iceberg;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.JsonNode;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.UncheckedIOException;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.util.JsonUtil;
+
+public class SnapshotAnnotationParser {
+
+  private SnapshotAnnotationParser() {
+  }
+
+  private static final String SNAPSHOT_ID = "snapshot-id";
+  private static final String SNAPSHOT_NAME = "snapshot-name";
+  private static final String EXPIRE_STRATEGY = "expire-strategy";
+  private static final String RETAINED_UNTIL = "retained-until";
+
+  public static String toJson(SnapshotAnnotation annotation) {
+    return toJson(annotation, false);
+  }
+
+  public static String toJson(SnapshotAnnotation annotation, boolean pretty) {
+    try {
+      StringWriter writer = new StringWriter();
+      JsonGenerator generator = JsonUtil.factory().createGenerator(writer);
+      if (pretty) {
+        generator.useDefaultPrettyPrinter();
+      }
+      toJson(annotation, generator);
+      generator.flush();
+      return writer.toString();
+    } catch (IOException e) {
+      throw new UncheckedIOException(e);
+    }
+  }
+
+  public static void toJson(SnapshotAnnotation annotation, JsonGenerator generator) throws IOException {

Review comment:
       Can we have a better name/signature for this, toJson infers it returns a string (was confused when reading the caller code). Maybe writeSnapshotAnnotation(JsonGenerator generator, SnapshotAnnotation annotation) if not too redundant

##########
File path: api/src/main/java/org/apache/iceberg/SnapshotAnnotation.java
##########
@@ -0,0 +1,128 @@
+/*
+ * 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.iceberg;
+
+import java.util.Objects;
+import org.apache.iceberg.exceptions.ValidationException;
+
+/**
+ * User-defined information of a named snapshot
+ */
+public class SnapshotAnnotation {
+
+  static final SnapshotExpireStrategy EXPIRE_STRATEGY_DEFAULT = SnapshotExpireStrategy.DEFAULT;
+
+  private final long snapshotId;
+  private final String snapshotName;
+  private final SnapshotExpireStrategy expireStrategy;
+  private final Long retainedUntilMillis;
+
+  private SnapshotAnnotation(
+      long snapshotId,
+      String snapshotName,
+      SnapshotExpireStrategy expireStrategy,
+      Long retainedUntilMillis) {
+    this.snapshotId = snapshotId;
+    this.snapshotName = snapshotName;
+    this.expireStrategy = expireStrategy;
+    this.retainedUntilMillis = retainedUntilMillis;
+  }
+
+  public long snapshotId() {
+    return snapshotId;
+  }
+
+  public String snapshotName() {
+    return snapshotName;
+  }
+
+  public SnapshotExpireStrategy expireStrategy() {
+    return expireStrategy;
+  }
+
+  public Long retainedUntilMillis() {
+    return retainedUntilMillis;
+  }
+
+  public static Builder builderFor(long snapshotId, String snapshotName) {
+    return new Builder(snapshotId, snapshotName);
+  }
+
+  public static class Builder {
+
+    private final Long snapshotId;
+    private final String snapshotName;
+
+    private SnapshotExpireStrategy expireStrategy = EXPIRE_STRATEGY_DEFAULT;
+    private Long retainedUntilMillis;
+
+    Builder(long snapshotId, String snapshotName) {
+      ValidationException.check(snapshotId > 0, "Invalid snapshot ID: must be greater than 0");
+      ValidationException.check(snapshotName != null, "Snapshot name must not be null for snapshot annotation");
+      this.snapshotId = snapshotId;
+      this.snapshotName = snapshotName;
+    }
+
+    public Builder expireStrategy(SnapshotExpireStrategy strategy) {
+      this.expireStrategy = strategy;
+      return this;
+    }
+
+    public Builder retainedUntilMillis(Long millis) {
+      this.retainedUntilMillis = millis;
+      return this;
+    }
+
+    public SnapshotAnnotation build() {
+      if (expireStrategy.equals(SnapshotExpireStrategy.RETAINED_UNTIL)) {
+        ValidationException.check(retainedUntilMillis != null,
+            "Retention expiration time must be set if snapshot lifecycle is RETENTION");
+      }
+
+      return new SnapshotAnnotation(snapshotId, snapshotName, expireStrategy, retainedUntilMillis);
+    }
+  }
+
+  @Override
+  public boolean equals(Object o) {

Review comment:
       Maybe I missed something, but what is different than default equals/hashcode?

##########
File path: core/src/main/java/org/apache/iceberg/util/JsonUtil.java
##########
@@ -73,6 +73,18 @@ public static long getLong(String property, JsonNode node) {
     return pNode.asLong();
   }
 
+  public static Long getLongOrNull(String property, JsonNode node) {
+    if (!node.has(property)) {
+      return null;
+    }
+
+    Preconditions.checkArgument(node.has(property), "Cannot parse missing long %s", property);

Review comment:
       No point anymore to this check anymore , right ?

##########
File path: core/src/main/java/org/apache/iceberg/SnapshotAnnotationParser.java
##########
@@ -0,0 +1,90 @@
+/*
+ * 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.iceberg;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.JsonNode;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.UncheckedIOException;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.util.JsonUtil;
+
+public class SnapshotAnnotationParser {
+
+  private SnapshotAnnotationParser() {
+  }
+
+  private static final String SNAPSHOT_ID = "snapshot-id";
+  private static final String SNAPSHOT_NAME = "snapshot-name";
+  private static final String EXPIRE_STRATEGY = "expire-strategy";
+  private static final String RETAINED_UNTIL = "retained-until";
+
+  public static String toJson(SnapshotAnnotation annotation) {
+    return toJson(annotation, false);
+  }
+
+  public static String toJson(SnapshotAnnotation annotation, boolean pretty) {
+    try {
+      StringWriter writer = new StringWriter();
+      JsonGenerator generator = JsonUtil.factory().createGenerator(writer);
+      if (pretty) {
+        generator.useDefaultPrettyPrinter();
+      }
+      toJson(annotation, generator);
+      generator.flush();
+      return writer.toString();
+    } catch (IOException e) {
+      throw new UncheckedIOException(e);
+    }
+  }
+
+  public static void toJson(SnapshotAnnotation annotation, JsonGenerator generator) throws IOException {

Review comment:
       And also do we need make it public?




-- 
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: issues-unsubscribe@iceberg.apache.org

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



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org