summaryrefslogtreecommitdiffstats
path: root/vdslib/src/tests/distribution/randombucket.cpp
blob: ab0f474b75ec6a800df93b7b146ff0c9f4c2d525 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "randombucket.h"
#include <vespa/vdslib/state/random.h>

namespace RandomBucket{

uint64_t _num_users;
uint64_t _locationbits;
bool _scheme = false;
storage::lib::RandomGen rg;

void setUserDocScheme(uint64_t num_users, uint64_t locationbits)
{
    _scheme = true;
    _num_users = num_users;
    _locationbits = locationbits;
}

void setDocScheme()
{
    _scheme = false;
}

uint64_t get()
{
    uint64_t u = rg.nextUint64();
    if(_scheme){ // userdoc
        uint64_t shift = 8 * sizeof(uint64_t) - _locationbits;
        uint64_t lsb = u << shift;
        lsb >>= shift;
        lsb %= _num_users;
        u >>= _locationbits;
        u <<= _locationbits;
        u |= lsb;
    }
    return u;
}

void setSeed(int seed)
{
    if(seed == -1){
        rg  = storage::lib::RandomGen();
    }
    else{
        rg.setSeed(seed);
    }
}

}