MongoDB  2.7.0
optime.h
1 /* Copyright 2009 10gen Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <boost/thread/condition.hpp>
19 #include <iostream>
20 #include <sstream>
21 
22 #include "mongo/util/assert_util.h"
23 #include "mongo/util/concurrency/mutex.h"
24 #include "mongo/util/time_support.h"
25 
26 namespace mongo {
27 
28  struct ClockSkewException : public DBException {
29  ClockSkewException() : DBException( "clock skew exception" , 20001 ) {}
30  };
31 
32  /* replsets used to use RSOpTime.
33  M/S uses OpTime.
34  But this is useable from both.
35  */
36  typedef unsigned long long ReplTime;
37 
38  /* Operation sequence #. A combination of current second plus an ordinal value.
39  */
40 #pragma pack(4)
41  class OpTime {
42  unsigned i; // ordinal comes first so we can do a single 64 bit compare on little endian
43  unsigned secs;
44  public:
45  unsigned getSecs() const {
46  return secs;
47  }
48  unsigned getInc() const {
49  return i;
50  }
51 
52  OpTime(Date_t date) {
53  reinterpret_cast<unsigned long long&>(*this) = date.millis;
54  dassert( (int)secs >= 0 );
55  }
56  OpTime(ReplTime x) {
57  reinterpret_cast<unsigned long long&>(*this) = x;
58  dassert( (int)secs >= 0 );
59  }
60  OpTime(unsigned a, unsigned b) {
61  secs = a;
62  i = b;
63  dassert( (int)secs >= 0 );
64  }
65  OpTime( const OpTime& other ) {
66  secs = other.secs;
67  i = other.i;
68  dassert( (int)secs >= 0 );
69  }
70  OpTime() {
71  secs = 0;
72  i = 0;
73  }
74 
75  // Maximum OpTime value.
76  static OpTime max();
77 
78  /* We store OpTime's in the database as BSON Date datatype -- we needed some sort of
79  64 bit "container" for these values. While these are not really "Dates", that seems a
80  better choice for now than say, Number, which is floating point. Note the BinData type
81  is perhaps the cleanest choice, lacking a true unsigned64 datatype, but BinData has 5
82  bytes of overhead.
83  */
84  unsigned long long asDate() const {
85  return reinterpret_cast<const unsigned long long*>(&i)[0];
86  }
87  long long asLL() const {
88  return reinterpret_cast<const long long*>(&i)[0];
89  }
90 
91  bool isNull() const { return secs == 0; }
92 
93  string toStringLong() const {
94  std::stringstream ss;
95  ss << time_t_to_String_short(secs) << ' ';
96  ss << std::hex << secs << ':' << i;
97  return ss.str();
98  }
99 
100  string toStringPretty() const {
101  std::stringstream ss;
102  ss << time_t_to_String_short(secs) << ':' << std::hex << i;
103  return ss.str();
104  }
105 
106  string toString() const {
107  std::stringstream ss;
108  ss << std::hex << secs << ':' << i;
109  return ss.str();
110  }
111 
112  bool operator==(const OpTime& r) const {
113  return i == r.i && secs == r.secs;
114  }
115  bool operator!=(const OpTime& r) const {
116  return !(*this == r);
117  }
118  bool operator<(const OpTime& r) const {
119  if ( secs != r.secs )
120  return secs < r.secs;
121  return i < r.i;
122  }
123  bool operator<=(const OpTime& r) const {
124  return *this < r || *this == r;
125  }
126  bool operator>(const OpTime& r) const {
127  return !(*this <= r);
128  }
129  bool operator>=(const OpTime& r) const {
130  return !(*this < r);
131  }
132  };
133 #pragma pack()
134 
135 } // namespace mongo
Most mongo exceptions inherit from this; this is commonly caught in most threads. ...
Definition: assert_util.h:96
Definition: time_support.h:35
Definition: optime.h:41
Definition: optime.h:28