OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef SYNC_INTERNAL_API_PUBLIC_BASE_UNIQUE_POSITION_H_ |
| 6 #define SYNC_INTERNAL_API_PUBLIC_BASE_UNIQUE_POSITION_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 |
| 12 namespace syncer { |
| 13 |
| 14 // A class to represent positions. |
| 15 // |
| 16 // By relying on the existence of unique suffixes, this class can ensure that it |
| 17 // is always possible to create a new position between any two existing |
| 18 // positions. It can also create new positions before or after any other |
| 19 // position, which makes it easier to support insertion at the head or tail of a |
| 20 // list. |
| 21 // |
| 22 // Note that these unique suffixes must be exactly |kSuffixLength| bytes long. |
| 23 // |
| 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. |
| 26 // |
| 27 // This class currently has several bookmarks-related assumptions built in, |
| 28 // though it could be adapted to be more generally useful. |
| 29 class UniquePosition { |
| 30 public: |
| 31 static const size_t kSuffixLength = (64 + 128) / 8; |
| 32 static const char kTerminatorByte = kuint8max; |
| 33 |
| 34 static bool IsValidSuffix(const std::string& suffix); |
| 35 static bool IsValidBytes(const std::string& bytes); |
| 36 |
| 37 // Returns an invalid position. |
| 38 static UniquePosition CreateInvalid(); |
| 39 |
| 40 // Converts bytes from 'ToInternalValue()' back into a UniquePosition. |
| 41 static UniquePosition FromBytes(const std::string& bytes); |
| 42 |
| 43 // 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 |
| 45 // passed in. |
| 46 static UniquePosition FromInt64(int64 i, const std::string& suffix); |
| 47 |
| 48 // Returns a valid position. Its ordering is not defined. |
| 49 static UniquePosition InitialPosition(const std::string& suffix); |
| 50 |
| 51 // Returns positions compare smaller than, greater than, or between the input |
| 52 // positions. |
| 53 static UniquePosition Before(const UniquePosition& x, |
| 54 const std::string& suffix); |
| 55 static UniquePosition After(const UniquePosition& x, |
| 56 const std::string& suffix); |
| 57 static UniquePosition Between(const UniquePosition& before, |
| 58 const UniquePosition& after, |
| 59 const std::string& suffix); |
| 60 |
| 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(); |
| 71 |
| 72 bool LessThan(const UniquePosition& other) const; |
| 73 bool Equals(const UniquePosition& other) const; |
| 74 |
| 75 // Serializes the position's internal state. To be used with FromBytes(). |
| 76 const std::string& ToInternalValue() const; |
| 77 |
| 78 // Returns a human-readable representation of this item's internal state. |
| 79 std::string ToDebugString() const; |
| 80 |
| 81 // Performs a lossy conversion to an int64 position. Positions converted to |
| 82 // and from int64s using this and the FromInt64 function should maintain their |
| 83 // relative orderings unless the int64 values conflict. |
| 84 int64 ToInt64() const; |
| 85 |
| 86 bool IsValid() const; |
| 87 |
| 88 private: |
| 89 friend class UniquePositionTest; |
| 90 |
| 91 // Returns a string X such that (X ++ |suffix| ++ |kTerminatorByte|) < |str|. |
| 92 // |str| must be a trailing substring of a valid ordinal. |
| 93 // |suffix| must be a valid unique suffix. |
| 94 static std::string FindSmallerWithSuffix(const std::string& str, |
| 95 const std::string& suffix); |
| 96 // Returns a string X such that (X ++ |suffix| ++ |kTerminatorByte|) > |str|. |
| 97 // |str| must be a trailing substring of a valid ordinal. |
| 98 // |suffix| must be a valid unique suffix. |
| 99 static std::string FindGreaterWithSuffix(const std::string& str, |
| 100 const std::string& suffix); |
| 101 // Returns a string X such that |
| 102 // |before| < (X ++ |suffix| ++ |kTerminatorByte|) < |after|. |
| 103 // |before| and after must be a trailing substrings of valid ordinals. |
| 104 // |suffix| must be a valid unique suffix. |
| 105 static std::string FindBetweenWithSuffix(const std::string& before, |
| 106 const std::string& after, |
| 107 const std::string& suffix); |
| 108 |
| 109 explicit UniquePosition(const std::string& internal_rep); |
| 110 UniquePosition(const std::string& prefix, const std::string& suffix); |
| 111 |
| 112 std::string bytes_; |
| 113 bool is_valid_; |
| 114 }; |
| 115 |
| 116 } // namespace syncer; |
| 117 |
| 118 #endif // SYNC_INTERNAL_API_PUBLIC_BASE_UNIQUE_POSITION_H_ |
OLD | NEW |