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