summaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/index/postinglistfile.h
blob: 194ac519a19b8fb1c72ce4c8eba87a8d9dc92f32 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "postinglistcounts.h"
#include "postinglisthandle.h"
#include "postinglistparams.h"
#include <vespa/searchlib/common/tunefileinfo.h>

class FastOS_FileInterface;

namespace search::common { class FileHeaderContext; }

namespace search::index {

class DocIdAndFeatures;

/**
 * Interface for posting list files containing document ids and features
 * for words.
 */
class PostingListFileSeqRead {
protected:
    PostingListCounts _counts;
    unsigned int _residueDocs;  // Docids left to read for word
public:
    PostingListFileSeqRead();

    virtual ~PostingListFileSeqRead();

    /**
     * Read document id and features.
     */
    virtual void readDocIdAndFeatures(DocIdAndFeatures &features) = 0;

    /**
     * Read counts for a word.
     */
    virtual void readCounts(const PostingListCounts &counts) = 0;

    /**
     * Open posting list file for sequential read.
     */
    virtual bool open(const vespalib::string &name, const TuneFileSeqRead &tuneFileRead) = 0;

    /**
     * Close posting list file.
     */
    virtual bool close() = 0;

    /*
     * Get current parameters.
     */
    virtual void getParams(PostingListParams &params);

    /*
     * Set (word, docid) feature parameters.
     *
     * Typically can only enable or disable cooked features.
     */
    virtual void setFeatureParams(const PostingListParams &params);

    /*
     * Get current (word, docid) feature parameters.
     */
    virtual void getFeatureParams(PostingListParams &params);

    // Methods used when generating posting list for common word pairs.

    /*
     * Get current posting offset, measured in bits.  First posting list
     * starts at 0, i.e.  file header is not accounted for here.
     *
     * @return current posting offset, measured in bits.
     */
    virtual uint64_t getCurrentPostingOffset() const = 0;

    /**
     * Set current posting offset, measured in bits.  First posting
     * list starts at 0, i.e.  file header is not accounted for here.
     *
     * @param Offset start of posting lists for word pair.
     * @param endOffset end of posting lists for word pair.
     * @param readAheadOffset end of posting list for either this or a
     *               later word pair, depending on disk seek cost.
     */
    virtual void setPostingOffset(uint64_t offset, uint64_t endOffset, uint64_t readAheadOffset) = 0;

    /**
     * Get counts read by last readCounts().
     */
    const PostingListCounts &getCounts() const { return _counts; }

    PostingListCounts &getCounts() { return _counts; }
};

/**
 * Interface for posting list files containing document ids and features
 * for words.
 */
class PostingListFileSeqWrite {
protected:
    PostingListCounts _counts;
public:
    PostingListFileSeqWrite();
    virtual ~PostingListFileSeqWrite();

    /**
     * Write document id and features.
     */
    virtual void writeDocIdAndFeatures(const DocIdAndFeatures &features) = 0;

    /**
     * Flush word (during write) after it is complete to buffers, i.e.
     * prepare for next word, but not for application crash.
     */
    virtual void flushWord() = 0;

    /**
     * Open posting list file for sequential write.
     */
    virtual bool
    open(const vespalib::string &name,
         const TuneFileSeqWrite &tuneFileWrite,
         const common::FileHeaderContext &fileHeaderContext) = 0;

    /**
     * Close posting list file.
     */
    virtual bool close() = 0;

    /*
     * Set parameters.
     */
    virtual void setParams(const PostingListParams &params);

    /*
     * Get current parameters.
     */
    virtual void getParams(PostingListParams &params);

    /*
     * Set (word, docid) feature parameters.
     */
    virtual void setFeatureParams(const PostingListParams &params);

    /*
     * Get current (word, docid) feature parameters.
     */
    virtual void getFeatureParams(PostingListParams &params);

    PostingListCounts &getCounts() { return _counts; }
};


/**
 * Interface for posting list files containing document ids and features
 * for words.
 */
class PostingListFileRandRead {
protected:
    // Can be examined after open
    bool _memoryMapped;
public:
    typedef std::shared_ptr<PostingListFileRandRead> SP;

    PostingListFileRandRead();
    virtual ~PostingListFileRandRead();

    /**
     * Create iterator for single word.  Semantic lifetime of counts and
     * handle must exceed lifetime of iterator.
     *
     * XXX: TODO: How to read next set of segments from disk if handle
     * didn't cover the whole word, probably need access to higher level
     * API above caches.
     */
    virtual search::queryeval::SearchIterator *
    createIterator(const PostingListCounts &counts,
                   const PostingListHandle &handle,
                   const search::fef::TermFieldMatchDataArray &matchData,
                   bool usebitVector) const = 0;


    /**
     * Read (possibly partial) posting list into handle.
     */
    virtual void
    readPostingList(const PostingListCounts &counts,
                    uint32_t firstSegment,
                    uint32_t numSegments,
                    PostingListHandle &handle) = 0;

    /**
     * Open posting list file for random read.
     */
    virtual bool open(const vespalib::string &name, const TuneFileRandRead &tuneFileRead) = 0;

    /**
     * Close posting list file.
     */
    virtual bool close() = 0;

    bool getMemoryMapped() const { return _memoryMapped; }

protected:
    void afterOpen(FastOS_FileInterface &file);
};


/**
 * Passthrough class.
 */
class PostingListFileRandReadPassThrough : public PostingListFileRandRead {
protected:
    PostingListFileRandRead *_lower;
    bool _ownLower;

public:
    PostingListFileRandReadPassThrough(PostingListFileRandRead *lower, bool ownLower);
    ~PostingListFileRandReadPassThrough();

    search::queryeval::SearchIterator *
    createIterator(const PostingListCounts &counts,
                   const PostingListHandle &handle,
                   const search::fef::TermFieldMatchDataArray &matchData,
                   bool usebitVector) const override;

    void readPostingList(const PostingListCounts &counts, uint32_t firstSegment,
                         uint32_t numSegments, PostingListHandle &handle) override;

    bool open(const vespalib::string &name, const TuneFileRandRead &tuneFileRead) override;
    bool close() override;
};

}