aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/common/documentsummary.cpp
blob: f004e5cc4cf21688b2229611eed5da2df49eda76 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "documentsummary.h"
#include <vespa/fastlib/io/bufferedfile.h>
#include <vespa/vespalib/util/size_literals.h>
#include <vespa/vespalib/util/error.h>

#include <vespa/log/log.h>
LOG_SETUP(".searchlib.docsummary.documentsummary");

using vespalib::getLastErrorString;

namespace search::docsummary {

bool
DocumentSummary::readDocIdLimit(const vespalib::string &dir, uint32_t &count)
{
    char numbuf[20];
    Fast_BufferedFile qcntfile(4_Ki);
    unsigned int qcnt;
    vespalib::string qcntname;
    const char *p;

    qcntname = dir + "/docsum.qcnt";

    count = qcnt = 0;
    // XXX no checking for success
    qcntfile.ReadOpen(qcntname.c_str());
    if (!qcntfile.IsOpened() || qcntfile.Eof())
        return false;
    p = qcntfile.ReadLine(numbuf, sizeof(numbuf));
    while (*p >= '0' && *p <= '9')
        qcnt = qcnt * 10 + *p++ - '0';
    count = qcnt;
    return true;
}


bool
DocumentSummary::writeDocIdLimit(const vespalib::string &dir, uint32_t count)
{
    vespalib::string qcntname = dir + "/docsum.qcnt";
    Fast_BufferedFile qcntfile(new FastOS_File);

    qcntfile.WriteOpen(qcntname.c_str());
    if (!qcntfile.IsOpened()) {
        LOG(error, "Could not open %s: %s", qcntname.c_str(), getLastErrorString().c_str());
        return false;
    }
    qcntfile.addNum(count, 0, ' ');
    qcntfile.WriteByte('\n');
    if ( ! qcntfile.Sync() ) {
        LOG(error, "Could not sync %s: %s", qcntname.c_str(), getLastErrorString().c_str());
        return false;
    }
    if ( ! qcntfile.Close() ) {
        LOG(error, "Could not sync %s: %s", qcntname.c_str(), getLastErrorString().c_str());
        return false;
    }
    return true;
}

}