| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 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_SYNCABLE_SYNCABLE_ID_H_ | 5 #ifndef COMPONENTS_SYNC_SYNCABLE_SYNCABLE_ID_H_ |
| 6 #define SYNC_SYNCABLE_SYNCABLE_ID_H_ | 6 #define COMPONENTS_SYNC_SYNCABLE_SYNCABLE_ID_H_ |
| 7 | 7 |
| 8 #include <iosfwd> | 8 #include <iosfwd> |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <sstream> | 11 #include <sstream> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/containers/hash_tables.h" | 14 #include "base/containers/hash_tables.h" |
| 15 #include "sync/base/sync_export.h" | 15 #include "components/sync/base/sync_export.h" |
| 16 | 16 |
| 17 namespace base { | 17 namespace base { |
| 18 class StringValue; | 18 class StringValue; |
| 19 } | 19 } |
| 20 | 20 |
| 21 namespace sql { | 21 namespace sql { |
| 22 class Statement; | 22 class Statement; |
| 23 } | 23 } |
| 24 | 24 |
| 25 namespace syncer { | 25 namespace syncer { |
| 26 namespace syncable { | 26 namespace syncable { |
| 27 struct EntryKernel; | 27 struct EntryKernel; |
| 28 class Id; | 28 class Id; |
| 29 | 29 |
| 30 SYNC_EXPORT std::ostream& operator<<(std::ostream& out, const Id& id); | 30 SYNC_EXPORT std::ostream& operator<<(std::ostream& out, const Id& id); |
| 31 | 31 |
| 32 // For historical reasons, 3 concepts got everloaded into the Id: | 32 // For historical reasons, 3 concepts got everloaded into the Id: |
| 33 // 1. A unique, opaque identifier for the object. | 33 // 1. A unique, opaque identifier for the object. |
| 34 // 2. Flag specifing whether server know about this object. | 34 // 2. Flag specifing whether server know about this object. |
| 35 // 3. Flag for root. | 35 // 3. Flag for root. |
| 36 // | 36 // |
| 37 // We originally wrapped an integer for this information, but now we use a | 37 // We originally wrapped an integer for this information, but now we use a |
| 38 // string. It will have one of three forms: | 38 // string. It will have one of three forms: |
| 39 // 1. c<client only opaque id> for client items that have not been committed. | 39 // 1. c<client only opaque id> for client items that have not been committed. |
| 40 // 2. r for the root item. | 40 // 2. r for the root item. |
| 41 // 3. s<server provided opaque id> for items that the server knows about. | 41 // 3. s<server provided opaque id> for items that the server knows about. |
| 42 class SYNC_EXPORT Id { | 42 class SYNC_EXPORT Id { |
| 43 public: | 43 public: |
| 44 inline Id() {} | 44 inline Id() {} |
| 45 inline Id(const Id& that) { | 45 inline Id(const Id& that) { Copy(that); } |
| 46 Copy(that); | 46 inline Id& operator=(const Id& that) { |
| 47 } | |
| 48 inline Id& operator = (const Id& that) { | |
| 49 Copy(that); | 47 Copy(that); |
| 50 return *this; | 48 return *this; |
| 51 } | 49 } |
| 52 inline void Copy(const Id& that) { | 50 inline void Copy(const Id& that) { this->s_ = that.s_; } |
| 53 this->s_ = that.s_; | 51 inline bool IsRoot() const { return "r" == s_; } |
| 54 } | |
| 55 inline bool IsRoot() const { | |
| 56 return "r" == s_; | |
| 57 } | |
| 58 inline bool ServerKnows() const { | 52 inline bool ServerKnows() const { |
| 59 return !IsNull() && (s_[0] == 's' || s_ == "r"); | 53 return !IsNull() && (s_[0] == 's' || s_ == "r"); |
| 60 } | 54 } |
| 61 | 55 |
| 62 inline bool IsNull() const { return s_.empty(); } | 56 inline bool IsNull() const { return s_.empty(); } |
| 63 inline void Clear() { s_.clear(); } | 57 inline void Clear() { s_.clear(); } |
| 64 inline int compare(const Id& that) const { | 58 inline int compare(const Id& that) const { return s_.compare(that.s_); } |
| 65 return s_.compare(that.s_); | 59 inline bool operator==(const Id& that) const { return s_ == that.s_; } |
| 66 } | 60 inline bool operator!=(const Id& that) const { return s_ != that.s_; } |
| 67 inline bool operator == (const Id& that) const { | 61 inline bool operator<(const Id& that) const { return s_ < that.s_; } |
| 68 return s_ == that.s_; | 62 inline bool operator>(const Id& that) const { return s_ > that.s_; } |
| 69 } | |
| 70 inline bool operator != (const Id& that) const { | |
| 71 return s_ != that.s_; | |
| 72 } | |
| 73 inline bool operator < (const Id& that) const { | |
| 74 return s_ < that.s_; | |
| 75 } | |
| 76 inline bool operator > (const Id& that) const { | |
| 77 return s_ > that.s_; | |
| 78 } | |
| 79 | 63 |
| 80 const std::string& value() const { | 64 const std::string& value() const { return s_; } |
| 81 return s_; | |
| 82 } | |
| 83 | 65 |
| 84 // Return the next highest ID in the lexicographic ordering. This is | 66 // Return the next highest ID in the lexicographic ordering. This is |
| 85 // useful for computing upper bounds on std::sets that are ordered | 67 // useful for computing upper bounds on std::sets that are ordered |
| 86 // by operator<. | 68 // by operator<. |
| 87 Id GetLexicographicSuccessor() const; | 69 Id GetLexicographicSuccessor() const; |
| 88 | 70 |
| 89 // Dumps the ID as a value and returns it. Transfers ownership of | 71 // Dumps the ID as a value and returns it. Transfers ownership of |
| 90 // the StringValue to the caller. | 72 // the StringValue to the caller. |
| 91 base::StringValue* ToValue() const; | 73 base::StringValue* ToValue() const; |
| 92 | 74 |
| 93 // Three functions are used to work with our proto buffers. | 75 // Three functions are used to work with our proto buffers. |
| 94 std::string GetServerId() const; | 76 std::string GetServerId() const; |
| 95 static Id CreateFromServerId(const std::string& server_id); | 77 static Id CreateFromServerId(const std::string& server_id); |
| 96 // This should only be used if you get back a reference to a local | 78 // This should only be used if you get back a reference to a local |
| 97 // id from the server. Returns a client only opaque id. | 79 // id from the server. Returns a client only opaque id. |
| 98 static Id CreateFromClientString(const std::string& local_id); | 80 static Id CreateFromClientString(const std::string& local_id); |
| 99 | 81 |
| 100 // This method returns an ID that will compare less than any valid ID. | 82 // This method returns an ID that will compare less than any valid ID. |
| 101 // The returned ID is not a valid ID itself. This is useful for | 83 // The returned ID is not a valid ID itself. This is useful for |
| 102 // computing lower bounds on std::sets that are ordered by operator<. | 84 // computing lower bounds on std::sets that are ordered by operator<. |
| 103 static Id GetLeastIdForLexicographicComparison(); | 85 static Id GetLeastIdForLexicographicComparison(); |
| 104 | 86 |
| 105 // Gets root ID. | 87 // Gets root ID. |
| 106 static Id GetRoot(); | 88 static Id GetRoot(); |
| 107 | 89 |
| 108 private: | 90 private: |
| 109 friend std::unique_ptr<EntryKernel> UnpackEntry(sql::Statement* statement, | 91 friend std::unique_ptr<EntryKernel> UnpackEntry(sql::Statement* statement, |
| 110 int* total_created_entries); | 92 int* total_created_entries); |
| 111 friend void BindFields(const EntryKernel& entry, | 93 friend void BindFields(const EntryKernel& entry, sql::Statement* statement); |
| 112 sql::Statement* statement); | |
| 113 SYNC_EXPORT friend std::ostream& operator<<(std::ostream& out, const Id& id); | 94 SYNC_EXPORT friend std::ostream& operator<<(std::ostream& out, const Id& id); |
| 114 friend class SyncableIdTest; | 95 friend class SyncableIdTest; |
| 115 | 96 |
| 116 std::string s_; | 97 std::string s_; |
| 117 }; | 98 }; |
| 118 | 99 |
| 119 } // namespace syncable | 100 } // namespace syncable |
| 120 } // namespace syncer | 101 } // namespace syncer |
| 121 | 102 |
| 122 #endif // SYNC_SYNCABLE_SYNCABLE_ID_H_ | 103 #endif // COMPONENTS_SYNC_SYNCABLE_SYNCABLE_ID_H_ |
| OLD | NEW |