luoluoyuyu commented on code in PR #16722: URL: https://github.com/apache/iotdb/pull/16722#discussion_r2516430546
########## iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/load/active/ActiveLoadPathHelper.java: ########## @@ -0,0 +1,301 @@ +/* + * 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.iotdb.db.storageengine.load.active; + +import org.apache.iotdb.commons.conf.IoTDBConstant; +import org.apache.iotdb.db.exception.sql.SemanticException; +import org.apache.iotdb.db.queryengine.plan.statement.crud.LoadTsFileStatement; +import org.apache.iotdb.db.storageengine.load.config.LoadTsFileConfigurator; + +import java.io.File; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; + +/** Utility methods for encoding and decoding load attributes into directory structures. */ +public final class ActiveLoadPathHelper { + + private static final String SEGMENT_SEPARATOR = "-"; + + private static final List<String> KEY_ORDER = + Collections.unmodifiableList( + Arrays.asList( + LoadTsFileConfigurator.DATABASE_NAME_KEY, + LoadTsFileConfigurator.DATABASE_LEVEL_KEY, + LoadTsFileConfigurator.CONVERT_ON_TYPE_MISMATCH_KEY, + LoadTsFileConfigurator.TABLET_CONVERSION_THRESHOLD_KEY, + LoadTsFileConfigurator.VERIFY_KEY)); + + private ActiveLoadPathHelper() { + throw new IllegalStateException("Utility class"); + } + + public static Map<String, String> buildAttributes( + final String databaseName, + final Integer databaseLevel, + final Boolean convertOnTypeMismatch, + final Boolean verify, + final Long tabletConversionThresholdBytes) { + + final Map<String, String> attributes = new LinkedHashMap<>(); + if (Objects.nonNull(databaseName) && !databaseName.isEmpty()) { + attributes.put(LoadTsFileConfigurator.DATABASE_NAME_KEY, databaseName); + } + if (Objects.nonNull(databaseLevel)) { + attributes.put(LoadTsFileConfigurator.DATABASE_LEVEL_KEY, databaseLevel.toString()); + } + if (Objects.nonNull(convertOnTypeMismatch)) { + attributes.put( + LoadTsFileConfigurator.CONVERT_ON_TYPE_MISMATCH_KEY, + Boolean.toString(convertOnTypeMismatch)); + } + if (Objects.nonNull(tabletConversionThresholdBytes)) { + attributes.put( + LoadTsFileConfigurator.TABLET_CONVERSION_THRESHOLD_KEY, + tabletConversionThresholdBytes.toString()); + } + if (Objects.nonNull(verify)) { + attributes.put(LoadTsFileConfigurator.VERIFY_KEY, Boolean.toString(verify)); + } + return attributes; + } + + public static File resolveTargetDir(final File baseDir, final Map<String, String> attributes) { + File current = baseDir; + for (final String key : KEY_ORDER) { + final String value = attributes.get(key); + if (value == null) { + continue; + } + current = new File(current, formatSegment(key, value)); + // compatibility: keep placing tablet conversion and verify under convert folder + if (LoadTsFileConfigurator.CONVERT_ON_TYPE_MISMATCH_KEY.equals(key)) { + final String threshold = + attributes.get(LoadTsFileConfigurator.TABLET_CONVERSION_THRESHOLD_KEY); + if (threshold != null) { + current = + new File( + current, + formatSegment(LoadTsFileConfigurator.TABLET_CONVERSION_THRESHOLD_KEY, threshold)); + } + final String verify = attributes.get(LoadTsFileConfigurator.VERIFY_KEY); + if (verify != null) { + current = new File(current, formatSegment(LoadTsFileConfigurator.VERIFY_KEY, verify)); + } + return current; + } + } + return current; + } + + public static Map<String, String> parseAttributes(final File file, final File pendingDir) { + if (file == null) { + return Collections.emptyMap(); + } + + final Map<String, String> attributes = new HashMap<>(); + File current = file.getParentFile(); + boolean convertFolderVisited = false; + while (current != null) { + final String dirName = current.getName(); + if (pendingDir != null && current.equals(pendingDir)) { + break; + } + for (final String key : KEY_ORDER) { + final String prefix = key + SEGMENT_SEPARATOR; + if (dirName.startsWith(prefix)) { + extractAndValidateAttributeValue(key, dirName, prefix.length()) + .ifPresent(value -> attributes.putIfAbsent(key, value)); + if (LoadTsFileConfigurator.CONVERT_ON_TYPE_MISMATCH_KEY.equals(key)) { + convertFolderVisited = true; + } + break; + } + } + if (convertFolderVisited + && !attributes.containsKey(LoadTsFileConfigurator.TABLET_CONVERSION_THRESHOLD_KEY) + && dirName.startsWith( + LoadTsFileConfigurator.TABLET_CONVERSION_THRESHOLD_KEY + SEGMENT_SEPARATOR)) { + extractAndValidateAttributeValue( + LoadTsFileConfigurator.TABLET_CONVERSION_THRESHOLD_KEY, + dirName, + (LoadTsFileConfigurator.TABLET_CONVERSION_THRESHOLD_KEY + SEGMENT_SEPARATOR) + .length()) + .ifPresent( + value -> + attributes.putIfAbsent( + LoadTsFileConfigurator.TABLET_CONVERSION_THRESHOLD_KEY, value)); + } + if (convertFolderVisited + && !attributes.containsKey(LoadTsFileConfigurator.VERIFY_KEY) + && dirName.startsWith(LoadTsFileConfigurator.VERIFY_KEY + SEGMENT_SEPARATOR)) { + extractAndValidateAttributeValue( + LoadTsFileConfigurator.VERIFY_KEY, + dirName, + (LoadTsFileConfigurator.VERIFY_KEY + SEGMENT_SEPARATOR).length()) + .ifPresent(value -> attributes.putIfAbsent(LoadTsFileConfigurator.VERIFY_KEY, value)); + } + current = current.getParentFile(); + } Review Comment: Because Async Load is based on Active Load, which is another feature exposed to users and allows them to customize the directory format, strict verification is required. See the documentation at https://iotdb.apache.org/UserGuide/latest-Table/Tools-System/Data-Import-Tool.html#_3-tsfile-auto-loading -- 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]
