aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/diskindex/bitvectoridxfile.cpp
blob: ba7f19b4b0b31ce5a40fd94efbe4b8c3f81d7fae (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "bitvectoridxfile.h"
#include <vespa/searchlib/common/fileheadercontext.h>
#include <vespa/searchlib/index/bitvectorkeys.h>
#include <vespa/searchlib/util/file_settings.h>
#include <vespa/vespalib/data/fileheader.h>
#include <vespa/vespalib/util/size_literals.h>
#include <vespa/fastlib/io/bufferedfile.h>
#include <cassert>

namespace search::diskindex {

using search::index::BitVectorWordSingleKey;
using search::common::FileHeaderContext;

namespace {

void
readHeader(vespalib::FileHeader &h, const vespalib::string &name)
{
    Fast_BufferedFile file(32_Ki);
    file.ReadOpenExisting(name.c_str());
    h.readFile(file);
}

}

BitVectorIdxFileWrite::BitVectorIdxFileWrite(BitVectorKeyScope scope)
    : _idxFile(),
      _numKeys(0),
      _docIdLimit(0),
      _idxHeaderLen(0),
      _scope(scope)
{
}

BitVectorIdxFileWrite::~BitVectorIdxFileWrite() = default;

uint64_t
BitVectorIdxFileWrite::idxSize() const
{
    return _idxHeaderLen +
        static_cast<int64_t>(_numKeys) * sizeof(BitVectorWordSingleKey);
}

void
BitVectorIdxFileWrite::open(const vespalib::string &name,
                         uint32_t docIdLimit,
                         const TuneFileSeqWrite &tuneFileWrite,
                         const FileHeaderContext &fileHeaderContext)
{
    if (_numKeys != 0) {
        assert(docIdLimit == _docIdLimit);
    } else {
        _docIdLimit = docIdLimit;
    }
    vespalib::string idxname = name + getBitVectorKeyScopeSuffix(_scope);

    assert( !_idxFile);
    _idxFile = std::make_unique<Fast_BufferedFile>(new FastOS_File());
    if (tuneFileWrite.getWantSyncWrites()) {
        _idxFile->EnableSyncWrites();
    }
    if (tuneFileWrite.getWantDirectIO()) {
        _idxFile->EnableDirectIO();
    }

    _idxFile->WriteOpen(idxname.c_str());

    if (_idxHeaderLen == 0) {
        assert(_numKeys == 0);
        makeIdxHeader(fileHeaderContext);
    }

    int64_t pos = idxSize();

    int64_t oldidxsize = _idxFile->getSize();
    assert(oldidxsize >= pos);
    (void) oldidxsize;

    _idxFile->SetSize(pos);

    assert(pos == _idxFile->getPosition());
}

void
BitVectorIdxFileWrite::makeIdxHeader(const FileHeaderContext &fileHeaderContext)
{
    vespalib::FileHeader h(FileSettings::DIRECTIO_ALIGNMENT);
    using Tag = vespalib::GenericHeader::Tag;
    fileHeaderContext.addTags(h, _idxFile->GetFileName());
    h.putTag(Tag("docIdLimit", _docIdLimit));
    h.putTag(Tag("numKeys", _numKeys));
    h.putTag(Tag("frozen", 0));
    if (_scope != BitVectorKeyScope::SHARED_WORDS) {
        h.putTag(Tag("fileBitSize", 0));
    }
    h.putTag(Tag("desc", "Bitvector dictionary file, single words"));
    _idxFile->SetPosition(0);
    _idxHeaderLen = h.writeFile(*_idxFile);
    _idxFile->Flush();
}

void
BitVectorIdxFileWrite::updateIdxHeader(uint64_t fileBitSize)
{
    vespalib::FileHeader h(FileSettings::DIRECTIO_ALIGNMENT);
    using Tag = vespalib::GenericHeader::Tag;
    readHeader(h, _idxFile->GetFileName());
    FileHeaderContext::setFreezeTime(h);
    h.putTag(Tag("numKeys", _numKeys));
    h.putTag(Tag("frozen", 1));
    if (_scope != BitVectorKeyScope::SHARED_WORDS) {
        h.putTag(Tag("fileBitSize", fileBitSize));
    }
    bool sync_ok = _idxFile->Sync();
    assert(sync_ok);
    assert(h.getSize() == _idxHeaderLen);
    _idxFile->SetPosition(0);
    h.writeFile(*_idxFile);
    sync_ok = _idxFile->Sync();
    assert(sync_ok);
}

void
BitVectorIdxFileWrite::addWordSingle(uint64_t wordNum, uint32_t numDocs)
{
    BitVectorWordSingleKey key;
    key._wordNum = wordNum;
    key._numDocs = numDocs;
    _idxFile->WriteBuf(&key, sizeof(key));
    ++_numKeys;
}

void
BitVectorIdxFileWrite::flush()
{
    _idxFile->Flush();

    uint64_t pos = _idxFile->getPosition();
    assert(pos == idxSize());
    (void) pos;
}

void
BitVectorIdxFileWrite::syncCommon()
{
    bool sync_ok = _idxFile->Sync();
    assert(sync_ok);
}

void
BitVectorIdxFileWrite::sync()
{
    flush();
    syncCommon();
}

void
BitVectorIdxFileWrite::close()
{
    if (_idxFile) {
        if (_idxFile->IsOpened()) {
            uint64_t pos = _idxFile->getPosition();
            assert(pos == idxSize());
            _idxFile->alignEndForDirectIO();
            updateIdxHeader(pos * 8);
            bool close_ok = _idxFile->Close();
            assert(close_ok);
        }
        _idxFile.reset();
    }
}

}