summaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/docstore/file_chunk/file_chunk_test.cpp
blob: 9af5eeabab0c9e2debef5106a8f35cc59dbc3919 (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
// Copyright 2017 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/testkit/test_kit.h>

#include <vespa/searchlib/common/fileheadercontext.h>
#include <vespa/searchlib/docstore/filechunk.h>
#include <vespa/searchlib/docstore/writeablefilechunk.h>
#include <vespa/searchlib/test/directory_handler.h>
#include <vespa/vespalib/test/insertion_operators.h>
#include <vespa/vespalib/util/threadstackexecutor.h>
#include <iomanip>
#include <iostream>

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

using namespace search;

using common::FileHeaderContext;
using vespalib::ThreadStackExecutor;

struct MyFileHeaderContext : public FileHeaderContext {
    virtual void addTags(vespalib::GenericHeader &header, const vespalib::string &name) const override {
        (void) header;
        (void) name;
    }
};

struct SetLidObserver : public ISetLid {
    std::vector<uint32_t> lids;
    virtual void setLid(const vespalib::LockGuard &guard, uint32_t lid, const LidInfo &lidInfo) override {
        (void) guard;
        (void) lidInfo;
        lids.push_back(lid);
    }
};

struct BucketizerObserver : public IBucketizer {
    mutable std::vector<uint32_t> lids;
    virtual document::BucketId getBucketOf(const vespalib::GenerationHandler::Guard &guard, uint32_t lid) const override {
        (void) guard;
        lids.push_back(lid);
        return document::BucketId();
    }
    virtual vespalib::GenerationHandler::Guard getGuard() const override {
        return vespalib::GenerationHandler::Guard();
    }
};

vespalib::string
getData(uint32_t lid)
{
    std::ostringstream oss;
    oss << "data_" << std::setw(5) << std::setfill('0') << lid;
    return oss.str();
}

struct Fixture {
    test::DirectoryHandler dir;
    ThreadStackExecutor executor;
    uint64_t serialNum;
    TuneFileSummary tuneFile;
    MyFileHeaderContext fileHeaderCtx;
    vespalib::Lock updateLock;
    SetLidObserver lidObserver;
    BucketizerObserver bucketizer;
    WriteableFileChunk chunk;

    uint64_t nextSerialNum() {
        return serialNum++;
    };

    Fixture(const vespalib::string &baseName,
            uint32_t docIdLimit,
            bool dirCleanup = true)
        : dir(baseName),
          executor(1, 0x10000),
          serialNum(1),
          tuneFile(),
          fileHeaderCtx(),
          updateLock(),
          lidObserver(),
          bucketizer(),
          chunk(executor,
                FileChunk::FileId(0),
                FileChunk::NameId(1234),
                baseName,
                serialNum,
                docIdLimit,
                WriteableFileChunk::Config(document::CompressionConfig(), 0x1000),
                tuneFile,
                fileHeaderCtx,
                &bucketizer,
                false)
    {
        dir.cleanup(dirCleanup);
    }
    ~Fixture() {}
    void flush() {
        chunk.flush(true, serialNum);
        chunk.flushPendingChunks(serialNum);
    }
    Fixture &append(uint32_t lid) {
        vespalib::string data = getData(lid);
        chunk.append(nextSerialNum(), lid, data.c_str(), data.size());
        return *this;
    }
    void updateLidMap(uint32_t docIdLimit) {
        vespalib::LockGuard guard(updateLock);
        chunk.updateLidMap(guard, lidObserver, serialNum, docIdLimit);
    }
    void assertLidMap(const std::vector<uint32_t> &expLids) {
        EXPECT_EQUAL(expLids, lidObserver.lids);
    }
    void assertBucketizer(const std::vector<uint32_t> &expLids) {
        EXPECT_EQUAL(expLids, bucketizer.lids);
    }
};

TEST_F("require that idx file without docIdLimit in header can be read", Fixture("without_doc_id_limit", 1000, false))
{
    EXPECT_EQUAL(std::numeric_limits<uint32_t>::max(), f.chunk.getDocIdLimit());
}

TEST("require that docIdLimit is written to and read from idx file header")
{
    {
        Fixture f("tmp", 1000, false);
    }
    {
        Fixture f("tmp", 0);
        EXPECT_EQUAL(1000u, f.chunk.getDocIdLimit());
    }
}

TEST("require that entries with lid >= docIdLimit are skipped in updateLidMap()")
{
    {
        Fixture f("tmp", 0, false);
        f.append(1).append(10).append(100).append(999).append(1000).append(1001).append(998).append(1002).append(999);
        f.flush();
    }
    {
        Fixture f("tmp", 0);
        f.updateLidMap(1000);
        f.assertLidMap({1,10,100,999,998,999});
        f.assertBucketizer({1,10,100,999,998,999});
        size_t entrySize = 10 + 8;
        EXPECT_EQUAL(9 * entrySize, f.chunk.getAddedBytes());
        EXPECT_EQUAL(3u, f.chunk.getBloatCount());
        EXPECT_EQUAL(3 * entrySize, f.chunk.getErasedBytes());
    }
}

TEST_MAIN() { TEST_RUN_ALL(); }