MongoDB  2.7.0
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/bson/util/misc.h"
23 #include "mongo/util/hex.h"
24 
25 namespace mongo {
26 
27 #pragma pack(1)
28 
42  class OID {
43  public:
44  OID() : a(0), b(0) { }
45 
46  enum {
47  kOIDSize = 12,
48  kIncSize = 3
49  };
50 
52  explicit OID(const std::string &s) { init(s); }
53 
55  explicit OID(const unsigned char (&arr)[kOIDSize]) {
56  memcpy(data, arr, sizeof(arr));
57  }
58 
60  void clear() { a = 0; b = 0; }
61 
62  const unsigned char *getData() const { return data; }
63 
64  bool operator==(const OID& r) const { return a==r.a && b==r.b; }
65  bool operator!=(const OID& r) const { return a!=r.a || b!=r.b; }
66  int compare( const OID& other ) const { return memcmp( data , other.data , kOIDSize ); }
67  bool operator<( const OID& other ) const { return compare( other ) < 0; }
68  bool operator<=( const OID& other ) const { return compare( other ) <= 0; }
69 
71  std::string str() const { return toHexLower(data, kOIDSize); }
72  std::string toString() const { return str(); }
74  std::string toIncString() const { return toHexLower(_inc, kIncSize); }
75 
76  static OID gen() { OID o; o.init(); return o; }
77 
79  void init();
80 
86  void initSequential();
87 
89  void init( const std::string& s );
90 
92  void init( Date_t date, bool max=false );
93 
94  time_t asTimeT();
95  Date_t asDateT() { return asTimeT() * (long long)1000; }
96 
97  bool isSet() const { return a || b; }
98 
103  void hash_combine(size_t &seed) const;
104 
106  static void justForked();
107 
108  static unsigned getMachineId(); // features command uses
109  static void regenMachineId(); // used by unit tests
110 
111  private:
112  struct MachineAndPid {
113  unsigned char _machineNumber[3];
114  unsigned short _pid;
115  bool operator!=(const OID::MachineAndPid& rhs) const;
116  };
117  static MachineAndPid ourMachine, ourMachineAndPid;
118  union {
119  struct {
120  // 12 bytes total
121  unsigned char _time[4];
122  MachineAndPid _machineAndPid;
123  unsigned char _inc[3];
124  };
125  struct {
126  long long a;
127  unsigned b;
128  };
129  struct {
130  // TODO: get rid of this eventually
131  // this is a hack because of hash_combine with older versions of boost
132  // on 32-bit platforms
133  int x;
134  int y;
135  int z;
136  };
137  unsigned char data[kOIDSize];
138  };
139 
140  static void foldInPid(MachineAndPid& x);
141  static MachineAndPid genMachineAndPid();
142  };
143 #pragma pack()
144 
145  std::ostream& operator<<( std::ostream &s, const OID &o );
146  inline StringBuilder& operator<< (StringBuilder& s, const OID& o) { return (s << o.str()); }
147 
160  };
161 
162  std::ostream& operator<<( std::ostream &s, const OID &o );
163 
164 }
void initSequential()
sets the contents to a new oid guaranteed to be sequential NOT guaranteed to be globally unique only ...
Definition: oid.cpp:124
JsonStringFormat
Formatting mode for generating JSON from BSON.
Definition: oid.h:152
strict RFC format
Definition: oid.h:154
OID(const std::string &s)
init from a 24 char hex string
Definition: oid.h:52
std::string toIncString() const
Definition: oid.h:74
OID(const unsigned char(&arr)[kOIDSize])
init from a reference to a 12-byte array
Definition: oid.h:55
void init()
sets the contents to a new oid / randomized value
Definition: oid.cpp:99
static void justForked()
call this after a fork to update the process id
Definition: oid.cpp:89
std::string str() const
Definition: oid.h:71
10gen format, which is close to JS format.
Definition: oid.h:157
void hash_combine(size_t &seed) const
this is not consistent do not store on disk
Definition: oid.cpp:36
Javascript JSON compatible.
Definition: oid.h:159
void clear()
initialize to 'null'
Definition: oid.h:60
Object ID type.
Definition: oid.h:42