diff options
author | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2015-06-20 00:22:50 -0400 |
---|---|---|
committer | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2015-06-20 10:56:02 -0400 |
commit | 9c2ed42daa8fbbef4a919c21ec564e2db55e8d60 (patch) | |
tree | 3814f79c10d7b490948d8cb7b112ac1dd41ceff1 /src/mongo/bson/oid.cpp | |
parent | 01965cf52bce6976637ecb8f4a622aeb05ab256a (diff) | |
download | mongo-9c2ed42daa8fbbef4a919c21ec564e2db55e8d60.tar.gz |
SERVER-18579: Clang-Format - reformat code, no comment reflow
Diffstat (limited to 'src/mongo/bson/oid.cpp')
-rw-r--r-- | src/mongo/bson/oid.cpp | 243 |
1 files changed, 120 insertions, 123 deletions
diff --git a/src/mongo/bson/oid.cpp b/src/mongo/bson/oid.cpp index 209d43a8a38..a78bcbde682 100644 --- a/src/mongo/bson/oid.cpp +++ b/src/mongo/bson/oid.cpp @@ -42,130 +42,127 @@ namespace mongo { namespace { - std::unique_ptr<AtomicUInt32> counter; - - const std::size_t kTimestampOffset = 0; - const std::size_t kInstanceUniqueOffset = kTimestampOffset + - OID::kTimestampSize; - const std::size_t kIncrementOffset = kInstanceUniqueOffset + - OID::kInstanceUniqueSize; - OID::InstanceUnique _instanceUnique; -} // namespace - - MONGO_INITIALIZER_GENERAL(OIDGeneration, MONGO_NO_PREREQUISITES, ("default")) - (InitializerContext* context) { - std::unique_ptr<SecureRandom> entropy(SecureRandom::create()); - counter.reset(new AtomicUInt32(uint32_t(entropy->nextInt64()))); - _instanceUnique = OID::InstanceUnique::generate(*entropy); - return Status::OK(); - } - - OID::Increment OID::Increment::next() { - uint64_t nextCtr = counter->fetchAndAdd(1); - OID::Increment incr; - - incr.bytes[0] = uint8_t(nextCtr >> 16); - incr.bytes[1] = uint8_t(nextCtr >> 8); - incr.bytes[2] = uint8_t(nextCtr); - - return incr; - } - - OID::InstanceUnique OID::InstanceUnique::generate(SecureRandom& entropy) { - int64_t rand = entropy.nextInt64(); - OID::InstanceUnique u; - std::memcpy(u.bytes, &rand, kInstanceUniqueSize); - return u; - } - - void OID::setTimestamp(const OID::Timestamp timestamp) { - _view().write<BigEndian<Timestamp>>(timestamp, kTimestampOffset); - } - - void OID::setInstanceUnique(const OID::InstanceUnique unique) { - // Byte order doesn't matter here - _view().write<InstanceUnique>(unique, kInstanceUniqueOffset); - } - - void OID::setIncrement(const OID::Increment inc) { - _view().write<Increment>(inc, kIncrementOffset); - } - - OID::Timestamp OID::getTimestamp() const { - return view().read<BigEndian<Timestamp>>(kTimestampOffset); - } - - OID::InstanceUnique OID::getInstanceUnique() const { - // Byte order doesn't matter here - return view().read<InstanceUnique>(kInstanceUniqueOffset); - } - - OID::Increment OID::getIncrement() const { - return view().read<Increment>(kIncrementOffset); - } +std::unique_ptr<AtomicUInt32> counter; - void OID::hash_combine(size_t &seed) const { - uint32_t v; - for (int i = 0; i != kOIDSize; i += sizeof(uint32_t)) { - memcpy(&v, _data + i, sizeof(uint32_t)); - boost::hash_combine(seed, v); - } - } - - size_t OID::Hasher::operator() (const OID& oid) const { - size_t seed = 0; - oid.hash_combine(seed); - return seed; - } - - void OID::regenMachineId() { - std::unique_ptr<SecureRandom> entropy(SecureRandom::create()); - _instanceUnique = InstanceUnique::generate(*entropy); - } - - unsigned OID::getMachineId() { - uint32_t ret = 0; - std::memcpy(&ret, _instanceUnique.bytes, sizeof(uint32_t)); - return ret; - } - - void OID::justForked() { - regenMachineId(); - } - - void OID::init() { - // each set* method handles endianness - setTimestamp(time(0)); - setInstanceUnique(_instanceUnique); - setIncrement(Increment::next()); - } - - void OID::init( const std::string& s ) { - verify( s.size() == 24 ); - const char *p = s.c_str(); - for (std::size_t i = 0; i < kOIDSize; i++) { - _data[i] = fromHex(p); - p += 2; - } - } - - void OID::init(Date_t date, bool max) { - setTimestamp(uint32_t(date.toMillisSinceEpoch() / 1000)); - uint64_t rest = max ? std::numeric_limits<uint64_t>::max() : 0u; - std::memcpy(_view().view(kInstanceUniqueOffset), &rest, - kInstanceUniqueSize + kIncrementSize); - } - - time_t OID::asTimeT() const { - return getTimestamp(); - } - - std::string OID::toString() const { - return toHexLower(_data, kOIDSize); - } +const std::size_t kTimestampOffset = 0; +const std::size_t kInstanceUniqueOffset = kTimestampOffset + OID::kTimestampSize; +const std::size_t kIncrementOffset = kInstanceUniqueOffset + OID::kInstanceUniqueSize; +OID::InstanceUnique _instanceUnique; +} // namespace - std::string OID::toIncString() const { - return toHexLower(getIncrement().bytes, kIncrementSize); - } +MONGO_INITIALIZER_GENERAL(OIDGeneration, MONGO_NO_PREREQUISITES, ("default")) +(InitializerContext* context) { + std::unique_ptr<SecureRandom> entropy(SecureRandom::create()); + counter.reset(new AtomicUInt32(uint32_t(entropy->nextInt64()))); + _instanceUnique = OID::InstanceUnique::generate(*entropy); + return Status::OK(); +} + +OID::Increment OID::Increment::next() { + uint64_t nextCtr = counter->fetchAndAdd(1); + OID::Increment incr; + + incr.bytes[0] = uint8_t(nextCtr >> 16); + incr.bytes[1] = uint8_t(nextCtr >> 8); + incr.bytes[2] = uint8_t(nextCtr); + + return incr; +} + +OID::InstanceUnique OID::InstanceUnique::generate(SecureRandom& entropy) { + int64_t rand = entropy.nextInt64(); + OID::InstanceUnique u; + std::memcpy(u.bytes, &rand, kInstanceUniqueSize); + return u; +} + +void OID::setTimestamp(const OID::Timestamp timestamp) { + _view().write<BigEndian<Timestamp>>(timestamp, kTimestampOffset); +} + +void OID::setInstanceUnique(const OID::InstanceUnique unique) { + // Byte order doesn't matter here + _view().write<InstanceUnique>(unique, kInstanceUniqueOffset); +} + +void OID::setIncrement(const OID::Increment inc) { + _view().write<Increment>(inc, kIncrementOffset); +} + +OID::Timestamp OID::getTimestamp() const { + return view().read<BigEndian<Timestamp>>(kTimestampOffset); +} + +OID::InstanceUnique OID::getInstanceUnique() const { + // Byte order doesn't matter here + return view().read<InstanceUnique>(kInstanceUniqueOffset); +} + +OID::Increment OID::getIncrement() const { + return view().read<Increment>(kIncrementOffset); +} + +void OID::hash_combine(size_t& seed) const { + uint32_t v; + for (int i = 0; i != kOIDSize; i += sizeof(uint32_t)) { + memcpy(&v, _data + i, sizeof(uint32_t)); + boost::hash_combine(seed, v); + } +} + +size_t OID::Hasher::operator()(const OID& oid) const { + size_t seed = 0; + oid.hash_combine(seed); + return seed; +} + +void OID::regenMachineId() { + std::unique_ptr<SecureRandom> entropy(SecureRandom::create()); + _instanceUnique = InstanceUnique::generate(*entropy); +} + +unsigned OID::getMachineId() { + uint32_t ret = 0; + std::memcpy(&ret, _instanceUnique.bytes, sizeof(uint32_t)); + return ret; +} + +void OID::justForked() { + regenMachineId(); +} + +void OID::init() { + // each set* method handles endianness + setTimestamp(time(0)); + setInstanceUnique(_instanceUnique); + setIncrement(Increment::next()); +} + +void OID::init(const std::string& s) { + verify(s.size() == 24); + const char* p = s.c_str(); + for (std::size_t i = 0; i < kOIDSize; i++) { + _data[i] = fromHex(p); + p += 2; + } +} + +void OID::init(Date_t date, bool max) { + setTimestamp(uint32_t(date.toMillisSinceEpoch() / 1000)); + uint64_t rest = max ? std::numeric_limits<uint64_t>::max() : 0u; + std::memcpy(_view().view(kInstanceUniqueOffset), &rest, kInstanceUniqueSize + kIncrementSize); +} + +time_t OID::asTimeT() const { + return getTimestamp(); +} + +std::string OID::toString() const { + return toHexLower(_data, kOIDSize); +} + +std::string OID::toIncString() const { + return toHexLower(getIncrement().bytes, kIncrementSize); +} } // namespace mongo |