github.com/petermattis/pebble@v0.0.0-20190905164901-ab51a2166067/internal/randvar/skewed_latest.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  // implied. See the License for the specific language governing
    13  // permissions and limitations under the License. See the AUTHORS file
    14  // for names of contributors.
    15  
    16  package randvar
    17  
    18  import (
    19  	"sync"
    20  
    21  	"golang.org/x/exp/rand"
    22  )
    23  
    24  // SkewedLatest is a random number generator that generates numbers in
    25  // the range [min, max], but skews it towards max using a zipfian
    26  // distribution.
    27  type SkewedLatest struct {
    28  	mu struct {
    29  		sync.Mutex
    30  		max  uint64
    31  		zipf *Zipf
    32  	}
    33  }
    34  
    35  // NewDefaultSkewedLatest constructs a new SkewedLatest generator with the
    36  // default parameters.
    37  func NewDefaultSkewedLatest(rng *rand.Rand) (*SkewedLatest, error) {
    38  	return NewSkewedLatest(rng, 1, defaultMax, defaultTheta)
    39  }
    40  
    41  // NewSkewedLatest constructs a new SkewedLatest generator with the given
    42  // parameters. It returns an error if the parameters are outside the accepted
    43  // range.
    44  func NewSkewedLatest(rng *rand.Rand, min, max uint64, theta float64) (*SkewedLatest, error) {
    45  	z := &SkewedLatest{}
    46  	z.mu.max = max
    47  	zipf, err := NewZipf(rng, 0, max-min, theta)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	z.mu.zipf = zipf
    52  	return z, nil
    53  }
    54  
    55  // IncMax increments max.
    56  func (z *SkewedLatest) IncMax(delta int) {
    57  	z.mu.Lock()
    58  	z.mu.zipf.IncMax(delta)
    59  	z.mu.max += uint64(delta)
    60  	z.mu.Unlock()
    61  }
    62  
    63  // Uint64 returns a random Uint64 between min and max, where keys near max are
    64  // most likely to be drawn.
    65  func (z *SkewedLatest) Uint64() uint64 {
    66  	z.mu.Lock()
    67  	result := z.mu.max - z.mu.zipf.Uint64()
    68  	z.mu.Unlock()
    69  	return result
    70  }