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

Unified Diff: blimp/net/helium/lww_register.h

Issue 2400303002: Add LwwRegister CRDT (Closed)
Patch Set: Update LwwRegister to use Coded(In/Out)putStream. LwwRegisterSerializer -> SyncablePrimitiveSeriali… 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
Index: blimp/net/helium/lww_register.h
diff --git a/blimp/net/helium/lww_register.h b/blimp/net/helium/lww_register.h
new file mode 100644
index 0000000000000000000000000000000000000000..b0f98d226b5e22cbad1499a301046d890915663c
--- /dev/null
+++ b/blimp/net/helium/lww_register.h
@@ -0,0 +1,121 @@
+// 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.
+
+#ifndef BLIMP_NET_HELIUM_LWW_REGISTER_H_
+#define BLIMP_NET_HELIUM_LWW_REGISTER_H_
+
+#include "base/memory/ptr_util.h"
+#include "blimp/net/blimp_net_export.h"
+#include "blimp/net/helium/bias.h"
+#include "blimp/net/helium/running_as.h"
+#include "blimp/net/helium/syncable.h"
+#include "blimp/net/helium/syncable_primitive_serializer.h"
+#include "blimp/net/helium/version_vector.h"
+#include "blimp/net/helium/version_vector_generator.h"
+#include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream_impl_lite.h"
+
+namespace blimp {
+
+// Provides a simple syncable and atomically-writable "register" holding
+// contents of type |RegisterType|. When there is a write conflict, it is
+// resolved by assuming the writer indicated by |bias| has the correct value.
+template <class RegisterType, Bias bias>
+class BLIMP_NET_EXPORT LwwRegister : public Syncable {
+ public:
+ LwwRegister(VersionVectorGenerator* version_gen, RunningAs running_as);
+ ~LwwRegister() = default;
+
+ void Set(const RegisterType& value);
Kevin M 2016/10/13 22:03:36 "set_value()" would also work.
steimel 2016/10/17 21:45:59 Acknowledged.
+
+ const RegisterType& get() const;
Kevin M 2016/10/13 22:03:36 "Get()", or "value()".
steimel 2016/10/17 21:45:59 Done.
+
+ // Syncable implementation.
+ bool ModifiedSince(const VersionVector& from) const override;
+ void CreateChangesetToCurrent(
+ const VersionVector& from,
+ google::protobuf::io::ZeroCopyOutputStream* output_stream) override;
+ void ApplyChangeset(
+ const VersionVector& from,
+ const VersionVector& to,
+ google::protobuf::io::ZeroCopyInputStream* input_stream) override;
+ void ReleaseCheckpointsBefore(const VersionVector& checkpoint) override;
+
+ private:
+ VersionVectorGenerator* version_gen_;
+ VersionVector last_modified_;
+ RunningAs running_as_;
+ RegisterType value_;
+
+ bool LocalWinsConflict() const;
+
+ DISALLOW_COPY_AND_ASSIGN(LwwRegister);
+};
+
+template <class RegisterType, Bias bias>
+LwwRegister<RegisterType, bias>::LwwRegister(
+ VersionVectorGenerator* version_gen,
+ RunningAs running_as)
+ : version_gen_(version_gen),
+ last_modified_(version_gen->current()),
+ running_as_(running_as) {}
+
+template <class RegisterType, Bias bias>
+void LwwRegister<RegisterType, bias>::Set(const RegisterType& value) {
+ value_ = value;
+ version_gen_->Increment();
+ last_modified_ = last_modified_.MergeWith(version_gen_->current());
+}
+
+template <class RegisterType, Bias bias>
+const RegisterType& LwwRegister<RegisterType, bias>::get() const {
+ return value_;
+}
+
+template <class RegisterType, Bias bias>
+bool LwwRegister<RegisterType, bias>::ModifiedSince(
+ const VersionVector& from) const {
+ return from.local_revision() < last_modified_.local_revision();
+}
+
+template <class RegisterType, Bias bias>
+void LwwRegister<RegisterType, bias>::CreateChangesetToCurrent(
+ const VersionVector& from,
+ google::protobuf::io::ZeroCopyOutputStream* output_stream) {
+ SyncablePrimitiveSerializer<RegisterType>::SerializeSyncablePrimitive(
+ value_, output_stream);
+}
+
+template <class RegisterType, Bias bias>
+void LwwRegister<RegisterType, bias>::ApplyChangeset(
+ const VersionVector& from,
+ const VersionVector& to,
+ google::protobuf::io::ZeroCopyInputStream* input_stream) {
+ VersionVector::Comparison cmp = last_modified_.CompareTo(to);
+ if (cmp == VersionVector::Comparison::LessThan ||
+ (cmp == VersionVector::Comparison::Conflict && !LocalWinsConflict())) {
+ SyncablePrimitiveSerializer<RegisterType>::DeserializeSyncablePrimitive(
+ value_, input_stream);
+ }
+ last_modified_ = last_modified_.MergeWith(to);
+}
+
+template <class RegisterType, Bias bias>
+void LwwRegister<RegisterType, bias>::ReleaseCheckpointsBefore(
+ const VersionVector& checkpoint) {
+ // no-op
+}
+
+template <class RegisterType, Bias bias>
+bool LwwRegister<RegisterType, bias>::LocalWinsConflict() const {
Kevin M 2016/10/13 22:03:36 Two cases = this should probably be an if/elseif.
Kevin M 2016/10/13 22:49:55 Can we just precompute this in the constructor, e.
steimel 2016/10/17 21:45:59 Done.
+ switch (running_as_) {
+ case RunningAs::Client:
+ return bias == Bias::ClientWins;
+ case RunningAs::Engine:
+ return bias == Bias::EngineWins;
+ }
+}
+
+} // namespace blimp
+
+#endif // BLIMP_NET_HELIUM_LWW_REGISTER_H_

Powered by Google App Engine
This is Rietveld 408576698