Skip to content

Commit

Permalink
[apache#4832] feat(core): Add basic framework to support multiple JDB…
Browse files Browse the repository at this point in the history
…C backend. (apache#4833)

### What changes were proposed in this pull request?

1. Add a framework to support multiple JDBC backend 
2. Modify all the mapper classes related to metalake, catalog, schema,
filesetset, table, topic and use new framework, others will use another
PR to avoid a large PR.

### Why are the changes needed?

To support more JDBC storage backend.

Fix: apache#4832 

### Does this PR introduce _any_ user-facing change?

N/A

### How was this patch tested?

Existing tests.
  • Loading branch information
yuqi1129 committed Sep 24, 2024
1 parent a639390 commit 6222cf6
Show file tree
Hide file tree
Showing 23 changed files with 2,113 additions and 859 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,10 @@ public void insertRelation(
}
}

enum JDBCBackendType {
public enum JDBCBackendType {
H2(true),
MYSQL(false);
MYSQL(false),
POSTGRESQL(false);

private final boolean embedded;

Expand All @@ -432,10 +433,25 @@ public static JDBCBackendType fromURI(String jdbcURI) {
return JDBCBackendType.H2;
} else if (jdbcURI.startsWith("jdbc:mysql")) {
return JDBCBackendType.MYSQL;
} else if (jdbcURI.startsWith("jdbc:postgresql")) {
return JDBCBackendType.POSTGRESQL;
} else {
throw new IllegalArgumentException("Unknown JDBC URI: " + jdbcURI);
}
}

public static JDBCBackendType fromString(String jdbcType) {
switch (jdbcType) {
case "h2":
return JDBCBackendType.H2;
case "mysql":
return JDBCBackendType.MYSQL;
case "postgresql":
return JDBCBackendType.POSTGRESQL;
default:
throw new IllegalArgumentException("Unknown JDBC type: " + jdbcType);
}
}
}

/** Start JDBC database if necessary. For example, start the H2 database if the backend is H2. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/*
* 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.gravitino.storage.relational.mapper;

import static org.apache.gravitino.storage.relational.mapper.CatalogMetaMapper.TABLE_NAME;

import org.apache.gravitino.storage.relational.po.CatalogPO;
import org.apache.ibatis.annotations.Param;

public class CatalogMetaBaseSQLProvider {
public String listCatalogPOsByMetalakeId(@Param("metalakeId") Long metalakeId) {
return "SELECT catalog_id as catalogId, catalog_name as catalogName,"
+ " metalake_id as metalakeId, type, provider,"
+ " catalog_comment as catalogComment, properties, audit_info as auditInfo,"
+ " current_version as currentVersion, last_version as lastVersion,"
+ " deleted_at as deletedAt"
+ " FROM "
+ TABLE_NAME
+ " WHERE metalake_id = #{metalakeId} AND deleted_at = 0";
}

public String selectCatalogIdByMetalakeIdAndName(
@Param("metalakeId") Long metalakeId, @Param("catalogName") String name) {
return "SELECT catalog_id as catalogId FROM "
+ TABLE_NAME
+ " WHERE metalake_id = #{metalakeId} AND catalog_name = #{catalogName} AND deleted_at = 0";
}

public String selectCatalogMetaByMetalakeIdAndName(
@Param("metalakeId") Long metalakeId, @Param("catalogName") String name) {
return "SELECT catalog_id as catalogId, catalog_name as catalogName,"
+ " metalake_id as metalakeId, type, provider,"
+ " catalog_comment as catalogComment, properties, audit_info as auditInfo,"
+ " current_version as currentVersion, last_version as lastVersion,"
+ " deleted_at as deletedAt"
+ " FROM "
+ TABLE_NAME
+ " WHERE metalake_id = #{metalakeId} AND catalog_name = #{catalogName} AND deleted_at = 0";
}

public String selectCatalogMetaById(@Param("catalogId") Long catalogId) {
return "SELECT catalog_id as catalogId, catalog_name as catalogName,"
+ " metalake_id as metalakeId, type, provider,"
+ " catalog_comment as catalogComment, properties, audit_info as auditInfo,"
+ " current_version as currentVersion, last_version as lastVersion,"
+ " deleted_at as deletedAt"
+ " FROM "
+ TABLE_NAME
+ " WHERE catalog_id = #{catalogId} AND deleted_at = 0";
}

public String insertCatalogMeta(@Param("catalogMeta") CatalogPO catalogPO) {
return "INSERT INTO "
+ TABLE_NAME
+ "(catalog_id, catalog_name, metalake_id,"
+ " type, provider, catalog_comment, properties, audit_info,"
+ " current_version, last_version, deleted_at)"
+ " VALUES("
+ " #{catalogMeta.catalogId},"
+ " #{catalogMeta.catalogName},"
+ " #{catalogMeta.metalakeId},"
+ " #{catalogMeta.type},"
+ " #{catalogMeta.provider},"
+ " #{catalogMeta.catalogComment},"
+ " #{catalogMeta.properties},"
+ " #{catalogMeta.auditInfo},"
+ " #{catalogMeta.currentVersion},"
+ " #{catalogMeta.lastVersion},"
+ " #{catalogMeta.deletedAt}"
+ " )";
}

public String insertCatalogMetaOnDuplicateKeyUpdate(@Param("catalogMeta") CatalogPO catalogPO) {
return "INSERT INTO "
+ TABLE_NAME
+ "(catalog_id, catalog_name, metalake_id,"
+ " type, provider, catalog_comment, properties, audit_info,"
+ " current_version, last_version, deleted_at)"
+ " VALUES("
+ " #{catalogMeta.catalogId},"
+ " #{catalogMeta.catalogName},"
+ " #{catalogMeta.metalakeId},"
+ " #{catalogMeta.type},"
+ " #{catalogMeta.provider},"
+ " #{catalogMeta.catalogComment},"
+ " #{catalogMeta.properties},"
+ " #{catalogMeta.auditInfo},"
+ " #{catalogMeta.currentVersion},"
+ " #{catalogMeta.lastVersion},"
+ " #{catalogMeta.deletedAt}"
+ " )"
+ " ON DUPLICATE KEY UPDATE"
+ " catalog_name = #{catalogMeta.catalogName},"
+ " metalake_id = #{catalogMeta.metalakeId},"
+ " type = #{catalogMeta.type},"
+ " provider = #{catalogMeta.provider},"
+ " catalog_comment = #{catalogMeta.catalogComment},"
+ " properties = #{catalogMeta.properties},"
+ " audit_info = #{catalogMeta.auditInfo},"
+ " current_version = #{catalogMeta.currentVersion},"
+ " last_version = #{catalogMeta.lastVersion},"
+ " deleted_at = #{catalogMeta.deletedAt}";
}

public String updateCatalogMeta(
@Param("newCatalogMeta") CatalogPO newCatalogPO,
@Param("oldCatalogMeta") CatalogPO oldCatalogPO) {
return "UPDATE "
+ TABLE_NAME
+ " SET catalog_name = #{newCatalogMeta.catalogName},"
+ " metalake_id = #{newCatalogMeta.metalakeId},"
+ " type = #{newCatalogMeta.type},"
+ " provider = #{newCatalogMeta.provider},"
+ " catalog_comment = #{newCatalogMeta.catalogComment},"
+ " properties = #{newCatalogMeta.properties},"
+ " audit_info = #{newCatalogMeta.auditInfo},"
+ " current_version = #{newCatalogMeta.currentVersion},"
+ " last_version = #{newCatalogMeta.lastVersion},"
+ " deleted_at = #{newCatalogMeta.deletedAt}"
+ " WHERE catalog_id = #{oldCatalogMeta.catalogId}"
+ " AND catalog_name = #{oldCatalogMeta.catalogName}"
+ " AND metalake_id = #{oldCatalogMeta.metalakeId}"
+ " AND type = #{oldCatalogMeta.type}"
+ " AND provider = #{oldCatalogMeta.provider}"
+ " AND (catalog_comment = #{oldCatalogMeta.catalogComment} "
+ " OR (catalog_comment IS NULL and #{oldCatalogMeta.catalogComment} IS NULL))"
+ " AND properties = #{oldCatalogMeta.properties}"
+ " AND audit_info = #{oldCatalogMeta.auditInfo}"
+ " AND current_version = #{oldCatalogMeta.currentVersion}"
+ " AND last_version = #{oldCatalogMeta.lastVersion}"
+ " AND deleted_at = 0";
}

public String softDeleteCatalogMetasByCatalogId(@Param("catalogId") Long catalogId) {
return "UPDATE "
+ TABLE_NAME
+ " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)"
+ " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000"
+ " WHERE catalog_id = #{catalogId} AND deleted_at = 0";
}

public String softDeleteCatalogMetasByMetalakeId(@Param("metalakeId") Long metalakeId) {
return "UPDATE "
+ TABLE_NAME
+ " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)"
+ " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000"
+ " WHERE metalake_id = #{metalakeId} AND deleted_at = 0";
}

public String deleteCatalogMetasByLegacyTimeline(
@Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) {
return "DELETE FROM "
+ TABLE_NAME
+ " WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT #{limit}";
}
}
Loading

0 comments on commit 6222cf6

Please sign in to comment.