aboutsummaryrefslogtreecommitdiffstats
path: root/document
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2020-08-20 14:47:39 +0200
committerGitHub <noreply@github.com>2020-08-20 14:47:39 +0200
commit2d68435a3261b3a70c472ab5138d39a5074cf9d5 (patch)
treea02190d11c90148a44c41964d412abbabed1d3d9 /document
parentb214393ad98132794d6c7a09f00599541c6ed372 (diff)
Revert "Revert "Balder/use an actual fieldset repo""
Diffstat (limited to 'document')
-rw-r--r--document/src/tests/fieldsettest.cpp17
-rw-r--r--document/src/vespa/document/base/field.h1
-rw-r--r--document/src/vespa/document/datatype/documenttype.h7
-rw-r--r--document/src/vespa/document/fieldset/fieldset.h8
-rw-r--r--document/src/vespa/document/fieldset/fieldsetrepo.cpp45
-rw-r--r--document/src/vespa/document/fieldset/fieldsetrepo.h11
-rw-r--r--document/src/vespa/document/fieldset/fieldsets.h17
7 files changed, 58 insertions, 48 deletions
diff --git a/document/src/tests/fieldsettest.cpp b/document/src/tests/fieldsettest.cpp
index 29581ff4549..af23e713735 100644
--- a/document/src/tests/fieldsettest.cpp
+++ b/document/src/tests/fieldsettest.cpp
@@ -31,7 +31,7 @@ TEST_F(FieldSetTest, testParsing)
(void) dynamic_cast<NoFields&>(*FieldSetRepo::parse(docRepo, NoFields::NAME));
(void) dynamic_cast<DocIdOnly&>(*FieldSetRepo::parse(docRepo, DocIdOnly::NAME));
- FieldSet::UP set = FieldSetRepo::parse(docRepo, "testdoctype1:headerval,content");
+ auto set = FieldSetRepo::parse(docRepo, "testdoctype1:headerval,content");
auto & coll = dynamic_cast<FieldCollection&>(*set);
std::ostringstream ost;
@@ -46,8 +46,8 @@ namespace {
bool checkContains(const DocumentTypeRepo& repo,
const std::string& str1, const std::string & str2) {
- FieldSet::UP set1 = FieldSetRepo::parse(repo, str1);
- FieldSet::UP set2 = FieldSetRepo::parse(repo, str2);
+ auto set1 = FieldSetRepo::parse(repo, str1);
+ auto set2 = FieldSetRepo::parse(repo, str2);
return set1->contains(*set2);
}
@@ -141,7 +141,7 @@ FieldSetTest::doCopyFields(const Document& src,
if (!dest) {
dest = &destDoc;
}
- FieldSet::UP fset = FieldSetRepo::parse(docRepo, fieldSetStr);
+ auto fset = FieldSetRepo::parse(docRepo, fieldSetStr);
FieldSet::copyFields(*dest, src, *fset);
return stringifyFields(*dest);
}
@@ -152,7 +152,7 @@ FieldSetTest::doStripFields(const Document& doc,
const std::string& fieldSetStr)
{
Document::UP copy(doc.clone());
- FieldSet::UP fset = FieldSetRepo::parse(docRepo, fieldSetStr);
+ auto fset = FieldSetRepo::parse(docRepo, fieldSetStr);
FieldSet::stripFields(*copy, *fset);
return stringifyFields(*copy);
}
@@ -198,7 +198,7 @@ FieldSetTest::doCopyDocument(const Document& src,
const DocumentTypeRepo& docRepo,
const std::string& fieldSetStr)
{
- FieldSet::UP fset = FieldSetRepo::parse(docRepo, fieldSetStr);
+ auto fset = FieldSetRepo::parse(docRepo, fieldSetStr);
Document::UP doc(FieldSet::createDocumentSubsetCopy(src, *fset));
return stringifyFields(*doc);
}
@@ -244,10 +244,9 @@ TEST_F(FieldSetTest, testSerialize)
"testdoctype1:content,hstringval"
};
- FieldSetRepo repo;
for (const char * fieldSet : fieldSets) {
- FieldSet::UP fs = FieldSetRepo::parse(docRepo, fieldSet);
- EXPECT_EQ(vespalib::string(fieldSet), repo.serialize(*fs));
+ auto fs = FieldSetRepo::parse(docRepo, fieldSet);
+ EXPECT_EQ(vespalib::string(fieldSet), FieldSetRepo::serialize(*fs));
}
}
diff --git a/document/src/vespa/document/base/field.h b/document/src/vespa/document/base/field.h
index 7580b2b692f..305021ef29a 100644
--- a/document/src/vespa/document/base/field.h
+++ b/document/src/vespa/document/base/field.h
@@ -88,7 +88,6 @@ public:
*/
Field(vespalib::stringref name, const DataType &dataType);
- Field* clone() const override { return new Field(*this); }
std::unique_ptr<FieldValue> createValue() const;
// Note that only id is checked for equality.
diff --git a/document/src/vespa/document/datatype/documenttype.h b/document/src/vespa/document/datatype/documenttype.h
index ed6e9e66ab5..fae65addb48 100644
--- a/document/src/vespa/document/datatype/documenttype.h
+++ b/document/src/vespa/document/datatype/documenttype.h
@@ -61,12 +61,10 @@ public:
DocumentType();
DocumentType(vespalib::stringref name, int32_t id);
- DocumentType(vespalib::stringref name, int32_t id,
- const StructDataType& fields);
+ DocumentType(vespalib::stringref name, int32_t id, const StructDataType& fields);
explicit DocumentType(vespalib::stringref name);
- DocumentType(vespalib::stringref name,
- const StructDataType& fields);
+ DocumentType(vespalib::stringref name, const StructDataType& fields);
~DocumentType() override;
@@ -101,6 +99,7 @@ public:
DocumentType & addFieldSet(const vespalib::string & name, FieldSet::Fields fields);
const FieldSet * getFieldSet(const vespalib::string & name) const;
+ const FieldSetMap & getFieldSets() const { return _fieldSets; }
const ImportedFieldNames& imported_field_names() const noexcept {
return _imported_field_names;
diff --git a/document/src/vespa/document/fieldset/fieldset.h b/document/src/vespa/document/fieldset/fieldset.h
index 35c912c5e45..3d74659ebf5 100644
--- a/document/src/vespa/document/fieldset/fieldset.h
+++ b/document/src/vespa/document/fieldset/fieldset.h
@@ -23,7 +23,8 @@ public:
DOCID
};
- typedef std::unique_ptr<FieldSet> UP;
+ using SP = std::shared_ptr<FieldSet>;
+ using UP = std::unique_ptr<FieldSet>;
virtual ~FieldSet() = default;
@@ -39,11 +40,6 @@ public:
virtual Type getType() const = 0;
/**
- * @return Returns a copy of this object.
- */
- virtual FieldSet* clone() const = 0;
-
- /**
* Copy all fields from src into dest that are contained within the
* given field set. If any copied field pre-exists in dest, it will
* be overwritten.
diff --git a/document/src/vespa/document/fieldset/fieldsetrepo.cpp b/document/src/vespa/document/fieldset/fieldsetrepo.cpp
index 33cbf6185c4..5bde291c8dd 100644
--- a/document/src/vespa/document/fieldset/fieldsetrepo.cpp
+++ b/document/src/vespa/document/fieldset/fieldsetrepo.cpp
@@ -5,6 +5,7 @@
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/vespalib/stllike/asciistream.h>
#include <vespa/document/repo/documenttyperepo.h>
+#include <vespa/vespalib/stllike/hash_map.hpp>
using vespalib::StringTokenizer;
@@ -12,27 +13,25 @@ namespace document {
namespace {
-FieldSet::UP
+FieldSet::SP
parseSpecialValues(vespalib::stringref name)
{
- FieldSet::UP fs;
if ((name.size() == 4) && (name[1] == 'i') && (name[2] == 'd') && (name[3] == ']')) {
- fs = std::make_unique<DocIdOnly>();
+ return std::make_shared<DocIdOnly>();
} else if ((name.size() == 5) && (name[1] == 'a') && (name[2] == 'l') && (name[3] == 'l') && (name[4] == ']')) {
- fs = std::make_unique<AllFields>();
+ return std::make_shared<AllFields>();
} else if ((name.size() == 6) && (name[1] == 'n') && (name[2] == 'o') && (name[3] == 'n') && (name[4] == 'e') && (name[5] == ']')) {
- fs = std::make_unique<NoFields>();
+ return std::make_shared<NoFields>();
} else if ((name.size() == 7) && (name[1] == 'd') && (name[2] == 'o') && (name[3] == 'c') && (name[4] == 'i') && (name[5] == 'd') && (name[6] == ']')) {
- fs = std::make_unique<DocIdOnly>();
+ return std::make_shared<DocIdOnly>();
} else {
throw vespalib::IllegalArgumentException(
"The only special names (enclosed in '[]') allowed are "
"id, all, none, not '" + name + "'.");
}
- return fs;
}
-FieldSet::UP
+FieldSet::SP
parseFieldCollection(const DocumentTypeRepo& repo,
vespalib::stringref docType,
vespalib::stringref fieldNames)
@@ -55,12 +54,12 @@ parseFieldCollection(const DocumentTypeRepo& repo,
builder.add(&type.getField(token));
}
}
- return std::make_unique<FieldCollection>(type, builder.build());
+ return std::make_shared<FieldCollection>(type, builder.build());
}
}
-FieldSet::UP
+FieldSet::SP
FieldSetRepo::parse(const DocumentTypeRepo& repo, vespalib::stringref str)
{
if (str[0] == '[') {
@@ -111,5 +110,31 @@ FieldSetRepo::serialize(const FieldSet& fieldSet)
}
}
+
+FieldSetRepo::FieldSetRepo(const DocumentTypeRepo& repo)
+ : _doumentTyperepo(repo),
+ _configuredFieldSets()
+{
+ repo.forEachDocumentType(*vespalib::makeClosure(this, &FieldSetRepo::configureDocumentType));
+}
+FieldSetRepo::~FieldSetRepo() = default;
+
+void
+FieldSetRepo::configureDocumentType(const DocumentType & documentType) {
+ for (const auto & entry : documentType.getFieldSets()) {
+ vespalib::string fieldSetName(documentType.getName());
+ fieldSetName.append(':').append(entry.first);
+ _configuredFieldSets[fieldSetName] = parse(_doumentTyperepo, fieldSetName);
+ }
+}
+FieldSet::SP
+FieldSetRepo::getFieldSet(vespalib::stringref fieldSetString) const {
+ auto found = _configuredFieldSets.find(fieldSetString);
+ if (found != _configuredFieldSets.end()) {
+ return found->second;
+ }
+ return parse(_doumentTyperepo, fieldSetString);
+}
+
}
diff --git a/document/src/vespa/document/fieldset/fieldsetrepo.h b/document/src/vespa/document/fieldset/fieldsetrepo.h
index a744aa572e5..d213230848a 100644
--- a/document/src/vespa/document/fieldset/fieldsetrepo.h
+++ b/document/src/vespa/document/fieldset/fieldsetrepo.h
@@ -16,10 +16,17 @@ class DocumentTypeRepo;
class FieldSetRepo
{
public:
- static FieldSet::UP parse(const DocumentTypeRepo& repo,
- vespalib::stringref fieldSetString);
+ FieldSetRepo(const DocumentTypeRepo& repo);
+ ~FieldSetRepo();
+ FieldSet::SP getFieldSet(vespalib::stringref fieldSetString) const;
+
+ static FieldSet::SP parse(const DocumentTypeRepo& repo, vespalib::stringref fieldSetString);
static vespalib::string serialize(const FieldSet& fs);
+private:
+ void configureDocumentType(const DocumentType & documentType);
+ const DocumentTypeRepo & _doumentTyperepo;
+ vespalib::hash_map<vespalib::string, FieldSet::SP> _configuredFieldSets;
};
}
diff --git a/document/src/vespa/document/fieldset/fieldsets.h b/document/src/vespa/document/fieldset/fieldsets.h
index 9537a5bdf61..e71a96e5a7e 100644
--- a/document/src/vespa/document/fieldset/fieldsets.h
+++ b/document/src/vespa/document/fieldset/fieldsets.h
@@ -13,7 +13,6 @@ public:
static constexpr const char * NAME = "[all]";
bool contains(const FieldSet&) const override { return true; }
Type getType() const override { return Type::ALL; }
- FieldSet* clone() const override { return new AllFields(); }
};
class NoFields final : public FieldSet
@@ -22,7 +21,6 @@ public:
static constexpr const char * NAME = "[none]";
bool contains(const FieldSet& f) const override { return f.getType() == Type::NONE; }
Type getType() const override { return Type::NONE; }
- FieldSet* clone() const override { return new NoFields(); }
};
class DocIdOnly final : public FieldSet
@@ -33,7 +31,6 @@ public:
return fields.getType() == Type::DOCID || fields.getType() == Type::NONE;
}
Type getType() const override { return Type::DOCID; }
- FieldSet* clone() const override { return new DocIdOnly(); }
};
class FieldCollection : public FieldSet
@@ -49,20 +46,8 @@ public:
bool contains(const FieldSet& fields) const override;
Type getType() const override { return Type::SET; }
- /**
- * @return Returns the document type the collection is associated with.
- */
- const DocumentType& getDocumentType() const {
- return _docType;
- }
-
- /**
- * Returns all the fields contained in this collection.
- */
+ const DocumentType& getDocumentType() const { return _docType; }
const Field::Set& getFields() const { return _set; }
-
- FieldSet* clone() const override { return new FieldCollection(*this); }
-
uint64_t hash() const { return _hash; }
private:
Field::Set _set;