| 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 #ifndef SYNC_INTERNAL_API_PUBLIC_BASE_UNIQUE_POSITION_H_ | 5 #ifndef SYNC_INTERNAL_API_PUBLIC_BASE_UNIQUE_POSITION_H_ |
| 6 #define SYNC_INTERNAL_API_PUBLIC_BASE_UNIQUE_POSITION_H_ | 6 #define SYNC_INTERNAL_API_PUBLIC_BASE_UNIQUE_POSITION_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 // | 21 // |
| 22 // Note that these unique suffixes must be exactly |kSuffixLength| bytes long. | 22 // Note that these unique suffixes must be exactly |kSuffixLength| bytes long. |
| 23 // | 23 // |
| 24 // The cost for all these features is potentially unbounded space usage. In | 24 // The cost for all these features is potentially unbounded space usage. In |
| 25 // practice, however, most ordinals should be not much longer than the suffix. | 25 // practice, however, most ordinals should be not much longer than the suffix. |
| 26 // | 26 // |
| 27 // This class currently has several bookmarks-related assumptions built in, | 27 // This class currently has several bookmarks-related assumptions built in, |
| 28 // though it could be adapted to be more generally useful. | 28 // though it could be adapted to be more generally useful. |
| 29 class UniquePosition { | 29 class UniquePosition { |
| 30 public: | 30 public: |
| 31 static const size_t kSuffixLength = (64 + 128) / 8; | 31 static const size_t kSuffixLength; |
| 32 static const char kTerminatorByte = kuint8max; | |
| 33 | 32 |
| 34 static bool IsValidSuffix(const std::string& suffix); | 33 static bool IsValidSuffix(const std::string& suffix); |
| 35 static bool IsValidBytes(const std::string& bytes); | 34 static bool IsValidBytes(const std::string& bytes); |
| 36 | 35 |
| 37 // Returns an invalid position. | 36 // Returns an invalid position. |
| 38 static UniquePosition CreateInvalid(); | 37 static UniquePosition CreateInvalid(); |
| 39 | 38 |
| 40 // Converts bytes from 'ToInternalValue()' back into a UniquePosition. | 39 // Converts bytes from 'ToInternalValue()' back into a UniquePosition. |
| 41 static UniquePosition FromBytes(const std::string& bytes); | 40 static UniquePosition FromBytes(const std::string& bytes); |
| 42 | 41 |
| 43 // Creates a position with the given suffix. Ordering among positions created | 42 // Creates a position with the given suffix. Ordering among positions created |
| 44 // from this function is the same as that of the integer parameters that were | 43 // from this function is the same as that of the integer parameters that were |
| 45 // passed in. | 44 // passed in. |
| 46 static UniquePosition FromInt64(int64 i, const std::string& suffix); | 45 static UniquePosition FromInt64(int64 i, const std::string& suffix); |
| 47 | 46 |
| 48 // Returns a valid position. Its ordering is not defined. | 47 // Returns a valid position. Its ordering is not defined. |
| 49 static UniquePosition InitialPosition(const std::string& suffix); | 48 static UniquePosition InitialPosition(const std::string& suffix); |
| 50 | 49 |
| 51 // Returns positions compare smaller than, greater than, or between the input | 50 // Returns positions compare smaller than, greater than, or between the input |
| 52 // positions. | 51 // positions. |
| 53 static UniquePosition Before(const UniquePosition& x, | 52 static UniquePosition Before(const UniquePosition& x, |
| 54 const std::string& suffix); | 53 const std::string& suffix); |
| 55 static UniquePosition After(const UniquePosition& x, | 54 static UniquePosition After(const UniquePosition& x, |
| 56 const std::string& suffix); | 55 const std::string& suffix); |
| 57 static UniquePosition Between(const UniquePosition& before, | 56 static UniquePosition Between(const UniquePosition& before, |
| 58 const UniquePosition& after, | 57 const UniquePosition& after, |
| 59 const std::string& suffix); | 58 const std::string& suffix); |
| 60 | 59 |
| 61 // Create a random suffix. Should be used only as a last resort. | |
| 62 static const std::string GenerateUniqueSuffix(); | |
| 63 | |
| 64 // Create a unique suffix based on the input parameters. The parameters are | |
| 65 // sufficient to uniquely identify any bookmark within the database. | |
| 66 static const std::string GenerateBookmarkSuffix( | |
| 67 const std::string& decoded_originator_cache_guid, | |
| 68 int64 numeric_originator_item_id); | |
| 69 | |
| 70 UniquePosition(); | 60 UniquePosition(); |
| 71 | 61 |
| 72 bool LessThan(const UniquePosition& other) const; | 62 bool LessThan(const UniquePosition& other) const; |
| 73 bool Equals(const UniquePosition& other) const; | 63 bool Equals(const UniquePosition& other) const; |
| 74 | 64 |
| 75 // Serializes the position's internal state. To be used with FromBytes(). | 65 // Serializes the position's internal state. To be used with FromBytes(). |
| 76 const std::string& ToInternalValue() const; | 66 const std::string& ToInternalValue() const; |
| 77 | 67 |
| 78 // Returns a human-readable representation of this item's internal state. | 68 // Returns a human-readable representation of this item's internal state. |
| 79 std::string ToDebugString() const; | 69 std::string ToDebugString() const; |
| 80 | 70 |
| 81 // Performs a lossy conversion to an int64 position. Positions converted to | 71 // Performs a lossy conversion to an int64 position. Positions converted to |
| 82 // and from int64s using this and the FromInt64 function should maintain their | 72 // and from int64s using this and the FromInt64 function should maintain their |
| 83 // relative orderings unless the int64 values conflict. | 73 // relative orderings unless the int64 values conflict. |
| 84 int64 ToInt64() const; | 74 int64 ToInt64() const; |
| 85 | 75 |
| 86 bool IsValid() const; | 76 bool IsValid() const; |
| 87 | 77 |
| 88 private: | 78 private: |
| 89 friend class UniquePositionTest; | 79 friend class UniquePositionTest; |
| 90 | 80 |
| 91 // Returns a string X such that (X ++ |suffix| ++ |kTerminatorByte|) < |str|. | 81 // Returns a string X such that (X ++ |suffix|) < |str|. |
| 92 // |str| must be a trailing substring of a valid ordinal. | 82 // |str| must be a trailing substring of a valid ordinal. |
| 93 // |suffix| must be a valid unique suffix. | 83 // |suffix| must be a valid unique suffix. |
| 94 static std::string FindSmallerWithSuffix(const std::string& str, | 84 static std::string FindSmallerWithSuffix(const std::string& str, |
| 95 const std::string& suffix); | 85 const std::string& suffix); |
| 96 // Returns a string X such that (X ++ |suffix| ++ |kTerminatorByte|) > |str|. | 86 // Returns a string X such that (X ++ |suffix|) > |str|. |
| 97 // |str| must be a trailing substring of a valid ordinal. | 87 // |str| must be a trailing substring of a valid ordinal. |
| 98 // |suffix| must be a valid unique suffix. | 88 // |suffix| must be a valid unique suffix. |
| 99 static std::string FindGreaterWithSuffix(const std::string& str, | 89 static std::string FindGreaterWithSuffix(const std::string& str, |
| 100 const std::string& suffix); | 90 const std::string& suffix); |
| 101 // Returns a string X such that | 91 // Returns a string X such that |before| < (X ++ |suffix|) < |after|. |
| 102 // |before| < (X ++ |suffix| ++ |kTerminatorByte|) < |after|. | |
| 103 // |before| and after must be a trailing substrings of valid ordinals. | 92 // |before| and after must be a trailing substrings of valid ordinals. |
| 104 // |suffix| must be a valid unique suffix. | 93 // |suffix| must be a valid unique suffix. |
| 105 static std::string FindBetweenWithSuffix(const std::string& before, | 94 static std::string FindBetweenWithSuffix(const std::string& before, |
| 106 const std::string& after, | 95 const std::string& after, |
| 107 const std::string& suffix); | 96 const std::string& suffix); |
| 108 | 97 |
| 109 explicit UniquePosition(const std::string& internal_rep); | 98 explicit UniquePosition(const std::string& internal_rep); |
| 110 UniquePosition(const std::string& prefix, const std::string& suffix); | 99 UniquePosition(const std::string& prefix, const std::string& suffix); |
| 111 | 100 |
| 112 std::string bytes_; | 101 std::string bytes_; |
| 113 bool is_valid_; | 102 bool is_valid_; |
| 114 }; | 103 }; |
| 115 | 104 |
| 116 } // namespace syncer; | 105 } // namespace syncer; |
| 117 | 106 |
| 118 #endif // SYNC_INTERNAL_API_PUBLIC_BASE_UNIQUE_POSITION_H_ | 107 #endif // SYNC_INTERNAL_API_PUBLIC_BASE_UNIQUE_POSITION_H_ |
| OLD | NEW |