aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/index/postinglisthandle.h
blob: 1f3a72a876f9f4400a7e3cbcba072521cccc9918 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "postinglistcounts.h"
#include <memory>
#include <cstdlib>

namespace search { class BitVector; }
namespace search::queryeval { class SearchIterator; }
namespace search::fef { class TermFieldMatchDataArray; }

namespace search::index {

class PostingListFileRandRead;

/**
 * Class for owning a posting list in memory after having read it from
 * posting list file, or referencing a chunk of memory containing the
 * posting list (if the file was memory mapped).
 */
class PostingListHandle {
public:
    using UP = std::unique_ptr<PostingListHandle>;
    // Key portion
    PostingListFileRandRead *_file; // File containing posting list
    uint64_t _bitOffset;    // posting list start relative to start of file
    uint64_t _bitLength;    // Length of posting list, in bits

    // Value portion
    uint32_t _firstSegment; // First segment for word
    uint32_t _numSegments;  // Number of segments
    uint64_t _bitOffsetMem; // _mem relative to start of file
    const void *_mem;       // Memory backing posting list after read/mmap
    void *_allocMem;        // What to free after posting list
    size_t _allocSize;      // Size of allocated memory

    PostingListHandle()
    : _file(nullptr),
      _bitOffset(0),
      _bitLength(0),
      _firstSegment(0),
      _numSegments(0),
      _bitOffsetMem(0),
      _mem(nullptr),
      _allocMem(nullptr),
      _allocSize(0)
    { }

    ~PostingListHandle()
    {
        if (_allocMem != nullptr) {
            free(_allocMem);
        }
    }

    /**
     * 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.
     */
    std::unique_ptr<search::queryeval::SearchIterator>
    createIterator(const PostingListCounts &counts,
                   const search::fef::TermFieldMatchDataArray &matchData,
                   bool useBitVector=false) const;

    /**
     * Drop value portion of handle.
     */
    void drop() {
        _firstSegment = 0;
        _numSegments = 0;
        _bitOffsetMem = 0;
        _mem = nullptr;
        if (_allocMem != nullptr) {
            free(_allocMem);
            _allocMem = nullptr;
        }
        _allocSize = 0;
    }
};

}