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"
60 class MONGO_CLIENT_API
OID {
68 size_t operator()(
const OID& oid)
const;
73 enum { kOIDSize = 12, kTimestampSize = 4, kInstanceUniqueSize = 5, kIncrementSize = 3 };
76 explicit OID(
const std::string& s) {
81 explicit OID(
const unsigned char(&arr)[kOIDSize]) {
82 std::memcpy(_data, arr,
sizeof(arr));
87 std::memset(_data, 0, kOIDSize);
90 int compare(
const OID& other)
const {
91 return memcmp(_data, other._data, kOIDSize);
95 std::string toString()
const;
97 std::string toIncString()
const;
99 static OID MONGO_CLIENT_FUNC gen() {
100 OID o((no_initialize_tag()));
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);
114 static OID MONGO_CLIENT_FUNC max() {
115 OID o((no_initialize_tag()));
116 std::memset(o._data, 0xFF, kOIDSize);
124 void init(
const std::string& s);
127 void init(Date_t date,
bool max =
false);
129 time_t asTimeT()
const;
130 Date_t asDateT()
const {
131 return asTimeT() * 1000LL;
136 return compare(OID()) != 0;
143 void hash_combine(
size_t& seed)
const;
146 static void MONGO_CLIENT_FUNC justForked();
148 static unsigned MONGO_CLIENT_FUNC getMachineId();
149 static void MONGO_CLIENT_FUNC regenMachineId();
157 uint8_t bytes[kInstanceUniqueSize];
162 static Increment MONGO_CLIENT_FUNC next();
163 uint8_t bytes[kIncrementSize];
166 void setTimestamp(Timestamp timestamp);
170 Timestamp getTimestamp()
const;
186 struct no_initialize_tag {};
187 explicit OID(no_initialize_tag) {}
189 char _data[kOIDSize];
192 MONGO_CLIENT_API
inline std::ostream& MONGO_CLIENT_FUNC operator<<(std::ostream& s,
const OID& o) {
193 return (s << o.toString());
196 MONGO_CLIENT_API
inline StringBuilder& MONGO_CLIENT_FUNC
197 operator<<(StringBuilder& s,
const OID& o) {
198 return (s << o.toString());
215 MONGO_CLIENT_API
inline bool MONGO_CLIENT_FUNC operator==(
const OID& lhs,
const OID& rhs) {
216 return lhs.compare(rhs) == 0;
218 MONGO_CLIENT_API
inline bool MONGO_CLIENT_FUNC operator!=(
const OID& lhs,
const OID& rhs) {
219 return lhs.compare(rhs) != 0;
221 MONGO_CLIENT_API
inline bool MONGO_CLIENT_FUNC operator<(
const OID& lhs,
const OID& rhs) {
222 return lhs.compare(rhs) < 0;
224 MONGO_CLIENT_API
inline bool MONGO_CLIENT_FUNC operator<=(
const OID& lhs,
const OID& rhs) {
225 return lhs.compare(rhs) <= 0;
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
strict RFC format
Definition: oid.h:207
10gen format, which is close to JS format.
Definition: oid.h:210
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