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

Unified Diff: blimp/helium/coded_value_serializer.cc

Issue 2400303002: Add LwwRegister CRDT (Closed)
Patch Set: Add comment on max string length constant 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/coded_value_serializer.h ('k') | blimp/helium/coded_value_serializer_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: blimp/helium/coded_value_serializer.cc
diff --git a/blimp/helium/coded_value_serializer.cc b/blimp/helium/coded_value_serializer.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e5e1733867099582f7474a965dfd0fe45ed67a11
--- /dev/null
+++ b/blimp/helium/coded_value_serializer.cc
@@ -0,0 +1,83 @@
+// 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/coded_value_serializer.h"
+
+#include <stdint.h>
+
+#include <string>
+
+#include "base/logging.h"
+#include "blimp/helium/version_vector.h"
+#include "third_party/protobuf/src/google/protobuf/io/coded_stream.h"
+
+namespace blimp {
+namespace helium {
+namespace {
+// Large (and arbitrary) size for sanity check on string deserialization.
+constexpr uint32_t kMaxStringLength = (10 << 20); // 10MB
+}
+
+// -----------------------------
+// int32_t serialization methods
+
+void CodedValueSerializer::Serialize(
+ const int32_t& value,
+ google::protobuf::io::CodedOutputStream* output_stream) {
+ output_stream->WriteVarint32(static_cast<uint32_t>(value));
+}
+
+bool CodedValueSerializer::Deserialize(
+ google::protobuf::io::CodedInputStream* input_stream,
+ int32_t* value) {
+ return input_stream->ReadVarint32(reinterpret_cast<uint32_t*>(value));
+}
+
+// ---------------------------------
+// std::string serialization methods
+
+void CodedValueSerializer::Serialize(
+ const std::string& value,
+ google::protobuf::io::CodedOutputStream* output_stream) {
+ DCHECK_LT(value.length(), kMaxStringLength);
+ output_stream->WriteVarint32(value.length());
+ output_stream->WriteString(value);
+}
+
+bool CodedValueSerializer::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;
+}
+
+// -----------------------------------
+// VersionVector serialization methods
+
+void CodedValueSerializer::Serialize(
+ const VersionVector& value,
+ google::protobuf::io::CodedOutputStream* output_stream) {
+ output_stream->WriteVarint64(value.local_revision());
+ output_stream->WriteVarint64(value.remote_revision());
+}
+
+bool CodedValueSerializer::Deserialize(
+ google::protobuf::io::CodedInputStream* input_stream,
+ VersionVector* value) {
+ Revision local_revision;
+ Revision remote_revision;
+ if (!input_stream->ReadVarint64(&local_revision) ||
+ !input_stream->ReadVarint64(&remote_revision)) {
+ return false;
+ }
+ value->set_local_revision(local_revision);
+ value->set_remote_revision(remote_revision);
+ return true;
+}
+
+} // namespace helium
+} // namespace blimp
« no previous file with comments | « blimp/helium/coded_value_serializer.h ('k') | blimp/helium/coded_value_serializer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698