wu-sheng commented on code in PR #10544: URL: https://github.com/apache/skywalking/pull/10544#discussion_r1138596144
########## oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/TableHelper.java: ########## @@ -0,0 +1,141 @@ +/* + * 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.skywalking.oap.server.storage.plugin.jdbc.common; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.skywalking.oap.server.core.Const; +import org.apache.skywalking.oap.server.core.CoreModule; +import org.apache.skywalking.oap.server.core.analysis.DownSampling; +import org.apache.skywalking.oap.server.core.analysis.FunctionCategory; +import org.apache.skywalking.oap.server.core.analysis.TimeBucket; +import org.apache.skywalking.oap.server.core.config.ConfigService; +import org.apache.skywalking.oap.server.core.storage.model.Model; +import org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCClient; +import org.apache.skywalking.oap.server.library.module.ModuleManager; +import org.apache.skywalking.oap.server.library.util.StringUtil; +import org.apache.skywalking.oap.server.storage.plugin.jdbc.TableMetaInfo; + +import java.sql.SQLException; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import java.util.stream.LongStream; + +/** + * Utility class to get table name for a given model. + */ +@Slf4j +@RequiredArgsConstructor +public class TableHelper { + private final ModuleManager moduleManager; + private final JDBCClient jdbcClient; + + public static String getTableName(Model model) { + final var aggFuncName = FunctionCategory.uniqueFunctionName(model.getStreamClass()).replaceAll("-", "_"); + return StringUtil.isNotBlank(aggFuncName) ? aggFuncName : model.getName(); + } + + public static String getTableForWrite(Model model) { + final var tableName = getTableName(model); + + if (!model.isTimeSeries()) { + return tableName; + } + + final var dayTimeBucket = TimeBucket.getTimeBucket(System.currentTimeMillis(), DownSampling.Day); + return tableName + Const.UNDERSCORE + dayTimeBucket; + } + + public static String getTable(Model model, long timeBucket) { + final var tableName = getTableName(model); + if (timeBucket == 0) { + timeBucket = TimeBucket.getTimeBucket(System.currentTimeMillis(), DownSampling.Day); + } + + if (!model.isTimeSeries()) { + return tableName; + } + + return tableName + Const.UNDERSCORE + TimeBucket.getTimeBucket(TimeBucket.getTimestamp(timeBucket), DownSampling.Day); + } + + public List<String> getTablesForRead(String modelName, long timeBucketStart, long timeBucketEnd) { + final var model = TableMetaInfo.get(modelName); + final var tableName = getTableName(model); + + if (!model.isTimeSeries()) { + return Collections.singletonList(tableName); + } + + timeBucketStart = TimeBucket.getTimeBucket(TimeBucket.getTimestamp(timeBucketStart), DownSampling.Day); + timeBucketEnd = TimeBucket.getTimeBucket(TimeBucket.getTimestamp(timeBucketEnd), DownSampling.Day); + + return LongStream + .rangeClosed(timeBucketStart, timeBucketEnd) + .distinct() + .mapToObj(it -> tableName + "_" + it) + .filter(table -> { + try { + return jdbcClient.tableExists(table); Review Comment: OK, I noticed this is another misguide by naming. This method is from TTL, so, most likely we should rename to `getExistingTables` or similar. ########## oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/TableHelper.java: ########## @@ -0,0 +1,141 @@ +/* + * 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.skywalking.oap.server.storage.plugin.jdbc.common; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.skywalking.oap.server.core.Const; +import org.apache.skywalking.oap.server.core.CoreModule; +import org.apache.skywalking.oap.server.core.analysis.DownSampling; +import org.apache.skywalking.oap.server.core.analysis.FunctionCategory; +import org.apache.skywalking.oap.server.core.analysis.TimeBucket; +import org.apache.skywalking.oap.server.core.config.ConfigService; +import org.apache.skywalking.oap.server.core.storage.model.Model; +import org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCClient; +import org.apache.skywalking.oap.server.library.module.ModuleManager; +import org.apache.skywalking.oap.server.library.util.StringUtil; +import org.apache.skywalking.oap.server.storage.plugin.jdbc.TableMetaInfo; + +import java.sql.SQLException; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import java.util.stream.LongStream; + +/** + * Utility class to get table name for a given model. + */ +@Slf4j +@RequiredArgsConstructor +public class TableHelper { + private final ModuleManager moduleManager; + private final JDBCClient jdbcClient; + + public static String getTableName(Model model) { + final var aggFuncName = FunctionCategory.uniqueFunctionName(model.getStreamClass()).replaceAll("-", "_"); + return StringUtil.isNotBlank(aggFuncName) ? aggFuncName : model.getName(); + } + + public static String getTableForWrite(Model model) { + final var tableName = getTableName(model); + + if (!model.isTimeSeries()) { + return tableName; + } + + final var dayTimeBucket = TimeBucket.getTimeBucket(System.currentTimeMillis(), DownSampling.Day); + return tableName + Const.UNDERSCORE + dayTimeBucket; + } + + public static String getTable(Model model, long timeBucket) { + final var tableName = getTableName(model); + if (timeBucket == 0) { + timeBucket = TimeBucket.getTimeBucket(System.currentTimeMillis(), DownSampling.Day); + } + + if (!model.isTimeSeries()) { + return tableName; + } + + return tableName + Const.UNDERSCORE + TimeBucket.getTimeBucket(TimeBucket.getTimestamp(timeBucket), DownSampling.Day); + } + + public List<String> getTablesForRead(String modelName, long timeBucketStart, long timeBucketEnd) { Review Comment: ```suggestion public List<String> getExistingTables(String modelName, long timeBucketStart, long timeBucketEnd) { ``` -- 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]
