[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / sync / syncable / syncable_id.h
blobbbfeb9325d7e09893e71a29380729376c2e6ddda
1 // Copyright 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.
5 #ifndef SYNC_SYNCABLE_SYNCABLE_ID_H_
6 #define SYNC_SYNCABLE_SYNCABLE_ID_H_
8 #include <iosfwd>
9 #include <limits>
10 #include <sstream>
11 #include <string>
13 #include "base/containers/hash_tables.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "sync/base/sync_export.h"
17 class MockConnectionManager;
19 namespace base {
20 class StringValue;
23 namespace sql {
24 class Statement;
27 namespace syncer {
28 namespace syncable {
29 struct EntryKernel;
30 class Id;
32 SYNC_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& out, const Id& id);
34 // For historical reasons, 3 concepts got everloaded into the Id:
35 // 1. A unique, opaque identifier for the object.
36 // 2. Flag specifing whether server know about this object.
37 // 3. Flag for root.
39 // We originally wrapped an integer for this information, but now we use a
40 // string. It will have one of three forms:
41 // 1. c<client only opaque id> for client items that have not been committed.
42 // 2. r for the root item.
43 // 3. s<server provided opaque id> for items that the server knows about.
44 class SYNC_EXPORT Id {
45 public:
46 // This constructor will be handy even when we move away from int64s, just
47 // for unit tests.
48 inline Id() : s_("r") { }
49 inline Id(const Id& that) {
50 Copy(that);
52 inline Id& operator = (const Id& that) {
53 Copy(that);
54 return *this;
56 inline void Copy(const Id& that) {
57 this->s_ = that.s_;
59 inline bool IsRoot() const {
60 return "r" == s_;
62 inline bool ServerKnows() const {
63 return s_[0] == 's' || s_ == "r";
66 // TODO(sync): We could use null here, but to ease conversion we use "r".
67 // fix this, this is madness :)
68 inline bool IsNull() const {
69 return IsRoot();
71 inline void Clear() {
72 s_ = "r";
74 inline int compare(const Id& that) const {
75 return s_.compare(that.s_);
77 inline bool operator == (const Id& that) const {
78 return s_ == that.s_;
80 inline bool operator != (const Id& that) const {
81 return s_ != that.s_;
83 inline bool operator < (const Id& that) const {
84 return s_ < that.s_;
86 inline bool operator > (const Id& that) const {
87 return s_ > that.s_;
90 const std::string& value() const {
91 return s_;
94 // Return the next highest ID in the lexicographic ordering. This is
95 // useful for computing upper bounds on std::sets that are ordered
96 // by operator<.
97 Id GetLexicographicSuccessor() const;
99 // Dumps the ID as a value and returns it. Transfers ownership of
100 // the StringValue to the caller.
101 base::StringValue* ToValue() const;
103 // Three functions are used to work with our proto buffers.
104 std::string GetServerId() const;
105 static Id CreateFromServerId(const std::string& server_id);
106 // This should only be used if you get back a reference to a local
107 // id from the server. Returns a client only opaque id.
108 static Id CreateFromClientString(const std::string& local_id);
110 // This method returns an ID that will compare less than any valid ID.
111 // The returned ID is not a valid ID itself. This is useful for
112 // computing lower bounds on std::sets that are ordered by operator<.
113 static Id GetLeastIdForLexicographicComparison();
115 private:
116 friend scoped_ptr<EntryKernel> UnpackEntry(sql::Statement* statement);
117 friend void BindFields(const EntryKernel& entry,
118 sql::Statement* statement);
119 SYNC_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& out,
120 const Id& id);
121 friend class MockConnectionManager;
122 friend class SyncableIdTest;
124 std::string s_;
127 SYNC_EXPORT_PRIVATE Id GetNullId();
129 } // namespace syncable
130 } // namespace syncer
132 #endif // SYNC_SYNCABLE_SYNCABLE_ID_H_