MongoDB C++ Driver  legacy-1.0.5
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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:
62 
68  struct Hasher {
69  size_t operator() (const OID& oid) const;
70  };
71 
72  OID() : _data() {}
73 
74  enum {
75  kOIDSize = 12,
76  kTimestampSize = 4,
77  kInstanceUniqueSize = 5,
78  kIncrementSize = 3
79  };
80 
82  explicit OID(const std::string &s) {
83  init(s);
84  }
85 
87  explicit OID(const unsigned char (&arr)[kOIDSize]) {
88  std::memcpy(_data, arr, sizeof(arr));
89  }
90 
92  void clear() { std::memset(_data, 0, kOIDSize); }
93 
94  int compare( const OID& other ) const { return memcmp( _data , other._data , kOIDSize ); }
95 
97  std::string toString() const;
99  std::string toIncString() const;
100 
101  static OID MONGO_CLIENT_FUNC gen() {
102  OID o((no_initialize_tag()));
103  o.init();
104  return o;
105  }
106 
107  // Caller must ensure that the buffer is valid for kOIDSize bytes.
108  // this is templated because some places use unsigned char vs signed char
109  template<typename T>
110  static OID MONGO_CLIENT_FUNC from(T* buf) {
111  OID o((no_initialize_tag()));
112  std::memcpy(o._data, buf, OID::kOIDSize);
113  return o;
114  }
115 
116  static OID MONGO_CLIENT_FUNC max() {
117  OID o((no_initialize_tag()));
118  std::memset(o._data, 0xFF, kOIDSize);
119  return o;
120  }
121 
123  void init();
124 
126  void init( const std::string& s );
127 
129  void init( Date_t date, bool max=false );
130 
131  time_t asTimeT() const;
132  Date_t asDateT() const { return asTimeT() * 1000LL; }
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 operator<<(StringBuilder& s, const OID& o) {
197  return (s << o.toString());
198  }
199 
212  };
213 
214  MONGO_CLIENT_API inline bool MONGO_CLIENT_FUNC operator==(const OID& lhs, const OID& rhs) { return lhs.compare(rhs) == 0; }
215  MONGO_CLIENT_API inline bool MONGO_CLIENT_FUNC operator!=(const OID& lhs, const OID& rhs) { return lhs.compare(rhs) != 0; }
216  MONGO_CLIENT_API inline bool MONGO_CLIENT_FUNC operator<(const OID& lhs, const OID& rhs) { return lhs.compare(rhs) < 0; }
217  MONGO_CLIENT_API inline bool MONGO_CLIENT_FUNC operator<=(const OID& lhs, const OID& rhs) { return lhs.compare(rhs) <= 0; }
218 
219 } // namespace mongo
Functor compatible with std::hash for std::unordered_{map,set} Warning: The hash function is subject ...
Definition: oid.h:68
JsonStringFormat
Formatting mode for generating JSON from BSON.
Definition: oid.h:204
the main MongoDB namespace
Definition: bulk_operation_builder.h:24
Object ID type.
Definition: oid.h:60
Updated to a Date with value next OpTime on insert.
Definition: bsontypes.h:76
Definition: oid.h:155
strict RFC format
Definition: oid.h:206
10gen format, which is close to JS format.
Definition: oid.h:209
Definition: oid.h:160
OID(const unsigned char(&arr)[kOIDSize])
init from a reference to a 12-byte array
Definition: oid.h:87
Javascript JSON compatible.
Definition: oid.h:211
OID(const std::string &s)
init from a 24 char hex string
Definition: oid.h:82
void clear()
initialize to 'null'
Definition: oid.h:92