eric-maynard commented on code in PR #247:
URL: https://github.com/apache/polaris/pull/247#discussion_r1759294752


##########
extension/persistence/eclipselink/src/main/java/org/apache/polaris/extension/persistence/impl/eclipselink/PolarisSequenceManager.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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.polaris.extension.persistence.impl.eclipselink;
+
+import jakarta.persistence.*;
+import java.util.Optional;
+import java.util.concurrent.atomic.AtomicBoolean;
+import org.apache.polaris.core.persistence.models.ModelSequenceId;
+import org.eclipse.persistence.internal.jpa.EntityManagerImpl;
+import org.eclipse.persistence.platform.database.DatabasePlatform;
+import org.eclipse.persistence.platform.database.PostgreSQLPlatform;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Used to generate sequence IDs for Polaris entities. If the legacy 
`POLARIS_SEQ` generator is
+ * available it will be used then cleaned up. In all other cases the 
`POLARIS_SEQUENCE` table is
+ * used directly.
+ */
+public class PolarisSequenceManager {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(PolarisSequenceManager.class);
+
+  private static AtomicBoolean sequenceCleaned = new AtomicBoolean(false);
+
+  /* Get the database platform associated with the `EntityManager` */
+  private static DatabasePlatform getDatabasePlatform(EntityManager session) {
+    EntityManagerImpl entityManagerImpl = 
session.unwrap(EntityManagerImpl.class);
+    return entityManagerImpl.getDatabaseSession().getPlatform();
+  }
+
+  private static void removeSequence(EntityManager session) {
+    LOGGER.info("Renaming legacy sequence `POLARIS_SEQ` to 
`POLARIS_SEQ_UNUSED`");
+    String renameSequenceQuery = "ALTER SEQUENCE POLARIS_SEQ RENAME TO 
POLARIS_SEQ_UNUSED";
+    session.createNativeQuery(renameSequenceQuery).executeUpdate();
+  }
+
+  private static synchronized Optional<Long> getSequenceId(EntityManager 
session) {
+    DatabasePlatform databasePlatform = getDatabasePlatform(session);
+    if (databasePlatform instanceof PostgreSQLPlatform) {
+      Optional<Long> result = Optional.empty();
+      if (!sequenceCleaned.get()) {
+        try {
+          LOGGER.info("Checking if the sequence POLARIS_SEQ exists");
+          String checkSequenceQuery =
+              "SELECT COUNT(*) FROM information_schema.sequences WHERE 
sequence_name IN ('polaris_seq', 'POLARIS_SEQ')";
+          int sequenceExists =
+              ((Number) 
session.createNativeQuery(checkSequenceQuery).getSingleResult()).intValue();
+
+          if (sequenceExists > 0) {
+            LOGGER.info("POLARIS_SEQ exists, calling NEXTVAL");
+            long queryResult =
+                (long) session.createNativeQuery("SELECT 
NEXTVAL('POLARIS_SEQ')").getSingleResult();
+            result = Optional.of(queryResult);
+          } else {
+            LOGGER.info("POLARIS_SEQ does not exist, skipping NEXTVAL");
+          }
+        } catch (Exception e) {
+          LOGGER.info(
+              "Encountered an exception when checking sequence or calling 
`NEXTVAL('POLARIS_SEQ')`",
+              e);
+        }
+        if (result.isPresent()) {
+          removeSequence(session);
+        }
+        sequenceCleaned.set(true);
+      }
+      return result;
+    } else {
+      LOGGER.info("Skipping POLARIS_SEQ / NEXTVAL check for platform " + 
databasePlatform);
+      return Optional.empty();
+    }
+  }
+
+  /**
+   * Prepare the `PolarisSequenceManager` to generate IDs. This may run a 
failing query, so it
+   * should be called for the first time outside the context of a transaction.
+   */
+  public static void initialize(EntityManager session) {
+    // Trigger cleanup of the POLARIS_SEQ if it is present
+    getSequenceId(session);
+  }
+
+  /**
+   * Generates a new ID from `POLARIS_SEQUENCE` or `POLARIS_SEQ` depending on 
availability. If
+   * `POLARIS_SEQ` exists, it will be renamed to `POLARIS_SEQ_UNUSED`.
+   */
+  public static Long getNewId(EntityManager session) {
+    ModelSequenceId modelSequenceId = new ModelSequenceId();
+
+    // If a legacy sequence ID is present, use that as an override:
+    getSequenceId(session).ifPresent(modelSequenceId::setId);
+

Review Comment:
   I think it's better to encapsulate the implementation of `getSequenceId` in 
that method. 
   
   The responsibility of `getNewId` is simply to create a `ModelSequenceId` and 
persist it. The implementation of how the `ModelSequenceId` gets its ID is not 
that relevant to readers of `getNewId`. I would be opposed to leaking 
`getSequenceId ` implementation details (e.g. databases X, Y support method A, 
database Z supports method B) into this `getNewId`. 



##########
extension/persistence/eclipselink/src/main/java/org/apache/polaris/extension/persistence/impl/eclipselink/PolarisSequenceManager.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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.polaris.extension.persistence.impl.eclipselink;
+
+import jakarta.persistence.*;
+import java.util.Optional;
+import java.util.concurrent.atomic.AtomicBoolean;
+import org.apache.polaris.core.persistence.models.ModelSequenceId;
+import org.eclipse.persistence.internal.jpa.EntityManagerImpl;
+import org.eclipse.persistence.platform.database.DatabasePlatform;
+import org.eclipse.persistence.platform.database.PostgreSQLPlatform;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Used to generate sequence IDs for Polaris entities. If the legacy 
`POLARIS_SEQ` generator is
+ * available it will be used then cleaned up. In all other cases the 
`POLARIS_SEQUENCE` table is
+ * used directly.
+ */
+public class PolarisSequenceManager {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(PolarisSequenceManager.class);
+
+  private static AtomicBoolean sequenceCleaned = new AtomicBoolean(false);
+
+  /* Get the database platform associated with the `EntityManager` */
+  private static DatabasePlatform getDatabasePlatform(EntityManager session) {
+    EntityManagerImpl entityManagerImpl = 
session.unwrap(EntityManagerImpl.class);
+    return entityManagerImpl.getDatabaseSession().getPlatform();
+  }
+
+  private static void removeSequence(EntityManager session) {
+    LOGGER.info("Renaming legacy sequence `POLARIS_SEQ` to 
`POLARIS_SEQ_UNUSED`");
+    String renameSequenceQuery = "ALTER SEQUENCE POLARIS_SEQ RENAME TO 
POLARIS_SEQ_UNUSED";
+    session.createNativeQuery(renameSequenceQuery).executeUpdate();
+  }
+
+  private static synchronized Optional<Long> getSequenceId(EntityManager 
session) {
+    DatabasePlatform databasePlatform = getDatabasePlatform(session);
+    if (databasePlatform instanceof PostgreSQLPlatform) {
+      Optional<Long> result = Optional.empty();
+      if (!sequenceCleaned.get()) {
+        try {
+          LOGGER.info("Checking if the sequence POLARIS_SEQ exists");
+          String checkSequenceQuery =
+              "SELECT COUNT(*) FROM information_schema.sequences WHERE 
sequence_name IN ('polaris_seq', 'POLARIS_SEQ')";
+          int sequenceExists =
+              ((Number) 
session.createNativeQuery(checkSequenceQuery).getSingleResult()).intValue();
+
+          if (sequenceExists > 0) {
+            LOGGER.info("POLARIS_SEQ exists, calling NEXTVAL");
+            long queryResult =
+                (long) session.createNativeQuery("SELECT 
NEXTVAL('POLARIS_SEQ')").getSingleResult();
+            result = Optional.of(queryResult);
+          } else {
+            LOGGER.info("POLARIS_SEQ does not exist, skipping NEXTVAL");
+          }
+        } catch (Exception e) {
+          LOGGER.info(
+              "Encountered an exception when checking sequence or calling 
`NEXTVAL('POLARIS_SEQ')`",
+              e);
+        }
+        if (result.isPresent()) {
+          removeSequence(session);
+        }
+        sequenceCleaned.set(true);
+      }
+      return result;
+    } else {
+      LOGGER.info("Skipping POLARIS_SEQ / NEXTVAL check for platform " + 
databasePlatform);
+      return Optional.empty();
+    }
+  }
+
+  /**
+   * Prepare the `PolarisSequenceManager` to generate IDs. This may run a 
failing query, so it
+   * should be called for the first time outside the context of a transaction.
+   */
+  public static void initialize(EntityManager session) {
+    // Trigger cleanup of the POLARIS_SEQ if it is present
+    getSequenceId(session);
+  }
+
+  /**
+   * Generates a new ID from `POLARIS_SEQUENCE` or `POLARIS_SEQ` depending on 
availability. If
+   * `POLARIS_SEQ` exists, it will be renamed to `POLARIS_SEQ_UNUSED`.
+   */
+  public static Long getNewId(EntityManager session) {
+    ModelSequenceId modelSequenceId = new ModelSequenceId();
+
+    // If a legacy sequence ID is present, use that as an override:
+    getSequenceId(session).ifPresent(modelSequenceId::setId);
+

Review Comment:
   I think it's better to encapsulate the implementation of `getSequenceId` in 
that private method. 
   
   The responsibility of `getNewId` is simply to create a `ModelSequenceId` and 
persist it. The implementation of how the `ModelSequenceId` gets its ID is not 
that relevant to readers of `getNewId`. I would be opposed to leaking 
`getSequenceId ` implementation details (e.g. databases X, Y support method A, 
database Z supports method B) into this `getNewId`. 



-- 
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