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

Side by Side Diff: sync/internal_api/public/base/unique_position.h

Issue 11569045: Initial UniquePositions implementation (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Update class comments Created 7 years, 11 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | sync/internal_api/public/base/unique_position.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 // Valid UniquePosition objects have the following properties:
17 //
18 // - a < b and b < c implies a < c (transitivity);
19 // - exactly one of a < b, b < a and a = b holds (trichotomy);
20 // - if a < b, there is a UniquePosition such that a < x < b (density);
21 // - there are UniquePositions x and y such that x < a < y (unboundedness);
22 // - if a and b were constructed with different unique suffixes, then a != b.
23 //
24 // As long as all UniquePositions used to sort a list were created with unique
25 // suffixes, then if any item changes its position in the list, only its
26 // UniquePosition value has to change to represent the new order, and all other
27 // values can stay the same.
28 //
29 // Note that the unique suffixes must be exactly |kSuffixLength| bytes long.
30 //
31 // The cost for all these features is potentially unbounded space usage. In
32 // practice, however, most ordinals should be not much longer than the suffix.
33 //
34 // This class currently has several bookmarks-related assumptions built in,
35 // though it could be adapted to be more generally useful.
36 class UniquePosition {
37 public:
38 static const size_t kSuffixLength;
39
40 static bool IsValidSuffix(const std::string& suffix);
41 static bool IsValidBytes(const std::string& bytes);
42
43 // Returns an invalid position.
44 static UniquePosition CreateInvalid();
45
46 // Converts bytes from 'ToInternalValue()' back into a UniquePosition.
47 static UniquePosition FromBytes(const std::string& bytes);
48
49 // Creates a position with the given suffix. Ordering among positions created
50 // from this function is the same as that of the integer parameters that were
51 // passed in.
52 static UniquePosition FromInt64(int64 i, const std::string& suffix);
53
54 // Returns a valid position. Its ordering is not defined.
55 static UniquePosition InitialPosition(const std::string& suffix);
56
57 // Returns positions compare smaller than, greater than, or between the input
58 // positions.
59 static UniquePosition Before(const UniquePosition& x,
60 const std::string& suffix);
61 static UniquePosition After(const UniquePosition& x,
62 const std::string& suffix);
63 static UniquePosition Between(const UniquePosition& before,
64 const UniquePosition& after,
65 const std::string& suffix);
66
67 // This constructor creates an invalid value.
68 UniquePosition();
69
70 bool LessThan(const UniquePosition& other) const;
71 bool Equals(const UniquePosition& other) const;
72
73 // Serializes the position's internal state. To be used with FromBytes().
74 const std::string& ToInternalValue() const;
75
76 // Returns a human-readable representation of this item's internal state.
77 std::string ToDebugString() const;
78
79 // Performs a lossy conversion to an int64 position. Positions converted to
80 // and from int64s using this and the FromInt64 function should maintain their
81 // relative orderings unless the int64 values conflict.
82 int64 ToInt64() const;
83
84 bool IsValid() const;
85
86 private:
87 friend class UniquePositionTest;
88
89 // Returns a string X such that (X ++ |suffix|) < |str|.
90 // |str| must be a trailing substring of a valid ordinal.
91 // |suffix| must be a valid unique suffix.
92 static std::string FindSmallerWithSuffix(const std::string& str,
93 const std::string& suffix);
94 // Returns a string X such that (X ++ |suffix|) > |str|.
95 // |str| must be a trailing substring of a valid ordinal.
96 // |suffix| must be a valid unique suffix.
97 static std::string FindGreaterWithSuffix(const std::string& str,
98 const std::string& suffix);
99 // Returns a string X such that |before| < (X ++ |suffix|) < |after|.
100 // |before| and after must be a trailing substrings of valid ordinals.
101 // |suffix| must be a valid unique suffix.
102 static std::string FindBetweenWithSuffix(const std::string& before,
103 const std::string& after,
104 const std::string& suffix);
105
106 explicit UniquePosition(const std::string& internal_rep);
107 UniquePosition(const std::string& prefix, const std::string& suffix);
108
109 std::string bytes_;
110 bool is_valid_;
111 };
112
113 } // namespace syncer;
114
115 #endif // SYNC_INTERNAL_API_PUBLIC_BASE_UNIQUE_POSITION_H_
OLDNEW
« no previous file with comments | « no previous file | sync/internal_api/public/base/unique_position.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698