summaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/datastore/unique_store_remapper.h
blob: e805e9c577ad55c03a2e6844fa91172dfd347e0c (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
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "entryref.h"
#include <vector>
#include <vespa/vespalib/stllike/allocator.h>

namespace vespalib::datastore {

/**
 * Remapper for related UniqueStore class, used for adjusting
 * references to unique store after compaction.
 */
template <typename RefT>
class UniqueStoreRemapper {
public:
    using RefType = RefT;

protected:
    std::vector<bool> _compacting_buffer;
    std::vector<std::vector<EntryRef, allocator_large<EntryRef>>> _mapping;
public:
    UniqueStoreRemapper()
        : _compacting_buffer(),
          _mapping()
    {
    }
    virtual ~UniqueStoreRemapper() = default;

    EntryRef remap(EntryRef ref) const {
        if (ref.valid()) {
            RefType internal_ref(ref);
            if (!_compacting_buffer[internal_ref.bufferId()]) {
                // No remapping for references to buffers not being compacted
                return ref;
            } else {
                auto &inner_mapping = _mapping[internal_ref.bufferId()];
                assert(internal_ref.unscaled_offset() < inner_mapping.size());
                EntryRef mapped_ref = inner_mapping[internal_ref.unscaled_offset()];
                assert(mapped_ref.valid());
                return mapped_ref;
            }
        } else {
            return EntryRef();
        }
    }

    void remap(vespalib::ArrayRef<EntryRef> refs) const {
        for (auto &ref : refs) {
            auto mapped_ref = remap(ref);
            if (mapped_ref != ref) {
                ref = mapped_ref;
            }
        }
    }

    virtual void done() = 0;
};

}