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

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

Issue 2400303002: Add LwwRegister CRDT (Closed)
Patch Set: Rebase with Syncable interface changes + 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 unified diff | Download patch
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/syncable_primitive_serializer.h"
6
7 #include <stdint.h>
8
9 #include <string>
10
11 #include "base/logging.h"
12 #include "blimp/helium/version_vector.h"
13 #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h"
14
15 namespace blimp {
16 namespace helium {
17 namespace {
18 constexpr uint32_t kMaxStringLength = (10 << 20); // 10MB
19 }
20
21 // -----------------------------
22 // int32_t serialization methods
23
24 void SyncablePrimitiveSerializer::Serialize(
25 const int32_t& value,
26 google::protobuf::io::CodedOutputStream* output_stream) {
27 output_stream->WriteVarint32(static_cast<uint32_t>(value));
28 }
29
30 bool SyncablePrimitiveSerializer::Deserialize(
31 google::protobuf::io::CodedInputStream* input_stream,
32 int32_t* value) {
33 return input_stream->ReadVarint32(reinterpret_cast<uint32_t*>(value));
34 }
35
36 // ---------------------------------
37 // std::string serialization methods
38
39 void SyncablePrimitiveSerializer::Serialize(
40 const std::string& value,
41 google::protobuf::io::CodedOutputStream* output_stream) {
42 DCHECK(value.length() < kMaxStringLength);
Kevin M 2016/10/20 00:38:24 DCHECK_LT
steimel 2016/10/20 00:57:38 Done.
43 output_stream->WriteVarint32(value.length());
44 output_stream->WriteString(value);
45 }
46
47 bool SyncablePrimitiveSerializer::Deserialize(
48 google::protobuf::io::CodedInputStream* input_stream,
49 std::string* value) {
50 uint32_t length;
51 if (input_stream->ReadVarint32(&length) && length < kMaxStringLength) {
52 return input_stream->ReadString(value, length);
53 }
54 return false;
55 }
56
57 // ------
Kevin M 2016/10/20 00:38:24 Moar dashes?
steimel 2016/10/20 00:57:38 Done.
58 // VersionVector serialization methods
59
60 void SyncablePrimitiveSerializer::Serialize(
61 const VersionVector& value,
62 google::protobuf::io::CodedOutputStream* output_stream) {
63 output_stream->WriteVarint64(value.local_revision());
64 output_stream->WriteVarint64(value.remote_revision());
65 }
66
67 bool SyncablePrimitiveSerializer::Deserialize(
68 google::protobuf::io::CodedInputStream* input_stream,
69 VersionVector* value) {
70 Revision local_revision;
71 Revision remote_revision;
72 if (input_stream->ReadVarint64(&local_revision) &&
73 input_stream->ReadVarint64(&remote_revision)) {
74 value->set_local_revision(local_revision);
75 value->set_remote_revision(remote_revision);
76 return true;
Kevin M 2016/10/20 00:38:24 Can you flip the conditional so that "return true"
steimel 2016/10/20 00:57:38 Done.
77 }
78 return false;
79 }
80
81 } // namespace helium
82 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698