diff options
author | Tor Egge <Tor.Egge@oath.com> | 2018-02-01 14:55:54 +0000 |
---|---|---|
committer | Tor Egge <Tor.Egge@oath.com> | 2018-02-01 15:14:57 +0000 |
commit | 7b9dd4b44b8d84f901b2bb496c7d7f7ca01028ba (patch) | |
tree | bc49b1db867448ac8766adbd56b4b39a3e913f75 /document | |
parent | 53ba441f3939f747159a581c4844ce8382679e34 (diff) |
Move FixedBucketSpaces to document module.
Diffstat (limited to 'document')
-rw-r--r-- | document/src/tests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | document/src/tests/fixed_bucket_spaces_test.cpp | 62 | ||||
-rw-r--r-- | document/src/vespa/document/bucket/CMakeLists.txt | 1 | ||||
-rw-r--r-- | document/src/vespa/document/bucket/fixed_bucket_spaces.cpp | 33 | ||||
-rw-r--r-- | document/src/vespa/document/bucket/fixed_bucket_spaces.h | 30 |
5 files changed, 127 insertions, 0 deletions
diff --git a/document/src/tests/CMakeLists.txt b/document/src/tests/CMakeLists.txt index 197f7cc508a..6edd113f17b 100644 --- a/document/src/tests/CMakeLists.txt +++ b/document/src/tests/CMakeLists.txt @@ -28,6 +28,7 @@ vespa_add_executable(document_testrunner_app TEST urltypetest.cpp fieldsettest.cpp gid_filter_test.cpp + fixed_bucket_spaces_test.cpp DEPENDS document AFTER diff --git a/document/src/tests/fixed_bucket_spaces_test.cpp b/document/src/tests/fixed_bucket_spaces_test.cpp new file mode 100644 index 00000000000..fab38c638a7 --- /dev/null +++ b/document/src/tests/fixed_bucket_spaces_test.cpp @@ -0,0 +1,62 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include <vespa/document/bucket/fixed_bucket_spaces.h> +#include <cppunit/extensions/HelperMacros.h> + +namespace document { + +struct FixedBucketSpacesTest : CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(FixedBucketSpacesTest); + CPPUNIT_TEST(bucket_space_from_name_is_defined_for_default_space); + CPPUNIT_TEST(bucket_space_from_name_is_defined_for_global_space); + CPPUNIT_TEST(bucket_space_from_name_throws_exception_for_unknown_space); + CPPUNIT_TEST(name_from_bucket_space_is_defined_for_default_space); + CPPUNIT_TEST(name_from_bucket_space_is_defined_for_global_space); + CPPUNIT_TEST(name_from_bucket_space_throws_exception_for_unknown_space); + CPPUNIT_TEST_SUITE_END(); + + void bucket_space_from_name_is_defined_for_default_space(); + void bucket_space_from_name_is_defined_for_global_space(); + void bucket_space_from_name_throws_exception_for_unknown_space(); + void name_from_bucket_space_is_defined_for_default_space(); + void name_from_bucket_space_is_defined_for_global_space(); + void name_from_bucket_space_throws_exception_for_unknown_space(); +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(FixedBucketSpacesTest); + +void FixedBucketSpacesTest::bucket_space_from_name_is_defined_for_default_space() { + CPPUNIT_ASSERT_EQUAL(FixedBucketSpaces::default_space(), FixedBucketSpaces::from_string("default")); +} + +void FixedBucketSpacesTest::bucket_space_from_name_is_defined_for_global_space() { + CPPUNIT_ASSERT_EQUAL(FixedBucketSpaces::global_space(), FixedBucketSpaces::from_string("global")); +} + +void FixedBucketSpacesTest::bucket_space_from_name_throws_exception_for_unknown_space() { + try { + FixedBucketSpaces::from_string("banana"); + CPPUNIT_FAIL("Expected exception on unknown bucket space name"); + } catch (document::UnknownBucketSpaceException& e) { + } +} + +void FixedBucketSpacesTest::name_from_bucket_space_is_defined_for_default_space() { + CPPUNIT_ASSERT_EQUAL(vespalib::stringref("default"), + FixedBucketSpaces::to_string(FixedBucketSpaces::default_space())); +} + +void FixedBucketSpacesTest::name_from_bucket_space_is_defined_for_global_space() { + CPPUNIT_ASSERT_EQUAL(vespalib::stringref("global"), + FixedBucketSpaces::to_string(FixedBucketSpaces::global_space())); +} + +void FixedBucketSpacesTest::name_from_bucket_space_throws_exception_for_unknown_space() { + try { + FixedBucketSpaces::to_string(BucketSpace(4567)); + CPPUNIT_FAIL("Expected exception on unknown bucket space value"); + } catch (document::UnknownBucketSpaceException& e) { + } +} + +} diff --git a/document/src/vespa/document/bucket/CMakeLists.txt b/document/src/vespa/document/bucket/CMakeLists.txt index 3e779211762..5e3bc02595a 100644 --- a/document/src/vespa/document/bucket/CMakeLists.txt +++ b/document/src/vespa/document/bucket/CMakeLists.txt @@ -8,6 +8,7 @@ vespa_add_library(document_bucket OBJECT bucketidlist.cpp bucketselector.cpp bucketspace.cpp + fixed_bucket_spaces.cpp DEPENDS AFTER document_documentconfig diff --git a/document/src/vespa/document/bucket/fixed_bucket_spaces.cpp b/document/src/vespa/document/bucket/fixed_bucket_spaces.cpp new file mode 100644 index 00000000000..4737c459bad --- /dev/null +++ b/document/src/vespa/document/bucket/fixed_bucket_spaces.cpp @@ -0,0 +1,33 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#include "fixed_bucket_spaces.h" + +namespace document { + +VESPA_IMPLEMENT_EXCEPTION(UnknownBucketSpaceException, vespalib::IllegalArgumentException) + +// Some sanity checks to ensure we don't mess up any legacy mappings. +static_assert(BucketSpace::placeHolder() != BucketSpace::invalid()); +static_assert(FixedBucketSpaces::default_space() == BucketSpace::placeHolder()); +static_assert(FixedBucketSpaces::global_space() != FixedBucketSpaces::default_space()); + +BucketSpace FixedBucketSpaces::from_string(vespalib::stringref name) { + if (name == "default") { + return default_space(); + } else if (name == "global") { + return global_space(); + } else { + throw UnknownBucketSpaceException("Unknown bucket space name: " + vespalib::string(name), VESPA_STRLOC); + } +} + +vespalib::stringref FixedBucketSpaces::to_string(BucketSpace space) { + if (space == default_space()) { + return "default"; + } else if (space == global_space()) { + return "global"; + } else { + throw UnknownBucketSpaceException("Unknown bucket space: " + space.toString(), VESPA_STRLOC); + } +} + +} diff --git a/document/src/vespa/document/bucket/fixed_bucket_spaces.h b/document/src/vespa/document/bucket/fixed_bucket_spaces.h new file mode 100644 index 00000000000..87722c7a21c --- /dev/null +++ b/document/src/vespa/document/bucket/fixed_bucket_spaces.h @@ -0,0 +1,30 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#pragma once + +#include "bucketspace.h" +#include <vespa/vespalib/util/exceptions.h> +#include <vespa/vespalib/stllike/string.h> + +namespace document { + +VESPA_DEFINE_EXCEPTION(UnknownBucketSpaceException, vespalib::IllegalArgumentException); + +/** + * Minimal repository/factory of bucket spaces hard coded for default and global + * distributions. + */ +struct FixedBucketSpaces { + static constexpr BucketSpace default_space() { return BucketSpace(1); }; + static constexpr BucketSpace global_space() { return BucketSpace(2); } + + // Post-condition: returned space has valid() == true iff name + // is either "default" or "global". + // Throws UnknownBucketSpaceException if name does not map to a known bucket space. + static BucketSpace from_string(vespalib::stringref name); + // Post-condition: returned string can be losslessly passed to from_string() + // iff space is equal to default_space() or global_space(). + // Throws UnknownBucketSpaceException if space does not map to a known name. + static vespalib::stringref to_string(BucketSpace space); +}; + +} |