Skip to content

Commit

Permalink
Add model management Java API
Browse files Browse the repository at this point in the history
  • Loading branch information
jerryshao committed Dec 26, 2024
1 parent 061f24b commit 4c36e5b
Show file tree
Hide file tree
Showing 7 changed files with 1,136 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@ static Catalog toCatalog(String metalake, CatalogDTO catalog, RESTClient client)
.withAudit((AuditDTO) catalog.auditInfo())
.withRestClient(client)
.build();
case MODEL:
return GenericModelCatalog.builder()
.withNamespace(namespace)
.withName(catalog.name())
.withType(catalog.type())
.withProvider(catalog.provider())
.withComment(catalog.comment())
.withProperties(catalog.properties())
.withAudit((AuditDTO) catalog.auditInfo())
.withRestClient(client)
.build();

default:
throw new UnsupportedOperationException("Unsupported catalog type: " + catalog.type());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,16 @@
import org.apache.gravitino.exceptions.MetalakeAlreadyExistsException;
import org.apache.gravitino.exceptions.MetalakeInUseException;
import org.apache.gravitino.exceptions.MetalakeNotInUseException;
import org.apache.gravitino.exceptions.ModelAlreadyExistsException;
import org.apache.gravitino.exceptions.ModelVersionAliasesAlreadyExistException;
import org.apache.gravitino.exceptions.NoSuchCatalogException;
import org.apache.gravitino.exceptions.NoSuchCredentialException;
import org.apache.gravitino.exceptions.NoSuchFilesetException;
import org.apache.gravitino.exceptions.NoSuchGroupException;
import org.apache.gravitino.exceptions.NoSuchMetadataObjectException;
import org.apache.gravitino.exceptions.NoSuchMetalakeException;
import org.apache.gravitino.exceptions.NoSuchModelException;
import org.apache.gravitino.exceptions.NoSuchModelVersionException;
import org.apache.gravitino.exceptions.NoSuchPartitionException;
import org.apache.gravitino.exceptions.NoSuchRoleException;
import org.apache.gravitino.exceptions.NoSuchSchemaException;
Expand Down Expand Up @@ -220,6 +224,15 @@ public static Consumer<ErrorResponse> ownerErrorHandler() {
return OwnerErrorHandler.INSTANCE;
}

/**
* Creates an error handler specific to Model operations.
*
* @return A Consumer representing the Model error handler.
*/
public static Consumer<ErrorResponse> modelErrorHandler() {
return ModelErrorHandler.INSTANCE;
}

private ErrorHandlers() {}

/**
Expand Down Expand Up @@ -987,6 +1000,69 @@ public void accept(ErrorResponse errorResponse) {
}
}

/** Error handler specific to Model operations. */
@SuppressWarnings("FormatStringAnnotation")
private static class ModelErrorHandler extends RestErrorHandler {

private static final ModelErrorHandler INSTANCE = new ModelErrorHandler();

@Override
public void accept(ErrorResponse errorResponse) {
String errorMsg = formatErrorMessage(errorResponse);

switch (errorResponse.getCode()) {
case ErrorConstants.ILLEGAL_ARGUMENTS_CODE:
throw new IllegalArgumentException(errorMsg);

case ErrorConstants.NOT_FOUND_CODE:
if (errorResponse.getType().equals(NoSuchSchemaException.class.getSimpleName())) {
throw new NoSuchSchemaException(errorMsg);
} else if (errorResponse.getType().equals(NoSuchModelException.class.getSimpleName())) {
throw new NoSuchModelException(errorMsg);
} else if (errorResponse
.getType()
.equals(NoSuchModelVersionException.class.getSimpleName())) {
throw new NoSuchModelVersionException(errorMsg);
} else {
throw new NotFoundException(errorMsg);
}

case ErrorConstants.ALREADY_EXISTS_CODE:
if (errorResponse.getType().equals(ModelAlreadyExistsException.class.getSimpleName())) {
throw new ModelAlreadyExistsException(errorMsg);
} else if (errorResponse
.getType()
.equals(ModelVersionAliasesAlreadyExistException.class.getSimpleName())) {
throw new ModelVersionAliasesAlreadyExistException(errorMsg);
} else {
throw new AlreadyExistsException(errorMsg);
}

case ErrorConstants.FORBIDDEN_CODE:
throw new ForbiddenException(errorMsg);

case ErrorConstants.INTERNAL_ERROR_CODE:
throw new RuntimeException(errorMsg);

case ErrorConstants.NOT_IN_USE_CODE:
if (errorResponse.getType().equals(CatalogNotInUseException.class.getSimpleName())) {
throw new CatalogNotInUseException(errorMsg);

} else if (errorResponse
.getType()
.equals(MetalakeNotInUseException.class.getSimpleName())) {
throw new MetalakeNotInUseException(errorMsg);

} else {
throw new NotInUseException(errorMsg);
}

default:
super.accept(errorResponse);
}
}
}

/** Generic error handler for REST requests. */
private static class RestErrorHandler extends ErrorHandler {
private static final ErrorHandler INSTANCE = new RestErrorHandler();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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 java.util.Map;
import org.apache.gravitino.Audit;
import org.apache.gravitino.authorization.SupportsRoles;
import org.apache.gravitino.dto.model.ModelDTO;
import org.apache.gravitino.model.Model;
import org.apache.gravitino.tag.SupportsTags;

/** Represents a generic model. */
class GenericModel implements Model {

private final ModelDTO modelDTO;

GenericModel(ModelDTO modelDTO) {
this.modelDTO = modelDTO;
}

@Override
public Audit auditInfo() {
return modelDTO.auditInfo();
}

@Override
public String name() {
return modelDTO.name();
}

@Override
public String comment() {
return modelDTO.comment();
}

@Override
public Map<String, String> properties() {
return modelDTO.properties();
}

@Override
public int latestVersion() {
return modelDTO.latestVersion();
}

@Override
public SupportsTags supportsTags() {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public SupportsRoles supportsRoles() {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof GenericModel)) {
return false;
}

GenericModel that = (GenericModel) o;
return modelDTO.equals(that.modelDTO);
}

@Override
public int hashCode() {
return modelDTO.hashCode();
}
}
Loading

0 comments on commit 4c36e5b

Please sign in to comment.