use of org.apache.pulsar.common.protocol.schema.ProtobufNativeSchemaData in project pulsar by apache.
the class ProtobufNativeSchemaUtils method serialize.
public static byte[] serialize(Descriptors.Descriptor descriptor) {
byte[] schemaDataBytes;
try {
Map<String, FileDescriptorProto> fileDescriptorProtoCache = new HashMap<>();
// recursively cache all FileDescriptorProto
serializeFileDescriptor(descriptor.getFile(), fileDescriptorProtoCache);
// extract root message path
String rootMessageTypeName = descriptor.getFullName();
String rootFileDescriptorName = descriptor.getFile().getFullName();
// build FileDescriptorSet, this is equal to < protoc --include_imports --descriptor_set_out >
byte[] fileDescriptorSet = FileDescriptorSet.newBuilder().addAllFile(fileDescriptorProtoCache.values()).build().toByteArray();
// serialize to bytes
ProtobufNativeSchemaData schemaData = ProtobufNativeSchemaData.builder().fileDescriptorSet(fileDescriptorSet).rootFileDescriptorName(rootFileDescriptorName).rootMessageTypeName(rootMessageTypeName).build();
schemaDataBytes = new ObjectMapper().writeValueAsBytes(schemaData);
logger.debug("descriptor '{}' serialized to '{}'.", descriptor.getFullName(), schemaDataBytes);
} catch (Exception e) {
e.printStackTrace();
throw new SchemaSerializationException(e);
}
return schemaDataBytes;
}
use of org.apache.pulsar.common.protocol.schema.ProtobufNativeSchemaData in project pulsar by yahoo.
the class ProtobufNativeSchemaUtils method serialize.
public static byte[] serialize(Descriptors.Descriptor descriptor) {
byte[] schemaDataBytes;
try {
Map<String, FileDescriptorProto> fileDescriptorProtoCache = new HashMap<>();
// recursively cache all FileDescriptorProto
serializeFileDescriptor(descriptor.getFile(), fileDescriptorProtoCache);
// extract root message path
String rootMessageTypeName = descriptor.getFullName();
String rootFileDescriptorName = descriptor.getFile().getFullName();
// build FileDescriptorSet, this is equal to < protoc --include_imports --descriptor_set_out >
byte[] fileDescriptorSet = FileDescriptorSet.newBuilder().addAllFile(fileDescriptorProtoCache.values()).build().toByteArray();
// serialize to bytes
ProtobufNativeSchemaData schemaData = ProtobufNativeSchemaData.builder().fileDescriptorSet(fileDescriptorSet).rootFileDescriptorName(rootFileDescriptorName).rootMessageTypeName(rootMessageTypeName).build();
schemaDataBytes = new ObjectMapper().writeValueAsBytes(schemaData);
logger.debug("descriptor '{}' serialized to '{}'.", descriptor.getFullName(), schemaDataBytes);
} catch (Exception e) {
e.printStackTrace();
throw new SchemaSerializationException(e);
}
return schemaDataBytes;
}
use of org.apache.pulsar.common.protocol.schema.ProtobufNativeSchemaData in project pulsar by yahoo.
the class ProtobufNativeSchemaUtils method deserialize.
public static Descriptors.Descriptor deserialize(byte[] schemaDataBytes) {
Descriptors.Descriptor descriptor;
try {
ProtobufNativeSchemaData schemaData = new ObjectMapper().readValue(schemaDataBytes, ProtobufNativeSchemaData.class);
Map<String, FileDescriptorProto> fileDescriptorProtoCache = new HashMap<>();
Map<String, Descriptors.FileDescriptor> fileDescriptorCache = new HashMap<>();
FileDescriptorSet fileDescriptorSet = FileDescriptorSet.parseFrom(schemaData.getFileDescriptorSet());
fileDescriptorSet.getFileList().forEach(fileDescriptorProto -> fileDescriptorProtoCache.put(fileDescriptorProto.getName(), fileDescriptorProto));
FileDescriptorProto rootFileDescriptorProto = fileDescriptorProtoCache.get(schemaData.getRootFileDescriptorName());
// recursively build FileDescriptor
deserializeFileDescriptor(rootFileDescriptorProto, fileDescriptorCache, fileDescriptorProtoCache);
// extract root fileDescriptor
Descriptors.FileDescriptor fileDescriptor = fileDescriptorCache.get(schemaData.getRootFileDescriptorName());
// trim package
String[] paths = StringUtils.removeFirst(schemaData.getRootMessageTypeName(), fileDescriptor.getPackage()).replaceFirst("\\.", "").split("\\.");
// extract root message
descriptor = fileDescriptor.findMessageTypeByName(paths[0]);
// extract nested message
for (int i = 1; i < paths.length; i++) {
descriptor = descriptor.findNestedTypeByName(paths[i]);
}
logger.debug("deserialize '{}' to descriptor: '{}'.", schemaDataBytes, descriptor.getFullName());
} catch (Exception e) {
e.printStackTrace();
throw new SchemaSerializationException(e);
}
return descriptor;
}
use of org.apache.pulsar.common.protocol.schema.ProtobufNativeSchemaData in project incubator-pulsar by apache.
the class ProtobufNativeSchemaUtils method serialize.
public static byte[] serialize(Descriptors.Descriptor descriptor) {
byte[] schemaDataBytes;
try {
Map<String, FileDescriptorProto> fileDescriptorProtoCache = new HashMap<>();
// recursively cache all FileDescriptorProto
serializeFileDescriptor(descriptor.getFile(), fileDescriptorProtoCache);
// extract root message path
String rootMessageTypeName = descriptor.getFullName();
String rootFileDescriptorName = descriptor.getFile().getFullName();
// build FileDescriptorSet, this is equal to < protoc --include_imports --descriptor_set_out >
byte[] fileDescriptorSet = FileDescriptorSet.newBuilder().addAllFile(fileDescriptorProtoCache.values()).build().toByteArray();
// serialize to bytes
ProtobufNativeSchemaData schemaData = ProtobufNativeSchemaData.builder().fileDescriptorSet(fileDescriptorSet).rootFileDescriptorName(rootFileDescriptorName).rootMessageTypeName(rootMessageTypeName).build();
schemaDataBytes = new ObjectMapper().writeValueAsBytes(schemaData);
logger.debug("descriptor '{}' serialized to '{}'.", descriptor.getFullName(), schemaDataBytes);
} catch (Exception e) {
e.printStackTrace();
throw new SchemaSerializationException(e);
}
return schemaDataBytes;
}
use of org.apache.pulsar.common.protocol.schema.ProtobufNativeSchemaData in project pulsar by apache.
the class ProtobufNativeSchemaUtils method deserialize.
public static Descriptors.Descriptor deserialize(byte[] schemaDataBytes) {
Descriptors.Descriptor descriptor;
try {
ProtobufNativeSchemaData schemaData = new ObjectMapper().readValue(schemaDataBytes, ProtobufNativeSchemaData.class);
Map<String, FileDescriptorProto> fileDescriptorProtoCache = new HashMap<>();
Map<String, Descriptors.FileDescriptor> fileDescriptorCache = new HashMap<>();
FileDescriptorSet fileDescriptorSet = FileDescriptorSet.parseFrom(schemaData.getFileDescriptorSet());
fileDescriptorSet.getFileList().forEach(fileDescriptorProto -> fileDescriptorProtoCache.put(fileDescriptorProto.getName(), fileDescriptorProto));
FileDescriptorProto rootFileDescriptorProto = fileDescriptorProtoCache.get(schemaData.getRootFileDescriptorName());
// recursively build FileDescriptor
deserializeFileDescriptor(rootFileDescriptorProto, fileDescriptorCache, fileDescriptorProtoCache);
// extract root fileDescriptor
Descriptors.FileDescriptor fileDescriptor = fileDescriptorCache.get(schemaData.getRootFileDescriptorName());
// trim package
String[] paths = StringUtils.removeFirst(schemaData.getRootMessageTypeName(), fileDescriptor.getPackage()).replaceFirst("\\.", "").split("\\.");
// extract root message
descriptor = fileDescriptor.findMessageTypeByName(paths[0]);
// extract nested message
for (int i = 1; i < paths.length; i++) {
descriptor = descriptor.findNestedTypeByName(paths[i]);
}
logger.debug("deserialize '{}' to descriptor: '{}'.", schemaDataBytes, descriptor.getFullName());
} catch (Exception e) {
e.printStackTrace();
throw new SchemaSerializationException(e);
}
return descriptor;
}
Aggregations