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