jerryshao commented on code in PR #12628:
URL: https://github.com/apache/gravitino/pull/12628#discussion_r4245276840


##########
clients/client-java/src/main/java/org/apache/gravitino/client/SemanticModelCatalogOperations.java:
##########
@@ -0,0 +1,266 @@
+/*
+ * 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.client;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.stream.Collectors;
+import javax.annotation.Nullable;
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.Namespace;
+import org.apache.gravitino.dto.requests.SemanticModelCreateRequest;
+import org.apache.gravitino.dto.requests.SemanticModelUpdateRequest;
+import org.apache.gravitino.dto.requests.SemanticModelUpdatesRequest;
+import org.apache.gravitino.dto.responses.DropResponse;
+import org.apache.gravitino.dto.responses.EntityListResponse;
+import org.apache.gravitino.dto.responses.SemanticModelResponse;
+import org.apache.gravitino.dto.semantic.SemanticModelDefinitionDTO;
+import org.apache.gravitino.exceptions.IllegalSemanticModelException;
+import org.apache.gravitino.exceptions.NoSuchSchemaException;
+import org.apache.gravitino.exceptions.NoSuchSemanticModelException;
+import org.apache.gravitino.exceptions.SemanticModelAlreadyExistsException;
+import org.apache.gravitino.rest.RESTUtils;
+import org.apache.gravitino.semantic.OssieDocument;
+import org.apache.gravitino.semantic.OssieFormat;
+import org.apache.gravitino.semantic.SemanticModel;
+import org.apache.gravitino.semantic.SemanticModelCatalog;
+import org.apache.gravitino.semantic.SemanticModelChange;
+import org.apache.gravitino.semantic.SemanticModelDefinition;
+import org.apache.hc.core5.http.HttpHeaders;
+
+/** Implements schema-scoped Semantic Model operations through the Gravitino 
REST API. */
+class SemanticModelCatalogOperations implements SemanticModelCatalog {
+
+  private static final String OSSIE_JSON_MEDIA_TYPE = "application/json";
+  private static final String OSSIE_YAML_MEDIA_TYPE = "application/yaml";

Review Comment:
   [Nit] These two constants restate the server's own media types 
(`SemanticModelOperations.java:77-79`) with nothing tying them together, so a 
change on either side drifts silently. The server also accepts 
`application/x-yaml` and `text/yaml`, which only this duplication makes 
invisible from the client side. A shared constant in `common` (or `api`, next 
to `OssieFormat`) would keep the two in step.
   
   Verified by: `grep -rn 'OSSIE_YAML_MEDIA_TYPE\s*=' --include=*.java`, which 
returns exactly these two definition sites and the server's 
`application/x-yaml` / `text/yaml` entries.



##########
clients/client-java/src/main/java/org/apache/gravitino/client/SemanticModelCatalogOperations.java:
##########
@@ -0,0 +1,266 @@
+/*
+ * 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.client;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.stream.Collectors;
+import javax.annotation.Nullable;
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.Namespace;
+import org.apache.gravitino.dto.requests.SemanticModelCreateRequest;
+import org.apache.gravitino.dto.requests.SemanticModelUpdateRequest;
+import org.apache.gravitino.dto.requests.SemanticModelUpdatesRequest;
+import org.apache.gravitino.dto.responses.DropResponse;
+import org.apache.gravitino.dto.responses.EntityListResponse;
+import org.apache.gravitino.dto.responses.SemanticModelResponse;
+import org.apache.gravitino.dto.semantic.SemanticModelDefinitionDTO;
+import org.apache.gravitino.exceptions.IllegalSemanticModelException;
+import org.apache.gravitino.exceptions.NoSuchSchemaException;
+import org.apache.gravitino.exceptions.NoSuchSemanticModelException;
+import org.apache.gravitino.exceptions.SemanticModelAlreadyExistsException;
+import org.apache.gravitino.rest.RESTUtils;
+import org.apache.gravitino.semantic.OssieDocument;
+import org.apache.gravitino.semantic.OssieFormat;
+import org.apache.gravitino.semantic.SemanticModel;
+import org.apache.gravitino.semantic.SemanticModelCatalog;
+import org.apache.gravitino.semantic.SemanticModelChange;
+import org.apache.gravitino.semantic.SemanticModelDefinition;
+import org.apache.hc.core5.http.HttpHeaders;
+
+/** Implements schema-scoped Semantic Model operations through the Gravitino 
REST API. */
+class SemanticModelCatalogOperations implements SemanticModelCatalog {
+
+  private static final String OSSIE_JSON_MEDIA_TYPE = "application/json";
+  private static final String OSSIE_YAML_MEDIA_TYPE = "application/yaml";
+
+  private final RESTClient restClient;
+  private final Namespace catalogNamespace;
+  private final String catalogName;
+
+  SemanticModelCatalogOperations(
+      RESTClient restClient, Namespace catalogNamespace, String catalogName) {
+    this.restClient = restClient;
+    this.catalogNamespace = catalogNamespace;
+    this.catalogName = catalogName;
+  }
+
+  /** {@inheritDoc} */
+  @Override
+  public NameIdentifier[] listSemanticModels(Namespace namespace) throws 
NoSuchSchemaException {
+    checkSemanticModelNamespace(namespace);
+
+    Namespace fullNamespace = getSemanticModelFullNamespace(namespace);
+    EntityListResponse response =
+        restClient.get(
+            formatSemanticModelRequestPath(fullNamespace),
+            EntityListResponse.class,
+            Collections.emptyMap(),
+            ErrorHandlers.semanticModelErrorHandler());
+    response.validate();
+
+    return Arrays.stream(response.identifiers())
+        .map(ident -> NameIdentifier.of(ident.namespace().level(2), 
ident.name()))
+        .toArray(NameIdentifier[]::new);
+  }
+
+  /** {@inheritDoc} */
+  @Override
+  public SemanticModel loadSemanticModel(NameIdentifier ident) throws 
NoSuchSemanticModelException {
+    checkSemanticModelNameIdentifier(ident);
+
+    Namespace fullNamespace = getSemanticModelFullNamespace(ident.namespace());
+    SemanticModelResponse response =
+        restClient.get(
+            formatSemanticModelRequestPath(fullNamespace)
+                + "/"
+                + RESTUtils.encodeString(ident.name()),
+            SemanticModelResponse.class,
+            Collections.emptyMap(),
+            ErrorHandlers.semanticModelErrorHandler());
+    response.validate();
+
+    return new GenericSemanticModel(response.getSemanticModel(), restClient, 
fullNamespace);
+  }
+
+  /** {@inheritDoc} */
+  @Override
+  public SemanticModel createSemanticModel(
+      NameIdentifier ident,
+      @Nullable String comment,
+      SemanticModelDefinition definition,
+      Map<String, String> properties)
+      throws NoSuchSchemaException, SemanticModelAlreadyExistsException,
+          IllegalSemanticModelException {
+    checkSemanticModelNameIdentifier(ident);
+    Preconditions.checkArgument(definition != null, "Semantic Model definition 
must not be null");
+
+    Namespace fullNamespace = getSemanticModelFullNamespace(ident.namespace());
+    SemanticModelCreateRequest request =
+        new SemanticModelCreateRequest(
+            ident.name(),
+            comment,
+            SemanticModelDefinitionDTO.fromDefinition(definition),
+            properties);
+    request.validate();
+
+    SemanticModelResponse response =
+        restClient.post(
+            formatSemanticModelRequestPath(fullNamespace),
+            request,
+            SemanticModelResponse.class,
+            Collections.emptyMap(),
+            ErrorHandlers.semanticModelErrorHandler());
+    response.validate();
+
+    return new GenericSemanticModel(response.getSemanticModel(), restClient, 
fullNamespace);
+  }
+
+  /** {@inheritDoc} */
+  @Override
+  public SemanticModel importOssieDocument(Namespace namespace, OssieDocument 
document)
+      throws NoSuchSchemaException, SemanticModelAlreadyExistsException,
+          IllegalSemanticModelException {
+    checkSemanticModelNamespace(namespace);
+    Preconditions.checkArgument(document != null, "Ossie document must not be 
null");
+
+    Namespace fullNamespace = getSemanticModelFullNamespace(namespace);
+    SemanticModelResponse response =
+        restClient.postRaw(
+            formatSemanticModelRequestPath(fullNamespace) + "/ossie",
+            document.content(),
+            ossieMediaType(document.format()),
+            SemanticModelResponse.class,
+            Collections.emptyMap(),
+            ErrorHandlers.semanticModelErrorHandler());
+    response.validate();
+
+    return new GenericSemanticModel(response.getSemanticModel(), restClient, 
fullNamespace);
+  }
+
+  /** {@inheritDoc} */
+  @Override
+  public OssieDocument exportOssieDocument(NameIdentifier ident, OssieFormat 
format)
+      throws NoSuchSemanticModelException, IllegalSemanticModelException {
+    checkSemanticModelNameIdentifier(ident);
+    Preconditions.checkArgument(format != null, "Ossie format must not be 
null");
+
+    Namespace fullNamespace = getSemanticModelFullNamespace(ident.namespace());
+    String content =
+        restClient.getRaw(
+            formatSemanticModelRequestPath(fullNamespace)
+                + "/"
+                + RESTUtils.encodeString(ident.name())
+                + "/ossie",
+            Collections.singletonMap("format", 
format.name().toLowerCase(Locale.ROOT)),
+            Collections.singletonMap(HttpHeaders.ACCEPT, 
ossieMediaType(format)),
+            ErrorHandlers.semanticModelErrorHandler());
+
+    return format == OssieFormat.JSON ? OssieDocument.json(content) : 
OssieDocument.yaml(content);
+  }
+
+  /** {@inheritDoc} */
+  @Override
+  public SemanticModel alterSemanticModel(NameIdentifier ident, 
SemanticModelChange... changes)
+      throws NoSuchSemanticModelException, SemanticModelAlreadyExistsException,
+          IllegalSemanticModelException {
+    checkSemanticModelNameIdentifier(ident);
+    Preconditions.checkArgument(changes != null, "Semantic Model changes must 
not be null");
+
+    List<SemanticModelUpdateRequest> updates =
+        Arrays.stream(changes)
+            .map(DTOConverters::toSemanticModelUpdateRequest)
+            .collect(Collectors.toList());
+    SemanticModelUpdatesRequest request = new 
SemanticModelUpdatesRequest(updates);
+    request.validate();
+
+    Namespace fullNamespace = getSemanticModelFullNamespace(ident.namespace());
+    SemanticModelResponse response =
+        restClient.put(
+            formatSemanticModelRequestPath(fullNamespace)
+                + "/"
+                + RESTUtils.encodeString(ident.name()),
+            request,
+            SemanticModelResponse.class,
+            Collections.emptyMap(),
+            ErrorHandlers.semanticModelErrorHandler());
+    response.validate();
+
+    return new GenericSemanticModel(response.getSemanticModel(), restClient, 
fullNamespace);
+  }
+
+  /** {@inheritDoc} */
+  @Override
+  public boolean dropSemanticModel(NameIdentifier ident) {
+    checkSemanticModelNameIdentifier(ident);
+
+    Namespace fullNamespace = getSemanticModelFullNamespace(ident.namespace());
+    DropResponse response =
+        restClient.delete(
+            formatSemanticModelRequestPath(fullNamespace)
+                + "/"
+                + RESTUtils.encodeString(ident.name()),
+            DropResponse.class,
+            Collections.emptyMap(),
+            ErrorHandlers.semanticModelErrorHandler());
+    response.validate();
+
+    return response.dropped();
+  }
+
+  @VisibleForTesting
+  static String formatSemanticModelRequestPath(Namespace namespace) {
+    Namespace schemaNamespace = Namespace.of(namespace.level(0), 
namespace.level(1));
+    return new StringBuilder()
+        .append(BaseSchemaCatalog.formatSchemaRequestPath(schemaNamespace))
+        .append("/")
+        .append(RESTUtils.encodeString(namespace.level(2)))
+        .append("/semantic-models")
+        .toString();
+  }
+
+  @VisibleForTesting
+  static void checkSemanticModelNamespace(Namespace namespace) {
+    Namespace.check(
+        namespace != null && namespace.length() == 1,
+        "Semantic Model namespace must be non-null and have 1 level, the input 
namespace is %s",
+        namespace);
+  }
+
+  @VisibleForTesting
+  static void checkSemanticModelNameIdentifier(NameIdentifier ident) {
+    NameIdentifier.check(ident != null, "NameIdentifier must not be null");
+    NameIdentifier.check(
+        ident.name() != null && !ident.name().isEmpty(), "NameIdentifier name 
must not be empty");
+    checkSemanticModelNamespace(ident.namespace());
+  }
+
+  private Namespace getSemanticModelFullNamespace(Namespace 
semanticModelNamespace) {
+    return Namespace.of(catalogNamespace.level(0), catalogName, 
semanticModelNamespace.level(0));
+  }
+
+  private static String ossieMediaType(OssieFormat format) {
+    return format == OssieFormat.JSON ? OSSIE_JSON_MEDIA_TYPE : 
OSSIE_YAML_MEDIA_TYPE;
+  }

Review Comment:
   [Nit] `ossieMediaType` collapses the `@Evolving` `OssieFormat` enum into a 
two-way branch, and `exportOssieDocument` (line 180) does the same in reverse: 
it labels the returned document by the *requested* format, never by the 
response. Add a third enum value and the client silently sends `Accept: 
application/yaml` with `format=<new>`, then tags whatever comes back as YAML. 
The server is explicit here by contrast - `parseOssieFormat` rejects anything 
it does not know (SemanticModelOperations.java:496-505, surfaced as 
"Unsupported Ossie format"). A `switch` with a `default` that throws in both 
places would fail loudly instead.
   
   Verified by: reading `OssieFormat` 
(api/src/main/java/org/apache/gravitino/semantic/OssieFormat.java:25-31, two 
values, annotated `@Evolving`) against lines 180 and 263-265 here, and 
comparing with the server's `parseOssieFormat`.



##########
clients/client-java/src/main/java/org/apache/gravitino/client/HTTPClient.java:
##########
@@ -436,6 +445,10 @@ private <T> T execute(
             response.getCode());
       }
 
+      if (responseType == String.class) {
+        return responseType.cast(responseBody);
+      }

Review Comment:
   [Question] `getRaw` reaches this branch only after the no-body short-circuit 
above (HTTPClient.java:426-429), which returns `null` for any 204 regardless of 
`responseType`. `SemanticModelCatalogOperations.exportOssieDocument` feeds the 
result straight into `OssieDocument.yaml(...)`/`json(...)` (lines 170-180), 
whose `Preconditions` rejects null or blank content - so a 204, or an empty 200 
body from a proxy, surfaces as `IllegalArgumentException: content must not be 
blank` rather than a REST-level error, and it is not among the exceptions 
`exportOssieDocument` declares. Today's server always writes a body 
(`Response.ok(document.content(), mediaType)`, 
SemanticModelOperations.java:470-474), so this is not reachable through it - is 
it worth having `getRaw` reject a null body with a `RESTException` so raw 
callers get the same failure mode as the typed ones?
   
   Verified by: reading the 204 short-circuit at HTTPClient.java:426-429 and 
`OssieDocument`'s private constructor 
(api/src/main/java/org/apache/gravitino/semantic/OssieDocument.java:36-40).



-- 
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]

Reply via email to