aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storage/bucketdb/btree_bucket_database.h
blob: 3cf77b5444b4bcfdcc1a26ab1d04b330a46efce8 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "bucketdatabase.h"
#include <memory>

namespace storage {

namespace bucketdb {
template <typename DataStoreTraitsT> class GenericBTreeBucketDatabase;
}

/*
 * Bucket database implementation built around lock-free single-writer/multiple-readers B+tree.
 *
 * Since a distributor must be able to handle multiple replicas for a given bucket, these
 * are handled via an ArrayStore indirection. A distributor bucket DB also includes state
 * for the _entire bucket_ itself, not just the replicas; last timestamp of bucket GC. Since
 * this is an uint32 we cheekily mangle it into the value, i.e. each bucket key maps to a
 * composite key of (gc_timestamp_u32 << 32) | array_ref_u32.
 *
 * Readers from contexts that are not guaranteed to be the main distributor thread MUST
 * only access the database via an acquired read guard.
 * Writing MUST only take place from the main distributor thread.
 */
// TODO create and use a new DB interface with better bulk loading, snapshot and iteration support
class BTreeBucketDatabase : public BucketDatabase {
    struct ReplicaValueTraits;
    using ImplType = bucketdb::GenericBTreeBucketDatabase<ReplicaValueTraits>;
    std::unique_ptr<ImplType> _impl;
public:
    BTreeBucketDatabase();
    ~BTreeBucketDatabase() override;

    void merge(MergingProcessor&) override;

    // Ye olde bucket DB API:
    Entry get(const document::BucketId& bucket) const override;
    void remove(const document::BucketId& bucket) override;
    void getParents(const document::BucketId& childBucket,
                    std::vector<Entry>& entries) const override;
    void getAll(const document::BucketId& bucket,
                std::vector<Entry>& entries) const override;
    void update(const Entry& newEntry) override;
    void process_update(const document::BucketId& bucket, EntryUpdateProcessor &processor, bool create_if_nonexisting) override;
    void for_each_lower_bound(EntryProcessor&, const document::BucketId& at_or_after) const override;
    void for_each_upper_bound(EntryProcessor&, const document::BucketId& after) const override;
    Entry upperBound(const document::BucketId& value) const override;
    uint64_t size() const override;
    void clear() override;
    document::BucketId getAppropriateBucket(
            uint16_t minBits,
            const document::BucketId& bid) override;
    uint32_t childCount(const document::BucketId&) const override;
    void print(std::ostream& out, bool verbose,
               const std::string& indent) const override;

private:
    class ReadGuardImpl;
    friend class ReadGuardImpl;
public:
    std::unique_ptr<bucketdb::ReadGuard<Entry, ConstEntryRef>> acquire_read_guard() const override;

    vespalib::MemoryUsage memory_usage() const noexcept override;
};

}