summaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/attribute/document_field_retriever.cpp
blob: 243d36bdeadb04c45be2e11d4e83340d9ade9d79 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "document_field_retriever.h"
#include <vespa/document/fieldvalue/arrayfieldvalue.h>
#include <vespa/document/fieldvalue/weightedsetfieldvalue.h>
#include <vespa/document/fieldvalue/tensorfieldvalue.h>
#include <vespa/searchcommon/attribute/attributecontent.h>
#include <vespa/searchlib/tensor/tensor_attribute.h>
#include <vespa/eval/tensor/tensor.h>
#include <vespa/vespalib/util/exceptions.h>

#include <vespa/log/log.h>
LOG_SETUP(".proton.attribute.document_field_retriever");

using document::ArrayFieldValue;
using document::Document;
using document::Field;
using document::FieldValue;
using document::TensorFieldValue;
using document::WeightedSetFieldValue;
using search::DocumentIdT;
using search::attribute::AttributeContent;
using search::attribute::BasicType;
using search::attribute::CollectionType;
using search::attribute::IAttributeVector;
using search::attribute::WeightedType;
using search::tensor::TensorAttribute;
using vespalib::IllegalStateException;

namespace proton {

namespace {

template <typename T>
void
setValue(DocumentIdT lid,
         Document &doc,
         const vespalib::string &fieldName,
         const IAttributeVector &attr)
{
    switch (attr.getCollectionType()) {
    case CollectionType::SINGLE:
    {
        if ( ! attr.isUndefined(lid) ) {
            AttributeContent<T> content;
            content.fill(attr, lid);
            doc.set(fieldName, content[0]);
        } else {
            doc.remove(fieldName);
        }
        break;
    }
    case CollectionType::ARRAY:
    {
        AttributeContent<T> content;
        content.fill(attr, lid);
        Field f = doc.getField(fieldName);
        if (!doc.getValue(f) && content.size() == 0) {
            break;
        }
        FieldValue::UP fv = f.getDataType().createFieldValue();
        if (fv.get() && fv->getClass().id() != ArrayFieldValue::classId) {
            throw IllegalStateException("Field " + fieldName + " does not contain an array.", VESPA_STRLOC);
        }
        ArrayFieldValue &array = static_cast<ArrayFieldValue &>(*fv.get());
        array.resize(content.size());
        for (uint32_t j(0); j < content.size(); ++j) {
            array[j] = content[j];
        }
        doc.setValue(f, *fv);
        break;
    }
    case CollectionType::WSET:
    {
        AttributeContent<WeightedType<T> > content;
        content.fill(attr, lid);
        Field f = doc.getField(fieldName);
        if (!doc.getValue(f) && content.size() == 0) {
            break;
        }
        FieldValue::UP fv = f.getDataType().createFieldValue();
        if (fv.get() && fv->getClass().id() != WeightedSetFieldValue::classId) {
            throw IllegalStateException("Field " + fieldName + " does not contain a wset.", VESPA_STRLOC);
        }
        WeightedSetFieldValue & wset(static_cast<WeightedSetFieldValue &>(*fv.get()));
        wset.resize(content.size());
        auto it(wset.begin());
        for (uint32_t j(0); j < content.size(); ++j, ++it) {
            *it->first = content[j].getValue();
            *it->second = content[j].getWeight();
        }
        doc.setValue(f, *fv);
        break;
    }
    default:
        LOG(warning, "Unknown attribute collection type in attribute.");
        break;
    }
}

}

void
DocumentFieldRetriever::populate(DocumentIdT lid,
                                 Document &doc,
                                 const vespalib::string &fieldName,
                                 const IAttributeVector &attr,
                                 bool isIndexField)
{
    switch (attr.getBasicType()) {
    case BasicType::UINT1:
    case BasicType::UINT2:
    case BasicType::UINT4:
    case BasicType::INT8:
    case BasicType::INT16:
    case BasicType::INT32:
    case BasicType::INT64:
        setValue<IAttributeVector::largeint_t>(
                lid, doc, fieldName, attr);
        break;
    case BasicType::FLOAT:
    case BasicType::DOUBLE:
        setValue<double>(lid, doc, fieldName, attr);
        break;
    case BasicType::STRING:
        // If it is a stringfield we also need to check if
        // it is an index field. In that case we shall
        // keep the original in order to preserve annotations.
        if (isIndexField) {
            break;
        }
        setValue<const char *>(lid, doc, fieldName, attr);
        break;
    case BasicType::PREDICATE:
        // Predicate attribute doesn't store documents, it only indexes them.
        break;
    case BasicType::TENSOR:
        // Tensor attribute is not authorative.  Partial updates must update
        // document store.
        break;
    case BasicType::REFERENCE:
        // Reference attribute doesn't store full document id.
        break;
    default:
        LOG(warning, "Unknown attribute data type in attribute.");
    }
}

} // namespace proton