uniform_reservoir.cpp 2.5 KB
Newer Older
羽飞's avatar
羽飞 已提交
1
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
羽飞's avatar
羽飞 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Longda on 2021/4/20.
//

#include "common/metrics/uniform_reservoir.h"

#include <stdint.h>

#include "common/lang/mutex.h"
#include "common/metrics/histogram_snapshot.h"

namespace common {

#define DEFAULT_SIZE 1023

26 27
UniformReservoir::UniformReservoir(RandomGenerator &random) : Reservoir(random), counter(0)
{
羽飞's avatar
羽飞 已提交
28 29 30 31 32 33 34 35 36
  pthread_mutexattr_t mutexatr;
  pthread_mutexattr_init(&mutexatr);
  pthread_mutexattr_settype(&mutexatr, PTHREAD_MUTEX_RECURSIVE);

  MUTEX_INIT(&mutex, &mutexatr);

  init(DEFAULT_SIZE);
}

37 38
UniformReservoir::UniformReservoir(RandomGenerator &random, size_t size) : Reservoir(random), counter(0)
{
羽飞's avatar
羽飞 已提交
39 40 41 42 43 44 45 46 47

  pthread_mutexattr_t mutexatr;
  pthread_mutexattr_init(&mutexatr);
  pthread_mutexattr_settype(&mutexatr, PTHREAD_MUTEX_RECURSIVE);

  MUTEX_INIT(&mutex, &mutexatr);
  init(size);
}

48 49
UniformReservoir::~UniformReservoir()
{
羽飞's avatar
羽飞 已提交
50 51 52 53 54 55
  if (snapshot_value_ == NULL) {
    delete snapshot_value_;
    snapshot_value_ = NULL;
  }
}

56 57
void UniformReservoir::init(size_t size)
{
羽飞's avatar
羽飞 已提交
58 59 60 61 62 63
  MUTEX_LOCK(&mutex);
  counter = 0;
  data.resize(size);
  MUTEX_UNLOCK(&mutex);
}

64 65
size_t UniformReservoir::size()
{
羽飞's avatar
羽飞 已提交
66 67 68 69 70 71
  MUTEX_LOCK(&mutex);
  size_t size = (counter < data.size()) ? counter : data.size();
  MUTEX_UNLOCK(&mutex);
  return size;
}

72 73
size_t UniformReservoir::get_count()
{
羽飞's avatar
羽飞 已提交
74 75 76 77 78 79
  MUTEX_LOCK(&mutex);
  size_t ret = counter;
  MUTEX_UNLOCK(&mutex);
  return ret;
}

80 81
void UniformReservoir::update(double value)
{
羽飞's avatar
羽飞 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94
  MUTEX_LOCK(&mutex);
  size_t count = ++counter;

  if (count < data.size()) {
    data[count] = (value);
  } else {
    size_t rcount = next(data.size());
    data[rcount] = (value);
  }

  MUTEX_UNLOCK(&mutex);
}

95 96
void UniformReservoir::snapshot()
{
羽飞's avatar
羽飞 已提交
97 98 99 100 101 102 103 104 105 106
  MUTEX_LOCK(&mutex);
  std::vector<double> output = data;
  MUTEX_UNLOCK(&mutex);

  if (snapshot_value_ == NULL) {
    snapshot_value_ = new HistogramSnapShot();
  }
  ((HistogramSnapShot *)snapshot_value_)->set_collection(output);
}

107 108
void UniformReservoir::reset()
{
羽飞's avatar
羽飞 已提交
109 110 111 112 113 114 115 116 117

  MUTEX_LOCK(&mutex);
  counter = 0;
  data.clear();

  // clear snapshot
  MUTEX_UNLOCK(&mutex);
}

118
}  // namespace common