diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2020-08-07 17:23:06 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2020-08-07 17:23:06 +0000 |
commit | 3fa3912a3af1d75751425bf11eae1b4e3940a8c7 (patch) | |
tree | 29ebb03b74f25a672438f45e9159f4e0fc96bc4b /document | |
parent | cab80fdeb028a82403eaf13a99b31a5b6483712e (diff) |
Make FieldCollection immutable.
Diffstat (limited to 'document')
-rw-r--r-- | document/src/tests/fieldsettest.cpp | 27 | ||||
-rw-r--r-- | document/src/vespa/document/fieldset/fieldsetrepo.cpp | 10 | ||||
-rw-r--r-- | document/src/vespa/document/fieldset/fieldsets.cpp | 21 | ||||
-rw-r--r-- | document/src/vespa/document/fieldset/fieldsets.h | 11 |
4 files changed, 19 insertions, 50 deletions
diff --git a/document/src/tests/fieldsettest.cpp b/document/src/tests/fieldsettest.cpp index 5f2caddcdb1..37c55385c0f 100644 --- a/document/src/tests/fieldsettest.cpp +++ b/document/src/tests/fieldsettest.cpp @@ -289,25 +289,14 @@ TEST(FieldCollectionTest, testHash ) { TestDocMan testDocMan; const DocumentTypeRepo& repo = testDocMan.getTypeRepo(); const DocumentType & type = *repo.getDocumentType("testdoctype1"); - FieldCollection fc(type); - EXPECT_EQ(0ul, fc.hash()); - fc.insertField(type.getField("headerval")); - EXPECT_EQ(0ul, fc.hash()); - fc.complete(); - EXPECT_EQ(0x548599858c77ef83ul, fc.hash()); - fc.insertField(type.getField("hstringval")).complete(); - EXPECT_EQ(0x4a7ff2406d36a9b0ul, fc.hash()); - fc.insertField(type.getField("headerval")).complete(); - EXPECT_EQ(0x4a7ff2406d36a9b0ul, fc.hash()); - - FieldCollection fc2(type); - EXPECT_EQ(0ul, fc2.hash()); - fc2.insertField(type.getField("hstringval")).complete(); - EXPECT_EQ(0x1e0918531b19734ul, fc2.hash()); - fc2.insertField(type.getField("headerval")).complete(); - EXPECT_EQ(fc.hash(), fc2.hash()); - fc2.insertField(type.getField("headerval")).complete(); - EXPECT_EQ(fc.hash(), fc2.hash()); + Field::Set set; + EXPECT_EQ(0ul, FieldCollection(type, set).hash()); + set.insert(&type.getField("headerval")); + EXPECT_EQ(0x548599858c77ef83ul, FieldCollection(type, set).hash()); + set.insert(&type.getField("hstringval")); + EXPECT_EQ(0x4a7ff2406d36a9b0ul, FieldCollection(type, set).hash()); + set.erase(&type.getField("headerval")); + EXPECT_EQ(0x1e0918531b19734ul, FieldCollection(type, set).hash()); } } // document diff --git a/document/src/vespa/document/fieldset/fieldsetrepo.cpp b/document/src/vespa/document/fieldset/fieldsetrepo.cpp index e202a370559..c576624b026 100644 --- a/document/src/vespa/document/fieldset/fieldsetrepo.cpp +++ b/document/src/vespa/document/fieldset/fieldsetrepo.cpp @@ -44,20 +44,18 @@ parseFieldCollection(const DocumentTypeRepo& repo, const DocumentType& type(*typePtr); StringTokenizer tokenizer(fieldNames, ","); - auto collection = std::make_unique<FieldCollection>(type); - + Field::Set fields; for (const auto & token : tokenizer) { const DocumentType::FieldSet * fs = type.getFieldSet(token); if (fs) { for (const auto & fieldName : fs->getFields()) { - collection->insertField(type.getField(fieldName)); + fields.insert(&type.getField(fieldName)); } } else { - collection->insertField(type.getField(token)); + fields.insert(&type.getField(token)); } } - collection->complete(); - return collection; + return std::make_unique<FieldCollection>(type, std::move(fields)); } } diff --git a/document/src/vespa/document/fieldset/fieldsets.cpp b/document/src/vespa/document/fieldset/fieldsets.cpp index ae0703ed66b..586cef0e5f4 100644 --- a/document/src/vespa/document/fieldset/fieldsets.cpp +++ b/document/src/vespa/document/fieldset/fieldsets.cpp @@ -12,6 +12,8 @@ namespace { uint64_t computeHash(const Field::Set & set) { + if (set.empty()) return 0ul; + vespalib::asciistream os; for (const Field * field : set) { os << field->getName() << ':'; @@ -21,12 +23,11 @@ computeHash(const Field::Set & set) { } -FieldCollection::FieldCollection(const DocumentType& type) - : _set(), - _hash(0), +FieldCollection::FieldCollection(const DocumentType& type, Field::Set set) + : _set(std::move(set)), + _hash(computeHash(_set)), _docType(type) -{ -} +{ } FieldCollection::FieldCollection(const FieldCollection&) = default; @@ -67,16 +68,6 @@ FieldCollection::contains(const FieldSet& fields) const return false; } -FieldCollection & -FieldCollection::insertField(const Field& f) { - _set.insert(&f); - return *this; -} -void -FieldCollection::complete() { - _hash = computeHash(_set); -} - void FieldSet::copyFields(Document& dest, const Document& src, const FieldSet& fields) { diff --git a/document/src/vespa/document/fieldset/fieldsets.h b/document/src/vespa/document/fieldset/fieldsets.h index 44b6b8a6405..d37d72d6109 100644 --- a/document/src/vespa/document/fieldset/fieldsets.h +++ b/document/src/vespa/document/fieldset/fieldsets.h @@ -41,7 +41,7 @@ class FieldCollection : public FieldSet public: typedef std::unique_ptr<FieldCollection> UP; - FieldCollection(const DocumentType& docType); + FieldCollection(const DocumentType& docType, Field::Set set); FieldCollection(const FieldCollection &); FieldCollection(FieldCollection&&) = default; ~FieldCollection() override; @@ -57,15 +57,6 @@ public: } /** - * Inserts the given field into the collection. - */ - FieldCollection & insertField(const Field& f); - /** - * Must be called after all fields are inserted - */ - void complete(); - - /** * Returns all the fields contained in this collection. */ const Field::Set& getFields() const { return _set; } |