yuqi1129 commented on code in PR #15070: URL: https://github.com/apache/gravitino/pull/15070#discussion_r4625570688
########## core/src/main/java/org/apache/gravitino/glossary/GlossaryManager.java: ########## @@ -0,0 +1,203 @@ +/* + * 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.glossary; + +import com.google.common.base.Preconditions; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.List; +import java.util.UUID; +import javax.annotation.Nullable; +import org.apache.gravitino.Entity.EntityType; +import org.apache.gravitino.EntityAlreadyExistsException; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.Namespace; +import org.apache.gravitino.exceptions.NoSuchEntityException; +import org.apache.gravitino.exceptions.NoSuchMetalakeException; +import org.apache.gravitino.exceptions.NoSuchTermException; +import org.apache.gravitino.exceptions.TermAlreadyExistsException; +import org.apache.gravitino.exceptions.TermVersionConflictException; +import org.apache.gravitino.meta.GlossaryTermEntity; +import org.apache.gravitino.storage.IdGenerator; +import org.apache.gravitino.storage.relational.service.GlossaryTermMetaService; +import org.apache.gravitino.storage.relational.service.OwnerMetaService; +import org.apache.gravitino.utils.NameIdentifierUtil; +import org.apache.gravitino.utils.NamespaceUtil; + +/** Manages live glossary definitions. Callers supply the metalake scope for every operation. */ +public final class GlossaryManager { + private final GlossaryTermMetaService terms = GlossaryTermMetaService.getInstance(); + private final IdGenerator ids; + private final boolean authorizationEnabled; + private final TermChangeHandler handler; + + /** Creates a manager that applies effective changes immediately. */ + public GlossaryManager(IdGenerator ids, boolean authorizationEnabled) { + this(ids, authorizationEnabled, (before, after, persist) -> persist.run()); + } + + GlossaryManager(IdGenerator ids, boolean authorizationEnabled, TermChangeHandler handler) { + this.ids = ids; + this.authorizationEnabled = authorizationEnabled; + this.handler = handler; + } + + /** + * Lists term definitions in the given metalake. + * + * @throws NoSuchMetalakeException if the metalake does not exist + */ + public List<GlossaryTermEntity> listTerms(String metalake) { + try { + return terms.list(NamespaceUtil.ofGlossaryTerm(metalake)); + } catch (NoSuchEntityException e) { + throw new NoSuchMetalakeException(e, "Metalake %s does not exist", metalake); + } + } + + /** Loads a definition by normalized name. */ + public GlossaryTermEntity getTerm(String metalake, String name) { + NameIdentifier ident = NameIdentifier.of(NamespaceUtil.ofGlossaryTerm(metalake), name); + try { + return terms.get(ident); + } catch (NoSuchEntityException e) { + throw new NoSuchTermException(e, "Term %s does not exist", ident); + } + } + + /** + * Creates a live definition. When authorization is enabled the creator must be a registered user + * in the metalake. The definition and creator ownership commit or roll back together. + * + * @throws NoSuchEntityException if the metalake or required creator is missing + */ + public GlossaryTermEntity createTerm(String metalake, TermSpec spec) { + Namespace ns = NamespaceUtil.ofGlossaryTerm(metalake); + GlossaryTermEntity term = TermEdits.create(ids.nextId(), ns, spec); + return terms.withHierarchyLock( Review Comment: Fixed in 4843882d94. The create path translates only failures identifying or locking the parent metalake, preserving the cause. Missing creator ownership still rolls the transaction back with its original exception. The new missing-parent regression and existing missing-creator rollback test both passed on H2. -- 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]
