aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/diskindex/fieldreader.cpp
blob: c5f44f60dcca2ce70f8974abf8c5a549e1096fe0 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "fieldreader.h"
#include "zcposocc.h"
#include "extposocc.h"
#include "pagedict4file.h"
#include "field_length_scanner.h"
#include <filesystem>

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

#define NO_DOC static_cast<uint32_t>(-1)

namespace {

vespalib::string PosOccIdCooked = "PosOcc.3.Cooked";
vespalib::string interleaved_features("interleaved_features");

uint16_t cap_u16(uint32_t val) { return std::min(val, static_cast<uint32_t>(std::numeric_limits<uint16_t>::max())); }

}

using search::index::FieldLengthInfo;
using search::index::Schema;
using search::index::SchemaUtil;
using search::bitcompression::PosOccFieldParams;
using search::bitcompression::PosOccFieldsParams;
namespace fs = std::filesystem;

namespace search::diskindex {

FieldReader::FieldReader()
    : _wordNum(noWordNumHigh()),
      _docIdAndFeatures(),
      _dictFile(),
      _oldposoccfile(),
      _wordNumMapper(),
      _docIdMapper(),
      _oldWordNum(noWordNumHigh()),
      _residue(0u),
      _docIdLimit(0u),
      _word()
{
}


FieldReader::~FieldReader() = default;


void
FieldReader::readCounts()
{
    PostingListCounts counts;
    _dictFile->readWord(_word, _oldWordNum, counts);
    _oldposoccfile->readCounts(counts);
    if (_oldWordNum != noWordNumHigh()) {
        _wordNum = _wordNumMapper.map(_oldWordNum);
        assert(_wordNum != noWordNum());
        assert(_wordNum != noWordNumHigh());
        _residue = counts._numDocs;
    } else
        _wordNum = _oldWordNum;
}


void
FieldReader::readDocIdAndFeatures()
{
    _oldposoccfile->readDocIdAndFeatures(_docIdAndFeatures);
    _docIdAndFeatures.set_doc_id(_docIdMapper.mapDocId(_docIdAndFeatures.doc_id()));
}


void
FieldReader::read()
{
    for (;;) {
        while (_residue == 0) {
            readCounts();
            if (_wordNum == noWordNumHigh()) {
                assert(_residue == 0);
                _docIdAndFeatures.set_doc_id(NO_DOC);
                return;
            }
        }
        --_residue;
        readDocIdAndFeatures();
        if (_docIdAndFeatures.doc_id() != NO_DOC) {
            return;
        }
    }
}


bool
FieldReader::allowRawFeatures()
{
    return true;
}

bool
FieldReader::need_regenerate_interleaved_features_scan()
{
    return false;
}

void
FieldReader::scan_element_lengths(uint32_t)
{
}

void
FieldReader::setup(const WordNumMapping &wordNumMapping,
                   const DocIdMapping &docIdMapping)
{
    _wordNumMapper.setup(wordNumMapping);
    _docIdMapper.setup(docIdMapping);
}


bool
FieldReader::open(const vespalib::string &prefix,
                  const TuneFileSeqRead &tuneFileRead)
{
    vespalib::string name = prefix + "posocc.dat.compressed";

    if (!fs::exists(fs::path(name))) {
        LOG(error, "Compressed posocc file %s does not exist.", name.c_str());
        return false;
    }

    _dictFile = std::make_unique<PageDict4FileSeqRead>();
    PostingListParams featureParams;
    _oldposoccfile = makePosOccRead(name, _dictFile.get(), featureParams, tuneFileRead);
    vespalib::string cname = prefix + "dictionary";

    if (!_dictFile->open(cname, tuneFileRead)) {
        LOG(error, "Could not open posocc count file %s for read", cname.c_str());
        return false;
    }

    // open posocc.dat
    if (!_oldposoccfile || !_oldposoccfile->open(name, tuneFileRead)) {
        LOG(error, "Could not open posocc file %s for read", name.c_str());
        return false;
    }
    _oldWordNum = noWordNum();
    _wordNum = _oldWordNum;
    PostingListParams params;
    _oldposoccfile->getParams(params);
    params.get("docIdLimit", _docIdLimit);
    return true;
}

bool
FieldReader::close()
{
    bool ret = true;

    if (_oldposoccfile) {
        bool closeRes = _oldposoccfile->close();
        if (!closeRes) {
            LOG(error, "Could not close posocc file for read");
            ret = false;
        }
        _oldposoccfile.reset();
    }
    if (_dictFile) {
        bool closeRes = _dictFile->close();
        if (!closeRes) {
            LOG(error, "Could not close posocc file for read");
            ret = false;
        }
        _dictFile.reset();
    }

    return ret;
}


void
FieldReader::setFeatureParams(const PostingListParams &params)
{
    _oldposoccfile->setFeatureParams(params);
}


void
FieldReader::getFeatureParams(PostingListParams &params)
{
    _oldposoccfile->getFeatureParams(params);
}

const FieldLengthInfo &
FieldReader::get_field_length_info() const
{
    return _oldposoccfile->get_field_length_info();
}

std::unique_ptr<FieldReader>
FieldReader::allocFieldReader(const SchemaUtil::IndexIterator &index,
                              const Schema &oldSchema,
                              std::shared_ptr<FieldLengthScanner> field_length_scanner)
{
    assert(index.isValid());
    if (index.hasMatchingOldFields(oldSchema)) {
        if (!index.use_interleaved_features() ||
            index.has_matching_use_interleaved_features(oldSchema)) {
            return std::make_unique<FieldReader>();      // The common case
        }
    }
    if (!index.hasOldFields(oldSchema)) {
        return std::make_unique<FieldReaderEmpty>(index); // drop data
    }
    // field exists in old schema with different collection type setting
    // or old field is missing wanted interleaved features.
    return std::make_unique<FieldReaderStripInfo>(index, field_length_scanner);   // degraded
}


FieldReaderEmpty::FieldReaderEmpty(const IndexIterator &index)
    : _index(index)
{
}


bool
FieldReaderEmpty::open(const vespalib::string &prefix,
                       const TuneFileSeqRead &tuneFileRead)
{
    (void) prefix;
    (void) tuneFileRead;
    return true;
}


void
FieldReaderEmpty::getFeatureParams(PostingListParams &params)
{
    PosOccFieldsParams fieldsParams;
    fieldsParams.setSchemaParams(_index.getSchema(), _index.getIndex());
    params.clear();
    fieldsParams.getParams(params);
}


FieldReaderStripInfo::FieldReaderStripInfo(const IndexIterator &index, std::shared_ptr<FieldLengthScanner> field_length_scanner)
    : _hasElements(false),
      _hasElementWeights(false),
      _want_interleaved_features(index.use_interleaved_features()),
      _regenerate_interleaved_features(false),
      _field_length_scanner(std::move(field_length_scanner))
{
    PosOccFieldsParams fieldsParams;
    fieldsParams.setSchemaParams(index.getSchema(), index.getIndex());
    assert(fieldsParams.getNumFields() > 0);
    const PosOccFieldParams &fieldParams = fieldsParams.getFieldParams()[0];
    _hasElements = fieldParams._hasElements;
    _hasElementWeights = fieldParams._hasElementWeights;
}


bool
FieldReaderStripInfo::allowRawFeatures()
{
    return false;
}

bool
FieldReaderStripInfo::open(const vespalib::string &prefix, const TuneFileSeqRead &tuneFileRead)
{
    if (!FieldReader::open(prefix, tuneFileRead)) {
        return false;
    }
    if (_want_interleaved_features) {
        PostingListParams params;
        bool decode_interleaved_features = false;
        _oldposoccfile->getParams(params);
        params.get(interleaved_features, decode_interleaved_features);
        if (!decode_interleaved_features) {
            _regenerate_interleaved_features = true;
        }
        if (!_hasElements) {
            _regenerate_interleaved_features = true;
        }
    }
    return true;
}

bool
FieldReaderStripInfo::need_regenerate_interleaved_features_scan()
{
    return (_regenerate_interleaved_features && _hasElements && _field_length_scanner);
}

void
FieldReaderStripInfo::scan_element_lengths(uint32_t scan_chunk)
{
    if (!isValid()) {
        return;
    }
    while (scan_chunk != 0u) {
        FieldReader::read();
        if (!isValid()) {
            break;
        }
        DocIdAndFeatures &features = _docIdAndFeatures;
        assert(!features.has_raw_data());
        _field_length_scanner->scan_features(features);
        --scan_chunk;
    }
}

void
FieldReaderStripInfo::read()
{
    for (;;) {
        FieldReader::read();
        DocIdAndFeatures &features = _docIdAndFeatures;
        if (_wordNum == noWordNumHigh()) {
            return;
        }
        assert(!features.has_raw_data());
        uint32_t numElements = features.elements().size();
        assert(numElements > 0);
        auto element = features.elements().begin();
        if (_hasElements) {
            if (!_hasElementWeights) {
                for (uint32_t elementDone = 0; elementDone < numElements; ++elementDone, ++element) {
                    element->setWeight(1);
                }
                assert(element == features.elements().end());
            }
        } else {
            if (element->getElementId() != 0) {
                continue;   // Drop this entry, try to read new entry
            }
            element->setWeight(1);
            features.word_positions().resize(element->getNumOccs());
            if (numElements > 1) {
                features.elements().resize(1);
            }
        }
        break;
    }
    if (_regenerate_interleaved_features) {
        // Regenerate interleaved featues from normal features.
        uint32_t field_length = 0;
        uint32_t num_occs = 0;
        DocIdAndFeatures &features = _docIdAndFeatures;
        for (const auto &element : features.elements()) {
            field_length += element.getElementLen();
            num_occs += element.getNumOccs();
        }
        if (_hasElements && _field_length_scanner) {
            field_length = _field_length_scanner->get_field_length(features.doc_id());
        }
        // cap interleaved features to 16 bits each, to match memory index
        features.set_field_length(cap_u16(field_length));
        features.set_num_occs(cap_u16(num_occs));
    }
}


void
FieldReaderStripInfo::getFeatureParams(PostingListParams &params)
{
    FieldReader::getFeatureParams(params);
    vespalib::string paramsPrefix = PosOccFieldParams::getParamsPrefix(0);
    vespalib::string collStr = paramsPrefix + ".collectionType";
    if (_hasElements) {
        if (_hasElementWeights) {
            params.setStr(collStr, "weightedSet");
        } else {
            params.setStr(collStr, "array");
        }
    } else
        params.setStr(collStr, "single");
    params.erase("encoding");
}

}