aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/reference/document_db_reference_resolver.cpp
blob: 30708b757ab3b2a13ea399b8613fd8b78dd2729b (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "document_db_reference_resolver.h"
#include "gid_to_lid_change_listener.h"
#include "gid_to_lid_change_registrator.h"
#include "i_document_db_reference.h"
#include "i_document_db_reference_registry.h"
#include <vespa/searchcore/proton/attribute/imported_attributes_repo.h>
#include <vespa/searchcommon/attribute/iattributevector.h>
#include <vespa/searchlib/attribute/iattributemanager.h>
#include <vespa/searchlib/attribute/imported_attribute_vector.h>
#include <vespa/searchlib/attribute/imported_attribute_vector_factory.h>
#include <vespa/searchlib/attribute/reference_attribute.h>
#include <vespa/config-imported-fields.h>
#include <vespa/document/datatype/documenttype.h>
#include <vespa/document/datatype/referencedatatype.h>

using document::DataType;
using document::DocumentType;
using document::DocumentTypeRepo;
using document::ReferenceDataType;
using search::attribute::BasicType;
using search::attribute::Config;
using search::attribute::IAttributeVector;
using search::attribute::ImportedAttributeVectorFactory;
using search::attribute::ReferenceAttribute;
using search::AttributeGuard;
using search::AttributeVector;
using search::IAttributeManager;
using search::NotImplementedAttribute;
using vespalib::ISequencedTaskExecutor;
using vespalib::MonitoredRefCount;
using vespalib::RetainGuard;
using vespa::config::search::ImportedFieldsConfig;

namespace proton {

namespace {

vespalib::string
getTargetDocTypeName(const vespalib::string &attrName,
                     const DocumentType &thisDocType)
{
    const DataType &attrType = thisDocType.getField(attrName).getDataType();
    const ReferenceDataType *refType = dynamic_cast<const ReferenceDataType *>(&attrType);
    assert(refType != nullptr);
    return refType->getTargetType().getName();
}

ReferenceAttribute::SP
getReferenceAttribute(const vespalib::string &name, const IAttributeManager &attrMgr)
{
    AttributeGuard::UP guard = attrMgr.getAttribute(name);
    assert(guard.get());
    assert(guard->get()->getBasicType() == BasicType::REFERENCE);
    return std::dynamic_pointer_cast<ReferenceAttribute>(guard->getSP());
}


std::vector<ReferenceAttribute::SP>
getReferenceAttributes(const IAttributeManager &attrMgr)
{
    std::vector<ReferenceAttribute::SP> result;
    std::vector<AttributeGuard> attributeList;
    attrMgr.getAttributeList(attributeList);
    for (auto &guard : attributeList) {
        AttributeVector &attr = *guard;
        if (attr.getBasicType() == BasicType::REFERENCE) {
            auto refAttr = std::dynamic_pointer_cast<ReferenceAttribute>(guard.getSP());
            assert(refAttr);
            result.push_back(std::move(refAttr));
        }
    }
    return result;
}

}

GidToLidChangeRegistrator &
DocumentDBReferenceResolver::getRegistrator(const vespalib::string &docTypeName)
{
    auto &result = _registrators[docTypeName];
    if (!result) {
        result = _registry.get(docTypeName)->makeGidToLidChangeRegistrator(_thisDocType.getName());
    }
    return *result;
}

IDocumentDBReference::SP
DocumentDBReferenceResolver::getTargetDocumentDB(const vespalib::string &refAttrName) const
{
    return _registry.get(getTargetDocTypeName(refAttrName, _thisDocType));
}

void
DocumentDBReferenceResolver::connectReferenceAttributesToGidMapper(const IAttributeManager &attrMgr)
{
    auto refAttrs(getReferenceAttributes(attrMgr));
    for (auto &attrSP : refAttrs) {
        auto &attr = *attrSP;
        IDocumentDBReference::SP targetDB = getTargetDocumentDB(attr.getName());
        attr.setGidToLidMapperFactory(targetDB->getGidToLidMapperFactory());
    }
}

void
DocumentDBReferenceResolver::detectOldListeners(const IAttributeManager &attrMgr)
{
    auto refAttrs(getReferenceAttributes(attrMgr));
    for (auto &attrSP : refAttrs) {
        vespalib::string docTypeName = getTargetDocTypeName(attrSP->getName(), _prevThisDocType);
        auto &registratorUP = _registrators[docTypeName];
        if (!registratorUP) {
            auto reference = _registry.tryGet(docTypeName);
            if (reference) {
                registratorUP = reference->makeGidToLidChangeRegistrator(_thisDocType.getName());
            }
        }
    }
}

void
DocumentDBReferenceResolver::listenToGidToLidChanges(const IAttributeManager &attrMgr)
{
    auto refAttrs(getReferenceAttributes(attrMgr));
    for (auto &attrSP : refAttrs) {
        auto &attr = *attrSP;
        vespalib::string docTypeName = getTargetDocTypeName(attr.getName(), _thisDocType);
        GidToLidChangeRegistrator &registrator = getRegistrator(docTypeName);
        auto listener = std::make_unique<GidToLidChangeListener>(_attributeFieldWriter, attrSP,
                                                                 RetainGuard(_refCount),
                                                                 attr.getName(), _thisDocType.getName());
        registrator.addListener(std::move(listener));
    }
}

ImportedAttributesRepo::UP
DocumentDBReferenceResolver::createImportedAttributesRepo(const IAttributeManager &attrMgr,
                                                          const std::shared_ptr<search::IDocumentMetaStoreContext> &documentMetaStore,
                                                          bool useSearchCache)
{
    auto result = std::make_unique<ImportedAttributesRepo>();
    if (_useReferences) {
        for (const auto &attr : _importedFieldsCfg.attribute) {
            ReferenceAttribute::SP refAttr = getReferenceAttribute(attr.referencefield, attrMgr);
            auto targetDocumentDB = getTargetDocumentDB(refAttr->getName());
            auto targetAttr = targetDocumentDB->getAttribute(attr.targetfield);
            if (targetAttr) {
                auto targetDocumentMetaStore = targetDocumentDB->getDocumentMetaStore();
                auto importedAttr = ImportedAttributeVectorFactory::create(attr.name, refAttr, documentMetaStore, targetAttr, targetDocumentMetaStore, useSearchCache);
                result->add(importedAttr->getName(), importedAttr);
            }
        }
    }
    return result;
}

DocumentDBReferenceResolver::DocumentDBReferenceResolver(const IDocumentDBReferenceRegistry &registry,
                                                         const DocumentType &thisDocType,
                                                         const ImportedFieldsConfig &importedFieldsCfg,
                                                         const document::DocumentType &prevThisDocType,
                                                         MonitoredRefCount &refCount,
                                                         ISequencedTaskExecutor &attributeFieldWriter,
                                                         bool useReferences)
    : _registry(registry),
      _thisDocType(thisDocType),
      _importedFieldsCfg(importedFieldsCfg),
      _prevThisDocType(prevThisDocType),
      _refCount(refCount),
      _attributeFieldWriter(attributeFieldWriter),
      _useReferences(useReferences),
      _registrators()
{
}

DocumentDBReferenceResolver::~DocumentDBReferenceResolver() = default;

ImportedAttributesRepo::UP
DocumentDBReferenceResolver::resolve(const IAttributeManager &newAttrMgr,
                                     const IAttributeManager &oldAttrMgr,
                                     const std::shared_ptr<search::IDocumentMetaStoreContext> &documentMetaStore,
                                     vespalib::duration visibilityDelay)
{
    detectOldListeners(oldAttrMgr);
    if (_useReferences) {
        connectReferenceAttributesToGidMapper(newAttrMgr);
        listenToGidToLidChanges(newAttrMgr);
    }
    return createImportedAttributesRepo(newAttrMgr, documentMetaStore, (visibilityDelay > vespalib::duration::zero()));
}

void
DocumentDBReferenceResolver::teardown(const IAttributeManager &oldAttrMgr)
{
    detectOldListeners(oldAttrMgr);
}

}