Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(317)

Side by Side Diff: blimp/helium/compound_syncable.cc

Issue 2402153002: Add CompoundSyncable class for synchronizing containers of Syncables. (Closed)
Patch Set: make literal unsigned Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « blimp/helium/compound_syncable.h ('k') | blimp/helium/compound_syncable_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "blimp/helium/compound_syncable.h"
6
7 #include <bitset>
8 #include <utility>
9 #include <vector>
10
11 #include "base/logging.h"
12 #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h"
13
14 namespace blimp {
15 namespace helium {
16
17 CompoundSyncable::CompoundSyncable() {}
18
19 CompoundSyncable::~CompoundSyncable() {}
20
21 void CompoundSyncable::CreateChangesetToCurrent(
22 Revision from,
23 google::protobuf::io::CodedOutputStream* changeset) {
24 // Write out a bitarray representing the modified state, where for each
25 // modified member |members_[i]|, the bit modified[i] is set to 1.
26 // The bits for unmodified members_ are left as zero.
27 DCHECK_LE(members_.size(), 64u);
28 std::bitset<64> modified;
29 for (size_t i = 0; i < members_.size(); ++i) {
30 if (members_[i]->GetVersionVector().local_revision() >= from) {
31 modified[i] = true;
32 }
33 }
34 DCHECK_GT(modified.count(), 0u);
35
36 changeset->WriteVarint64(modified.to_ullong());
37 for (size_t i = 0; i < members_.size(); ++i) {
38 if (modified[i]) {
39 int prev_bytes_written = changeset->ByteCount();
40
41 members_[i]->CreateChangesetToCurrent(from, changeset);
42
43 // Ensure all "modified" children have serialized non-empty payloads.
44 DCHECK_GT(changeset->ByteCount(), prev_bytes_written);
45 }
46 }
47 }
48
49 Result CompoundSyncable::ApplyChangeset(
50 Revision to,
51 google::protobuf::io::CodedInputStream* changeset) {
52 uint64_t modified_header;
53 if (!changeset->ReadVarint64(&modified_header)) {
54 DLOG(FATAL) << "Couldn't read modification header.";
55 return Result::ERR_PROTOCOL_ERROR;
56 }
57 if (modified_header > (1u << members_.size())) {
58 DLOG(FATAL) << "Invalid modification header.";
59 return Result::ERR_PROTOCOL_ERROR;
60 }
61
62 std::bitset<64> modified(modified_header);
63
64 for (size_t member_id = 0u; member_id < members_.size(); ++member_id) {
65 if (!modified[member_id]) {
66 continue;
67 }
68
69 Result child_result = members_[member_id]->ApplyChangeset(to, changeset);
70 if (child_result != Result::SUCCESS) {
71 return child_result;
72 }
73 }
74
75 return Result::SUCCESS;
76 }
77
78 VersionVector CompoundSyncable::GetVersionVector() const {
79 VersionVector merged;
80 for (const auto& member : members_) {
81 merged = merged.MergeWith(member->GetVersionVector());
82 }
83 return merged;
84 }
85
86 void CompoundSyncable::ReleaseBefore(Revision from) {
87 for (const auto& next_child : members_) {
88 next_child->ReleaseBefore(from);
89 }
90 }
91
92 } // namespace helium
93 } // namespace blimp
OLDNEW
« no previous file with comments | « blimp/helium/compound_syncable.h ('k') | blimp/helium/compound_syncable_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698