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

#include "dictionarywordreader.h"
#include <vespa/searchlib/index/schemautil.h>
#include <vespa/vespalib/util/error.h>
#include <vespa/fastlib/io/bufferedfile.h>

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

namespace search::diskindex {

using vespalib::getLastErrorString;
using index::SchemaUtil;

DictionaryWordReader::DictionaryWordReader()
    : _word(),
      _wordNum(noWordNumHigh()),
      _old2newwordfile(),
      _dictFile()
{
}


DictionaryWordReader::~DictionaryWordReader() = default;


bool
DictionaryWordReader::open(const vespalib::string & dictionaryName,
                           const vespalib::string & wordMapName,
                           const TuneFileSeqRead &tuneFileRead)
{
    _old2newwordfile.reset(new Fast_BufferedFile(new FastOS_File));
    _dictFile.reset(new PageDict4FileSeqRead);
    if (!_dictFile->open(dictionaryName, tuneFileRead)) {
        LOG(error, "Could not open dictionary %s: %s",
            dictionaryName.c_str(), getLastErrorString().c_str());
        return false;
    }
    _wordNum = noWordNum();

    // Make a mapping from old to new wordID
    if (tuneFileRead.getWantDirectIO()) {
        _old2newwordfile->EnableDirectIO();
    }
    // no checking possible
    _old2newwordfile->OpenWriteOnly(wordMapName.c_str());
    _old2newwordfile->SetSize(0);

    return true;
}

void
DictionaryWordReader::writeNewWordNum(uint64_t newWordNum) {
    _old2newwordfile->WriteBuf(&newWordNum, sizeof(newWordNum));
}

void
DictionaryWordReader::close()
{
    if (!_dictFile->close()) {
        LOG(error, "Error closing input dictionary");
    }
    bool sync_ok = _old2newwordfile->Sync();
    assert(sync_ok);
    bool close_ok = _old2newwordfile->Close();
    assert(close_ok);
}

}