jerryshao commented on code in PR #5728: URL: https://github.com/apache/gravitino/pull/5728#discussion_r1878053613
########## core/src/main/java/org/apache/gravitino/storage/relational/service/ModelVersionMetaService.java: ########## @@ -0,0 +1,288 @@ +/* + * 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.service; + +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; +import java.io.IOException; +import java.util.Collections; +import java.util.List; +import java.util.Locale; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Collectors; +import org.apache.gravitino.Entity; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.Namespace; +import org.apache.gravitino.exceptions.NoSuchEntityException; +import org.apache.gravitino.meta.ModelEntity; +import org.apache.gravitino.meta.ModelVersionEntity; +import org.apache.gravitino.storage.relational.mapper.ModelMetaMapper; +import org.apache.gravitino.storage.relational.mapper.ModelVersionAliasRelMapper; +import org.apache.gravitino.storage.relational.mapper.ModelVersionMetaMapper; +import org.apache.gravitino.storage.relational.po.ModelPO; +import org.apache.gravitino.storage.relational.po.ModelVersionAliasRelPO; +import org.apache.gravitino.storage.relational.po.ModelVersionPO; +import org.apache.gravitino.storage.relational.utils.ExceptionUtils; +import org.apache.gravitino.storage.relational.utils.POConverters; +import org.apache.gravitino.storage.relational.utils.SessionUtils; +import org.apache.gravitino.utils.NameIdentifierUtil; +import org.apache.gravitino.utils.NamespaceUtil; +import org.glassfish.jersey.internal.guava.Lists; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ModelVersionMetaService { + + private static final Logger LOG = LoggerFactory.getLogger(ModelVersionMetaService.class); + + private static final ModelVersionMetaService INSTANCE = new ModelVersionMetaService(); + + public static ModelVersionMetaService getInstance() { + return INSTANCE; + } + + private ModelVersionMetaService() {} + + public List<ModelVersionEntity> listModelVersionsByNamespace(Namespace ns) { + NamespaceUtil.checkModelVersion(ns); + + NameIdentifier modelIdent = NameIdentifier.of(ns.levels()); + // Will throw a NoSuchEntityException if the model does not exist. + ModelEntity modelEntity = ModelMetaService.getInstance().getModelByIdentifier(modelIdent); + + List<ModelVersionPO> modelVersionPOs = + SessionUtils.getWithoutCommit( + ModelVersionMetaMapper.class, + mapper -> mapper.listModelVersionMetasByModelId(modelEntity.id())); + + if (modelVersionPOs.isEmpty()) { + return Collections.emptyList(); + } + + // Get the aliases for all the model versions. + List<ModelVersionAliasRelPO> aliasRelPOs = + SessionUtils.getWithoutCommit( + ModelVersionAliasRelMapper.class, + mapper -> mapper.selectModelVersionAliasRelByModelId(modelEntity.id())); + Multimap<Integer, ModelVersionAliasRelPO> aliasRelPOsByModelVersion = + ArrayListMultimap.create(); + aliasRelPOs.forEach(r -> aliasRelPOsByModelVersion.put(r.getModelVersion(), r)); + + return modelVersionPOs.stream() + .map( + m -> { + List<ModelVersionAliasRelPO> versionAliasRelPOs = + Lists.newArrayList(aliasRelPOsByModelVersion.get(m.getModelVersion())); + return POConverters.fromModelVersionPO(modelIdent, m, versionAliasRelPOs); + }) + .collect(Collectors.toList()); + } + + public ModelVersionEntity getModelVersionByIdentifier(NameIdentifier ident) { + NameIdentifierUtil.checkModelVersion(ident); + + NameIdentifier modelIdent = NameIdentifier.of(ident.namespace().levels()); + // Will throw a NoSuchEntityException if the model does not exist. + ModelEntity modelEntity = ModelMetaService.getInstance().getModelByIdentifier(modelIdent); + + Integer modelVersion; + try { + modelVersion = Integer.valueOf(ident.name()); Review Comment: It is mainly due to the limitation of the current entity store interface, which only accepts the NameIdentifier as the key, so we have to convert the resource description into a name identifier. Here it is a little tricky, but seems there's no better way unless we refactor the entity store interface. You can also check the current column's name identifier implementation, it also has 5 levels, so I think it is a feasible way. -- 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]
