aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/index/postinglistfile.h
blob: 93d0dd362f794bf6a81de9375f6646134f60be33 (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
// Copyright Yahoo. 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 <vespa/searchlib/common/tunefileinfo.h>
#include <vespa/vespalib/stllike/string.h>

class FastOS_FileInterface;

namespace search::common { class FileHeaderContext; }

namespace search::index {

class DocIdAndFeatures;
class FieldLengthInfo;
class PostingListParams;

/**
 * Interface for posting list files containing document ids and features
 * for words.
 */
class PostingListFileSeqRead {
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);

    virtual const FieldLengthInfo &get_field_length_info() const = 0;
};

/**
 * 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:
    using SP = std::shared_ptr<PostingListFileRandRead>;

    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 std::unique_ptr<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;

    virtual const FieldLengthInfo &get_field_length_info() const = 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();

    std::unique_ptr<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;
};

}