aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/attribute/multi_value_mapping_base.h
blob: 2ecb29b8174fd101752806fd804a85adae0115b6 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <vespa/vespalib/datastore/atomic_entry_ref.h>
#include <vespa/vespalib/util/address_space.h>
#include <vespa/vespalib/util/rcuvector.h>
#include <functional>

namespace vespalib::datastore {
class CompactionStrategy;
}

namespace search::attribute {

/**
 * Base class for mapping from from document id to an array of values.
 */
class MultiValueMappingBase
{
public:
    using CompactionStrategy = vespalib::datastore::CompactionStrategy;
    using AtomicEntryRef = vespalib::datastore::AtomicEntryRef;
    using EntryRef = vespalib::datastore::EntryRef;
    using RefVector = vespalib::RcuVectorBase<AtomicEntryRef>;

protected:
    std::shared_ptr<vespalib::alloc::MemoryAllocator> _memory_allocator;
    RefVector _indices;
    size_t    _totalValues;

    MultiValueMappingBase(const vespalib::GrowStrategy &gs, vespalib::GenerationHolder &genHolder, std::shared_ptr<vespalib::alloc::MemoryAllocator> memory_allocator);
    virtual ~MultiValueMappingBase();

    void updateValueCount(size_t oldValues, size_t newValues) {
        _totalValues += newValues - oldValues;
    }

    EntryRef acquire_entry_ref(uint32_t docId) const noexcept { return _indices.acquire_elem_ref(docId).load_acquire(); }
public:
    virtual vespalib::MemoryUsage getArrayStoreMemoryUsage() const = 0;
    virtual vespalib::AddressSpace getAddressSpaceUsage() const = 0;
    vespalib::MemoryUsage getMemoryUsage() const;
    size_t getTotalValueCnt() const { return _totalValues; }

    // Called when making snapshot of indices in saver or unit test.
    const RefVector& get_ref_vector() const noexcept { return _indices; }

    /*
     * isFull() should be called from writer only.
     * Const type qualifier removed to prevent call from reader.
     */
    bool isFull() { return _indices.isFull(); }
    void addDoc(uint32_t &docId);
    void shrink(uint32_t docidLimit);
    void reserve(uint32_t lidLimit);
    void clearDocs(uint32_t lidLow, uint32_t lidLimit, std::function<void(uint32_t)> clearDoc);
    /*
     * size() should be called from writer only.
     * Const type qualifier removed to prevent call from reader.
     */
    uint32_t size() { return _indices.size(); }

    /*
     * getNumKeys() should be called from writer only.
     * Const type qualifier removed to prevent call from reader.
     */
    uint32_t getNumKeys() { return _indices.size(); }
    /*
     * getCapacityKeys() should be called from writer only.
     * Const type qualifier removed to prevent call from reader.
     */
    uint32_t getCapacityKeys() { return _indices.capacity(); }
};

}