【源码】protobuf 中各个压缩(Serialize)、解压缩(Parse)函数辨析

本文详细介绍了ProtocolBuffers库中的关键函数,如数据解析(ParseFromString,ParsePartialFromString)、从流中合并消息(MergeFromCodedStream)、序列化(SerializeToCodedStream)以及计算字节大小(ByteSizeLong),展示了如何在C++中进行数据的编码和解码操作。

PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromString(ConstStringParam data);

PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParsePartialFromString(

ConstStringParam data);

// Parse a protocol buffer contained in an array of bytes.

PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromArray(const void* data,

int size);

PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParsePartialFromArray(const void* data,

int size);

// Reads a protocol buffer from the stream and merges it into this

// Message. Singular fields read from the what is

// already in the Message and repeated fields are appended to those

// already present.

//

// It is the responsibility of the caller to call input->LastTagWas()

// (for groups) or input->ConsumedEntireMessage() (for non-groups) after

// this returns to verify that the message’s end was delimited correctly.

//

// ParseFromCodedStream() is implemented as Clear() followed by

// MergeFromCodedStream().

bool MergeFromCodedStream(io::CodedInputStream* input);

// Like MergeFromCodedStream(), but succeeds even if required fields are

// missing in the input.

//

// MergeFromCodedStream() is just implemented as MergePartialFromCodedStream()

// followed by IsInitialized().

bool MergePartialFromCodedStream(io::CodedInputStream* input);

// Merge a protocol buffer contained in a string.

bool MergeFromString(ConstStringParam data);


Serialization


Methods for serializing in protocol buffer format. Most of these are just simple wrappers around ByteSize() and SerializeWithCachedSizes().

// Write a protocol buffer of this message to the given output. Returns

// false on a write error. If the message is missing required fields,

// this may GOOGLE_CHECK-fail.

bool SerializeToCodedStream(io::CodedOutputStream* output) const;

bool SerializePartialToCodedStream(io::CodedOutputStream* output) const;

// Write the message to the given zero-copy output stream. All required

// fields must be set.

bool SerializeToZeroCopyStream(io::ZeroCopyOutputStream* output) const;

bool SerializePartialToZeroCopyStream(io::ZeroCopyOutputStream* output) const;

// Serialize the message and store it in the given string. All required

// fields must be set.

bool SerializeToString(std::string* output) const;

bool SerializePartialToString(std::string* output) const;

// Serialize the message and store it in the given byte array. All required

// fields must be set.

bool SerializeToArray(void* data, int size) const;

bool SerializePartialToArray(void* data, int size) const;

// Make a string encoding the message. Is equivalent to calling

// SerializeToString() on a string and using that. Returns the empty

// string if SerializeToString() would have returned an error.

// Note: If you intend to generate many such strings, you may

// reduce heap fragmentation by instead re-using the same string

// object with calls to SerializeToString().

std::string SerializeAsString() const;

std::string SerializePartialAsString() const;

// Serialize the message and write it to the given file descriptor. All

// required fields must be set.

bool SerializeToFileDescriptor(int file_descriptor) const;

bool SerializePartialToFileDescriptor(int file_descriptor) const;

// Serialize the message and write it to the given C++ ostream. All

// required fields must be set.

bool SerializeToOstream(std::ostream* output) const;

bool SerializePartialToOstream(std::ostream* output) const;

// Like SerializeToString(), but appends to the data to the string’s

// existing contents. All required fields must be set.

bool AppendToString(std::string* output) const;

// Like AppendToString(), but allows missing required fields.

bool AppendPartialToString(std::string* output) const;

// Computes the serialized size of the message. This recursively calls

// ByteSizeLong() on all embedded messages.

//

// ByteSizeLong() is generally linear in the number of fields defined for the

// proto.

virtual size_t ByteSizeLong() const = 0;

// Legacy ByteSize() API.

PROTOBUF_DEPRECATED_MSG(“Please use ByteSizeLong() instead”)

int ByteSize() const { return internal::ToIntSize(ByteSizeLong()); }

// Serializes the message without recomputing the size. The message must not

// have changed since the last call to ByteSize(), and the value returned by

// ByteSize must be non-negative. Otherwise the results are undefined.

void SerializeWithCachedSizes(io::CodedOutputStream* output) const {

output->SetCur(_InternalSerialize(output->Cur(), output->EpsCopy()));

}

// Functions below here are not part of the public interface. It isn’t

// enforced, but they should be treated as private, and will be private

// at some future time. Unfortunately the implementation of the “friend”

// keyword in GCC is broken at the moment, but we expect it will be fixed.

// Like SerializeWithCachedSizes, but writes directly to *target, returning
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

总结

上述知识点,囊括了目前互联网企业的主流应用技术以及能让你成为“香饽饽”的高级架构知识,每个笔记里面几乎都带有实战内容。

很多人担心学了容易忘,这里教你一个方法,那就是重复学习。

打个比方,假如你正在学习 spring 注解,突然发现了一个注解@Aspect,不知道干什么用的,你可能会去查看源码或者通过博客学习,花了半小时终于弄懂了,下次又看到@Aspect 了,你有点郁闷了,上次好像在哪哪哪学习,你快速打开网页花了五分钟又学会了。

从半小时和五分钟的对比中可以发现多学一次就离真正掌握知识又近了一步。

人的本性就是容易遗忘,只有不断加深印象、重复学习才能真正掌握,所以很多书我都是推荐大家多看几遍。哪有那么多天才,他只是比你多看了几遍书。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

从半小时和五分钟的对比中可以发现多学一次就离真正掌握知识又近了一步。

[外链图片转存中…(img-M42kdJIC-1712731732365)]

人的本性就是容易遗忘,只有不断加深印象、重复学习才能真正掌握,所以很多书我都是推荐大家多看几遍。哪有那么多天才,他只是比你多看了几遍书。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值