Skip to content

Allow serializing arbitrary length FieldValues #889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 12, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions Firestore/core/src/firebase/firestore/remote/serializer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -509,13 +509,30 @@ std::map<std::string, FieldValue> DecodeObject(pb_istream_t* stream) {

void Serializer::EncodeFieldValue(const FieldValue& field_value,
std::vector<uint8_t>* out_bytes) {
// TODO(rsgowman): how large should the output buffer be? Do some
// investigation to see if we can get nanopb to tell us how much space it's
// going to need. (Hint: use a sizing stream, i.e. PB_OSTREAM_SIZING)
uint8_t buf[1024];
pb_ostream_t stream = pb_ostream_from_buffer(buf, sizeof(buf));
// TODO(rsgowman): find a better home for this constant.
// A document is defined to have a max size of 1MiB - 4 bytes.
static const size_t kMaxDocumentSize = 1 * 1024 * 1024 - 4;

// Construct a nanopb output stream.
//
// Set the max_size to be the max document size (as an upper bound; one would
// expect individual FieldValue's to be smaller than this).
//
// bytes_written is (always) initialized to 0. (NB: nanopb does not know or
// care about the underlying output vector, so where we are in the vector
// itself is irrelevant. i.e. don't use out_bytes->size())
pb_ostream_t stream = {
/*callback=*/[](pb_ostream_t* stream, const pb_byte_t* buf,
size_t count) -> bool {
auto* out_bytes = static_cast<std::vector<uint8_t>*>(stream->state);
out_bytes->insert(out_bytes->end(), buf, buf + count);
return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this need to update bytes_written?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

},
/*state=*/out_bytes,
/*max_size=*/kMaxDocumentSize,
/*bytes_written=*/0,
/*errmsg=*/NULL};
EncodeFieldValueImpl(&stream, field_value);
out_bytes->insert(out_bytes->end(), buf, buf + stream.bytes_written);
}

FieldValue Serializer::DecodeFieldValue(const uint8_t* bytes, size_t length) {
Expand Down