summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2016-12-21 11:50:26 +0100
committerHenning Baldersheim <balder@yahoo-inc.com>2016-12-21 11:50:26 +0100
commit908ed134931c4b09b6cbda4932d9bff3818198d3 (patch)
treeba99d8d62935e50e1671bbe61b16ee2fcfa9b5d1
parentbb810a7c6ebe3e8ec250a26186d785d2c6009787 (diff)
hide the mask better and provide utility for validating useBits.
-rw-r--r--document/src/vespa/document/base/globalid.h2
-rw-r--r--document/src/vespa/document/bucket/bucketid.cpp53
-rw-r--r--document/src/vespa/document/bucket/bucketid.h31
-rw-r--r--searchcore/src/vespa/searchcore/proton/documentmetastore/documentmetastore.cpp17
-rw-r--r--searchcore/src/vespa/searchcore/proton/documentmetastore/documentmetastoresaver.cpp19
-rw-r--r--searchcore/src/vespa/searchcore/proton/documentmetastore/raw_document_meta_data.h100
-rw-r--r--vespalib/src/vespa/vespalib/util/array.h2
7 files changed, 78 insertions, 146 deletions
diff --git a/document/src/vespa/document/base/globalid.h b/document/src/vespa/document/base/globalid.h
index c0047df6a50..dcfee4c76ff 100644
--- a/document/src/vespa/document/base/globalid.h
+++ b/document/src/vespa/document/base/globalid.h
@@ -24,7 +24,7 @@
*/
#pragma once
-#include <stdint.h>
+#include <cstdint>
#include <vespa/vespalib/stllike/string.h>
#include <vespa/document/bucket/bucketid.h>
diff --git a/document/src/vespa/document/bucket/bucketid.cpp b/document/src/vespa/document/bucket/bucketid.cpp
index 01a3d1bd428..e1396c1a0e0 100644
--- a/document/src/vespa/document/bucket/bucketid.cpp
+++ b/document/src/vespa/document/bucket/bucketid.cpp
@@ -8,8 +8,7 @@
#include <vespa/vespalib/stllike/hash_set.hpp>
using vespalib::nbostream;
-
-template class vespalib::Array<document::BucketId>;
+using vespalib::asciistream;
namespace document {
@@ -34,39 +33,43 @@ const unsigned char reverseBitTable[256] =
0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
};
-std::vector<BucketId::Type>
-BucketId::getUsedMasks()
+BucketId::Type BucketId::_usedMasks[BucketId::maxNumBits+1];
+BucketId::Type BucketId::_stripMasks[BucketId::maxNumBits+1];
+
+namespace {
+
+void fillUsedMasks(BucketId::Type * masks, uint8_t maxBits)
{
typedef BucketId::Type Type;
- uint8_t maxBits = BucketId::maxNumBits();
- std::vector<Type> masks(maxBits + 1);
for (uint32_t usedBits = 0; usedBits <= maxBits; ++usedBits) {
uint8_t notused = 8 * sizeof(Type) - usedBits;
- masks[usedBits] = (std::numeric_limits<Type>::max() << notused)
- >> notused;
+ masks[usedBits] = (std::numeric_limits<Type>::max() << notused) >> notused;
}
- return masks;
}
-std::vector<BucketId::Type>
-BucketId::getStripMasks()
+void fillStripMasks(BucketId::Type * masks, uint8_t maxBits)
{
typedef BucketId::Type Type;
- uint8_t maxBits = BucketId::maxNumBits();
- std::vector<Type> masks(maxBits + 1);
for (uint32_t usedBits = 0; usedBits <= maxBits; ++usedBits) {
uint8_t notused = 8 * sizeof(Type) - usedBits;
- Type usedMask = (std::numeric_limits<Type>::max() << notused)
- >> notused;
- Type countMask = (std::numeric_limits<Type>::max() >> maxBits)
- << maxBits;
+ Type usedMask = (std::numeric_limits<Type>::max() << notused) >> notused;
+ Type countMask = (std::numeric_limits<Type>::max() >> maxBits) << maxBits;
masks[usedBits] = usedMask | countMask;
}
- return masks;
}
-std::vector<BucketId::Type> BucketId::_usedMasks = getUsedMasks();
-std::vector<BucketId::Type> BucketId::_stripMasks = getStripMasks();
+
+ struct Initialize {
+ Initialize() {
+ BucketId::initialize();
+ }
+ };
+}
+
+void BucketId::initialize() {
+ fillUsedMasks(BucketId::_usedMasks, BucketId::maxNumBits);
+ fillStripMasks(BucketId::_stripMasks, BucketId::maxNumBits);
+}
vespalib::string
BucketId::toString() const
@@ -100,7 +103,7 @@ BucketId::keyToBucketId(Type key)
{
Type retVal = reverse(key);
- Type usedCountMSB = key << maxNumBits();
+ Type usedCountMSB = key << maxNumBits;
retVal <<= CountBits;
retVal >>= CountBits;
retVal |= usedCountMSB;
@@ -118,13 +121,12 @@ BucketId::contains(const BucketId& id) const
return (copy.getId() == getId());
}
-vespalib::asciistream& operator<<(vespalib::asciistream& os, const BucketId& id)
+vespalib::asciistream& operator<<(asciistream& os, const BucketId& id)
{
- vespalib::asciistream::StateSaver stateSaver(os);
+ asciistream::StateSaver stateSaver(os);
return os << "BucketId(0x"
<< vespalib::hex << vespalib::setw(sizeof(BucketId::Type)*2) << vespalib::setfill('0')
- << id.getId()
- << ")";
+ << id.getId() << ")";
}
std::ostream& operator<<(std::ostream& os, const BucketId& id)
@@ -148,4 +150,5 @@ operator>>(nbostream &is, BucketId &bucketId)
} // document
+template class vespalib::Array<document::BucketId>;
VESPALIB_HASH_SET_INSTANTIATE_H(document::BucketId, document::BucketId::hash); \ No newline at end of file
diff --git a/document/src/vespa/document/bucket/bucketid.h b/document/src/vespa/document/bucket/bucketid.h
index 6dcad244adb..b1e0040bc31 100644
--- a/document/src/vespa/document/bucket/bucketid.h
+++ b/document/src/vespa/document/bucket/bucketid.h
@@ -21,7 +21,6 @@
#pragma once
-#include <vector>
#include <vespa/vespalib/stllike/string.h>
#include <vespa/vespalib/util/array.h>
@@ -47,8 +46,8 @@ public:
* The primitive type used to store bucket identifiers. If you use the
* typedef when needed we can alter this later with less code changes.
*/
- typedef uint64_t Type;
- typedef vespalib::Array<BucketId> List;
+ using Type = uint64_t;
+ using List = vespalib::Array<BucketId>;
/** Create an initially unset bucket id. */
BucketId() : _id(0) {}
/** Create a bucket id with the given raw unchecked content. */
@@ -65,8 +64,11 @@ public:
vespalib::string toString() const;
bool valid() const {
- uint32_t usedBits(getUsedBits());
- return (usedBits >= minNumBits() && usedBits <= maxNumBits());
+ return validateUsedBits(getUsedBits());
+ }
+
+ static bool validateUsedBits(uint32_t usedBits) {
+ return (usedBits >= minNumBits) && (usedBits <= maxNumBits);
}
bool isSet() const {
@@ -92,13 +94,13 @@ public:
/** Number of MSB bits used to count LSB bits used. */
enum { CountBits = 6 };
- static uint32_t maxNumBits() { return (8 * sizeof(Type) - CountBits);}
- static uint32_t minNumBits() { return 1u; } // See comment above.
+ static constexpr uint32_t maxNumBits = (8 * sizeof(Type) - CountBits);
+ static constexpr uint32_t minNumBits = 1u; // See comment above.
- uint32_t getUsedBits() const { return _id >> maxNumBits(); }
+ uint32_t getUsedBits() const { return _id >> maxNumBits; }
void setUsedBits(uint32_t used) {
- uint32_t availBits = maxNumBits();
+ uint32_t availBits = maxNumBits;
if (used > availBits) {
throwFailedSetUsedBits(used, availBits);
}
@@ -129,7 +131,7 @@ public:
static Type bucketIdToKey(Type id) {
Type retVal = reverse(id);
- Type usedCountLSB = id >> maxNumBits();
+ Type usedCountLSB = id >> maxNumBits;
retVal >>= CountBits;
retVal <<= CountBits;
retVal |= usedCountLSB;
@@ -158,11 +160,10 @@ public:
return (_id & ((Type)1 << n)) == 0 ? 0 : 1;
}
+ static void initialize();
private:
- static std::vector<Type> _usedMasks;
- static std::vector<Type> _stripMasks;
- static std::vector<BucketId::Type> getUsedMasks();
- static std::vector<BucketId::Type> getStripMasks();
+ static Type _usedMasks[maxNumBits+1];
+ static Type _stripMasks[maxNumBits+1];
Type _id;
@@ -175,7 +176,7 @@ private:
}
static Type createUsedBits(uint32_t used, Type id) {
- uint32_t availBits = maxNumBits();
+ uint32_t availBits = maxNumBits;
Type usedCount(used);
usedCount <<= availBits;
diff --git a/searchcore/src/vespa/searchcore/proton/documentmetastore/documentmetastore.cpp b/searchcore/src/vespa/searchcore/proton/documentmetastore/documentmetastore.cpp
index a307392230f..7fa14620241 100644
--- a/searchcore/src/vespa/searchcore/proton/documentmetastore/documentmetastore.cpp
+++ b/searchcore/src/vespa/searchcore/proton/documentmetastore/documentmetastore.cpp
@@ -815,8 +815,7 @@ DocumentMetaStore::getLids(const BucketId &bucketId, std::vector<DocId> &lids)
assert(validLid(lid));
const RawDocumentMetaData &metaData = getRawMetaData(lid);
uint8_t bucketUsedBits = metaData.getBucketUsedBits();
- assert(bucketUsedBits >= BucketId::minNumBits() &&
- bucketUsedBits <= BucketId::maxNumBits());
+ assert(BucketId::validateUsedBits(bucketUsedBits));
if (bucketUsedBits != bucketId.getUsedBits())
continue; // Skip document belonging to overlapping bucket
lids.push_back(lid);
@@ -847,8 +846,7 @@ DocumentMetaStore::handleSplit(const bucketdb::SplitBucketSession &session)
assert(validLid(lid));
RawDocumentMetaData &metaData = _metaDataStore[lid];
uint8_t bucketUsedBits = metaData.getBucketUsedBits();
- assert(bucketUsedBits >= BucketId::minNumBits() &&
- bucketUsedBits <= BucketId::maxNumBits());
+ assert(BucketId::validateUsedBits(bucketUsedBits));
if (bucketUsedBits == source.getUsedBits()) {
BucketId t1(metaData.getGid().convertToBucketId());
BucketId t2(t1);
@@ -890,19 +888,14 @@ DocumentMetaStore::handleJoin(const bucketdb::JoinBucketsSession &session)
DocId lid = itr.getKey();
assert(validLid(lid));
RawDocumentMetaData &metaData = _metaDataStore[lid];
- uint8_t bucketUsedBits = metaData.getBucketUsedBits();
- (void) bucketUsedBits;
- assert(bucketUsedBits >= BucketId::minNumBits() &&
- bucketUsedBits <= BucketId::maxNumBits());
+ assert(BucketId::validateUsedBits(metaData.getBucketUsedBits()));
BucketId s(metaData.getBucketId());
if (source1.valid() && s == source1) {
metaData.setBucketUsedBits(target.getUsedBits());
- deltas._delta1.add(metaData.getGid(), metaData.getTimestamp(),
- _subDbType);
+ deltas._delta1.add(metaData.getGid(), metaData.getTimestamp(), _subDbType);
} else if (source2.valid() && s == source2) {
metaData.setBucketUsedBits(target.getUsedBits());
- deltas._delta2.add(metaData.getGid(), metaData.getTimestamp(),
- _subDbType);
+ deltas._delta2.add(metaData.getGid(), metaData.getTimestamp(), _subDbType);
}
}
if (_subDbType == SubDbType::READY) {
diff --git a/searchcore/src/vespa/searchcore/proton/documentmetastore/documentmetastoresaver.cpp b/searchcore/src/vespa/searchcore/proton/documentmetastore/documentmetastoresaver.cpp
index 569677cf6a7..bcf4dbc1ced 100644
--- a/searchcore/src/vespa/searchcore/proton/documentmetastore/documentmetastoresaver.cpp
+++ b/searchcore/src/vespa/searchcore/proton/documentmetastore/documentmetastoresaver.cpp
@@ -1,8 +1,5 @@
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-#include <vespa/fastos/fastos.h>
-#include <vespa/log/log.h>
-LOG_SETUP(".proton.documentmetastore.documentmetastoresaver");
#include "documentmetastoresaver.h"
#include <vespa/searchlib/util/bufferwriter.h>
@@ -29,13 +26,11 @@ class WriteMetaData
using BucketId = documentmetastore::IStore::BucketId;
using Timestamp = documentmetastore::IStore::Timestamp;
public:
- WriteMetaData(search::BufferWriter &datWriter,
- const MetaDataStore &metaDataStore)
+ WriteMetaData(search::BufferWriter &datWriter, const MetaDataStore &metaDataStore)
: _datWriter(datWriter),
_metaDataStore(&metaDataStore[0]),
_metaDataStoreSize(metaDataStore.size())
- {
- }
+ { }
void operator()(uint32_t lid) {
assert(lid < _metaDataStoreSize);
@@ -43,8 +38,7 @@ public:
const GlobalId &gid = metaData.getGid();
// 6 bits used for bucket bits
uint8_t bucketUsedBits = metaData.getBucketUsedBits();
- assert(bucketUsedBits >= BucketId::minNumBits() &&
- bucketUsedBits <= BucketId::maxNumBits());
+ assert(BucketId::validateUsedBits(bucketUsedBits));
assert((bucketUsedBits >> BucketId::CountBits) == 0);
Timestamp::Type timestamp = metaData.getTimestamp();
search::BufferWriter &datWriter(_datWriter);
@@ -67,13 +61,10 @@ DocumentMetaStoreSaver(vespalib::GenerationHandler::Guard &&guard,
: AttributeSaver(std::move(guard), cfg),
_gidIterator(gidIterator),
_metaDataStore(metaDataStore)
-{
-}
+{ }
-DocumentMetaStoreSaver::~DocumentMetaStoreSaver()
-{
-}
+DocumentMetaStoreSaver::~DocumentMetaStoreSaver() { }
bool
diff --git a/searchcore/src/vespa/searchcore/proton/documentmetastore/raw_document_meta_data.h b/searchcore/src/vespa/searchcore/proton/documentmetastore/raw_document_meta_data.h
index 1ccf6b5ccf4..d13ccbabe3c 100644
--- a/searchcore/src/vespa/searchcore/proton/documentmetastore/raw_document_meta_data.h
+++ b/searchcore/src/vespa/searchcore/proton/documentmetastore/raw_document_meta_data.h
@@ -13,23 +13,20 @@ namespace proton {
*/
struct RawDocumentMetaData
{
- typedef document::GlobalId GlobalId;
- typedef document::BucketId BucketId;
- typedef storage::spi::Timestamp Timestamp;
- GlobalId _gid;
- uint8_t _bucketUsedBits;
+ using GlobalId = document::GlobalId;
+ using BucketId = document::BucketId;
+ using Timestamp = storage::spi::Timestamp;
+ GlobalId _gid;
+ uint8_t _bucketUsedBits;
Timestamp _timestamp;
RawDocumentMetaData(void)
: _gid(),
- _bucketUsedBits(BucketId::minNumBits()),
+ _bucketUsedBits(BucketId::minNumBits),
_timestamp()
- {
- }
+ { }
- RawDocumentMetaData(const GlobalId &gid,
- const BucketId &bucketId,
- const Timestamp &timestamp)
+ RawDocumentMetaData(const GlobalId &gid, const BucketId &bucketId, const Timestamp &timestamp)
: _gid(gid),
_bucketUsedBits(bucketId.getUsedBits()),
_timestamp(timestamp)
@@ -41,73 +38,28 @@ struct RawDocumentMetaData
bucketId.getRawId() == verId.getId());
}
- bool
- operator<(const GlobalId &rhs) const
- {
- return _gid < rhs;
- }
-
- bool
- operator==(const GlobalId &rhs) const
- {
- return _gid == rhs;
- }
-
- bool
- operator<(const RawDocumentMetaData &rhs) const
- {
- return _gid < rhs._gid;
- }
-
- bool
- operator==(const RawDocumentMetaData &rhs) const
- {
- return _gid == rhs._gid;
- }
+ bool operator<(const GlobalId &rhs) const { return _gid < rhs; }
+ bool operator==(const GlobalId &rhs) const { return _gid == rhs; }
+ bool operator<(const RawDocumentMetaData &rhs) const { return _gid < rhs._gid; }
+ bool operator==(const RawDocumentMetaData &rhs) const { return _gid == rhs._gid; }
- const GlobalId &
- getGid(void) const
- {
- return _gid;
- }
+ const GlobalId &getGid() const { return _gid; }
+ GlobalId &getGid() { return _gid; }
+ void setGid(const GlobalId &rhs) { _gid = rhs; }
+ uint8_t getBucketUsedBits() const { return _bucketUsedBits; }
- GlobalId &
- getGid(void)
- {
- return _gid;
- }
-
- void
- setGid(const GlobalId &rhs)
- {
- _gid = rhs;
- }
-
- uint8_t
- getBucketUsedBits(void) const
- {
- return _bucketUsedBits;
- }
-
- BucketId
- getBucketId(void) const
- {
+ BucketId getBucketId() const {
BucketId ret(_gid.convertToBucketId());
ret.setUsedBits(_bucketUsedBits);
return ret;
}
- void
- setBucketUsedBits(uint8_t bucketUsedBits)
- {
- assert(bucketUsedBits >= BucketId::minNumBits() &&
- bucketUsedBits <= BucketId::maxNumBits());
+ void setBucketUsedBits(uint8_t bucketUsedBits) {
+ assert(BucketId::validateUsedBits(bucketUsedBits));
_bucketUsedBits = bucketUsedBits;
}
- void
- setBucketId(const BucketId &bucketId)
- {
+ void setBucketId(const BucketId &bucketId) {
assert(bucketId.valid());
uint8_t bucketUsedBits = bucketId.getUsedBits();
BucketId verId(_gid.convertToBucketId());
@@ -117,17 +69,9 @@ struct RawDocumentMetaData
_bucketUsedBits = bucketUsedBits;
}
- Timestamp
- getTimestamp(void) const
- {
- return _timestamp;
- }
+ Timestamp getTimestamp(void) const { return _timestamp; }
- void
- setTimestamp(const Timestamp &timestamp)
- {
- _timestamp = timestamp;
- }
+ void setTimestamp(const Timestamp &timestamp) { _timestamp = timestamp; }
};
} // namespace proton
diff --git a/vespalib/src/vespa/vespalib/util/array.h b/vespalib/src/vespa/vespalib/util/array.h
index 2aba4255e25..269bf67bc26 100644
--- a/vespalib/src/vespa/vespalib/util/array.h
+++ b/vespalib/src/vespa/vespalib/util/array.h
@@ -1,7 +1,7 @@
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once
-#include <stdint.h>
+#include <cstdint>
#include <sys/types.h>
#include <algorithm>
#include <vespa/vespalib/util/alloc.h>