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

Unified 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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: blimp/helium/compound_syncable.cc
diff --git a/blimp/helium/compound_syncable.cc b/blimp/helium/compound_syncable.cc
new file mode 100644
index 0000000000000000000000000000000000000000..bc4181cba86e4cf2216552e12d295adc64f39ea0
--- /dev/null
+++ b/blimp/helium/compound_syncable.cc
@@ -0,0 +1,93 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "blimp/helium/compound_syncable.h"
+
+#include <bitset>
+#include <utility>
+#include <vector>
+
+#include "base/logging.h"
+#include "third_party/protobuf/src/google/protobuf/io/coded_stream.h"
+
+namespace blimp {
+namespace helium {
+
+CompoundSyncable::CompoundSyncable() {}
+
+CompoundSyncable::~CompoundSyncable() {}
+
+void CompoundSyncable::CreateChangesetToCurrent(
+ Revision from,
+ google::protobuf::io::CodedOutputStream* changeset) {
+ // Write out a bitarray representing the modified state, where for each
+ // modified member |members_[i]|, the bit modified[i] is set to 1.
+ // The bits for unmodified members_ are left as zero.
+ DCHECK_LE(members_.size(), 64u);
+ std::bitset<64> modified;
+ for (size_t i = 0; i < members_.size(); ++i) {
+ if (members_[i]->GetVersionVector().local_revision() >= from) {
+ modified[i] = true;
+ }
+ }
+ DCHECK_GT(modified.count(), 0u);
+
+ changeset->WriteVarint64(modified.to_ullong());
+ for (size_t i = 0; i < members_.size(); ++i) {
+ if (modified[i]) {
+ int prev_bytes_written = changeset->ByteCount();
+
+ members_[i]->CreateChangesetToCurrent(from, changeset);
+
+ // Ensure all "modified" children have serialized non-empty payloads.
+ DCHECK_GT(changeset->ByteCount(), prev_bytes_written);
+ }
+ }
+}
+
+Result CompoundSyncable::ApplyChangeset(
+ Revision to,
+ google::protobuf::io::CodedInputStream* changeset) {
+ uint64_t modified_header;
+ if (!changeset->ReadVarint64(&modified_header)) {
+ DLOG(FATAL) << "Couldn't read modification header.";
+ return Result::ERR_PROTOCOL_ERROR;
+ }
+ if (modified_header > (1u << members_.size())) {
+ DLOG(FATAL) << "Invalid modification header.";
+ return Result::ERR_PROTOCOL_ERROR;
+ }
+
+ std::bitset<64> modified(modified_header);
+
+ for (size_t member_id = 0u; member_id < members_.size(); ++member_id) {
+ if (!modified[member_id]) {
+ continue;
+ }
+
+ Result child_result = members_[member_id]->ApplyChangeset(to, changeset);
+ if (child_result != Result::SUCCESS) {
+ return child_result;
+ }
+ }
+
+ return Result::SUCCESS;
+}
+
+VersionVector CompoundSyncable::GetVersionVector() const {
+ VersionVector merged;
+ for (const auto& member : members_) {
+ merged = merged.MergeWith(member->GetVersionVector());
+ }
+ return merged;
+}
+
+void CompoundSyncable::ReleaseBefore(Revision from) {
+ for (const auto& next_child : members_) {
+ next_child->ReleaseBefore(from);
+ }
+}
+
+} // namespace helium
+} // namespace blimp
« 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