aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/attribute/attribute_writer.h
blob: 8c9b756cd895acf6049dddf328d1852dcad8c305 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "i_attribute_manager.h"
#include "i_attribute_writer.h"
#include <vespa/searchcore/proton/common/commit_time_tracker.h>
#include <vespa/document/base/fieldpath.h>
#include <vespa/searchlib/common/isequencedtaskexecutor.h>
#include <vespa/vespalib/stllike/hash_map.h>

namespace document { class DocumentType; }

namespace proton {

/**
 * Concrete attribute writer that handles writes in form of put, update and remove
 * to the attribute vectors managed by the underlying attribute manager.
 */
class AttributeWriter : public IAttributeWriter
{
private:
    typedef search::AttributeVector AttributeVector;
    typedef document::FieldPath FieldPath;
    typedef document::DataType DataType;
    typedef document::DocumentType DocumentType;
    typedef document::FieldValue FieldValue;
    const IAttributeManager::SP _mgr;
    search::ISequencedTaskExecutor &_attributeFieldWriter;
    using ExecutorId = search::ISequencedTaskExecutor::ExecutorId;
public:
    class WriteField
    {
        FieldPath        _fieldPath;
        AttributeVector &_attribute;
        bool             _structFieldAttribute; // in array/map of struct
    public:
        WriteField(AttributeVector &attribute);
        ~WriteField();
        AttributeVector &getAttribute() const { return _attribute; }
        const FieldPath &getFieldPath() const { return _fieldPath; }
        void buildFieldPath(const DocumentType &docType);
        bool isStructFieldAttribute() const { return _structFieldAttribute; }
    };
    class WriteContext
    {
        ExecutorId _executorId;
        std::vector<WriteField> _fields;
        bool _hasStructFieldAttribute;
    public:
        WriteContext(ExecutorId executorId);
        WriteContext(WriteContext &&rhs) noexcept;
        ~WriteContext();
        WriteContext &operator=(WriteContext &&rhs) noexcept;
        void buildFieldPaths(const DocumentType &docType);
        void add(AttributeVector &attr);
        ExecutorId getExecutorId() const { return _executorId; }
        const std::vector<WriteField> &getFields() const { return _fields; }
        bool hasStructFieldAttribute() const { return _hasStructFieldAttribute; }
    };
private:
    using AttrWithId = std::pair<search::AttributeVector *, ExecutorId>;
    using AttrMap = vespalib::hash_map<vespalib::string, AttrWithId>;
    std::vector<WriteContext> _writeContexts;
    const DataType           *_dataType;
    bool                      _hasStructFieldAttribute;
    AttrMap                   _attrMap;

    void setupWriteContexts();
    void setupAttriuteMapping();
    void buildFieldPaths(const DocumentType &docType, const DataType *dataType);
    void internalPut(SerialNum serialNum, const Document &doc, DocumentIdT lid,
                     bool immediateCommit, bool allAttributes, OnWriteDoneType onWriteDone);
    void internalRemove(SerialNum serialNum, DocumentIdT lid,
                        bool immediateCommit, OnWriteDoneType onWriteDone);

public:
    AttributeWriter(proton::IAttributeManager::SP mgr);
    ~AttributeWriter();

    /* Only for in tests that add attributes after AttributeWriter construction. */

    /**
     * Implements IAttributeWriter.
     */
    std::vector<search::AttributeVector *> getWritableAttributes() const override;
    search::AttributeVector *getWritableAttribute(const vespalib::string &name) const override;
    void put(SerialNum serialNum, const Document &doc, DocumentIdT lid,
             bool immediateCommit, OnWriteDoneType onWriteDone) override;
    void remove(SerialNum serialNum, DocumentIdT lid,
                bool immediateCommit, OnWriteDoneType onWriteDone) override;
    void remove(const LidVector &lidVector, SerialNum serialNum,
                bool immediateCommit, OnWriteDoneType onWriteDone) override;
    void update(SerialNum serialNum, const DocumentUpdate &upd, DocumentIdT lid,
                bool immediateCommit, OnWriteDoneType onWriteDone, IFieldUpdateCallback & onUpdate) override;
    void update(SerialNum serialNum, const Document &doc, DocumentIdT lid,
                bool immediateCommit, OnWriteDoneType onWriteDone) override;
    void heartBeat(SerialNum serialNum) override;
    void compactLidSpace(uint32_t wantedLidLimit, SerialNum serialNum) override;
    const proton::IAttributeManager::SP &getAttributeManager() const override {
        return _mgr;
    }
    void forceCommit(SerialNum serialNum, OnWriteDoneType onWriteDone) override;

    void onReplayDone(uint32_t docIdLimit) override;
    bool hasStructFieldAttribute() const override;
};

} // namespace proton