|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.gravitino.catalog; |
| 20 | + |
| 21 | +import static org.apache.gravitino.catalog.CapabilityHelpers.applyCapabilities; |
| 22 | +import static org.apache.gravitino.catalog.CapabilityHelpers.applyCaseSensitive; |
| 23 | +import static org.apache.gravitino.catalog.CapabilityHelpers.getCapability; |
| 24 | + |
| 25 | +import java.util.Map; |
| 26 | +import org.apache.commons.lang3.ArrayUtils; |
| 27 | +import org.apache.gravitino.NameIdentifier; |
| 28 | +import org.apache.gravitino.Namespace; |
| 29 | +import org.apache.gravitino.connector.capability.Capability; |
| 30 | +import org.apache.gravitino.exceptions.ModelAlreadyExistsException; |
| 31 | +import org.apache.gravitino.exceptions.ModelVersionAliasesAlreadyExistException; |
| 32 | +import org.apache.gravitino.exceptions.NoSuchModelException; |
| 33 | +import org.apache.gravitino.exceptions.NoSuchModelVersionException; |
| 34 | +import org.apache.gravitino.exceptions.NoSuchSchemaException; |
| 35 | +import org.apache.gravitino.model.Model; |
| 36 | +import org.apache.gravitino.model.ModelVersion; |
| 37 | + |
| 38 | +public class ModelNormalizeDispatcher implements ModelDispatcher { |
| 39 | + private final CatalogManager catalogManager; |
| 40 | + private final ModelDispatcher dispatcher; |
| 41 | + |
| 42 | + public ModelNormalizeDispatcher(ModelDispatcher dispatcher, CatalogManager catalogManager) { |
| 43 | + this.dispatcher = dispatcher; |
| 44 | + this.catalogManager = catalogManager; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public NameIdentifier[] listModels(Namespace namespace) throws NoSuchSchemaException { |
| 49 | + // The constraints of the name spec may be more strict than underlying catalog, |
| 50 | + // and for compatibility reasons, we only apply case-sensitive capabilities here. |
| 51 | + Namespace caseSensitiveNs = normalizeCaseSensitive(namespace); |
| 52 | + NameIdentifier[] identifiers = dispatcher.listModels(caseSensitiveNs); |
| 53 | + return normalizeCaseSensitive(identifiers); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public Model getModel(NameIdentifier ident) throws NoSuchModelException { |
| 58 | + // The constraints of the name spec may be more strict than underlying catalog, |
| 59 | + // and for compatibility reasons, we only apply case-sensitive capabilities here. |
| 60 | + return dispatcher.getModel(normalizeCaseSensitive(ident)); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public boolean modelExists(NameIdentifier ident) { |
| 65 | + // The constraints of the name spec may be more strict than underlying catalog, |
| 66 | + // and for compatibility reasons, we only apply case-sensitive capabilities here. |
| 67 | + return dispatcher.modelExists(normalizeCaseSensitive(ident)); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public Model registerModel(NameIdentifier ident, String comment, Map<String, String> properties) |
| 72 | + throws ModelAlreadyExistsException { |
| 73 | + return dispatcher.registerModel(normalizeNameIdentifier(ident), comment, properties); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public Model registerModel( |
| 78 | + NameIdentifier ident, |
| 79 | + String uri, |
| 80 | + String[] aliases, |
| 81 | + String comment, |
| 82 | + Map<String, String> properties) |
| 83 | + throws ModelAlreadyExistsException, ModelVersionAliasesAlreadyExistException { |
| 84 | + return dispatcher.registerModel( |
| 85 | + normalizeNameIdentifier(ident), uri, aliases, comment, properties); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public boolean deleteModel(NameIdentifier ident) { |
| 90 | + // The constraints of the name spec may be more strict than underlying catalog, |
| 91 | + // and for compatibility reasons, we only apply case-sensitive capabilities here. |
| 92 | + return dispatcher.deleteModel(normalizeCaseSensitive(ident)); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public int[] listModelVersions(NameIdentifier ident) throws NoSuchModelException { |
| 97 | + return dispatcher.listModelVersions(normalizeCaseSensitive(ident)); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public ModelVersion getModelVersion(NameIdentifier ident, int version) |
| 102 | + throws NoSuchModelVersionException { |
| 103 | + return dispatcher.getModelVersion(normalizeCaseSensitive(ident), version); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public ModelVersion getModelVersion(NameIdentifier ident, String alias) |
| 108 | + throws NoSuchModelVersionException { |
| 109 | + return dispatcher.getModelVersion(normalizeCaseSensitive(ident), alias); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public boolean modelVersionExists(NameIdentifier ident, int version) { |
| 114 | + return dispatcher.modelVersionExists(normalizeCaseSensitive(ident), version); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public boolean modelVersionExists(NameIdentifier ident, String alias) { |
| 119 | + return dispatcher.modelVersionExists(normalizeCaseSensitive(ident), alias); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public ModelVersion linkModelVersion( |
| 124 | + NameIdentifier ident, |
| 125 | + String uri, |
| 126 | + String[] aliases, |
| 127 | + String comment, |
| 128 | + Map<String, String> properties) |
| 129 | + throws NoSuchModelException, ModelVersionAliasesAlreadyExistException { |
| 130 | + return dispatcher.linkModelVersion( |
| 131 | + normalizeCaseSensitive(ident), uri, aliases, comment, properties); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public boolean deleteModelVersion(NameIdentifier ident, int version) { |
| 136 | + return dispatcher.deleteModelVersion(normalizeCaseSensitive(ident), version); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public boolean deleteModelVersion(NameIdentifier ident, String alias) { |
| 141 | + return dispatcher.deleteModelVersion(normalizeCaseSensitive(ident), alias); |
| 142 | + } |
| 143 | + |
| 144 | + private Namespace normalizeCaseSensitive(Namespace namespace) { |
| 145 | + Capability capabilities = getCapability(NameIdentifier.of(namespace.levels()), catalogManager); |
| 146 | + return applyCaseSensitive(namespace, Capability.Scope.MODEL, capabilities); |
| 147 | + } |
| 148 | + |
| 149 | + private NameIdentifier normalizeCaseSensitive(NameIdentifier ident) { |
| 150 | + Capability capabilities = getCapability(ident, catalogManager); |
| 151 | + return applyCaseSensitive(ident, Capability.Scope.MODEL, capabilities); |
| 152 | + } |
| 153 | + |
| 154 | + private NameIdentifier[] normalizeCaseSensitive(NameIdentifier[] idents) { |
| 155 | + if (ArrayUtils.isEmpty(idents)) { |
| 156 | + return idents; |
| 157 | + } |
| 158 | + |
| 159 | + Capability capabilities = getCapability(idents[0], catalogManager); |
| 160 | + return applyCaseSensitive(idents, Capability.Scope.MODEL, capabilities); |
| 161 | + } |
| 162 | + |
| 163 | + private NameIdentifier normalizeNameIdentifier(NameIdentifier ident) { |
| 164 | + Capability capability = getCapability(ident, catalogManager); |
| 165 | + return applyCapabilities(ident, Capability.Scope.MODEL, capability); |
| 166 | + } |
| 167 | +} |
0 commit comments