aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/bitcompression/posocc_field_params.cpp
blob: 1276ed410d9f57176d1618f6f5a4a817333accae (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "posocc_field_params.h"
#include <vespa/searchcommon/common/schema.h>
#include <vespa/searchlib/index/postinglistparams.h>
#include <vespa/vespalib/data/fileheader.h>
#include <vespa/vespalib/stllike/asciistream.h>
#include <cassert>

#include <vespa/log/log.h>
LOG_SETUP(".posocc_field_params");

using vespalib::GenericHeader;

namespace search::bitcompression {

namespace schema = search::index::schema;

PosOccFieldParams::PosOccFieldParams()
    : _elemLenK(0),
      _hasElements(false),
      _hasElementWeights(false),
      _avgElemLen(512),
      _collectionType(SINGLE),
      _name(),
      _field_length_info()
{ }


bool
PosOccFieldParams::operator==(const PosOccFieldParams &rhs) const
{
    return _collectionType == rhs._collectionType &&
              _avgElemLen == rhs._avgElemLen &&
                     _name == rhs._name;
}


vespalib::string
PosOccFieldParams::getParamsPrefix(uint32_t idx)
{
    vespalib::asciistream paramsPrefix;
    paramsPrefix << "fieldParams.";
    paramsPrefix << idx;
    return paramsPrefix.str();
}


void
PosOccFieldParams::getParams(PostingListParams &params, uint32_t idx) const
{
    vespalib::string paramsPrefix = getParamsPrefix(idx);
    vespalib::string collStr = paramsPrefix + ".collectionType";
    vespalib::string avgElemLenStr = paramsPrefix + ".avgElemLen";
    vespalib::string nameStr = paramsPrefix + ".name";

    switch (_collectionType) {
    case SINGLE:
        params.setStr(collStr, "single");
        break;
    case ARRAY:
        params.setStr(collStr, "array");
        break;
    case WEIGHTEDSET:
        params.setStr(collStr, "weightedSet");
        break;
    }
    params.set(avgElemLenStr, _avgElemLen);
    params.setStr(nameStr, _name);
}


void
PosOccFieldParams::setParams(const PostingListParams &params, uint32_t idx)
{
    vespalib::string paramsPrefix = getParamsPrefix(idx);
    vespalib::string collStr = paramsPrefix + ".collectionType";
    vespalib::string avgElemLenStr = paramsPrefix + ".avgElemLen";
    vespalib::string nameStr = paramsPrefix + ".name";

    if (params.isSet(collStr)) {
        vespalib::string collVal = params.getStr(collStr);
        if (collVal == "single") {
            _collectionType = SINGLE;
            _hasElements = false;
            _hasElementWeights = false;
        } else if (collVal == "array") {
            _collectionType = ARRAY;
            _hasElements = true;
            _hasElementWeights = false;
        } else if (collVal == "weightedSet") {
            _collectionType = WEIGHTEDSET;
            _hasElements = true;
            _hasElementWeights = true;
        }
    }
    params.get(avgElemLenStr, _avgElemLen);
    if (params.isSet(nameStr)) {
        _name = params.getStr(nameStr);
    }
}


void
PosOccFieldParams::setSchemaParams(const Schema &schema, uint32_t fieldId)
{
    assert(fieldId < schema.getNumIndexFields());
    const Schema::IndexField &field = schema.getIndexField(fieldId);
    switch (field.getCollectionType()) {
    case schema::CollectionType::SINGLE:
        _collectionType = SINGLE;
        _hasElements = false;
        _hasElementWeights = false;
        break;
    case schema::CollectionType::ARRAY:
        _collectionType = ARRAY;
        _hasElements = true;
        _hasElementWeights = false;
        break;
    case schema::CollectionType::WEIGHTEDSET:
        _collectionType = WEIGHTEDSET;
        _hasElements = true;
        _hasElementWeights = true;
        break;
    default:
        LOG(error, "Bad collection type");
        LOG_ABORT("should not be reached");
    }
    _avgElemLen = field.getAvgElemLen();
    _name = field.getName();
}

namespace {

vespalib::string field_length_infix = "field_length.";

struct FieldLengthKeys {
    vespalib::string _average;
    vespalib::string _samples;
    FieldLengthKeys(const vespalib::string &prefix);
    ~FieldLengthKeys();
};

FieldLengthKeys::FieldLengthKeys(const vespalib::string &prefix)
    : _average(prefix + field_length_infix + "average"),
      _samples(prefix + field_length_infix + "samples")
{
}

FieldLengthKeys::~FieldLengthKeys() = default;

}

void
PosOccFieldParams::readHeader(const GenericHeader &header,
                              const vespalib::string &prefix)
{
    using Tag = GenericHeader::Tag;
    vespalib::string nameKey(prefix + "fieldName");
    vespalib::string collKey(prefix + "collectionType");
    vespalib::string avgElemLenKey(prefix + "avgElemLen");
    FieldLengthKeys field_length_keys(prefix);

    _name = header.getTag(nameKey).asString();
    Schema::CollectionType ct = schema::collectionTypeFromName(header.getTag(collKey).asString());
    switch (ct) {
    case schema::CollectionType::SINGLE:
        _collectionType = SINGLE;
        _hasElements = false;
        _hasElementWeights = false;
        break;
    case schema::CollectionType::ARRAY:
        _collectionType = ARRAY;
        _hasElements = true;
        _hasElementWeights = false;
        break;
    case schema::CollectionType::WEIGHTEDSET:
        _collectionType = WEIGHTEDSET;
        _hasElements = true;
        _hasElementWeights = true;
        break;
    default:
        LOG_ABORT("Bad collection type when reading field param in header");
    }
    _avgElemLen = header.getTag(avgElemLenKey).asInteger();
    if (header.hasTag(field_length_keys._average) &&
        header.hasTag(field_length_keys._samples)) {
        const auto &average_field_length_tag = header.getTag(field_length_keys._average);
        const auto &field_length_samples_tag = header.getTag(field_length_keys._samples);
        if (average_field_length_tag.getType() == Tag::Type::TYPE_FLOAT &&
            field_length_samples_tag.getType() == Tag::Type::TYPE_INTEGER) {
            _field_length_info = index::FieldLengthInfo(average_field_length_tag.asFloat(), field_length_samples_tag.asInteger());
        }
    }
}


void
PosOccFieldParams::writeHeader(GenericHeader &header,
                               const vespalib::string &prefix) const
{
    using Tag = GenericHeader::Tag;
    vespalib::string nameKey(prefix + "fieldName");
    vespalib::string collKey(prefix + "collectionType");
    vespalib::string avgElemLenKey(prefix + "avgElemLen");
    FieldLengthKeys field_length_keys(prefix);
    header.putTag(Tag(nameKey, _name));
    Schema::CollectionType ct(schema::CollectionType::SINGLE);
    switch (_collectionType) {
    case SINGLE:
        ct = schema::CollectionType::SINGLE;
        break;
    case ARRAY:
        ct = schema::CollectionType::ARRAY;
        break;
    case WEIGHTEDSET:
        ct = schema::CollectionType::WEIGHTEDSET;
        break;
    default:
        LOG_ABORT("Bad collection type when writing field param in header");
    }
    header.putTag(Tag(collKey, schema::getTypeName(ct)));
    header.putTag(Tag(avgElemLenKey, _avgElemLen));
    header.putTag(Tag(field_length_keys._average, _field_length_info.get_average_field_length()));
    header.putTag(Tag(field_length_keys._samples, static_cast<int64_t>(_field_length_info.get_num_samples())));
}

}