OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sync/internal_api/public/base/unique_position.h" | 5 #include "sync/internal_api/public/base/unique_position.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/rand_util.h" | |
10 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
11 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
12 #include "sync/protocol/unique_position.pb.h" | 11 #include "sync/protocol/unique_position.pb.h" |
13 #include "third_party/zlib/zlib.h" | 12 #include "third_party/zlib/zlib.h" |
14 | 13 |
15 namespace syncer { | 14 namespace syncer { |
16 | 15 |
17 const size_t UniquePosition::kSuffixLength = 28; | 16 const size_t UniquePosition::kSuffixLength = 28; |
18 const size_t UniquePosition::kCompressBytesThreshold = 128; | 17 const size_t UniquePosition::kCompressBytesThreshold = 128; |
19 | 18 |
20 // static. | 19 // static. |
21 bool UniquePosition::IsValidSuffix(const std::string& suffix) { | 20 bool UniquePosition::IsValidSuffix(const std::string& suffix) { |
22 // The suffix must be exactly the specified length, otherwise unique suffixes | 21 // The suffix must be exactly the specified length, otherwise unique suffixes |
23 // are not sufficient to guarantee unique positions (because prefix + suffix | 22 // are not sufficient to guarantee unique positions (because prefix + suffix |
24 // == p + refixsuffix). | 23 // == p + refixsuffix). |
25 return suffix.length() == kSuffixLength | 24 return suffix.length() == kSuffixLength; |
26 && suffix[kSuffixLength-1] != 0; | |
27 } | 25 } |
28 | 26 |
29 // static. | 27 // static. |
30 bool UniquePosition::IsValidBytes(const std::string& bytes) { | 28 bool UniquePosition::IsValidBytes(const std::string& bytes) { |
31 // The first condition ensures that our suffix uniqueness is sufficient to | 29 // The first condition ensures that our suffix uniqueness is sufficient to |
32 // guarantee position uniqueness. Otherwise, it's possible the end of some | 30 // guarantee position uniqueness. Otherwise, it's possible the end of some |
33 // prefix + some short suffix == some long suffix. | 31 // prefix + some short suffix == some long suffix. |
34 // The second condition ensures that FindSmallerWithSuffix can always return a | 32 // The second condition ensures that FindSmallerWithSuffix can always return a |
35 // result. | 33 // result. |
36 return bytes.length() >= kSuffixLength | 34 return bytes.length() >= kSuffixLength |
37 && bytes[bytes.length()-1] != 0; | 35 && bytes[bytes.length()-1] != 0; |
38 } | 36 } |
39 | 37 |
40 // static. | 38 // static. |
41 std::string UniquePosition::RandomSuffix() { | |
42 // Users random data for all but the last byte. The last byte must not be | |
43 // zero. We arbitrarily set it to 0x7f. | |
44 return base::RandBytesAsString(kSuffixLength - 1) + "\x7f"; | |
45 } | |
46 | |
47 // static. | |
48 UniquePosition UniquePosition::CreateInvalid() { | 39 UniquePosition UniquePosition::CreateInvalid() { |
49 UniquePosition pos; | 40 UniquePosition pos; |
50 DCHECK(!pos.IsValid()); | 41 DCHECK(!pos.IsValid()); |
51 return pos; | 42 return pos; |
52 } | 43 } |
53 | 44 |
54 // static. | 45 // static. |
55 UniquePosition UniquePosition::FromProto(const sync_pb::UniquePosition& proto) { | 46 UniquePosition UniquePosition::FromProto(const sync_pb::UniquePosition& proto) { |
56 if (proto.has_custom_compressed_v1()) { | 47 if (proto.has_custom_compressed_v1()) { |
57 return UniquePosition(proto.custom_compressed_v1()); | 48 return UniquePosition(proto.custom_compressed_v1()); |
(...skipping 557 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
615 return false; | 606 return false; |
616 } | 607 } |
617 } | 608 } |
618 } | 609 } |
619 // We don't bother looking for the existence or checking the validity of | 610 // We don't bother looking for the existence or checking the validity of |
620 // any partial blocks. There's no way they could be invalid anyway. | 611 // any partial blocks. There's no way they could be invalid anyway. |
621 return true; | 612 return true; |
622 } | 613 } |
623 | 614 |
624 } // namespace syncer | 615 } // namespace syncer |
OLD | NEW |