aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/bucketdb/bucket_db_owner.h
blob: 285ea7a30b3265875dbbdb0dc0d045e268aeb4e1 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "bucketdb.h"
#include <mutex>

namespace proton::bucketdb {

class Guard
{
public:
    Guard(BucketDB *bucketDB, std::mutex &mutex);
    Guard(const Guard &) = delete;
    Guard(Guard &&rhs);
    Guard &operator=(const Guard &) = delete;
    Guard &operator=(Guard &&rhs) = delete;
    BucketDB *operator->() { return _bucketDB; }
    BucketDB &operator*() { return *_bucketDB; }
    const BucketDB *operator->() const { return _bucketDB; }
    const BucketDB &operator*() const { return *_bucketDB; }
private:
    BucketDB *_bucketDB;
    std::unique_lock<std::mutex> _guard;
};

/**
 * Class that owns and provides guarded access to a bucket database.
 */
class BucketDBOwner
{
public:
    BucketDBOwner();
    Guard takeGuard() {
        return Guard(&_bucketDB, _mutex);
    }
    size_t getNumActiveDocs() const { return _bucketDB.getNumActiveDocs(); }
private:
    BucketDB   _bucketDB;
    std::mutex _mutex;
};

}