MongoDB C++ Driver
legacy-1.1.2
|
Object ID type. More...
#include <oid.h>
Classes | |
struct | Hasher |
Functor compatible with std::hash for std::unordered_{map,set} Warning: The hash function is subject to change. More... | |
struct | Increment |
struct | InstanceUnique |
Public Member Functions | |
OID (const std::string &s) | |
init from a 24 char hex string | |
OID (const unsigned char(&arr)[kOIDSize]) | |
init from a reference to a 12-byte array | |
void | clear () |
initialize to 'null' | |
std::string | toString () const |
std::string | toIncString () const |
void | init () |
sets the contents to a new oid / randomized value | |
void | init (const std::string &s) |
init from a 24 char hex string | |
void | init (Date_t date, bool max=false) |
Set to the min/max OID that could be generated at given timestamp. More... | |
void | hash_combine (size_t &seed) const |
this is not consistent do not store on disk | |
Static Public Member Functions | |
static void MONGO_CLIENT_FUNC | justForked () |
call this after a fork to update the process id | |
Object ID type.
BSON objects typically have an _id field for the object id. This field should be the first member of the object when present. The OID class is a special type that is a 12 byte id which is likely to be unique to the system. You may also use other types for _id's. When _id field is missing from a BSON object, on an insert the database may insert one automatically in certain circumstances.
The BSON ObjectID is a 12-byte value consisting of a 4-byte timestamp (seconds since epoch), in the highest order 4 bytes followed by a 5 byte value unique to this machine AND process, followed by a 3 byte counter.
4 byte timestamp 5 byte process unique 3 byte counter |<----------------->|<---------------------->|<------------->
OID layout: [-—|-—|-—|-—|-—|-—|-—|-—|-—|-—|-—|-—] 0 4 8 12
The timestamp is a big endian 4 byte signed-integer.
The process unique is an arbitrary sequence of 5 bytes. There are no endianness concerns since it is never interpreted as a multi-byte value.
The counter is a big endian 3 byte unsigned integer.
Note: The timestamp and counter are big endian (in contrast to the rest of BSON) because we use memcmp to order OIDs, and we want to ensure an increasing order.
Warning: You MUST call OID::justForked() after a fork(). This ensures that each process will generate unique OIDs.
void mongo::OID::init | ( | Date_t | date, |
bool | max = false |
||
) |
Set to the min/max OID that could be generated at given timestamp.
std::string mongo::OID::toIncString | ( | ) | const |
std::string mongo::OID::toString | ( | ) | const |