Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CORE-3182] Schema Registry json external references #24125

Open
wants to merge 10 commits into
base: dev
Choose a base branch
from
18 changes: 15 additions & 3 deletions src/v/pandaproxy/schema_registry/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,33 @@

#include "pandaproxy/schema_registry/util.h"

#include "pandaproxy/schema_registry/schema_getter.h"

namespace pandaproxy::schema_registry {

ss::future<collected_schema> collect_schema(
schema_getter& store,
collected_schema collected,
ss::sstring name,
canonical_schema schema) {
for (const auto& ref : schema.def().refs()) {
canonical_schema_definition::references refs) {
for (const auto& ref : refs) {
if (!collected.contains(ref.name)) {
auto ss = co_await store.get_subject_schema(
ref.sub, ref.version, include_deleted::no);
collected = co_await collect_schema(
store, std::move(collected), ref.name, std::move(ss.schema));
}
}

co_return std::move(collected);
}

ss::future<collected_schema> collect_schema(
schema_getter& store,
collected_schema collected,
ss::sstring name,
canonical_schema schema) {
collected = co_await collect_schema(
store, std::move(collected), schema.def().refs());
collected.insert(std::move(name), std::move(schema).def());

co_return std::move(collected);
Expand Down
35 changes: 23 additions & 12 deletions src/v/pandaproxy/schema_registry/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
#include "pandaproxy/schema_registry/errors.h"
#include "pandaproxy/schema_registry/schema_getter.h"
#include "pandaproxy/schema_registry/types.h"
#include "utils/absl_sstring_hash.h"

#include <absl/container/flat_hash_set.h>
#include <absl/container/flat_hash_map.h>
#include <fmt/core.h>
#include <fmt/format.h>
#include <rapidjson/error/en.h>
Expand Down Expand Up @@ -62,34 +63,44 @@ ss::sstring to_string(named_type<iobuf, Tag> def) {
}
class collected_schema {
public:
bool contains(const ss::sstring& name) const {
return _names.contains(name);
using map_t = absl::flat_hash_map<
ss::sstring,
canonical_schema_definition::raw_string,
sstring_hash,
sstring_eq>;

bool contains(std::string_view name) const {
return _schemas.contains(name);
}

bool insert(ss::sstring name, canonical_schema_definition def) {
bool inserted = _names.insert(std::move(name)).second;
if (inserted) {
_schemas.push_back(std::move(def).raw());
}
return inserted;
return _schemas.emplace(std::move(name), std::move(def).raw()).second;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think this commit would be easier to review if it was squashed into the previous one

}

canonical_schema_definition::raw_string flatten() && {
iobuf out;
for (auto& s : _schemas) {
for (auto& [_, s] : _schemas) {
out.append(std::move(s));
out.append("\n", 1);
}
return canonical_schema_definition::raw_string{std::move(out)};
}

map_t get() && { return std::exchange(_schemas, {}); }

private:
absl::flat_hash_set<ss::sstring> _names;
std::vector<canonical_schema_definition::raw_string> _schemas;
map_t _schemas;
};

ss::future<collected_schema> collect_schema(
schema_getter& store,
collected_schema collected,
ss::sstring name,
ss::sstring opt_name,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this could stay as name, I think, since it is not renamed in the .cc file

Suggested change
ss::sstring opt_name,
ss::sstring name,

canonical_schema schema);

ss::future<collected_schema> collect_schema(
schema_getter& store,
collected_schema collected,
canonical_schema_definition::references refs);

} // namespace pandaproxy::schema_registry