zachjsh commented on code in PR #13165:
URL: https://github.com/apache/druid/pull/13165#discussion_r1004988215


##########
extensions-core/druid-catalog/src/main/java/org/apache/druid/catalog/storage/sql/SQLCatalogManager.java:
##########
@@ -0,0 +1,578 @@
+/*
+ * 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.druid.catalog.storage.sql;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import com.google.inject.Inject;
+import org.apache.druid.catalog.model.TableId;
+import org.apache.druid.catalog.model.TableMetadata;
+import org.apache.druid.catalog.model.TableSpec;
+import org.apache.druid.catalog.storage.MetastoreManager;
+import org.apache.druid.guice.ManageLifecycle;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.java.util.common.lifecycle.LifecycleStart;
+import org.apache.druid.metadata.SQLMetadataConnector;
+import org.skife.jdbi.v2.Handle;
+import org.skife.jdbi.v2.IDBI;
+import org.skife.jdbi.v2.Query;
+import org.skife.jdbi.v2.ResultIterator;
+import org.skife.jdbi.v2.Update;
+import org.skife.jdbi.v2.exceptions.CallbackFailedException;
+import org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException;
+import org.skife.jdbi.v2.tweak.HandleCallback;
+
+import java.util.Deque;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentLinkedDeque;
+import java.util.function.Function;
+
+@ManageLifecycle
+public class SQLCatalogManager implements CatalogManager
+{
+  public static final String TABLES_TABLE = "tableDefs";
+
+  private static final String INSERT_TABLE =
+      "INSERT INTO %s\n" +
+      "  (schemaName, name, creationTime, updateTime, state, payload)\n" +
+      "  VALUES(:schemaName, :name, :creationTime, :updateTime, :state, 
:payload)";
+
+  private static final String UPDATE_HEAD =
+      "UPDATE %s\n SET\n";
+
+  private static final String WHERE_TABLE_ID =
+      "WHERE schemaName = :schemaName\n" +
+      "  AND name = :name\n";
+
+  private static final String SAFETY_CHECK =
+      "  AND updateTime = :oldVersion";
+
+  private static final String UPDATE_DEFN_UNSAFE =
+      UPDATE_HEAD +
+      "  payload = :payload,\n" +
+      "  updateTime = :updateTime\n" +
+      WHERE_TABLE_ID;
+
+  private static final String UPDATE_DEFN_SAFE =
+      UPDATE_DEFN_UNSAFE +
+      SAFETY_CHECK;
+
+  private static final String UPDATE_STATE =
+      UPDATE_HEAD +
+      "  state = :state,\n" +
+      "  updateTime = :updateTime\n" +
+      WHERE_TABLE_ID;
+
+  private static final String SELECT_TABLE =
+      "SELECT creationTime, updateTime, state, payload\n" +
+      "FROM %s\n" +
+      WHERE_TABLE_ID;
+
+  private static final String SELECT_PAYLOAD =
+      "SELECT state, payload\n" +
+      "FROM %s\n" +
+      WHERE_TABLE_ID;
+
+  private static final String SELECT_ALL_TABLES =
+      "SELECT schemaName, name\n" +
+      "FROM %s\n" +
+      "ORDER BY schemaName, name";
+
+  private static final String SELECT_TABLES_IN_SCHEMA =
+      "SELECT name\n" +
+      "FROM %s\n" +
+      "WHERE schemaName = :schemaName\n" +
+      "ORDER BY name";
+
+  private static final String SELECT_TABLE_DETAILS_IN_SCHEMA =
+      "SELECT name, creationTime, updateTime, state, payload\n" +
+      "FROM %s\n" +
+      "WHERE schemaName = :schemaName\n" +
+      "ORDER BY name";
+
+  private static final String DELETE_TABLE =
+      "DELETE FROM %s\n" +
+      WHERE_TABLE_ID;
+
+  private final MetastoreManager metastoreManager;
+  private final SQLMetadataConnector connector;
+  private final ObjectMapper jsonMapper;
+  private final IDBI dbi;
+  private final String tableName;
+  private final Deque<Listener> listeners = new ConcurrentLinkedDeque<>();
+
+  @Inject
+  public SQLCatalogManager(MetastoreManager metastoreManager)
+  {
+    if (!metastoreManager.isSql()) {
+      throw new ISE("SQLCatalogManager only works with SQL based metadata 
store at this time");
+    }
+    this.metastoreManager = metastoreManager;
+    this.connector = metastoreManager.sqlConnector();
+    this.dbi = connector.getDBI();
+    this.jsonMapper = metastoreManager.jsonMapper();
+    this.tableName = getTableDefnTable();
+  }
+
+  @Override
+  @LifecycleStart
+  public void start()
+  {
+    createTableDefnTable();
+  }
+
+  @Override
+  public void stop()
+  {
+  }
+
+  // Mimics what MetadataStorageTablesConfig should do.
+  public String getTableDefnTable()
+  {
+    final String base = metastoreManager.tablesConfig().getBase();
+    if (Strings.isNullOrEmpty(base)) {
+      return TABLES_TABLE;
+    } else {
+      return StringUtils.format("%s_%s", base, TABLES_TABLE);
+    }
+  }
+
+  // TODO: Move to SqlMetadataConnector
+  @Override
+  public void createTableDefnTable()
+  {
+    if (!metastoreManager.createTables()) {
+      return;
+    }
+    connector.createTable(
+        tableName,
+        ImmutableList.of(
+            StringUtils.format(
+                "CREATE TABLE %s (\n"
+                + "  schemaName VARCHAR(255) NOT NULL,\n"
+                + "  name VARCHAR(255) NOT NULL,\n"
+                + "  creationTime BIGINT NOT NULL,\n"
+                + "  updateTime BIGINT NOT NULL,\n"
+                + "  state CHAR(1) NOT NULL,\n"
+                + "  payload %s,\n"
+                + "  PRIMARY KEY(schemaName, name)\n"

Review Comment:
   Does this need a IF NOT EXIST clause? This seems to be called on lifecycle 
start, which I believe is called whenever there is a leadership change to this 
service instance / the cluster comes up. Wont this fail if the table already 
exists?



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to