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

Unified Diff: blimp/helium/syncable_primitive_serializer.cc

Issue 2400303002: Add LwwRegister CRDT (Closed)
Patch Set: Refactor SyncablePrimitiveSerializer tests and other code review feedback. 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/helium/syncable_primitive_serializer.cc
diff --git a/blimp/helium/syncable_primitive_serializer.cc b/blimp/helium/syncable_primitive_serializer.cc
new file mode 100644
index 0000000000000000000000000000000000000000..4840d41e19b549d5a0b92fa4344fb9620c45b7da
--- /dev/null
+++ b/blimp/helium/syncable_primitive_serializer.cc
@@ -0,0 +1,55 @@
+// 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/syncable_primitive_serializer.h"
+
+#include <stdint.h>
+
+#include <string>
+
+#include "third_party/protobuf/src/google/protobuf/io/coded_stream.h"
+
+namespace blimp {
+namespace helium {
+namespace {
+constexpr uint32_t kMaxStringLength = (10 << 20); // 10MB
+}
+
+// -----------------------------
+// int32_t serialization methods
+
+void SyncablePrimitiveSerializer::Serialize(
+ const int32_t& value,
+ google::protobuf::io::CodedOutputStream* output_stream) {
+ output_stream->WriteVarint32(static_cast<uint32_t>(value));
+}
+
+bool SyncablePrimitiveSerializer::Deserialize(
+ google::protobuf::io::CodedInputStream* input_stream,
+ int32_t* value) {
+ return input_stream->ReadVarint32(reinterpret_cast<uint32_t*>(value));
+}
+
+// ---------------------------------
+// std::string serialization methods
+
+void SyncablePrimitiveSerializer::Serialize(
+ const std::string& value,
+ google::protobuf::io::CodedOutputStream* output_stream) {
+ output_stream->WriteVarint32(value.length());
+ output_stream->WriteString(value);
+}
+
+bool SyncablePrimitiveSerializer::Deserialize(
+ google::protobuf::io::CodedInputStream* input_stream,
+ std::string* value) {
+ uint32_t length;
+ if (input_stream->ReadVarint32(&length) && length < kMaxStringLength) {
+ return input_stream->ReadString(value, length);
+ }
+ return false;
+}
+
+} // namespace helium
+} // namespace blimp

Powered by Google App Engine
This is Rietveld 408576698