Search in sources :

Example 1 with ProtobufNativeSchemaData

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;
}
Also used : HashMap(java.util.HashMap) ProtobufNativeSchemaData(org.apache.pulsar.common.protocol.schema.ProtobufNativeSchemaData) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException)

Example 2 with ProtobufNativeSchemaData

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;
}
Also used : HashMap(java.util.HashMap) ProtobufNativeSchemaData(org.apache.pulsar.common.protocol.schema.ProtobufNativeSchemaData) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException)

Example 3 with ProtobufNativeSchemaData

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;
}
Also used : HashMap(java.util.HashMap) FileDescriptorSet(com.google.protobuf.DescriptorProtos.FileDescriptorSet) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) ProtobufNativeSchemaData(org.apache.pulsar.common.protocol.schema.ProtobufNativeSchemaData) Descriptors(com.google.protobuf.Descriptors) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto)

Example 4 with ProtobufNativeSchemaData

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;
}
Also used : HashMap(java.util.HashMap) ProtobufNativeSchemaData(org.apache.pulsar.common.protocol.schema.ProtobufNativeSchemaData) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException)

Example 5 with ProtobufNativeSchemaData

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;
}
Also used : HashMap(java.util.HashMap) FileDescriptorSet(com.google.protobuf.DescriptorProtos.FileDescriptorSet) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) ProtobufNativeSchemaData(org.apache.pulsar.common.protocol.schema.ProtobufNativeSchemaData) Descriptors(com.google.protobuf.Descriptors) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 FileDescriptorProto (com.google.protobuf.DescriptorProtos.FileDescriptorProto)6 HashMap (java.util.HashMap)6 SchemaSerializationException (org.apache.pulsar.client.api.SchemaSerializationException)6 ProtobufNativeSchemaData (org.apache.pulsar.common.protocol.schema.ProtobufNativeSchemaData)6 FileDescriptorSet (com.google.protobuf.DescriptorProtos.FileDescriptorSet)3 Descriptors (com.google.protobuf.Descriptors)3