@@ -1496,6 +1496,7 @@ MutationResult Serializer::DecodeMutationResult(
1496
1496
write_result.has_update_time
1497
1497
? DecodeSnapshotVersion (reader, write_result.update_time )
1498
1498
: commit_version;
1499
+
1499
1500
absl::optional<std::vector<FieldValue>> transform_results;
1500
1501
if (write_result.transform_results_count > 0 ) {
1501
1502
transform_results = std::vector<FieldValue>{};
@@ -1504,17 +1505,19 @@ MutationResult Serializer::DecodeMutationResult(
1504
1505
DecodeFieldValue (reader, write_result.transform_results [i]));
1505
1506
}
1506
1507
}
1507
- return MutationResult (std::move (version), std::move (transform_results));
1508
+
1509
+ return MutationResult (version, std::move (transform_results));
1508
1510
}
1509
1511
1510
1512
std::unordered_map<std::string, std::string>
1511
1513
Serializer::EncodeListenRequestLabels (const QueryData& query_data) const {
1512
1514
auto value = EncodeLabel (query_data.purpose ());
1513
-
1514
- std::unordered_map<std::string, std::string> result (1 );
1515
- if (!value.empty ()) {
1516
- result[" goog-listen-tags" ] = value;
1515
+ if (value.empty ()) {
1516
+ return {};
1517
1517
}
1518
+
1519
+ std::unordered_map<std::string, std::string> result;
1520
+ result[" goog-listen-tags" ] = std::move (value);
1518
1521
return result;
1519
1522
}
1520
1523
@@ -1526,9 +1529,8 @@ std::string Serializer::EncodeLabel(QueryPurpose purpose) const {
1526
1529
return " existence-filter-mismatch" ;
1527
1530
case QueryPurpose::LimboResolution:
1528
1531
return " limbo-document" ;
1529
- default :
1530
- HARD_FAIL (" Unrecognized query purpose: %s" , purpose);
1531
1532
}
1533
+ UNREACHABLE ();
1532
1534
}
1533
1535
1534
1536
std::unique_ptr<WatchChange> Serializer::DecodeWatchChange (
@@ -1549,11 +1551,8 @@ std::unique_ptr<WatchChange> Serializer::DecodeWatchChange(
1549
1551
1550
1552
case google_firestore_v1_ListenResponse_filter_tag:
1551
1553
return DecodeExistenceFilterWatchChange (reader, watch_change.filter );
1552
-
1553
- default :
1554
- HARD_FAIL (" Unknown WatchChange.changeType %s" ,
1555
- watch_change.which_response_type );
1556
1554
}
1555
+ UNREACHABLE ();
1557
1556
}
1558
1557
1559
1558
SnapshotVersion Serializer::DecodeVersion (
@@ -1569,28 +1568,18 @@ SnapshotVersion Serializer::DecodeVersion(
1569
1568
if (listen_response.target_change .target_ids_count != 0 ) {
1570
1569
return SnapshotVersion::None ();
1571
1570
}
1572
- return DecodeSnapshotVersion (reader, listen_response.target_change .read_time );
1573
- }
1574
-
1575
- std::vector<TargetId> Serializer::DecodeTargetIdArray (nanopb::Reader* reader,
1576
- int32_t * array,
1577
- pb_size_t size) const {
1578
- std::vector<TargetId> target_ids;
1579
1571
1580
- for (pb_size_t i = 0 ; i < size; i++) {
1581
- target_ids.push_back (array[i]);
1582
- }
1583
- return target_ids;
1572
+ return DecodeSnapshotVersion (reader, listen_response.target_change .read_time );
1584
1573
}
1585
1574
1586
1575
std::unique_ptr<WatchChange> Serializer::DecodeTargetChange (
1587
1576
nanopb::Reader* reader,
1588
1577
const google_firestore_v1_TargetChange& change) const {
1589
1578
WatchTargetChangeState state =
1590
1579
DecodeTargetChangeState (reader, change.target_change_type );
1591
- auto target_ids =
1592
- DecodeTargetIdArray (reader, change.target_ids , change.target_ids_count );
1593
- ByteString resume_token = ByteString (change.resume_token );
1580
+ std::vector<TargetId> target_ids (change. target_ids ,
1581
+ change.target_ids + change.target_ids_count );
1582
+ ByteString resume_token (change.resume_token );
1594
1583
1595
1584
util::Status cause;
1596
1585
if (change.has_cause ) {
@@ -1616,9 +1605,8 @@ WatchTargetChangeState Serializer::DecodeTargetChangeState(
1616
1605
return WatchTargetChangeState::Current;
1617
1606
case google_firestore_v1_TargetChange_TargetChangeType_RESET:
1618
1607
return WatchTargetChangeState::Reset;
1619
- default :
1620
- HARD_FAIL (" Unexpected TargetChange.state: %s" , state);
1621
1608
}
1609
+ UNREACHABLE ();
1622
1610
}
1623
1611
1624
1612
std::unique_ptr<WatchChange> Serializer::DecodeDocumentChange (
@@ -1627,19 +1615,23 @@ std::unique_ptr<WatchChange> Serializer::DecodeDocumentChange(
1627
1615
ObjectValue value = ObjectValue::FromMap (DecodeFields (
1628
1616
reader, change.document .fields_count , change.document .fields ));
1629
1617
DocumentKey key = DecodeKey (reader, change.document .name );
1618
+
1619
+ HARD_ASSERT (change.document .has_update_time ,
1620
+ " Got a document change with no snapshot version" );
1630
1621
SnapshotVersion version =
1631
1622
DecodeSnapshotVersion (reader, change.document .update_time );
1632
- HARD_ASSERT (version != SnapshotVersion::None (),
1633
- " Got a document change with no snapshot version" );
1634
- // The document may soon be re-serialized back to protos in order to store it
1635
- // in local persistence. Memoize the encoded form to avoid encoding it again.
1636
- Document document (std::move (value), key, version, DocumentState::kSynced ,
1637
- change.document );
1638
1623
1639
- auto updated_target_ids =
1640
- DecodeTargetIdArray (reader, change.target_ids , change.target_ids_count );
1641
- auto removed_target_ids = DecodeTargetIdArray (
1642
- reader, change.removed_target_ids , change.removed_target_ids_count );
1624
+ // TODO(wuandy): Originally `document` is constructed with `change.document`
1625
+ // as last argument, such that it does not have to encode the proto again
1626
+ // when saving it. It's dangerous because last argument is an `any` type.
1627
+ // Revisit this when porting local serializer to see if we can do it safely.
1628
+ Document document (std::move (value), key, version, DocumentState::kSynced );
1629
+
1630
+ std::vector<TargetId> updated_target_ids (
1631
+ change.target_ids , change.target_ids + change.target_ids_count );
1632
+ std::vector<TargetId> removed_target_ids (
1633
+ change.removed_target_ids ,
1634
+ change.removed_target_ids + change.removed_target_ids_count );
1643
1635
1644
1636
return absl::make_unique<DocumentWatchChange>(
1645
1637
std::move (updated_target_ids), std::move (removed_target_ids),
@@ -1651,14 +1643,15 @@ std::unique_ptr<WatchChange> Serializer::DecodeDocumentDelete(
1651
1643
const google_firestore_v1_DocumentDelete& change) const {
1652
1644
DocumentKey key = DecodeKey (reader, change.document );
1653
1645
// Note that version might be unset in which case we use
1654
- // SnapshotVersion::None()
1646
+ // SnapshotVersion::None().
1655
1647
SnapshotVersion version =
1656
1648
change.has_read_time ? DecodeSnapshotVersion (reader, change.read_time )
1657
1649
: SnapshotVersion::None ();
1658
1650
NoDocument document (key, version, /* has_committed_mutations= */ false );
1659
1651
1660
- std::vector<TargetId> removed_target_ids = DecodeTargetIdArray (
1661
- reader, change.removed_target_ids , change.removed_target_ids_count );
1652
+ std::vector<TargetId> removed_target_ids (
1653
+ change.removed_target_ids ,
1654
+ change.removed_target_ids + change.removed_target_ids_count );
1662
1655
1663
1656
return absl::make_unique<DocumentWatchChange>(
1664
1657
std::vector<TargetId>{}, std::move (removed_target_ids), std::move (key),
@@ -1669,8 +1662,9 @@ std::unique_ptr<WatchChange> Serializer::DecodeDocumentRemove(
1669
1662
nanopb::Reader* reader,
1670
1663
const google_firestore_v1_DocumentRemove& change) const {
1671
1664
DocumentKey key = DecodeKey (reader, change.document );
1672
- std::vector<TargetId> removed_target_ids = DecodeTargetIdArray (
1673
- reader, change.removed_target_ids , change.removed_target_ids_count );
1665
+ std::vector<TargetId> removed_target_ids (
1666
+ change.removed_target_ids ,
1667
+ change.removed_target_ids + change.removed_target_ids_count );
1674
1668
1675
1669
return absl::make_unique<DocumentWatchChange>(std::vector<TargetId>{},
1676
1670
std::move (removed_target_ids),
0 commit comments