Java Code Examples for com.google.protobuf.CodedOutputStream#computeDoubleSizeNoTag()

The following examples show how to use com.google.protobuf.CodedOutputStream#computeDoubleSizeNoTag() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RawSerDes.java    From blueflood with Apache License 2.0 6 votes vote down vote up
private int sizeOf(Object obj) throws SerializationException {
    int sz = sizeOfSize();
    sz += sizeOfType();

    if ( obj instanceof Integer ) {
        sz += CodedOutputStream.computeRawVarint32Size((Integer) obj);
    } else if ( obj instanceof Long ) {
        sz += CodedOutputStream.computeRawVarint64Size((Long)obj);
    } else if ( obj instanceof Double ) {
        sz += CodedOutputStream.computeDoubleSizeNoTag((Double)obj);
    } else if ( obj instanceof Float ) {
        sz += CodedOutputStream.computeDoubleSizeNoTag(((Float)obj).doubleValue());
    } else {
        throw new SerializationException("Unexpected type: " + obj.getClass().getName());
    }
    return sz;
}
 
Example 2
Source File: StatsSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
public int sizeOf(T stat) {
    int sz = 1 + 1; // type + isFP.
    sz += stat.isFloatingPoint() ?
            CodedOutputStream.computeDoubleSizeNoTag(stat.toDouble()) :
            CodedOutputStream.computeRawVarint64Size(stat.toLong());
    return sz;
}
 
Example 3
Source File: CounterSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
private int sizeOf(BluefloodCounterRollup counterRollup) {
    int sz = sizeOfSize();
    sz += sizeOfType();
    if (counterRollup.getCount() instanceof Long || counterRollup.getCount() instanceof Integer)
        sz += CodedOutputStream.computeRawVarint64Size(counterRollup.getCount().longValue());
    else if (counterRollup.getCount() instanceof Double || counterRollup.getCount() instanceof Float)
        sz += CodedOutputStream.computeDoubleSizeNoTag(counterRollup.getCount().doubleValue());
    sz += CodedOutputStream.computeDoubleSizeNoTag(counterRollup.getRate());
    sz += CodedOutputStream.computeRawVarint32Size(counterRollup.getSampleCount());
    return sz;
}
 
Example 4
Source File: BasicRollupSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
protected int sizeOf(BasicRollup basicRollup, byte version) {

        int sz = sizeOfBaseRollup( basicRollup );
        if( version == VERSION_2_ROLLUP )
            sz += CodedOutputStream.computeDoubleSizeNoTag(basicRollup.getSum());

        return sz;
    }
 
Example 5
Source File: TimerRollupSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
private int sizeOf(BluefloodTimerRollup bluefloodTimerRollup, byte timerVersion) throws SerializationException {

        int sz = sizeOfSize();

        if (timerVersion == VERSION_1_TIMER) {
            sz += CodedOutputStream.computeRawVarint64Size((long) bluefloodTimerRollup.getSum());
        } else if (timerVersion == VERSION_2_TIMER) {
            sz += CodedOutputStream.computeDoubleSizeNoTag(bluefloodTimerRollup.getSum());
        } else {
            throw new SerializationException(String.format("Unexpected timer serialization version: %d", (int)timerVersion));
        }
        sz += CodedOutputStream.computeRawVarint64Size(bluefloodTimerRollup.getCount());
        sz += CodedOutputStream.computeDoubleSizeNoTag(bluefloodTimerRollup.getRate());
        sz += CodedOutputStream.computeRawVarint32Size(bluefloodTimerRollup.getSampleCount());
        sz += averageStatDeSer.sizeOf(bluefloodTimerRollup.getAverage());
        sz += maxStatDeSer.sizeOf(bluefloodTimerRollup.getMaxValue());
        sz += minStatDeSer.sizeOf(bluefloodTimerRollup.getMinValue());
        sz += varianceStatDeSer.sizeOf(bluefloodTimerRollup.getVariance());

        Map<String, BluefloodTimerRollup.Percentile> percentiles = bluefloodTimerRollup.getPercentiles();
        sz += CodedOutputStream.computeRawVarint32Size(bluefloodTimerRollup.getPercentiles().size());
        for (Map.Entry<String, BluefloodTimerRollup.Percentile> entry : percentiles.entrySet()) {
            sz += CodedOutputStream.computeStringSizeNoTag(entry.getKey());
            Number[] pctComponents = new Number[] {
                    entry.getValue().getMean(),
            };
            for (Number num : pctComponents) {
                sz += sizeOfType();
                if (num instanceof Long || num instanceof Integer) {
                    sz += CodedOutputStream.computeRawVarint64Size(num.longValue());
                } else if (num instanceof Double || num instanceof Float) {
                    sz += CodedOutputStream.computeDoubleSizeNoTag(num.doubleValue());
                }
            }
        }
        return sz;
    }
 
Example 6
Source File: GaugeSerDes.java    From blueflood with Apache License 2.0 5 votes vote down vote up
private int sizeOf(BluefloodGaugeRollup gaugeRollup) {
    // just like rollup up until a point.
    int sz = sizeOfBaseRollup( gaugeRollup );

    // here's where it gets different.
    sz += CodedOutputStream.computeRawVarint64Size(gaugeRollup.getTimestamp());
    sz += 1; // type of latest value.
    if (gaugeRollup.getLatestNumericValue() instanceof Long || gaugeRollup.getLatestNumericValue() instanceof Integer)
        sz += CodedOutputStream.computeRawVarint64Size(gaugeRollup.getLatestNumericValue().longValue());
    else if (gaugeRollup.getLatestNumericValue() instanceof Double || gaugeRollup.getLatestNumericValue() instanceof Float)
        sz += CodedOutputStream.computeDoubleSizeNoTag(gaugeRollup.getLatestNumericValue().doubleValue());
    return sz;
}
 
Example 7
Source File: CodedConstant.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
/**
 * get object size by {@link FieldType}.
 *
 * @param order the order
 * @param o the o
 * @param type the type
 * @param list the list
 * @param debug the debug
 * @param path the path
 * @return the int
 */
public static int computeSize(int order, Object o, FieldType type, boolean list, boolean debug, File path) {
    int size = 0;
    if (o == null) {
        return size;
    }

    if (type == FieldType.OBJECT) {
        Class cls = o.getClass();
        Codec target = ProtobufProxy.create(cls, debug, path);
        try {
            size = target.size(o);
            size = size + CodedOutputStream.computeRawVarint32Size(size);
            return size + CodedOutputStream.computeTagSize(order);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    if (type == FieldType.STRING) {
        size = CodedOutputStream.computeStringSizeNoTag(String.valueOf(o));
    } else if (type == FieldType.BOOL) {
        size = CodedOutputStream.computeBoolSizeNoTag(Boolean.valueOf(String.valueOf(o)));
    } else if (type == FieldType.BYTES) {
        byte[] bb = (byte[]) o;
        size = CodedOutputStream.computeBytesSizeNoTag(ByteString.copyFrom(bb));
    } else if (type == FieldType.DOUBLE) {
        size = CodedOutputStream.computeDoubleSizeNoTag(Double.valueOf(o.toString()));
    } else if (type == FieldType.FIXED32 || type == FieldType.INT32 || type == FieldType.SFIXED32
            || type == FieldType.SINT32 || type == FieldType.UINT32) {
        size = CodedOutputStream.computeInt32SizeNoTag(Integer.valueOf(o.toString()));
    } else if (type == FieldType.FIXED64 || type == FieldType.INT64 || type == FieldType.SFIXED64
            || type == FieldType.SINT64 || type == FieldType.UINT64) {
        size = CodedOutputStream.computeInt64SizeNoTag(Long.valueOf(o.toString()));
    } else if (type == FieldType.FLOAT) {
        size = CodedOutputStream.computeFloatSizeNoTag(Float.valueOf(o.toString()));
    } else if (type == FieldType.ENUM) {
        if (o instanceof EnumReadable) {
            size = CodedOutputStream.computeInt32SizeNoTag(((EnumReadable) o).value());
        } else if (o instanceof Enum) {
            size = CodedOutputStream.computeInt32SizeNoTag(((Enum) o).ordinal());
        }
    }

    return size;
}
 
Example 8
Source File: CodedConstant.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
/**
 * Compute the number of bytes that would be needed to encode a particular value of arbitrary type, excluding tag.
 *
 * @param type The field's type.
 * @param value Object representing the field's value. Must be of the exact type which would be returned by
 *            {@link Message#getField(Descriptors.FieldDescriptor)} for this field.
 * @return the int
 */
public static int computeElementSizeNoTag(final WireFormat.FieldType type, final Object value) {
    switch (type) {
        // Note: Minor violation of 80-char limit rule here because this would
        // actually be harder to read if we wrapped the lines.
        case DOUBLE:
            return CodedOutputStream.computeDoubleSizeNoTag((Double) value);
        case FLOAT:
            return CodedOutputStream.computeFloatSizeNoTag((Float) value);
        case INT64:
            return CodedOutputStream.computeInt64SizeNoTag((Long) value);
        case UINT64:
            return CodedOutputStream.computeUInt64SizeNoTag((Long) value);
        case INT32:
            return CodedOutputStream.computeInt32SizeNoTag((Integer) value);
        case FIXED64:
            return CodedOutputStream.computeFixed64SizeNoTag((Long) value);
        case FIXED32:
            return CodedOutputStream.computeFixed32SizeNoTag((Integer) value);
        case BOOL:
            return CodedOutputStream.computeBoolSizeNoTag((Boolean) value);
        case STRING:
            return CodedOutputStream.computeStringSizeNoTag((String) value);
        case GROUP:
            return CodedOutputStream.computeGroupSizeNoTag((MessageLite) value);
        case BYTES:
            if (value instanceof ByteString) {
                return CodedOutputStream.computeBytesSizeNoTag((ByteString) value);
            } else {
                if (value instanceof Byte[]) {
                    return computeLengthDelimitedFieldSize(((Byte[]) value).length);
                }
                return CodedOutputStream.computeByteArraySizeNoTag((byte[]) value);
            }
        case UINT32:
            return CodedOutputStream.computeUInt32SizeNoTag((Integer) value);
        case SFIXED32:
            return CodedOutputStream.computeSFixed32SizeNoTag((Integer) value);
        case SFIXED64:
            return CodedOutputStream.computeSFixed64SizeNoTag((Long) value);
        case SINT32:
            return CodedOutputStream.computeSInt32SizeNoTag((Integer) value);
        case SINT64:
            return CodedOutputStream.computeSInt64SizeNoTag((Long) value);

        case MESSAGE:
            if (value instanceof LazyField) {
                return CodedOutputStream.computeLazyFieldSizeNoTag((LazyField) value);
            } else {
                return computeObjectSizeNoTag(value);
            }

        case ENUM:
            if (value instanceof Internal.EnumLite) {
                return CodedOutputStream.computeEnumSizeNoTag(((Internal.EnumLite) value).getNumber());
            } else {
                if (value instanceof EnumReadable) {
                    return CodedOutputStream.computeEnumSizeNoTag(((EnumReadable) value).value());
                } else if (value instanceof Enum) {
                    return CodedOutputStream.computeEnumSizeNoTag(((Enum) value).ordinal());
                }

                return CodedOutputStream.computeEnumSizeNoTag((Integer) value);
            }
    }

    throw new RuntimeException("There is no way to get here, but the compiler thinks otherwise.");
}
 
Example 9
Source File: CodedConstant.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
/**
 * get object size by {@link FieldType}
 * 
 * @param o
 * @param type
 * @return
 */
public static int computeSize(int order, Object o, FieldType type, boolean list, boolean debug, File path) {
    int size = 0;
    if (o == null) {
        return size;
    }

    if (type == FieldType.OBJECT) {
        Class cls = o.getClass();
        Codec target = ProtobufProxy.create(cls);
        try {
            size = target.size(o);
            size = size + CodedOutputStream.computeRawVarint32Size(size);
            return size + CodedOutputStream.computeTagSize(order);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    if (type == FieldType.STRING) {
        size = CodedOutputStream.computeStringSizeNoTag(String.valueOf(o));
    } else if (type == FieldType.BOOL) {
        size = CodedOutputStream.computeBoolSizeNoTag(Boolean.valueOf(String.valueOf(o)));
    } else if (type == FieldType.BYTES) {
        byte[] bb = (byte[]) o;
        size = CodedOutputStream.computeBytesSizeNoTag(ByteString.copyFrom(bb));
    } else if (type == FieldType.DOUBLE) {
        size = CodedOutputStream.computeDoubleSizeNoTag(Double.valueOf(o.toString()));
    } else if (type == FieldType.FIXED32 || type == FieldType.INT32 || type == FieldType.SFIXED32
            || type == FieldType.SINT32 || type == FieldType.UINT32) {
        size = CodedOutputStream.computeInt32SizeNoTag(Integer.valueOf(o.toString()));
    } else if (type == FieldType.FIXED64 || type == FieldType.INT64 || type == FieldType.SFIXED64
            || type == FieldType.SINT64 || type == FieldType.UINT64) {
        size = CodedOutputStream.computeInt64SizeNoTag(Long.valueOf(o.toString()));
    } else if (type == FieldType.FLOAT) {
        size = CodedOutputStream.computeFloatSizeNoTag(Float.valueOf(o.toString()));
    } else if (type == FieldType.ENUM) {
        if (o instanceof EnumReadable) {
            size = CodedOutputStream.computeInt32SizeNoTag(((EnumReadable) o).value());
        } else if (o instanceof Enum) {
            size = CodedOutputStream.computeInt32SizeNoTag(((Enum) o).ordinal());
        }
    }

    return size;
}