io.streamnative.pulsar.handlers.kop.schemaregistry.providers.AbstractSchemaProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pulsar-kafka-schema-registry Show documentation
Show all versions of pulsar-kafka-schema-registry Show documentation
Kafka Compatible Schema Registry
The newest version!
/**
* Copyright (c) 2019 - 2024 StreamNative, Inc.. All Rights Reserved.
*/
/**
* 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.streamnative.pulsar.handlers.kop.schemaregistry.providers;
import io.streamnative.pulsar.handlers.kop.schemaregistry.model.Schema;
import io.streamnative.pulsar.handlers.kop.schemaregistry.model.SchemaVersionFetcher;
import io.streamnative.pulsar.handlers.kop.schemaregistry.model.rest.SchemaReference;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public abstract class AbstractSchemaProvider implements SchemaProvider {
private SchemaVersionFetcher schemaVersionFetcher;
@Override
public void configure(Map configs) {
schemaVersionFetcher =
(SchemaVersionFetcher) configs.get(SchemaProvider.SCHEMA_VERSION_FETCHER_CONFIG);
}
public SchemaVersionFetcher schemaVersionFetcher() {
return schemaVersionFetcher;
}
protected Map resolveReferences(List references) {
if (references == null) {
return Collections.emptyMap();
}
Map result = new LinkedHashMap<>();
Set visited = new HashSet<>();
resolveReferences(references, result, visited);
return result;
}
private void resolveReferences(
List references, Map schemas, Set visited) {
for (SchemaReference reference : references) {
if (reference.getName() == null
|| reference.getSubject() == null
|| reference.getVersion() == null) {
throw new IllegalStateException("Invalid reference: " + reference);
}
if (visited.contains(reference.getName())) {
continue;
} else {
visited.add(reference.getName());
}
String subject = reference.getSubject();
if (!schemas.containsKey(reference.getName())) {
// TODO: Use async call here.
Schema schema = schemaVersionFetcher().getByVersion(subject, reference.getVersion(), true).join();
if (schema == null) {
throw new IllegalStateException("No schema reference found for subject \""
+ subject
+ "\" and version "
+ reference.getVersion());
}
if (reference.getVersion() == -1) {
// Update the version with the latest
reference.setVersion(schema.getVersion());
}
resolveReferences(schema.getReferences(), schemas, visited);
schemas.put(reference.getName(), schema.getSchema());
}
}
}
}