libfoedus-core
FOEDUS Core Library
epoch.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015, Hewlett-Packard Development Company, LP.
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11  * more details. You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  * HP designates this particular file as subject to the "Classpath" exception
16  * as provided by HP in the LICENSE.txt file that accompanied this code.
17  */
18 #ifndef FOEDUS_EPOCH_HPP_
19 #define FOEDUS_EPOCH_HPP_
20 
21 #include <stdint.h>
22 
23 #include <iosfwd>
24 
25 #include "foedus/assert_nd.hpp"
26 #include "foedus/cxx11.hpp"
27 
28 namespace foedus {
61 class Epoch {
62  public:
64  typedef uint32_t EpochInteger;
66  enum Constants {
79  kEpochBits = 28,
83  kEpochIntHalf = (1 << (kEpochBits - 1)),
84  };
85 
89  explicit Epoch(EpochInteger value) CXX11_NOEXCEPT : epoch_(value) {
91  }
92  // default copy-constructor/assignment/destructor suffice
93 
95  void reset() { epoch_ = kEpochInvalid; }
96  bool is_valid() const {
97  ASSERT_ND(epoch_ < kEpochIntOverflow);
98  return epoch_ != kEpochInvalid;
99  }
100 
102  EpochInteger value() const { return epoch_; }
103 
105  ASSERT_ND(is_valid()); // we prohibit increment from invalid epoch
106  if (epoch_ == kEpochIntOverflow - 1) {
107  epoch_ = 1; // skip 0, which is always an invalid epoch.
108  } else {
109  ++epoch_;
110  }
111  return *this;
112  }
114  ASSERT_ND(is_valid()); // we prohibit decrement from invalid epoch
115  if (epoch_ == 1) {
116  epoch_ = kEpochIntOverflow - 1; // skip 0, which is always an invalid epoch.
117  } else {
118  --epoch_;
119  }
120  return *this;
121  }
122  Epoch one_less() const {
123  Epoch tmp(epoch_);
124  tmp.operator--();
125  return tmp;
126  }
127  Epoch one_more() const {
128  Epoch tmp(epoch_);
129  tmp.operator++();
130  return tmp;
131  }
132 
137  uint32_t subtract(const Epoch& other) const {
138  ASSERT_ND(is_valid());
139  if (epoch_ >= other.epoch_) {
140  return epoch_ - other.epoch_;
141  } else {
142  // wrap around
143  return epoch_ + kEpochIntOverflow - other.epoch_;
144  }
145  }
146 
153  void store_min(const Epoch& other) {
154  ASSERT_ND(other.is_valid());
155  if (!is_valid() || other < *this) {
156  epoch_ = other.epoch_;
157  }
158  }
165  void store_max(const Epoch& other) {
166  ASSERT_ND(other.is_valid());
167  if (!is_valid() || other > *this) {
168  epoch_ = other.epoch_;
169  }
170  }
171 
176  bool before(const Epoch &other) const {
177  ASSERT_ND(is_valid());
178  ASSERT_ND(other.is_valid());
179  int64_t diff = static_cast<int64_t>(other.epoch_) - static_cast<int64_t>(epoch_);
180  return diff <= -kEpochIntHalf || (diff > 0 && diff < kEpochIntHalf);
181  }
182 
183  bool operator==(const Epoch &other) const { return epoch_ == other.epoch_; }
184  bool operator!=(const Epoch &other) const { return epoch_ != other.epoch_; }
185  bool operator<(const Epoch &other) const { return before(other); }
186  bool operator>(const Epoch &other) const { return other.before(*this); }
187  bool operator<=(const Epoch &other) const { return !operator>(other); }
188  bool operator>=(const Epoch &other) const { return !operator<(other); }
189 
190  friend std::ostream& operator<<(std::ostream& o, const Epoch& v);
191 
192  private:
197  EpochInteger epoch_;
198 };
199 
205 } // namespace foedus
206 
207 #endif // FOEDUS_EPOCH_HPP_
Epoch one_less() const
Definition: epoch.hpp:122
Constants
Defines constant values.
Definition: epoch.hpp:66
bool operator!=(const Epoch &other) const
Definition: epoch.hpp:184
The first epoch (before wrap-around) that might have transactions is ep-3.
Definition: epoch.hpp:77
Bits to represent an epoch.
Definition: epoch.hpp:79
Usually, current epoch -1 is the grace period before being durable.
Definition: epoch.hpp:75
Epoch values wrap around at this value.
Definition: epoch.hpp:81
uint32_t subtract(const Epoch &other) const
Returns the number epochs from the given epoch to this epoch accounting for wrap-around.
Definition: epoch.hpp:137
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
void reset()
Clears this epoch variable so that it points to an invalid epoch.
Definition: epoch.hpp:95
bool operator<=(const Epoch &other) const
Definition: epoch.hpp:187
uint32_t EpochInteger
Unsigned integer representation of epoch.
Definition: epoch.hpp:64
#define CXX11_NOEXCEPT
Used in public headers in place of "noexcept" of C++11.
Definition: cxx11.hpp:133
Represents a time epoch.
Definition: epoch.hpp:61
friend std::ostream & operator<<(std::ostream &o, const Epoch &v)
Definition: epoch.cpp:23
Zero is always reserved for invalid epoch.
Definition: epoch.hpp:68
bool operator>(const Epoch &other) const
Definition: epoch.hpp:186
bool operator<(const Epoch &other) const
Definition: epoch.hpp:185
Epoch(EpochInteger value) noexcept
Construct an epoch of specified integer representation.
Definition: epoch.hpp:89
bool operator==(const Epoch &other) const
Definition: epoch.hpp:183
Used for before().
Definition: epoch.hpp:83
Epoch one_more() const
Definition: epoch.hpp:127
Epoch & operator--()
Definition: epoch.hpp:113
Epoch() noexcept
Construct an invalid epoch.
Definition: epoch.hpp:87
bool operator>=(const Epoch &other) const
Definition: epoch.hpp:188
void store_max(const Epoch &other)
Kind of std::max(this, other).
Definition: epoch.hpp:165
bool is_valid() const
Definition: epoch.hpp:96
const Epoch INVALID_EPOCH
A constant epoch object that represents an invalid epoch.
Definition: epoch.hpp:204
As there is no transaction in ep-1, initial durable_epoch is 1.
Definition: epoch.hpp:70
#define ASSERT_ND(x)
A warning-free wrapper macro of assert() that has no performance effect in release mode even when 'x'...
Definition: assert_nd.hpp:72
bool before(const Epoch &other) const
Returns if this epoch is before the given epoch in the sense of distance defined in RFC 1982...
Definition: epoch.hpp:176
Epoch & operator++()
Definition: epoch.hpp:104
EpochInteger value() const
Returns the raw integer representation.
Definition: epoch.hpp:102
void store_min(const Epoch &other)
Kind of std::min(this, other).
Definition: epoch.hpp:153