aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/fieldset/fieldset.h
blob: 5b67701e39536b36c34d7f669225d52b2038df30 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <memory>

namespace document {

class Document;
class DocumentTypeRepo;

/**
 * FieldSet class. Represents a subset of fields in a document type.
 * Note that the document id is counted as a field in this context,
 * but referenced by the special name "[id]"
 */
class FieldSet
{
public:
    enum class Type {
        FIELD,
        SET,
        ALL,
        NONE,
        DOCID,
        DOCUMENT_ONLY
    };

    using SP = std::shared_ptr<FieldSet>;
    using UP = std::unique_ptr<FieldSet>;

    virtual ~FieldSet() = default;

    /**
     * @return Return true if all the fields in "fields" are contained in
     * this field set.
     */
    virtual bool contains(const FieldSet& fields) const = 0;

    /**
     * @return Returns the type of field set this is.
     */
    virtual Type getType() 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.
     * NOTE: causes each field to be explicitly copied so thus not very
     * efficient. Prefer using stripFields for cases where a document
     * needs to only contain fields matching a given field set and can
     * readily be modified in-place.
     */
    static void copyFields(Document& dest, const Document& src, const FieldSet& fields);

    /**
     * Creates a copy of document src containing only the fields given by
     * the fieldset. Document type and identifier remain the same.
     * See comment for copyFields() for performance notes.
     * @return The new, (partially) copied document instance.
     */
    static std::unique_ptr<Document> createDocumentSubsetCopy(const DocumentTypeRepo& type_repo, const Document& src, const FieldSet& fields);

    /**
     * Strip all fields _except_ the ones that are contained within the
     * fieldsToKeep fieldset. Modifies original document in-place.
     */
    static void stripFields(Document& doc, const FieldSet& fieldsToKeep);
};

}