mchades commented on code in PR #10982: URL: https://github.com/apache/gravitino/pull/10982#discussion_r3207220236
########## server/src/main/java/org/apache/gravitino/server/web/rest/ViewOperations.java: ########## @@ -0,0 +1,223 @@ +/* + * 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.server.web.rest; + +import static org.apache.gravitino.dto.util.DTOConverters.fromDTOs; + +import com.codahale.metrics.annotation.ResponseMetered; +import com.codahale.metrics.annotation.Timed; +import javax.inject.Inject; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.Namespace; +import org.apache.gravitino.catalog.ViewDispatcher; +import org.apache.gravitino.dto.requests.ViewCreateRequest; +import org.apache.gravitino.dto.requests.ViewUpdateRequest; +import org.apache.gravitino.dto.requests.ViewUpdatesRequest; +import org.apache.gravitino.dto.responses.DropResponse; +import org.apache.gravitino.dto.responses.EntityListResponse; +import org.apache.gravitino.dto.responses.ViewResponse; +import org.apache.gravitino.dto.util.DTOConverters; +import org.apache.gravitino.metrics.MetricNames; +import org.apache.gravitino.rel.View; +import org.apache.gravitino.rel.ViewChange; +import org.apache.gravitino.server.web.Utils; +import org.apache.gravitino.utils.NameIdentifierUtil; +import org.apache.gravitino.utils.NamespaceUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Path("metalakes/{metalake}/catalogs/{catalog}/schemas/{schema}/views") +public class ViewOperations { + + private static final Logger LOG = LoggerFactory.getLogger(ViewOperations.class); + + private final ViewDispatcher dispatcher; + + @Context private HttpServletRequest httpRequest; + + @Inject + public ViewOperations(ViewDispatcher dispatcher) { + this.dispatcher = dispatcher; + } + + @GET + @Produces("application/vnd.gravitino.v1+json") + @Timed(name = "list-view." + MetricNames.HTTP_PROCESS_DURATION, absolute = true) + @ResponseMetered(name = "list-view", absolute = true) + public Response listViews( + @PathParam("metalake") String metalake, + @PathParam("catalog") String catalog, + @PathParam("schema") String schema) { + LOG.info("Received list views request for schema: {}.{}.{}", metalake, catalog, schema); + try { + return Utils.doAs( + httpRequest, + () -> { + Namespace viewNS = NamespaceUtil.ofView(metalake, catalog, schema); + NameIdentifier[] idents = dispatcher.listViews(viewNS); + Response response = Utils.ok(new EntityListResponse(idents)); + LOG.info( + "List {} views under schema: {}.{}.{}", idents.length, metalake, catalog, schema); + return response; + }); + + } catch (Exception e) { + return ExceptionHandlers.handleViewException(OperationType.LIST, "", schema, e); + } + } + + @POST + @Produces("application/vnd.gravitino.v1+json") + @Timed(name = "create-view." + MetricNames.HTTP_PROCESS_DURATION, absolute = true) + @ResponseMetered(name = "create-view", absolute = true) + public Response createView( + @PathParam("metalake") String metalake, + @PathParam("catalog") String catalog, + @PathParam("schema") String schema, + ViewCreateRequest request) { + LOG.info( + "Received create view request: {}.{}.{}.{}", metalake, catalog, schema, request.getName()); + try { + return Utils.doAs( + httpRequest, + () -> { + request.validate(); + NameIdentifier ident = + NameIdentifierUtil.ofView(metalake, catalog, schema, request.getName()); + + View view = + dispatcher.createView( + ident, + request.getComment(), + fromDTOs(request.getColumns()), + DTOConverters.fromDTOs(request.getRepresentations()), + request.getDefaultCatalog(), + request.getDefaultSchema(), + request.getProperties()); + Response response = Utils.ok(new ViewResponse(DTOConverters.toDTO(view))); + LOG.info("View created: {}.{}.{}.{}", metalake, catalog, schema, request.getName()); + return response; + }); + + } catch (Exception e) { + return ExceptionHandlers.handleViewException( + OperationType.CREATE, request.getName(), schema, e); + } + } + + @GET + @Path("{view}") + @Produces("application/vnd.gravitino.v1+json") + @Timed(name = "load-view." + MetricNames.HTTP_PROCESS_DURATION, absolute = true) + @ResponseMetered(name = "load-view", absolute = true) + public Response loadView( + @PathParam("metalake") String metalake, + @PathParam("catalog") String catalog, + @PathParam("schema") String schema, + @PathParam("view") String view) { + LOG.info("Received load view request for view: {}.{}.{}.{}", metalake, catalog, schema, view); + try { + return Utils.doAs( + httpRequest, + () -> { + NameIdentifier ident = NameIdentifierUtil.ofView(metalake, catalog, schema, view); + View v = dispatcher.loadView(ident); + Response response = Utils.ok(new ViewResponse(DTOConverters.toDTO(v))); + LOG.info("View loaded: {}.{}.{}.{}", metalake, catalog, schema, view); + return response; + }); + } catch (Exception e) { + return ExceptionHandlers.handleViewException(OperationType.LOAD, view, schema, e); + } + } + + @PUT Review Comment: Same rationale as above for PUT: we verified current endpoints in this package (`TableOperations#alterTable`, `FilesetOperations#alterFileset`) and they do not declare `@Consumes`. We keep `ViewOperations#alterView` consistent with existing implementations. ########## server/src/main/java/org/apache/gravitino/server/web/rest/ViewOperations.java: ########## @@ -0,0 +1,223 @@ +/* + * 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.server.web.rest; + +import static org.apache.gravitino.dto.util.DTOConverters.fromDTOs; + +import com.codahale.metrics.annotation.ResponseMetered; +import com.codahale.metrics.annotation.Timed; +import javax.inject.Inject; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.Namespace; +import org.apache.gravitino.catalog.ViewDispatcher; +import org.apache.gravitino.dto.requests.ViewCreateRequest; +import org.apache.gravitino.dto.requests.ViewUpdateRequest; +import org.apache.gravitino.dto.requests.ViewUpdatesRequest; +import org.apache.gravitino.dto.responses.DropResponse; +import org.apache.gravitino.dto.responses.EntityListResponse; +import org.apache.gravitino.dto.responses.ViewResponse; +import org.apache.gravitino.dto.util.DTOConverters; +import org.apache.gravitino.metrics.MetricNames; +import org.apache.gravitino.rel.View; +import org.apache.gravitino.rel.ViewChange; +import org.apache.gravitino.server.web.Utils; +import org.apache.gravitino.utils.NameIdentifierUtil; +import org.apache.gravitino.utils.NamespaceUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Path("metalakes/{metalake}/catalogs/{catalog}/schemas/{schema}/views") +public class ViewOperations { + + private static final Logger LOG = LoggerFactory.getLogger(ViewOperations.class); + + private final ViewDispatcher dispatcher; + + @Context private HttpServletRequest httpRequest; + + @Inject + public ViewOperations(ViewDispatcher dispatcher) { + this.dispatcher = dispatcher; + } + + @GET + @Produces("application/vnd.gravitino.v1+json") + @Timed(name = "list-view." + MetricNames.HTTP_PROCESS_DURATION, absolute = true) + @ResponseMetered(name = "list-view", absolute = true) + public Response listViews( + @PathParam("metalake") String metalake, + @PathParam("catalog") String catalog, + @PathParam("schema") String schema) { + LOG.info("Received list views request for schema: {}.{}.{}", metalake, catalog, schema); + try { + return Utils.doAs( + httpRequest, + () -> { + Namespace viewNS = NamespaceUtil.ofView(metalake, catalog, schema); + NameIdentifier[] idents = dispatcher.listViews(viewNS); + Response response = Utils.ok(new EntityListResponse(idents)); + LOG.info( + "List {} views under schema: {}.{}.{}", idents.length, metalake, catalog, schema); + return response; + }); + + } catch (Exception e) { + return ExceptionHandlers.handleViewException(OperationType.LIST, "", schema, e); + } + } + + @POST + @Produces("application/vnd.gravitino.v1+json") Review Comment: Thanks. We checked existing REST resources in this package and aligned with current behavior there: - `TableOperations#createTable` / `alterTable` do not declare `@Consumes` - `FilesetOperations#createFileset` / `alterFileset` do not declare `@Consumes` To stay consistent with those endpoints, we did not keep `@Consumes` only on `ViewOperations`. ########## common/src/main/java/org/apache/gravitino/dto/requests/ViewCreateRequest.java: ########## @@ -0,0 +1,142 @@ +/* + * 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.dto.requests; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.common.base.Preconditions; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import javax.annotation.Nullable; +import lombok.Builder; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.ToString; +import lombok.extern.jackson.Jacksonized; +import org.apache.commons.lang3.StringUtils; +import org.apache.gravitino.dto.rel.ColumnDTO; +import org.apache.gravitino.dto.rel.RepresentationDTO; +import org.apache.gravitino.dto.rel.SQLRepresentationDTO; +import org.apache.gravitino.rest.RESTRequest; + +/** Represents a request to create a view. */ +@Getter +@EqualsAndHashCode +@ToString +@Builder +@Jacksonized +@JsonIgnoreProperties(ignoreUnknown = true) +public class ViewCreateRequest implements RESTRequest { + + @JsonProperty("name") + private final String name; + + @JsonProperty("comment") + @Nullable + private final String comment; + + @JsonProperty("columns") + private final ColumnDTO[] columns; + + @JsonProperty("representations") + private final RepresentationDTO[] representations; + + @JsonProperty("defaultCatalog") + @Nullable + private final String defaultCatalog; + + @JsonProperty("defaultSchema") + @Nullable + private final String defaultSchema; + + @JsonProperty("properties") + @Nullable + private final Map<String, String> properties; + + /** Default constructor for Jackson deserialization. */ + public ViewCreateRequest() { Review Comment: Thanks for the suggestion. We verified the current request DTO pattern in this repo and kept it consistent here: several classes use `@Builder + @Jacksonized` together with explicit constructors, for example: - `common/src/main/java/org/apache/gravitino/dto/requests/TableCreateRequest.java` - `common/src/main/java/org/apache/gravitino/dto/requests/TopicCreateRequest.java` - `common/src/main/java/org/apache/gravitino/dto/requests/RoleCreateRequest.java` So we are keeping the explicit constructors in `ViewCreateRequest` for consistency with existing code style. -- 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]
