diff options
author | Bjørn Christian Seime <bjorn.christian@seime.no> | 2016-11-22 15:39:52 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-22 15:39:52 +0100 |
commit | 2e747ea9504fdee687ce6641f0203a2604290fdb (patch) | |
tree | e0e321dccde1178498e341ccefcf57b1d16ace25 /document | |
parent | 60dfa94d2647a047910db3d068ae8711f4946a8b (diff) | |
parent | 0a05e44eb7dd03fcd49d6443dbaebd1acb542469 (diff) |
Merge pull request #1135 from yahoo/bjorncs/bucketspace
Bjorncs/bucketspace
Diffstat (limited to 'document')
-rw-r--r-- | document/src/tests/buckettest.cpp | 38 | ||||
-rw-r--r-- | document/src/vespa/document/bucket/CMakeLists.txt | 4 | ||||
-rw-r--r-- | document/src/vespa/document/bucket/bucket.cpp | 30 | ||||
-rw-r--r-- | document/src/vespa/document/bucket/bucket.h | 52 | ||||
-rw-r--r-- | document/src/vespa/document/bucket/bucketid.cpp | 9 | ||||
-rw-r--r-- | document/src/vespa/document/bucket/bucketspace.cpp | 34 | ||||
-rw-r--r-- | document/src/vespa/document/bucket/bucketspace.h | 41 |
7 files changed, 201 insertions, 7 deletions
diff --git a/document/src/tests/buckettest.cpp b/document/src/tests/buckettest.cpp index 2f2f3df9ab6..505ba5196c6 100644 --- a/document/src/tests/buckettest.cpp +++ b/document/src/tests/buckettest.cpp @@ -5,26 +5,32 @@ #include <vespa/document/bucket/bucketidfactory.h> #include <vespa/document/base/documentid.h> #include <vespa/vespalib/util/random.h> +#include <vespa/document/bucket/bucketspace.h> +#include <vespa/document/bucket/bucket.h> namespace document { class BucketTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(BucketTest); - CPPUNIT_TEST(testBucket); + CPPUNIT_TEST(testBucketId); CPPUNIT_TEST(testBucketGeneration); CPPUNIT_TEST(testBucketSerialization); CPPUNIT_TEST(testReverseBucket); CPPUNIT_TEST(testContains); CPPUNIT_TEST(testGetBit); + CPPUNIT_TEST(testToString); + CPPUNIT_TEST(testOperators); CPPUNIT_TEST_SUITE_END(); public: - void testBucket(); + void testBucketId(); void testBucketGeneration(); void testBucketSerialization(); void testReverseBucket(); void testContains(); void testGetBit(); + void testToString(); + void testOperators(); }; CPPUNIT_TEST_SUITE_REGISTRATION(BucketTest); @@ -41,7 +47,7 @@ inline std::ostream& operator<<(std::ostream& out, const Hex& h) { return out; } -void BucketTest::testBucket() +void BucketTest::testBucketId() { // Test empty (invalid) buckets BucketId id1; @@ -255,4 +261,30 @@ void BucketTest::testContains() { CPPUNIT_ASSERT(!id.contains(BucketId(16, 0x123456789ULL))); } +void BucketTest::testToString() { + BucketSpace bucketSpace(0x123450006789ULL); + CPPUNIT_ASSERT_EQUAL(vespalib::string("BucketSpace(0x0000123450006789)"), bucketSpace.toString()); + Bucket bucket(bucketSpace, BucketId(0x123456789ULL)); + CPPUNIT_ASSERT_EQUAL( + vespalib::string("Bucket(BucketSpace(0x0000123450006789), BucketId(0x0000000123456789))"), + bucket.toString()); +} + +void BucketTest::testOperators() { + CPPUNIT_ASSERT(BucketSpace(0x1) == BucketSpace(0x1)); + CPPUNIT_ASSERT(BucketSpace(0x1) != BucketSpace(0x2)); + CPPUNIT_ASSERT(BucketSpace(0x1) < BucketSpace(0x2)); + + CPPUNIT_ASSERT(Bucket(BucketSpace(0x1), BucketId(0x123456789ULL)) == + Bucket(BucketSpace(0x1), BucketId(0x123456789ULL))); + CPPUNIT_ASSERT(Bucket(BucketSpace(0x1), BucketId(0x123456789ULL)) != + Bucket(BucketSpace(0x2), BucketId(0x123456789ULL))); + CPPUNIT_ASSERT(Bucket(BucketSpace(0x1), BucketId(0x123456789ULL)) != + Bucket(BucketSpace(0x1), BucketId(0x987654321ULL))); + CPPUNIT_ASSERT(Bucket(BucketSpace(0x1), BucketId(0x123456789ULL)) < + Bucket(BucketSpace(0x1), BucketId(0x987654321ULL))); + CPPUNIT_ASSERT(Bucket(BucketSpace(0x1), BucketId(0x123456789ULL)) < + Bucket(BucketSpace(0x2), BucketId(0x123456789ULL))); +} + } // document diff --git a/document/src/vespa/document/bucket/CMakeLists.txt b/document/src/vespa/document/bucket/CMakeLists.txt index 6a1372f6607..757884f0050 100644 --- a/document/src/vespa/document/bucket/CMakeLists.txt +++ b/document/src/vespa/document/bucket/CMakeLists.txt @@ -1,10 +1,12 @@ # Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. vespa_add_library(document_bucket OBJECT SOURCES + bucket.cpp + bucketdistribution.cpp bucketid.cpp bucketidfactory.cpp bucketselector.cpp - bucketdistribution.cpp + bucketspace.cpp DEPENDS AFTER document_documentconfig diff --git a/document/src/vespa/document/bucket/bucket.cpp b/document/src/vespa/document/bucket/bucket.cpp new file mode 100644 index 00000000000..e4227298740 --- /dev/null +++ b/document/src/vespa/document/bucket/bucket.cpp @@ -0,0 +1,30 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include "bucket.h" +#include <iomanip> + +namespace document { + +vespalib::string Bucket::toString() const +{ + vespalib::asciistream os; + os << *this; + return os.str(); +} + +void Bucket::print(std::ostream& os) const +{ + os << toString(); +} + +vespalib::asciistream& operator<<(vespalib::asciistream& os, const Bucket& id) +{ + return os << "Bucket(" << id.getBucketSpace() << ", " << id.getBucketId() << ")"; +} + +std::ostream& operator<<(std::ostream& os, const Bucket& id) +{ + return os << id.toString(); +} + +} diff --git a/document/src/vespa/document/bucket/bucket.h b/document/src/vespa/document/bucket/bucket.h new file mode 100644 index 00000000000..cc7aceb4ea7 --- /dev/null +++ b/document/src/vespa/document/bucket/bucket.h @@ -0,0 +1,52 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +#include "bucketspace.h" +#include "bucketid.h" +#include <vespa/vespalib/stllike/string.h> +#include <vespa/vespalib/stllike/asciistream.h> +#include <cstdint> +#include <iostream> + +namespace document { + +class Bucket { +public: + Bucket(const Bucket&) noexcept = default; + Bucket& operator=(const Bucket&) noexcept = default; + Bucket(BucketSpace bucketSpace, BucketId bucketId) noexcept : _bucketSpace(bucketSpace), _bucketId(bucketId) {} + + bool operator<(const Bucket& other) const noexcept { + if (_bucketSpace == other._bucketSpace) { + return _bucketId < other._bucketId; + } + return _bucketSpace < other._bucketSpace; + } + bool operator==(const Bucket& other) const noexcept { + return _bucketSpace == other._bucketSpace && _bucketId == other._bucketId; + } + bool operator!=(const Bucket& other) const noexcept { return !(operator==(other)); } + + BucketSpace getBucketSpace() const noexcept { return _bucketSpace; } + BucketId getBucketId() const noexcept { return _bucketId; } + void print(std::ostream& os) const; + vespalib::string toString() const; + + struct hash { + size_t operator () (const Bucket& b) const { + size_t hash1 = BucketId::hash()(b.getBucketId()); + size_t hash2 = BucketSpace::hash()(b.getBucketSpace()); + // Formula taken from std::hash_combine proposal + return hash1 ^ (hash2 + 0x9e3779b9 + (hash1<<6) + (hash1>>2)); + } + }; +private: + BucketSpace _bucketSpace; + BucketId _bucketId; +}; + +std::ostream& operator<<(std::ostream&, const Bucket&); +vespalib::asciistream& operator<<(vespalib::asciistream&, const Bucket&); + +}
\ No newline at end of file diff --git a/document/src/vespa/document/bucket/bucketid.cpp b/document/src/vespa/document/bucket/bucketid.cpp index 14d86d84307..520d3675982 100644 --- a/document/src/vespa/document/bucket/bucketid.cpp +++ b/document/src/vespa/document/bucket/bucketid.cpp @@ -77,8 +77,7 @@ vespalib::string BucketId::toString() const { vespalib::asciistream stream; - stream << "BucketId(0x" << vespalib::hex << vespalib::setw(sizeof(Type)*2) - << vespalib::setfill('0') << getId() << ")" << vespalib::dec; + stream << *this; return stream.str(); } @@ -126,7 +125,11 @@ BucketId::contains(const BucketId& id) const vespalib::asciistream& operator<<(vespalib::asciistream& os, const BucketId& id) { - return os << id.toString(); + vespalib::asciistream::StateSaver stateSaver(os); + return os << "BucketId(0x" + << vespalib::hex << vespalib::setw(sizeof(BucketId::Type)*2) << vespalib::setfill('0') + << id.getId() + << ")"; } std::ostream& operator<<(std::ostream& os, const BucketId& id) diff --git a/document/src/vespa/document/bucket/bucketspace.cpp b/document/src/vespa/document/bucket/bucketspace.cpp new file mode 100644 index 00000000000..c29d229208f --- /dev/null +++ b/document/src/vespa/document/bucket/bucketspace.cpp @@ -0,0 +1,34 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include "bucketspace.h" +#include <iomanip> + +namespace document { + +vespalib::string BucketSpace::toString() const +{ + vespalib::asciistream os; + os << *this; + return os.str(); +} + +void BucketSpace::print(std::ostream& os) const +{ + os << toString(); +} + +vespalib::asciistream& operator<<(vespalib::asciistream& os, const BucketSpace& id) +{ + vespalib::asciistream::StateSaver stateSaver(os); + return os << "BucketSpace(0x" + << vespalib::hex << vespalib::setw(sizeof(BucketSpace::Type)*2) << vespalib::setfill('0') + << id.getId() + << ")"; +} + +std::ostream& operator<<(std::ostream& os, const BucketSpace& id) +{ + return os << id.toString(); +} + +}
\ No newline at end of file diff --git a/document/src/vespa/document/bucket/bucketspace.h b/document/src/vespa/document/bucket/bucketspace.h new file mode 100644 index 00000000000..a78004b271b --- /dev/null +++ b/document/src/vespa/document/bucket/bucketspace.h @@ -0,0 +1,41 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +#include <vespa/vespalib/stllike/string.h> +#include <vespa/vespalib/stllike/asciistream.h> +#include <cstdint> +#include <functional> +#include <iostream> + +namespace document { + +class BucketSpace { +public: + using Type = uint64_t; + + BucketSpace(const BucketSpace&) noexcept = default; + BucketSpace& operator=(const BucketSpace&) noexcept = default; + explicit BucketSpace(Type id) noexcept : _id(id) {} + + bool operator<(const BucketSpace& bucket) const noexcept { return _id < bucket._id; } + bool operator==(const BucketSpace& bucket) const noexcept { return _id == bucket._id; } + bool operator!=(const BucketSpace& bucket) const noexcept { return _id != bucket._id; } + + Type getId() const noexcept { return _id; } + void print(std::ostream& out) const; + vespalib::string toString() const; + + struct hash { + size_t operator () (const BucketSpace& bs) const { + return std::hash<Type>()(bs.getId()); + } + }; +private: + Type _id; +}; + +std::ostream& operator<<(std::ostream&, const BucketSpace&); +vespalib::asciistream& operator<<(vespalib::asciistream&, const BucketSpace&); + +} |