summaryrefslogtreecommitdiffstats
path: root/vdslib/src/vespa/vdslib/bucketdistribution.h
blob: 14572728d80963c1a111cbb5eda436e19a6d926f (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespa/document/bucket/bucketid.h>
#include <vespa/vespalib/util/sync.h>
#include <vector>

namespace vdslib {

/**
 * Stable algorithmic hash distribution; this class assigns hash buckets to targets. The number of hash buckets should
 * be large compared to the number of targets. The mapping from hash value to hash bucket is performed outside this
 * class.
 */
class BucketDistribution {
public:
    /**
     * Constructs a new bucket distribution object with a given number of columns and buckets.
     *
     * @param numColumns    The number of columns to distribute to.
     * @param numBucketBits The number of bits to use for bucket id.
     */
    BucketDistribution(uint32_t numColumns, uint32_t numBucketBits);
    BucketDistribution(const BucketDistribution &);
    BucketDistribution & operator = (const BucketDistribution &);
    BucketDistribution(BucketDistribution &&) = default;
    BucketDistribution & operator = (BucketDistribution &&) = default;
    ~BucketDistribution();

    /**
     * Returns the number of buckets that the given number of bucket bits will allow.
     *
     * @param numBucketBits The number of bits to use for bucket id.
     * @return              The number of buckets allowed.
     */
    static uint32_t getNumBuckets(uint32_t numBucketBits) { return 1 << numBucketBits; }

    /**
     * This method returns a list that contains the distributions of the given number of buckets over the given number
     * of columns.
     *
     * @param numColumns    The number of columns to distribute to.
     * @param numBucketBits The number of bits to use for bucket id.
     * @param ret           List to fill with the bucket distribution.
     */
    static void getBucketCount(uint32_t numColumns, uint32_t numBucketBits, std::vector<uint32_t> &ret);

    /**
     * This method returns a list similar to {@link this#getBucketCount(int,int)}, except that the returned list
     * contains the number of buckets that will have to be migrated from each column if an additional column was added.
     *
     * @param numColumns    The original number of columns.
     * @param numBucketBits The number of bits to use for bucket id.
     * @param ret           List to fill with the number of buckets to migrate, one value per column.
     */
    static void getBucketMigrateCount(uint32_t numColumns, uint32_t numBucketBits, std::vector<uint32_t> &ret);

    /**
     * Sets the number of columns to distribute to to 1, and resets the content of the internal bucket-to-column map so
     * that it all buckets point to that single column.
     */
    void reset();

    /**
     * Sets the number of columns to use for this document distribution object. This will reset and setup this object
     * from scratch. The original number of buckets is maintained.
     *
     * @param numColumns The new number of columns to distribute to.
     */
    void setNumColumns(uint32_t numColumns);

    /**
     * Returns the number of columns to distribute to.
     *
     * @return The number of columns.
     */
    uint32_t getNumColumns() const { return _numColumns; }

    /**
     * Sets the number of buckets to use for this document distribution object. This will reset and setup this object
     * from scratch. The original number of columns is maintained.
     *
     * @param numBucketBits The new number of bits to use for bucket id.
     */
    void setNumBucketBits(uint32_t numBucketBits);

    /**
     * Returns the number of bits used for bucket identifiers.
     *
     * @return The number of bits.
     */
    uint32_t getNumBucketBits() const { return _numBucketBits; }

    /**
     * Returns the number of buckets available using the configured number of bucket bits.
     *
     * @return The number of buckets.
     */
    uint32_t getNumBuckets() const { return getNumBuckets(_numBucketBits); }

    /**
     * This method maps the given bucket id to its corresponding column.
     *
     * @param bucketId The bucket whose column to lookup.
     * @return         The column to distribute the bucket to.
     */
    uint32_t getColumn(const document::BucketId &bucketId) const;

private:
    /**
     * Adds a single column to this bucket distribution object. This will modify the internal bucket-to-column map so
     * that it takes into account the new column.
     */
    void addColumn();

private:
    uint32_t              _numColumns;     // The number of columns to distribute to.
    uint32_t              _numBucketBits;  // The number of bits to use for bucket identification.
    std::vector<uint32_t> _bucketToColumn; // A map from bucket id to column index.
    vespalib::Lock        _lock;
};

}