MongoDB C++ Driver  legacy-1.1.2
oid.h
1 // oid.h
2 
3 /* Copyright 2009 10gen Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include <string>
21 
22 #include "mongo/base/data_view.h"
23 #include "mongo/bson/util/builder.h"
24 #include "mongo/client/export_macros.h"
25 #include "mongo/util/time_support.h"
26 
27 namespace mongo {
28 class SecureRandom;
29 
60 class MONGO_CLIENT_API OID {
61 public:
67  struct Hasher {
68  size_t operator()(const OID& oid) const;
69  };
70 
71  OID() : _data() {}
72 
73  enum { kOIDSize = 12, kTimestampSize = 4, kInstanceUniqueSize = 5, kIncrementSize = 3 };
74 
76  explicit OID(const std::string& s) {
77  init(s);
78  }
79 
81  explicit OID(const unsigned char(&arr)[kOIDSize]) {
82  std::memcpy(_data, arr, sizeof(arr));
83  }
84 
86  void clear() {
87  std::memset(_data, 0, kOIDSize);
88  }
89 
90  int compare(const OID& other) const {
91  return memcmp(_data, other._data, kOIDSize);
92  }
93 
95  std::string toString() const;
97  std::string toIncString() const;
98 
99  static OID MONGO_CLIENT_FUNC gen() {
100  OID o((no_initialize_tag()));
101  o.init();
102  return o;
103  }
104 
105  // Caller must ensure that the buffer is valid for kOIDSize bytes.
106  // this is templated because some places use unsigned char vs signed char
107  template <typename T>
108  static OID MONGO_CLIENT_FUNC from(T* buf) {
109  OID o((no_initialize_tag()));
110  std::memcpy(o._data, buf, OID::kOIDSize);
111  return o;
112  }
113 
114  static OID MONGO_CLIENT_FUNC max() {
115  OID o((no_initialize_tag()));
116  std::memset(o._data, 0xFF, kOIDSize);
117  return o;
118  }
119 
121  void init();
122 
124  void init(const std::string& s);
125 
127  void init(Date_t date, bool max = false);
128 
129  time_t asTimeT() const;
130  Date_t asDateT() const {
131  return asTimeT() * 1000LL;
132  }
133 
134  // True iff the OID is not empty
135  bool isSet() const {
136  return compare(OID()) != 0;
137  }
138 
143  void hash_combine(size_t& seed) const;
144 
146  static void MONGO_CLIENT_FUNC justForked();
147 
148  static unsigned MONGO_CLIENT_FUNC getMachineId(); // used by the 'features' command
149  static void MONGO_CLIENT_FUNC regenMachineId();
150 
151  // Timestamp is 4 bytes so we just use int32_t
152  typedef int32_t Timestamp;
153 
154  // Wrappers so we can return stuff by value.
155  struct InstanceUnique {
156  static InstanceUnique MONGO_CLIENT_FUNC generate(SecureRandom& entropy);
157  uint8_t bytes[kInstanceUniqueSize];
158  };
159 
160  struct Increment {
161  public:
162  static Increment MONGO_CLIENT_FUNC next();
163  uint8_t bytes[kIncrementSize];
164  };
165 
166  void setTimestamp(Timestamp timestamp);
167  void setInstanceUnique(InstanceUnique unique);
168  void setIncrement(Increment inc);
169 
170  Timestamp getTimestamp() const;
171  InstanceUnique getInstanceUnique() const;
172  Increment getIncrement() const;
173 
174  ConstDataView view() const {
175  return ConstDataView(_data);
176  }
177 
178 private:
179  // Internal mutable view
180  DataView _view() {
181  return DataView(_data);
182  }
183 
184  // When we are going to immediately overwrite the bytes, there is no point in zero
185  // initializing the data first.
186  struct no_initialize_tag {};
187  explicit OID(no_initialize_tag) {}
188 
189  char _data[kOIDSize];
190 };
191 
192 MONGO_CLIENT_API inline std::ostream& MONGO_CLIENT_FUNC operator<<(std::ostream& s, const OID& o) {
193  return (s << o.toString());
194 }
195 
196 MONGO_CLIENT_API inline StringBuilder& MONGO_CLIENT_FUNC
197 operator<<(StringBuilder& s, const OID& o) {
198  return (s << o.toString());
199 }
200 
213 };
214 
215 MONGO_CLIENT_API inline bool MONGO_CLIENT_FUNC operator==(const OID& lhs, const OID& rhs) {
216  return lhs.compare(rhs) == 0;
217 }
218 MONGO_CLIENT_API inline bool MONGO_CLIENT_FUNC operator!=(const OID& lhs, const OID& rhs) {
219  return lhs.compare(rhs) != 0;
220 }
221 MONGO_CLIENT_API inline bool MONGO_CLIENT_FUNC operator<(const OID& lhs, const OID& rhs) {
222  return lhs.compare(rhs) < 0;
223 }
224 MONGO_CLIENT_API inline bool MONGO_CLIENT_FUNC operator<=(const OID& lhs, const OID& rhs) {
225  return lhs.compare(rhs) <= 0;
226 }
227 
228 } // namespace mongo
More secure random numbers Suitable for nonce/crypto Slower than PseudoRandom, so only use when reall...
Definition: random.h:80
Functor compatible with std::hash for std::unordered_{map,set} Warning: The hash function is subject ...
Definition: oid.h:67
JsonStringFormat
Formatting mode for generating JSON from BSON.
Definition: oid.h:205
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
Object ID type.
Definition: oid.h:60
Definition: data_view.h:30
Updated to a Date with value next OpTime on insert.
Definition: bsontypes.h:76
Definition: oid.h:155
strict RFC format
Definition: oid.h:207
10gen format, which is close to JS format.
Definition: oid.h:210
Definition: oid.h:160
OID(const unsigned char(&arr)[kOIDSize])
init from a reference to a 12-byte array
Definition: oid.h:81
Javascript JSON compatible.
Definition: oid.h:212
OID(const std::string &s)
init from a 24 char hex string
Definition: oid.h:76
Definition: data_view.h:71
void clear()
initialize to 'null'
Definition: oid.h:86