00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #pragma once
00019
00020 #include "../util/hex.h"
00021
00022 namespace mongo {
00023
00024 #pragma pack(1)
00025
00039 class OID {
00040 public:
00041 OID() : a(0), b(0) { }
00042
00044 explicit OID(const string &s) { init(s); }
00045
00047 void clear() { a = 0; b = 0; }
00048
00049 const unsigned char *getData() const { return data; }
00050
00051 bool operator==(const OID& r) const { return a==r.a && b==r.b; }
00052 bool operator!=(const OID& r) const { return a!=r.a || b!=r.b; }
00053 int compare( const OID& other ) const { return memcmp( data , other.data , 12 ); }
00054 bool operator<( const OID& other ) const { return compare( other ) < 0; }
00055 bool operator<=( const OID& other ) const { return compare( other ) <= 0; }
00056
00058 string str() const { return toHexLower(data, 12); }
00059 string toString() const { return str(); }
00060
00061 static OID gen() { OID o; o.init(); return o; }
00062
00064 void init();
00065
00067 void init( string s );
00068
00070 void init( Date_t date, bool max=false );
00071
00072 time_t asTimeT();
00073 Date_t asDateT() { return asTimeT() * (long long)1000; }
00074
00075 bool isSet() const { return a || b; }
00076
00078 static void justForked();
00079
00080 static unsigned getMachineId();
00081 static void regenMachineId();
00082
00083 private:
00084 struct MachineAndPid {
00085 unsigned char _machineNumber[3];
00086 unsigned short _pid;
00087 bool operator!=(const OID::MachineAndPid& rhs) const;
00088 };
00089 static MachineAndPid ourMachine, ourMachineAndPid;
00090 union {
00091 struct {
00092
00093 unsigned char _time[4];
00094 MachineAndPid _machineAndPid;
00095 unsigned char _inc[3];
00096 };
00097 struct {
00098 long long a;
00099 unsigned b;
00100 };
00101 unsigned char data[12];
00102 };
00103
00104 static unsigned ourPid();
00105 static void foldInPid(MachineAndPid& x);
00106 static MachineAndPid genMachineAndPid();
00107 };
00108 #pragma pack()
00109
00110 ostream& operator<<( ostream &s, const OID &o );
00111 inline StringBuilder& operator<< (StringBuilder& s, const OID& o) { return (s << o.str()); }
00112
00117 enum JsonStringFormat {
00119 Strict,
00122 TenGen,
00124 JS
00125 };
00126
00127 inline ostream& operator<<( ostream &s, const OID &o ) {
00128 s << o.str();
00129 return s;
00130 }
00131
00132 }