-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[C++] Support protobuf native schema (#11388)
* Add C++ ProtobufNativeSchema implementation * Add tests for ProtobufNativeSchema * Fix schema type error and add tests for protobuf native schema * Upgrade pulsar-build image's protobuf to 3.17.1 * Update protobuf dependency version in documents * Add missed comments * Fix CentOS 7 build
- Loading branch information
1 parent
5ad4059
commit 992760e
Showing
17 changed files
with
297 additions
and
1,207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* 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. | ||
*/ | ||
#pragma once | ||
|
||
#include <pulsar/Schema.h> | ||
#include <google/protobuf/descriptor.h> | ||
|
||
namespace pulsar { | ||
|
||
/** | ||
* Create a protobuf native schema using a descriptor. | ||
* | ||
* @param descriptor the Descriptor object of the target class | ||
* @return the protobuf native schema | ||
* @throw std::invalid_argument if descriptor is nullptr | ||
*/ | ||
PULSAR_PUBLIC SchemaInfo createProtobufNativeSchema(const google::protobuf::Descriptor* descriptor); | ||
|
||
} // namespace pulsar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* 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. | ||
*/ | ||
#include "pulsar/ProtobufNativeSchema.h" | ||
|
||
#include <stdexcept> | ||
#include <vector> | ||
|
||
#include <boost/archive/iterators/base64_from_binary.hpp> | ||
#include <boost/archive/iterators/transform_width.hpp> | ||
#include <google/protobuf/descriptor.pb.h> | ||
|
||
using google::protobuf::FileDescriptor; | ||
using google::protobuf::FileDescriptorSet; | ||
|
||
namespace pulsar { | ||
|
||
void internalCollectFileDescriptors(const FileDescriptor* fileDescriptor, | ||
FileDescriptorSet& fileDescriptorSet); | ||
|
||
SchemaInfo createProtobufNativeSchema(const google::protobuf::Descriptor* descriptor) { | ||
if (!descriptor) { | ||
throw std::invalid_argument("descriptor is null"); | ||
} | ||
|
||
const auto fileDescriptor = descriptor->file(); | ||
const std::string rootMessageTypeName = descriptor->full_name(); | ||
const std::string rootFileDescriptorName = fileDescriptor->name(); | ||
|
||
FileDescriptorSet fileDescriptorSet; | ||
internalCollectFileDescriptors(fileDescriptor, fileDescriptorSet); | ||
|
||
using namespace boost::archive::iterators; | ||
using base64 = base64_from_binary<transform_width<const char*, 6, 8>>; | ||
|
||
std::vector<char> bytes(fileDescriptorSet.ByteSizeLong()); | ||
fileDescriptorSet.SerializeToArray(bytes.data(), bytes.size()); | ||
|
||
const std::string schemaJson = | ||
R"({"fileDescriptorSet":")" + std::string(base64(bytes.data()), base64(bytes.data() + bytes.size())) + | ||
R"(","rootMessageTypeName":")" + rootMessageTypeName + | ||
R"(","rootFileDescriptorName":")" + rootFileDescriptorName + R"("})"; | ||
|
||
return SchemaInfo(SchemaType::PROTOBUF_NATIVE, "", schemaJson); | ||
} | ||
|
||
void internalCollectFileDescriptors(const FileDescriptor* fileDescriptor, | ||
FileDescriptorSet& fileDescriptorSet) { | ||
fileDescriptor->CopyTo(fileDescriptorSet.add_file()); | ||
for (int i = 0; i < fileDescriptor->dependency_count(); i++) { | ||
// collect the file descriptors recursively | ||
internalCollectFileDescriptors(fileDescriptor->dependency(i), fileDescriptorSet); | ||
} | ||
} | ||
|
||
} // namespace pulsar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.