summaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/diskindex/fieldwriter.cpp
blob: 7449a946286bb2bcc652e741bb002564e0a21062 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/fastos/fastos.h>
#include <vespa/log/log.h>
#include <vespa/vespalib/objects/nbostream.h>
#include "fieldwriter.h"
#include "zcposocc.h"
#include "extposocc.h"
#include <vespa/vespalib/util/error.h>
#include "pagedict4file.h"
LOG_SETUP(".diskindex.fieldwriter");

namespace search
{

namespace diskindex
{

using vespalib::nbostream;
using vespalib::getLastErrorString;
using common::FileHeaderContext;

FieldWriter::FieldWriter(uint32_t docIdLimit,
                         uint64_t numWordIds)
    : _wordNum(noWordNum()),
      _prevDocId(0),
      _dictFile(),
      _posoccfile(),
      _bvc(docIdLimit),
      _bmapfile(BitVectorKeyScope::PERFIELD_WORDS),
      _docIdLimit(docIdLimit),
      _numWordIds(numWordIds),
      _prefix(),
      _compactWordNum(0),
      _word()
{
}


void
FieldWriter::earlyOpen(const vespalib::string &prefix,
                       uint32_t minSkipDocs,
                       uint32_t minChunkDocs,
                       bool dynamicKPosOccFormat,
                       const Schema &schema,
                       const uint32_t indexId,
                       const TuneFileSeqWrite &tuneFileWrite)
{
    _prefix = prefix;
    vespalib::string name = prefix + "posocc.dat.compressed";

    PostingListParams params;
    PostingListParams featureParams;
    PostingListParams countParams;

    diskindex::setupDefaultPosOccParameters(&countParams,
            &params,
            _numWordIds,
            _docIdLimit);

    if (minSkipDocs != 0) {
        countParams.set("minSkipDocs", minSkipDocs);
        params.set("minSkipDocs", minSkipDocs);
    }
    if (minChunkDocs != 0) {
        countParams.set("minChunkDocs", minChunkDocs);
        params.set("minChunkDocs", minChunkDocs);
    }

    _dictFile.reset(new PageDict4FileSeqWrite);
    _dictFile->setParams(countParams);

    _posoccfile.reset(diskindex::makePosOccWrite(name,
                                                 _dictFile.get(),
                                                 dynamicKPosOccFormat,
                                                 params,
                                                 featureParams,
                                                 schema,
                                                 indexId,
                                                 tuneFileWrite));
}


bool
FieldWriter::lateOpen(const TuneFileSeqWrite &tuneFileWrite,
                      const FileHeaderContext &fileHeaderContext)
{
    vespalib::string cname = _prefix + "dictionary";
    vespalib::string name = _prefix + "posocc.dat.compressed";

    // Open output dictionary file
    if (!_dictFile->open(cname, tuneFileWrite, fileHeaderContext)) {
        LOG(error, "Could not open posocc count file %s for write: %s",
            cname.c_str(), getLastErrorString().c_str());
        return false;
    }

    // Open output posocc.dat file
    if (!_posoccfile->open(name, tuneFileWrite, fileHeaderContext)) {
        LOG(error, "Could not open posocc file %s for write: %s",
            name.c_str(), getLastErrorString().c_str());
        return false;
    }

    // Open output boolocc.bdat file
    vespalib::string booloccbidxname = _prefix + "boolocc";
    _bmapfile.open(booloccbidxname.c_str(), _docIdLimit, tuneFileWrite,
                   fileHeaderContext);

    return true;
}


void
FieldWriter::flush(void)
{
    _posoccfile->flushWord();
    PostingListCounts &counts = _posoccfile->getCounts();
    if (counts._numDocs != 0) {
        assert(_compactWordNum != 0);
        _dictFile->writeWord(_word, counts);
        // Write bitmap entries
        if (_bvc.getCrossedBitVectorLimit())
            _bmapfile.addWordSingle(_compactWordNum, _bvc.getBitVector());
        _bvc.clear();
        counts.clear();
    } else {
        assert(counts._bitLength == 0);
        assert(_bvc.empty());
        assert(_compactWordNum == 0);
    }
}


void
FieldWriter::newWord(uint64_t wordNum, const vespalib::stringref &word)
{
    assert(wordNum <= _numWordIds);
    assert(wordNum != noWordNum());
    assert(wordNum > _wordNum);
    flush();
    _wordNum = wordNum;
    ++_compactWordNum;
    _word = word;
    _prevDocId = 0;
}


void
FieldWriter::newWord(const vespalib::stringref &word)
{
    newWord(_wordNum + 1, word);
}


bool
FieldWriter::close(void)
{
    bool ret = true;
    flush();
    _wordNum = noWordNum();
    if (_posoccfile) {
        bool closeRes = _posoccfile->close();
        if (!closeRes) {
            LOG(error,
                "Could not close posocc file for write");
            ret = false;
        }
        _posoccfile.reset();
    }
    if (_dictFile) {
        bool closeRes = _dictFile->close();
        if (!closeRes) {
            LOG(error,
                "Could not close posocc count file for write");
            ret = false;
        }
        _dictFile.reset();
    }

    _bmapfile.close();
    return ret;
}


void
FieldWriter::checkPointWrite(nbostream &out)
{
    out << _wordNum << _prevDocId;
    out << _docIdLimit << _numWordIds;
    out << _compactWordNum << _word;
    _posoccfile->checkPointWrite(out);
    _dictFile->checkPointWrite(out);
    _bvc.checkPointWrite(out);
    _bmapfile.checkPointWrite(out);
}


void
FieldWriter::checkPointRead(nbostream &in)
{
    in >> _wordNum >> _prevDocId;
    uint32_t checkDocIdLimit = 0;
    uint64_t checkNumWordIds = 0;
    in >> checkDocIdLimit >> checkNumWordIds;
    assert(checkDocIdLimit == _docIdLimit);
    assert(checkNumWordIds == _numWordIds);
    in >> _compactWordNum >> _word;
    _posoccfile->checkPointRead(in);
    _dictFile->checkPointRead(in);
    _bvc.checkPointRead(in);
    _bmapfile.checkPointRead(in);
}


void
FieldWriter::setFeatureParams(const PostingListParams &params)
{
    _posoccfile->setFeatureParams(params);
}


void
FieldWriter::getFeatureParams(PostingListParams &params)
{
    _posoccfile->getFeatureParams(params);
}


static const char *termOccNames[] =
{
    "boolocc.bdat",
    "boolocc.bidx",
    "boolocc.idx",
    "posocc.ccnt",
    "posocc.cnt",
    "posocc.dat.compressed",
    "dictionary.pdat",
    "dictionary.spdat",
    "dictionary.ssdat",
    "dictionary.words",
    NULL,
};


void
FieldWriter::remove(const vespalib::string &prefix)
{
    for (const char **j = termOccNames; *j != NULL; ++j) {
        vespalib::string tmpName = prefix + *j;
        FastOS_File::Delete(tmpName.c_str());
    }
}


} // namespace diskindex

} // namespace search