Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.amoro.server.persistence.mapper.PlatformFileMapper;
import org.apache.amoro.server.persistence.mapper.ResourceMapper;
import org.apache.amoro.server.persistence.mapper.TableBlockerMapper;
import org.apache.amoro.server.persistence.mapper.TableCleanupMapper;
import org.apache.amoro.server.persistence.mapper.TableMetaMapper;
import org.apache.amoro.server.persistence.mapper.TableProcessMapper;
import org.apache.amoro.server.persistence.mapper.TableRuntimeMapper;
Expand Down Expand Up @@ -74,6 +75,7 @@ public void init(DataSource dataSource) throws SQLException {
configuration.addMapper(TableBlockerMapper.class);
configuration.addMapper(TableProcessMapper.class);
configuration.addMapper(TableRuntimeMapper.class);
configuration.addMapper(TableCleanupMapper.class);

PageInterceptor interceptor = new PageInterceptor();
Properties interceptorProperties = new Properties();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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.amoro.server.persistence.converter;

import org.apache.amoro.server.table.cleanup.CleanupOperation;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
* MyBatis type handler for converting between CleanupOperation enum and database integer values.
* This handler maps CleanupOperation enum values to their corresponding integer codes for
* persistence, and converts integer codes back to enum values when reading from the database.
*/
@MappedJdbcTypes(JdbcType.INTEGER)
@MappedTypes(CleanupOperation.class)
public class CleanupOperationConverter extends BaseTypeHandler<CleanupOperation> {
private static final Logger LOG = LoggerFactory.getLogger(CleanupOperationConverter.class);

@Override
public void setNonNullParameter(
PreparedStatement ps, int i, CleanupOperation parameter, JdbcType jdbcType)
throws SQLException {
ps.setInt(i, parameter.getCode());
}

@Override
public CleanupOperation getNullableResult(ResultSet rs, String columnName) throws SQLException {
return convertFromString(rs.getString(columnName));
}

@Override
public CleanupOperation getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return convertFromString(rs.getString(columnIndex));
}

@Override
public CleanupOperation getNullableResult(CallableStatement cs, int columnIndex)
throws SQLException {
return convertFromString(cs.getString(columnIndex));
}

/**
* Converts a string value to a CleanupOperation enum value. Handles null values and parsing
* errors gracefully.
*
* @param s The string value from the database
* @return The corresponding CleanupOperation enum value, or null if conversion fails
*/
private CleanupOperation convertFromString(String s) {
if (s == null) {
return null;
}
try {
return CleanupOperation.fromCode(Integer.parseInt(s));
} catch (NumberFormatException e) {
LOG.warn("Failed to parse CleanupOperation from string: {}", s, e);
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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.amoro.server.persistence.mapper;

import org.apache.amoro.server.persistence.converter.Long2TsConverter;
import org.apache.amoro.server.table.cleanup.CleanupOperation;
import org.apache.amoro.server.table.cleanup.TableCleanupProcessMeta;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface TableCleanupMapper {
String TABLE_NAME = "table_cleanup_process";

/** Insert a new table cleanup process record */
@Insert(
"INSERT INTO "
+ TABLE_NAME
+ " (cleanup_process_id, table_id, catalog_name, db_name, table_name, cleanup_operation_code, last_cleanup_end_time) "
+ "VALUES (#{cleanupProcessMeta.cleanupProcessId},"
+ " #{cleanupProcessMeta.tableId}, #{cleanupProcessMeta.catalogName}, "
+ "#{cleanupProcessMeta.dbName}, #{cleanupProcessMeta.tableName},"
+ "#{cleanupProcessMeta.cleanupOperation,"
+ " typeHandler=org.apache.amoro.server.persistence.converter.CleanupOperationConverter},"
+ "#{cleanupProcessMeta.lastCleanupEndTime,"
+ " typeHandler=org.apache.amoro.server.persistence.converter.Long2TsConverter})")
void insertTableCleanupProcess(
@Param("cleanupProcessMeta") TableCleanupProcessMeta cleanupProcessMeta);

/** Update lastCleanupEndTime field by table_id and cleanup_operation_code */
@Update(
"UPDATE "
+ TABLE_NAME
+ " SET last_cleanup_end_time = #{lastCleanupEndTime, typeHandler=org.apache.amoro.server.persistence.converter.Long2TsConverter} "
+ "WHERE table_id = #{tableId} AND cleanup_operation_code = #{cleanupOperation,"
+ " typeHandler=org.apache.amoro.server.persistence.converter.CleanupOperationConverter}")
int updateLastCleanupEndTimeByTableIdAndCleanupOperation(
@Param("tableId") long tableId,
@Param("cleanupOperation") CleanupOperation cleanupOperation,
@Param("lastCleanupEndTime") long lastCleanupEndTime);

/**
* Select table_id and last_cleanup_end_time by cleanup_operation_code and table_ids If
* last_cleanup_end_time is NULL in database, it will be converted to 0L as default value
*/
@Select(
"<script>"
+ "SELECT table_id,last_cleanup_end_time "
+ "FROM "
+ TABLE_NAME
+ " WHERE cleanup_operation_code = #{cleanupOperation,"
+ " typeHandler=org.apache.amoro.server.persistence.converter.CleanupOperationConverter}"
+ " AND table_id IN "
+ "<foreach item='tableId' collection='tableIds' open='(' separator=',' close=')'>"
+ "#{tableId}"
+ "</foreach>"
+ "</script>")
@Results({
@Result(property = "tableId", column = "table_id"),
@Result(
property = "lastCleanupEndTime",
column = "last_cleanup_end_time",
typeHandler = Long2TsConverter.class)
})
List<TableCleanupProcessMeta> selectTableIdAndLastCleanupEndTime(
@Param("tableIds") List<Long> tableIds,
@Param("cleanupOperation") CleanupOperation cleanupOperation);

/** Delete all cleanup process records for a specific table */
@Delete("DELETE FROM " + TABLE_NAME + " WHERE table_id = #{tableId}")
void deleteTableCleanupProcesses(@Param("tableId") long tableId);
}
Loading