io.cdap.cdap.internal.app.SchemaFinder Maven / Gradle / Ivy
The newest version!
/*
* Copyright © 2014 Cask Data, Inc.
*
* Licensed 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 io.cdap.cdap.internal.app;
import io.cdap.cdap.api.data.schema.Schema;
import io.cdap.cdap.common.utils.ImmutablePair;
import java.util.Set;
import javax.annotation.Nullable;
/**
* This class consolidates the functionalities related to find schemas that are compatible for a
* connection.
*/
public final class SchemaFinder {
/**
* Given two schema's checks if there exists compatibility or equality.
*
* @param output Set of output {@link Schema}.
* @param input Set of input {@link Schema}.
* @return true if and only if they are equal or compatible with constraints
*/
public static boolean checkSchema(Set output, Set input) {
return findSchema(output, input) != null;
}
/**
* Finds the right schema to be used for the connections.
*
* A connection should have the following:
*
* - Equal overrides compatible : So if there is equal, we use that
* - In case of compatible, we try to find one schema and only one. More than one is a error.
*
*
*
* @param output Set of output {@link Schema}.
* @param input Set of input {@link Schema}.
* @return An {@link ImmutablePair} with first as output schema and second as input schema.
*/
@Nullable
public static ImmutablePair findSchema(Set output, Set input) {
ImmutablePair compatibleSchema = null;
for (Schema outputSchema : output) {
for (Schema inputSchema : input) {
if (outputSchema.equals(inputSchema)) {
return new ImmutablePair<>(inputSchema, outputSchema);
}
if (outputSchema.isCompatible(inputSchema)) {
// If there are more than one compatible, then it's a problem
// we should have only strictly one.
if (compatibleSchema != null) {
return null;
}
compatibleSchema = new ImmutablePair<>(outputSchema, inputSchema);
}
}
}
return compatibleSchema;
}
private SchemaFinder() {
}
}