World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#15280] feat(api): Define ontology interfaces (#15282)
mchades3e760f465786
diff --git a/api/src/main/java/org/apache/gravitino/exceptions/IllegalOntologyException.java b/api/src/main/java/org/apache/gravitino/exceptions/IllegalOntologyException.javanew file mode 100644index 0000000000..77977f0541--- /dev/null+++ b/api/src/main/java/org/apache/gravitino/exceptions/IllegalOntologyException.java@@ -0,0 +1,49 @@+/*+ * 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.exceptions;++import com.google.errorprone.annotations.FormatMethod;+import com.google.errorprone.annotations.FormatString;++/** Exception thrown when an ontology definition or change is invalid. */+public class IllegalOntologyException extends IllegalArgumentException {++ /**+ * Constructs an exception with a formatted detail message.+ *+ * @param message The detail message format.+ * @param args The arguments referenced by the format specifiers in the detail message.+ */+ @FormatMethod+ public IllegalOntologyException(@FormatString String message, Object... args) {+ super(String.format(message, args));+ }++ /**+ * Constructs an exception with a cause and a formatted detail message.+ *+ * @param cause The cause of the exception.+ * @param message The detail message format.+ * @param args The arguments referenced by the format specifiers in the detail message.+ */+ @FormatMethod+ public IllegalOntologyException(Throwable cause, @FormatString String message, Object... args) {+ super(String.format(message, args), cause);+ }+}diff --git a/api/src/main/java/org/apache/gravitino/exceptions/NoSuchOntologyException.java b/api/src/main/java/org/apache/gravitino/exceptions/NoSuchOntologyException.javanew file mode 100644index 0000000000..487c0c9a64--- /dev/null+++ b/api/src/main/java/org/apache/gravitino/exceptions/NoSuchOntologyException.java@@ -0,0 +1,49 @@+/*+ * 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.exceptions;++import com.google.errorprone.annotations.FormatMethod;+import com.google.errorprone.annotations.FormatString;++/** Exception thrown when an ontology with the specified identifier does not exist. */+public class NoSuchOntologyException extends NotFoundException {++ /**+ * Constructs an exception with a formatted detail message.+ *+ * @param message The detail message format.+ * @param args The arguments referenced by the format specifiers in the detail message.+ */+ @FormatMethod+ public NoSuchOntologyException(@FormatString String message, Object... args) {+ super(message, args);+ }++ /**+ * Constructs an exception with a cause and a formatted detail message.+ *+ * @param cause The cause of the exception.+ * @param message The detail message format.+ * @param args The arguments referenced by the format specifiers in the detail message.+ */+ @FormatMethod+ public NoSuchOntologyException(Throwable cause, @FormatString String message, Object... args) {+ super(cause, message, args);+ }+}diff --git a/api/src/main/java/org/apache/gravitino/exceptions/OntologyAlreadyExistsException.java b/api/src/main/java/org/apache/gravitino/exceptions/OntologyAlreadyExistsException.javanew file mode 100644index 0000000000..ff5666966d--- /dev/null+++ b/api/src/main/java/org/apache/gravitino/exceptions/OntologyAlreadyExistsException.java@@ -0,0 +1,50 @@+/*+ * 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.exceptions;++import com.google.errorprone.annotations.FormatMethod;+import com.google.errorprone.annotations.FormatString;++/** Exception thrown when an ontology with the specified identifier already exists. */+public class OntologyAlreadyExistsException extends AlreadyExistsException {++ /**+ * Constructs an exception with a formatted detail message.+ *+ * @param message The detail message format.+ * @param args The arguments referenced by the format specifiers in the detail message.+ */+ @FormatMethod+ public OntologyAlreadyExistsException(@FormatString String message, Object... args) {+ super(message, args);+ }++ /**+ * Constructs an exception with a cause and a formatted detail message.+ *+ * @param cause The cause of the exception.+ * @param message The detail message format.+ * @param args The arguments referenced by the format specifiers in the detail message.+ */+ @FormatMethod+ public OntologyAlreadyExistsException(+ Throwable cause, @FormatString String message, Object... args) {+ super(cause, message, args);+ }+}diff --git a/api/src/main/java/org/apache/gravitino/exceptions/OntologyInUseException.java b/api/src/main/java/org/apache/gravitino/exceptions/OntologyInUseException.javanew file mode 100644index 0000000000..aa7353f799--- /dev/null+++ b/api/src/main/java/org/apache/gravitino/exceptions/OntologyInUseException.java@@ -0,0 +1,36 @@+/*+ * 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.exceptions;++import com.google.errorprone.annotations.FormatMethod;+import com.google.errorprone.annotations.FormatString;++/** Thrown when a live mapping prevents replacing an ontology's definition or dropping it. */+public class OntologyInUseException extends InUseException {+ /**+ * Constructs a new exception with the specified detail message.+ *+ * @param message the detail message.+ * @param args the arguments to the message.+ */+ @FormatMethod+ public OntologyInUseException(@FormatString String message, Object... args) {+ super(message, args);+ }+}diff --git a/api/src/main/java/org/apache/gravitino/ontology/DefinitionFields.java b/api/src/main/java/org/apache/gravitino/ontology/DefinitionFields.javanew file mode 100644index 0000000000..18fbba997a--- /dev/null+++ b/api/src/main/java/org/apache/gravitino/ontology/DefinitionFields.java@@ -0,0 +1,72 @@+/*+ * 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.ontology;++import java.math.BigDecimal;+import java.math.BigInteger;+import java.util.ArrayList;+import java.util.Collections;+import java.util.LinkedHashMap;+import java.util.List;+import java.util.Map;+import javax.annotation.Nullable;++/** Copies JSON-compatible values without retaining mutable caller data. */+final class DefinitionFields {++ private DefinitionFields() {}++ static Map<String, Object> copy(Map<?, ?> source) {+ Map<String, Object> target = new LinkedHashMap<>();+ source.forEach(+ (key, value) -> {+ if (!(key instanceof String)) {+ throw new IllegalArgumentException("Definition keys must be strings");+ }+ target.put((String) key, copyValue(value));+ });+ return Collections.unmodifiableMap(target);+ }++ @Nullable+ private static Object copyValue(@Nullable Object value) {+ if (value instanceof Map) {+ return copy((Map<?, ?>) value);+ }+ if (value instanceof List) {+ List<Object> copy = new ArrayList<>();+ ((List<?>) value).forEach(item -> copy.add(copyValue(item)));+ return Collections.unmodifiableList(copy);+ }+ if (value == null+ || value instanceof String+ || value instanceof Boolean+ || value instanceof Byte+ || value instanceof Short+ || value instanceof Integer+ || value instanceof Long+ || value instanceof BigInteger+ || value instanceof BigDecimal+ || (value instanceof Double && Double.isFinite((Double) value))+ || (value instanceof Float && Float.isFinite((Float) value))) {+ return value;+ }+ throw new IllegalArgumentException("Definition values must be representable as JSON");+ }+}diff --git a/api/src/main/java/org/apache/gravitino/ontology/Ontology.java b/api/src/main/java/org/apache/gravitino/ontology/Ontology.javanew file mode 100644index 0000000000..99da615a70--- /dev/null+++ b/api/src/main/java/org/apache/gravitino/ontology/Ontology.java@@ -0,0 +1,66 @@+/*+ * 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.ontology;++import java.util.Map;+import javax.annotation.Nullable;+import org.apache.gravitino.Auditable;+import org.apache.gravitino.annotation.Evolving;+import org.apache.gravitino.policy.SupportsPolicies;+import org.apache.gravitino.tag.SupportsTags;++/** A metalake-scoped vocabulary of concepts and relationships. */+@Evolving+public interface Ontology extends Auditable {++ /**+ * @return The current ontology name.+ */+ String name();++ /**+ * @return The ontology description, if present.+ */+ @Nullable+ String comment();++ /**+ * @return The current ontology definition.+ */+ OntologyDefinition definition();++ /**+ * @return The ontology's immutable Gravitino properties.+ */+ Map<String, String> properties();++ /**+ * @return Tag operations for this ontology.+ */+ default SupportsTags supportsTags() {+ throw new UnsupportedOperationException("This ontology does not support tags.");+ }++ /**+ * @return Policy operations for this ontology.+ */+ default SupportsPolicies supportsPolicies() {+ throw new UnsupportedOperationException("This ontology does not support policies.");+ }+}diff --git a/api/src/main/java/org/apache/gravitino/ontology/OntologyChange.java b/api/src/main/java/org/apache/gravitino/ontology/OntologyChange.javanew file mode 100644index 0000000000..666687356c--- /dev/null+++ b/api/src/main/java/org/apache/gravitino/ontology/OntologyChange.java@@ -0,0 +1,323 @@+/*+ * 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.ontology;++import com.google.common.base.Preconditions;+import java.util.Objects;+import javax.annotation.Nullable;+import org.apache.commons.lang3.StringUtils;+import org.apache.gravitino.annotation.Evolving;++/** A change that can be applied to an ontology through {@link SupportsOntologies#alterOntology}. */+@Evolving+public interface OntologyChange {++ /**+ * Creates a change that renames an ontology.+ *+ * @param newName The new ontology name.+ * @return The rename change.+ */+ static OntologyChange rename(String newName) {+ return new RenameOntology(newName);+ }++ /**+ * Creates a change that replaces an ontology comment.+ *+ * @param newComment The new comment, or {@code null} to remove the current comment.+ * @return The comment change.+ */+ static OntologyChange updateComment(@Nullable String newComment) {+ return new UpdateComment(newComment);+ }++ /**+ * Creates a change that sets a Gravitino-specific ontology property.+ *+ * <p>If the property already exists, its value is replaced.+ *+ * @param property The property name.+ * @param value The property value.+ * @return The property change.+ */+ static OntologyChange setProperty(String property, String value) {+ return new SetProperty(property, value);+ }++ /**+ * Creates a change that removes a Gravitino-specific ontology property.+ *+ * <p>If the property does not exist, applying the change succeeds without changing the+ * properties.+ *+ * @param property The property name.+ * @return The property removal change.+ */+ static OntologyChange removeProperty(String property) {+ return new RemoveProperty(property);+ }++ /**+ * Creates a change that atomically replaces the complete ontology definition.+ *+ * <p>The ontology name, comment, and Gravitino-specific properties are not affected.+ *+ * @param definition The replacement definition.+ * @return The definition replacement change.+ */+ static OntologyChange replaceDefinition(OntologyDefinition definition) {+ return new ReplaceDefinition(definition);+ }++ /** A change that renames an ontology. */+ final class RenameOntology implements OntologyChange {+ private final String newName;++ private RenameOntology(String newName) {+ Preconditions.checkArgument(+ StringUtils.isNotBlank(newName), "New name must not be null or blank");+ this.newName = newName;+ }++ /**+ * Returns the new ontology name.+ *+ * @return The new name.+ */+ public String getNewName() {+ return newName;+ }++ /** {@inheritDoc} */+ @Override+ public boolean equals(Object obj) {+ if (this == obj) {+ return true;+ }+ if (!(obj instanceof RenameOntology)) {+ return false;+ }+ RenameOntology that = (RenameOntology) obj;+ return newName.equals(that.newName);+ }++ /** {@inheritDoc} */+ @Override+ public int hashCode() {+ return Objects.hash(newName);+ }++ /** {@inheritDoc} */+ @Override+ public String toString() {+ return "RenameOntology{newName='" + newName + "'}";+ }+ }++ /** A change that replaces an ontology comment. */+ final class UpdateComment implements OntologyChange {+ @Nullable private final String newComment;++ private UpdateComment(@Nullable String newComment) {+ this.newComment = newComment;+ }++ /**+ * Returns the new ontology comment.+ *+ * @return The new comment, or {@code null} if the current comment should be removed.+ */+ @Nullable+ public String getNewComment() {+ return newComment;+ }++ /** {@inheritDoc} */+ @Override+ public boolean equals(Object obj) {+ if (this == obj) {+ return true;+ }+ if (!(obj instanceof UpdateComment)) {+ return false;+ }+ UpdateComment that = (UpdateComment) obj;+ return Objects.equals(newComment, that.newComment);+ }++ /** {@inheritDoc} */+ @Override+ public int hashCode() {+ return Objects.hash(newComment);+ }++ /** {@inheritDoc} */+ @Override+ public String toString() {+ return "UpdateComment{newComment='" + newComment + "'}";+ }+ }++ /** A change that sets a Gravitino-specific ontology property. */+ final class SetProperty implements OntologyChange {+ private final String property;+ private final String value;++ private SetProperty(String property, String value) {+ Preconditions.checkArgument(+ StringUtils.isNotBlank(property), "Property name must not be null or blank");+ Preconditions.checkArgument(value != null, "Property value must not be null");+ this.property = property;+ this.value = value;+ }++ /**+ * Returns the property name.+ *+ * @return The property name.+ */+ public String getProperty() {+ return property;+ }++ /**+ * Returns the property value.+ *+ * @return The property value.+ */+ public String getValue() {+ return value;+ }++ /** {@inheritDoc} */+ @Override+ public boolean equals(Object obj) {+ if (this == obj) {+ return true;+ }+ if (!(obj instanceof SetProperty)) {+ return false;+ }+ SetProperty that = (SetProperty) obj;+ return property.equals(that.property) && value.equals(that.value);+ }++ /** {@inheritDoc} */+ @Override+ public int hashCode() {+ return Objects.hash(property, value);+ }++ /** {@inheritDoc} */+ @Override+ public String toString() {+ return "SetProperty{property='" + property + "', value='" + value + "'}";+ }+ }++ /** A change that removes a Gravitino-specific ontology property. */+ final class RemoveProperty implements OntologyChange {+ private final String property;++ private RemoveProperty(String property) {+ Preconditions.checkArgument(+ StringUtils.isNotBlank(property), "Property name must not be null or blank");+ this.property = property;+ }++ /**+ * Returns the property name.+ *+ * @return The property name.+ */+ public String getProperty() {+ return property;+ }++ /** {@inheritDoc} */+ @Override+ public boolean equals(Object obj) {+ if (this == obj) {+ return true;+ }+ if (!(obj instanceof RemoveProperty)) {+ return false;+ }+ RemoveProperty that = (RemoveProperty) obj;+ return property.equals(that.property);+ }++ /** {@inheritDoc} */+ @Override+ public int hashCode() {+ return Objects.hash(property);+ }++ /** {@inheritDoc} */+ @Override+ public String toString() {+ return "RemoveProperty{property='" + property + "'}";+ }+ }++ /** A change that atomically replaces the complete ontology definition. */+ final class ReplaceDefinition implements OntologyChange {+ private final OntologyDefinition definition;++ private ReplaceDefinition(OntologyDefinition definition) {+ Preconditions.checkArgument(definition != null, "Definition must not be null");+ this.definition = definition;+ }++ /**+ * Returns the immutable replacement definition.+ *+ * @return The replacement definition.+ */+ public OntologyDefinition getDefinition() {+ return definition;+ }++ /** {@inheritDoc} */+ @Override+ public boolean equals(Object obj) {+ if (this == obj) {+ return true;+ }+ if (!(obj instanceof ReplaceDefinition)) {+ return false;+ }+ ReplaceDefinition that = (ReplaceDefinition) obj;+ return definition.equals(that.definition);+ }++ /** {@inheritDoc} */+ @Override+ public int hashCode() {+ return Objects.hash(definition);+ }++ /** {@inheritDoc} */+ @Override+ public String toString() {+ return "ReplaceDefinition{definition=" + definition + "}";+ }+ }+}diff --git a/api/src/main/java/org/apache/gravitino/ontology/OntologyDefinition.java b/api/src/main/java/org/apache/gravitino/ontology/OntologyDefinition.javanew file mode 100644index 0000000000..18930de0b8--- /dev/null+++ b/api/src/main/java/org/apache/gravitino/ontology/OntologyDefinition.java@@ -0,0 +1,70 @@+/*+ * 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.ontology;++import java.util.Map;+import org.apache.gravitino.annotation.Evolving;++/**+ * The immutable, JSON-compatible content of an ontology. Field names match the Gravitino REST+ * definition, including {@code ontology}.+ */+@Evolving+public final class OntologyDefinition {++ private final Map<String, Object> fields;++ private OntologyDefinition(Map<String, ?> fields) {+ this.fields = DefinitionFields.copy(fields);+ }++ /**+ * Creates a definition from its native, camel-case fields.+ *+ * @param fields The definition fields.+ * @return The immutable definition.+ */+ public static OntologyDefinition of(Map<String, ?> fields) {+ return new OntologyDefinition(fields);+ }++ /**+ * @return An immutable view of the definition fields.+ */+ public Map<String, Object> fields() {+ return fields;+ }++ /** {@inheritDoc} */+ @Override+ public boolean equals(Object obj) {+ return obj instanceof OntologyDefinition && fields.equals(((OntologyDefinition) obj).fields);+ }++ /** {@inheritDoc} */+ @Override+ public int hashCode() {+ return fields.hashCode();+ }+ /** {@inheritDoc} */+ @Override+ public String toString() {+ return fields.toString();+ }+}diff --git a/api/src/main/java/org/apache/gravitino/ontology/SupportsOntologies.java b/api/src/main/java/org/apache/gravitino/ontology/SupportsOntologies.javanew file mode 100644index 0000000000..f4c8527fc8--- /dev/null+++ b/api/src/main/java/org/apache/gravitino/ontology/SupportsOntologies.java@@ -0,0 +1,104 @@+/*+ * 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.ontology;++import java.util.Map;+import javax.annotation.Nullable;+import org.apache.gravitino.NameIdentifier;+import org.apache.gravitino.annotation.Evolving;+import org.apache.gravitino.exceptions.OntologyInUseException;+import org.apache.gravitino.semantic.OssieDocument;+import org.apache.gravitino.semantic.OssieFormat;++/** Manages ontologies in a metalake. */+@Evolving+public interface SupportsOntologies {+ /**+ * @return The names of the visible ontologies.+ */+ String[] listOntologies();++ /**+ * Loads the current ontology.+ *+ * @param name The ontology name.+ * @return The current ontology.+ */+ Ontology loadOntology(String name);++ /**+ * Creates a new ontology.+ *+ * @param name The new ontology name.+ * @param comment The optional description.+ * @param definition The ontology definition.+ * @param properties The Gravitino properties.+ * @return The created ontology.+ */+ Ontology createOntology(+ String name,+ @Nullable String comment,+ OntologyDefinition definition,+ Map<String, String> properties);++ /**+ * Imports an Ossie document and creates an ontology.+ *+ * @param document The Ossie document.+ * @return The imported ontology.+ */+ Ontology importOntology(OssieDocument document);++ /**+ * Exports the current ontology as an Ossie document.+ *+ * @param name The ontology name.+ * @param format The requested document format.+ * @return The Ossie document.+ */+ OssieDocument exportOntology(String name, OssieFormat format);++ /**+ * Applies changes to an ontology in order and atomically.+ *+ * @param name The ontology name.+ * @param changes The ordered changes.+ * @return The updated ontology.+ * @throws OntologyInUseException If the replacement definition would invalidate a live mapping+ * that references the ontology. None of the changes are applied in this case.+ */+ Ontology alterOntology(String name, OntologyChange... changes);++ /**+ * Drops an ontology.+ *+ * @param name The ontology name.+ * @return {@code true} if the ontology was dropped, or {@code false} if it did not exist.+ * @throws OntologyInUseException If any live mapping references the ontology.+ */+ boolean dropOntology(String name);++ /**+ * Lists the visible mappings of an ontology.+ *+ * @param name The ontology name.+ * @return Mapping identifiers relative to the metalake.+ */+ NameIdentifier[] listOntologyMappings(String name);+}diff --git a/api/src/test/java/org/apache/gravitino/ontology/TestOntologyApi.java b/api/src/test/java/org/apache/gravitino/ontology/TestOntologyApi.javanew file mode 100644index 0000000000..d47d8ffa09--- /dev/null+++ b/api/src/test/java/org/apache/gravitino/ontology/TestOntologyApi.java@@ -0,0 +1,100 @@+/*+ * 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.ontology;++import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertInstanceOf;+import static org.junit.jupiter.api.Assertions.assertNull;+import static org.junit.jupiter.api.Assertions.assertThrows;++import java.util.ArrayList;+import java.util.LinkedHashMap;+import java.util.List;+import java.util.Map;+import org.junit.jupiter.api.Test;++class TestOntologyApi {++ @Test+ void testDefinitionCopiesNestedFields() {+ Map<String, Object> concept = new LinkedHashMap<>(Map.of("concept", "Customer"));+ List<Map<String, Object>> concepts = new ArrayList<>(List.of(concept));+ Map<String, Object> fields = new LinkedHashMap<>(Map.of("ontology", concepts));+ OntologyDefinition definition = OntologyDefinition.of(fields);++ concept.put("concept", "Order");+ concepts.clear();+ fields.clear();++ List<?> storedConcepts = assertInstanceOf(List.class, definition.fields().get("ontology"));+ Map<?, ?> storedConcept = assertInstanceOf(Map.class, storedConcepts.get(0));+ assertEquals("Customer", storedConcept.get("concept"));+ assertThrows(UnsupportedOperationException.class, definition.fields()::clear);+ assertThrows(UnsupportedOperationException.class, storedConcepts::clear);+ assertThrows(UnsupportedOperationException.class, storedConcept::clear);+ }++ @Test+ void testDefinitionRejectsNonJsonFields() {+ assertThrows(+ IllegalArgumentException.class,+ () -> OntologyDefinition.of(Map.of("annotations", Map.of(1, "value"))));+ assertThrows(+ IllegalArgumentException.class,+ () -> OntologyDefinition.of(Map.of("annotations", Double.NaN)));+ assertThrows(+ IllegalArgumentException.class,+ () -> OntologyDefinition.of(Map.of("annotations", new Object())));+ }++ @Test+ void testChangeFactories() {+ OntologyDefinition definition = OntologyDefinition.of(Map.of("ontology", List.of()));+ OntologyChange.RenameOntology rename =+ assertInstanceOf(OntologyChange.RenameOntology.class, OntologyChange.rename("Sales"));+ OntologyChange.UpdateComment comment =+ assertInstanceOf(OntologyChange.UpdateComment.class, OntologyChange.updateComment(null));+ OntologyChange.SetProperty set =+ assertInstanceOf(+ OntologyChange.SetProperty.class, OntologyChange.setProperty("team", "bi"));+ OntologyChange.RemoveProperty remove =+ assertInstanceOf(+ OntologyChange.RemoveProperty.class, OntologyChange.removeProperty("team"));+ OntologyChange.ReplaceDefinition replace =+ assertInstanceOf(+ OntologyChange.ReplaceDefinition.class, OntologyChange.replaceDefinition(definition));++ assertEquals("Sales", rename.getNewName());+ assertNull(comment.getNewComment());+ assertEquals("team", set.getProperty());+ assertEquals("bi", set.getValue());+ assertEquals("team", remove.getProperty());+ assertEquals(definition, replace.getDefinition());+ }++ @Test+ void testRejectInvalidChanges() {+ assertThrows(IllegalArgumentException.class, () -> OntologyChange.rename(""));+ assertThrows(IllegalArgumentException.class, () -> OntologyChange.setProperty("", "value"));+ assertThrows(+ IllegalArgumentException.class, () -> OntologyChange.setProperty("property", null));+ assertThrows(IllegalArgumentException.class, () -> OntologyChange.removeProperty(""));+ assertThrows(IllegalArgumentException.class, () -> OntologyChange.replaceDefinition(null));+ }+}