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

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 algorithm Created 8 years 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
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 // By relying on the existence of unique suffixes, this class can ensure that it
akalin 2012/12/29 10:17:13 would like to have a blurb about the algebraic pro
rlarocque 2013/01/07 23:22:12 Done.
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;
32
33 static bool IsValidSuffix(const std::string& suffix);
34 static bool IsValidBytes(const std::string& bytes);
35
36 // Returns an invalid position.
37 static UniquePosition CreateInvalid();
38
39 // Converts bytes from 'ToInternalValue()' back into a UniquePosition.
40 static UniquePosition FromBytes(const std::string& bytes);
41
42 // Creates a position with the given suffix. Ordering among positions created
43 // from this function is the same as that of the integer parameters that were
44 // passed in.
45 static UniquePosition FromInt64(int64 i, const std::string& suffix);
46
47 // Returns a valid position. Its ordering is not defined.
48 static UniquePosition InitialPosition(const std::string& suffix);
49
50 // Returns positions compare smaller than, greater than, or between the input
51 // positions.
52 static UniquePosition Before(const UniquePosition& x,
53 const std::string& suffix);
54 static UniquePosition After(const UniquePosition& x,
55 const std::string& suffix);
56 static UniquePosition Between(const UniquePosition& before,
57 const UniquePosition& after,
58 const std::string& suffix);
59
60 UniquePosition();
akalin 2012/12/29 10:17:13 comment that this creates an invalid value
rlarocque 2013/01/07 23:22:12 Done.
61
62 bool LessThan(const UniquePosition& other) const;
63 bool Equals(const UniquePosition& other) const;
64
65 // Serializes the position's internal state. To be used with FromBytes().
66 const std::string& ToInternalValue() const;
67
68 // Returns a human-readable representation of this item's internal state.
69 std::string ToDebugString() const;
70
71 // Performs a lossy conversion to an int64 position. Positions converted to
72 // and from int64s using this and the FromInt64 function should maintain their
73 // relative orderings unless the int64 values conflict.
74 int64 ToInt64() const;
75
76 bool IsValid() const;
77
78 private:
79 friend class UniquePositionTest;
80
81 // Returns a string X such that (X ++ |suffix|) < |str|.
82 // |str| must be a trailing substring of a valid ordinal.
83 // |suffix| must be a valid unique suffix.
84 static std::string FindSmallerWithSuffix(const std::string& str,
85 const std::string& suffix);
86 // Returns a string X such that (X ++ |suffix|) > |str|.
87 // |str| must be a trailing substring of a valid ordinal.
88 // |suffix| must be a valid unique suffix.
89 static std::string FindGreaterWithSuffix(const std::string& str,
90 const std::string& suffix);
91 // Returns a string X such that |before| < (X ++ |suffix|) < |after|.
92 // |before| and after must be a trailing substrings of valid ordinals.
93 // |suffix| must be a valid unique suffix.
94 static std::string FindBetweenWithSuffix(const std::string& before,
95 const std::string& after,
96 const std::string& suffix);
97
98 explicit UniquePosition(const std::string& internal_rep);
99 UniquePosition(const std::string& prefix, const std::string& suffix);
100
101 std::string bytes_;
102 bool is_valid_;
103 };
104
105 } // namespace syncer;
106
107 #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') | sync/internal_api/public/base/unique_position.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698