Search in sources :

Example 1 with FieldDescriptor

use of org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FieldDescriptor in project hbase by apache.

the class GeneratedMessage method getAllFieldsMutable.

/**
   * Internal helper to return a modifiable map containing all the fields.
   * The returned Map is modifialbe so that the caller can add additional
   * extension fields to implement {@link #getAllFields()}.
   *
   * @param getBytesForString whether to generate ByteString for string fields
   */
private Map<FieldDescriptor, Object> getAllFieldsMutable(boolean getBytesForString) {
    final TreeMap<FieldDescriptor, Object> result = new TreeMap<FieldDescriptor, Object>();
    final Descriptor descriptor = internalGetFieldAccessorTable().descriptor;
    final List<FieldDescriptor> fields = descriptor.getFields();
    for (int i = 0; i < fields.size(); i++) {
        FieldDescriptor field = fields.get(i);
        final OneofDescriptor oneofDescriptor = field.getContainingOneof();
        /*
       * If the field is part of a Oneof, then at maximum one field in the Oneof is set
       * and it is not repeated. There is no need to iterate through the others.
       */
        if (oneofDescriptor != null) {
            // Skip other fields in the Oneof we know are not set
            i += oneofDescriptor.getFieldCount() - 1;
            if (!hasOneof(oneofDescriptor)) {
                // If no field is set in the Oneof, skip all the fields in the Oneof
                continue;
            }
            // Get the pointer to the only field which is set in the Oneof
            field = getOneofFieldDescriptor(oneofDescriptor);
        } else {
            // If we are not in a Oneof, we need to check if the field is set and if it is repeated
            if (field.isRepeated()) {
                final List<?> value = (List<?>) getField(field);
                if (!value.isEmpty()) {
                    result.put(field, value);
                }
                continue;
            }
            if (!hasField(field)) {
                continue;
            }
        }
        // Add the field to the map
        if (getBytesForString && field.getJavaType() == FieldDescriptor.JavaType.STRING) {
            result.put(field, getFieldRaw(field));
        } else {
            result.put(field, getField(field));
        }
    }
    return result;
}
Also used : OneofDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.OneofDescriptor) Descriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.Descriptor) FileDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FileDescriptor) EnumValueDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.EnumValueDescriptor) EnumDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.EnumDescriptor) FieldDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FieldDescriptor) ArrayList(java.util.ArrayList) List(java.util.List) TreeMap(java.util.TreeMap) OneofDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.OneofDescriptor) FieldDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FieldDescriptor)

Example 2 with FieldDescriptor

use of org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FieldDescriptor in project hbase by apache.

the class MessageReflection method getSerializedSize.

static int getSerializedSize(Message message, Map<FieldDescriptor, Object> fields) {
    int size = 0;
    final boolean isMessageSet = message.getDescriptorForType().getOptions().getMessageSetWireFormat();
    for (final Map.Entry<Descriptors.FieldDescriptor, Object> entry : fields.entrySet()) {
        final Descriptors.FieldDescriptor field = entry.getKey();
        final Object value = entry.getValue();
        if (isMessageSet && field.isExtension() && field.getType() == Descriptors.FieldDescriptor.Type.MESSAGE && !field.isRepeated()) {
            size += CodedOutputStream.computeMessageSetExtensionSize(field.getNumber(), (Message) value);
        } else {
            size += FieldSet.computeFieldSize(field, value);
        }
    }
    final UnknownFieldSet unknownFields = message.getUnknownFields();
    if (isMessageSet) {
        size += unknownFields.getSerializedSizeAsMessageSet();
    } else {
        size += unknownFields.getSerializedSize();
    }
    return size;
}
Also used : FieldDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FieldDescriptor) TreeMap(java.util.TreeMap) Map(java.util.Map) FieldDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FieldDescriptor)

Example 3 with FieldDescriptor

use of org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FieldDescriptor in project hbase by apache.

the class AbstractMessage method compareFields.

/**
   * Compares two set of fields.
   * This method is used to implement {@link AbstractMessage#equals(Object)}
   * and {@link AbstractMutableMessage#equals(Object)}. It takes special care
   * of bytes fields because immutable messages and mutable messages use
   * different Java type to reprensent a bytes field and this method should be
   * able to compare immutable messages, mutable messages and also an immutable
   * message to a mutable message.
   */
static boolean compareFields(Map<FieldDescriptor, Object> a, Map<FieldDescriptor, Object> b) {
    if (a.size() != b.size()) {
        return false;
    }
    for (FieldDescriptor descriptor : a.keySet()) {
        if (!b.containsKey(descriptor)) {
            return false;
        }
        Object value1 = a.get(descriptor);
        Object value2 = b.get(descriptor);
        if (descriptor.getType() == FieldDescriptor.Type.BYTES) {
            if (descriptor.isRepeated()) {
                List list1 = (List) value1;
                List list2 = (List) value2;
                if (list1.size() != list2.size()) {
                    return false;
                }
                for (int i = 0; i < list1.size(); i++) {
                    if (!compareBytes(list1.get(i), list2.get(i))) {
                        return false;
                    }
                }
            } else {
                // Compares a singular bytes field.
                if (!compareBytes(value1, value2)) {
                    return false;
                }
            }
        } else if (descriptor.isMapField()) {
            if (!compareMapField(value1, value2)) {
                return false;
            }
        } else {
            // Compare non-bytes fields.
            if (!value1.equals(value2)) {
                return false;
            }
        }
    }
    return true;
}
Also used : List(java.util.List) FieldDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FieldDescriptor)

Example 4 with FieldDescriptor

use of org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FieldDescriptor in project hbase by apache.

the class GeneratedMessageV3 method getAllFieldsMutable.

/**
   * Internal helper to return a modifiable map containing all the fields.
   * The returned Map is modifialbe so that the caller can add additional
   * extension fields to implement {@link #getAllFields()}.
   *
   * @param getBytesForString whether to generate ByteString for string fields
   */
private Map<FieldDescriptor, Object> getAllFieldsMutable(boolean getBytesForString) {
    final TreeMap<FieldDescriptor, Object> result = new TreeMap<FieldDescriptor, Object>();
    final Descriptor descriptor = internalGetFieldAccessorTable().descriptor;
    final List<FieldDescriptor> fields = descriptor.getFields();
    for (int i = 0; i < fields.size(); i++) {
        FieldDescriptor field = fields.get(i);
        final OneofDescriptor oneofDescriptor = field.getContainingOneof();
        /*
       * If the field is part of a Oneof, then at maximum one field in the Oneof is set
       * and it is not repeated. There is no need to iterate through the others.
       */
        if (oneofDescriptor != null) {
            // Skip other fields in the Oneof we know are not set
            i += oneofDescriptor.getFieldCount() - 1;
            if (!hasOneof(oneofDescriptor)) {
                // If no field is set in the Oneof, skip all the fields in the Oneof
                continue;
            }
            // Get the pointer to the only field which is set in the Oneof
            field = getOneofFieldDescriptor(oneofDescriptor);
        } else {
            // If we are not in a Oneof, we need to check if the field is set and if it is repeated
            if (field.isRepeated()) {
                final List<?> value = (List<?>) getField(field);
                if (!value.isEmpty()) {
                    result.put(field, value);
                }
                continue;
            }
            if (!hasField(field)) {
                continue;
            }
        }
        // Add the field to the map
        if (getBytesForString && field.getJavaType() == FieldDescriptor.JavaType.STRING) {
            result.put(field, getFieldRaw(field));
        } else {
            result.put(field, getField(field));
        }
    }
    return result;
}
Also used : OneofDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.OneofDescriptor) Descriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.Descriptor) FileDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FileDescriptor) EnumValueDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.EnumValueDescriptor) EnumDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.EnumDescriptor) FieldDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FieldDescriptor) ArrayList(java.util.ArrayList) List(java.util.List) TreeMap(java.util.TreeMap) OneofDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.OneofDescriptor) FieldDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FieldDescriptor)

Example 5 with FieldDescriptor

use of org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FieldDescriptor in project hbase by apache.

the class MessageReflection method writeMessageTo.

static void writeMessageTo(Message message, Map<FieldDescriptor, Object> fields, CodedOutputStream output, boolean alwaysWriteRequiredFields) throws IOException {
    final boolean isMessageSet = message.getDescriptorForType().getOptions().getMessageSetWireFormat();
    if (alwaysWriteRequiredFields) {
        fields = new TreeMap<FieldDescriptor, Object>(fields);
        for (final FieldDescriptor field : message.getDescriptorForType().getFields()) {
            if (field.isRequired() && !fields.containsKey(field)) {
                fields.put(field, message.getField(field));
            }
        }
    }
    for (final Map.Entry<Descriptors.FieldDescriptor, Object> entry : fields.entrySet()) {
        final Descriptors.FieldDescriptor field = entry.getKey();
        final Object value = entry.getValue();
        if (isMessageSet && field.isExtension() && field.getType() == Descriptors.FieldDescriptor.Type.MESSAGE && !field.isRepeated()) {
            output.writeMessageSetExtension(field.getNumber(), (Message) value);
        } else {
            FieldSet.writeField(field, value, output);
        }
    }
    final UnknownFieldSet unknownFields = message.getUnknownFields();
    if (isMessageSet) {
        unknownFields.writeAsMessageSetTo(output);
    } else {
        unknownFields.writeTo(output);
    }
}
Also used : FieldDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FieldDescriptor) TreeMap(java.util.TreeMap) Map(java.util.Map) FieldDescriptor(org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FieldDescriptor)

Aggregations

FieldDescriptor (org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FieldDescriptor)8 List (java.util.List)4 TreeMap (java.util.TreeMap)4 Map (java.util.Map)3 ArrayList (java.util.ArrayList)2 Descriptor (org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.Descriptor)2 EnumDescriptor (org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.EnumDescriptor)2 EnumValueDescriptor (org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.EnumValueDescriptor)2 FileDescriptor (org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.FileDescriptor)2 OneofDescriptor (org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.OneofDescriptor)2 HashMap (java.util.HashMap)1 EnumLite (org.apache.hadoop.hbase.shaded.com.google.protobuf.Internal.EnumLite)1