aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/common/bitvectorcache.h
blob: 5622910a69afe57a76b6f1bd309dcf49210030c8 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "condensedbitvectors.h"
#include <vespa/vespalib/util/sync.h>
#include <vespa/vespalib/stllike/hash_set.h>
#include <vespa/fastos/dynamiclibrary.h>

namespace search {

class PopulateInterface
{
public:
    class Iterator {
    public:
        typedef std::unique_ptr<Iterator> UP;
        virtual ~Iterator() { }
        virtual int32_t getNext() = 0;
    };
    virtual ~PopulateInterface() { }
    virtual Iterator::UP lookup(uint64_t key) const = 0;
};

class BitVectorCache
{
public:
    typedef uint64_t Key;
    typedef vespalib::hash_set<Key> KeySet;
    typedef std::vector<std::pair<Key, size_t>> KeyAndCountSet;
    typedef CondensedBitVector::CountVector CountVector;
    typedef vespalib::GenerationHolder GenerationHolder;

    BitVectorCache(GenerationHolder &genHolder);
    ~BitVectorCache();
    void computeCountVector(KeySet & keys, CountVector & v) const;
    KeySet lookupCachedSet(const KeyAndCountSet & keys);
    void set(Key key, uint32_t index, bool v);
    bool get(Key key, uint32_t index) const;
    void removeIndex(uint32_t index);
    void adjustDocIdLimit(uint32_t docId);
    void populate(uint32_t count, const PopulateInterface &);
    bool needPopulation() const { return _needPopulation; }
private:
    class KeyMeta {
    public:
        KeyMeta() :
            _lookupCount(0),
            _bitCount(0),
            _chunkId(-1),
            _chunkIndex(0)
        { }
        double       cost()  const { return _bitCount * _lookupCount; }
        bool     isCached()  const { return _chunkId >= 0; }
        size_t   bitCount()  const { return _bitCount; }
        size_t chunkIndex()  const { return _chunkIndex; }
        size_t    chunkId()  const { return _chunkId; }
        size_t lookupCount() const { return _lookupCount; }
        KeyMeta & incBits() {    _bitCount++; return *this; }
        KeyMeta & decBits() {    _bitCount--; return *this; }
        KeyMeta &  lookup() { _lookupCount++; return *this; }
        KeyMeta &   bitCount(uint32_t v) {   _bitCount = v; return *this; }
        KeyMeta &    chunkId(uint32_t v) {    _chunkId = v; return *this; }
        KeyMeta & chunkIndex(uint32_t v) { _chunkIndex = v; return *this; }
        KeyMeta & unCache()              { _chunkId = -1; return *this; }
    private:
        size_t   _lookupCount;
        uint32_t _bitCount;
        int32_t  _chunkId;
        uint32_t _chunkIndex;
    };
    typedef vespalib::hash_map<Key, KeyMeta> Key2Index;
    typedef std::vector<std::pair<Key, KeyMeta *>> SortedKeyMeta;
    typedef std::vector<CondensedBitVector::SP> ChunkV;

    VESPA_DLL_LOCAL static SortedKeyMeta getSorted(Key2Index & keys);
    VESPA_DLL_LOCAL static void populate(Key2Index & newKeys, CondensedBitVector & chunk, const PopulateInterface & lookup);
    VESPA_DLL_LOCAL bool hasCostChanged(const vespalib::LockGuard &);

    uint64_t       _lookupCount;
    bool           _needPopulation;
    vespalib::Lock _lock;
    Key2Index      _keys;
    ChunkV         _chunks;
    GenerationHolder &_genHolder;
};

}