Line data Source code
1 : // 2 : // Copyright (c) 2016 Juniper Networks, Inc. All rights reserved. 3 : // 4 : 5 : #include "watermark.h" 6 : 7 250297 : WaterMarkTuple::WaterMarkTuple() : 8 250297 : last_count_(0) { 9 250296 : } 10 : 11 1 : void WaterMarkTuple::SetHighWaterMark(const WaterMarkInfos &high_water) { 12 1 : high_water_ = high_water; 13 1 : } 14 : 15 1631 : void WaterMarkTuple::SetHighWaterMark(const WaterMarkInfo& hwm_info) { 16 1631 : high_water_.insert(hwm_info); 17 1631 : } 18 : 19 226375 : void WaterMarkTuple::ResetHighWaterMark() { 20 226375 : high_water_.clear(); 21 226375 : } 22 : 23 11 : WaterMarkInfos WaterMarkTuple::GetHighWaterMark() const { 24 11 : return high_water_; 25 : } 26 : 27 4 : void WaterMarkTuple::SetLowWaterMark(const WaterMarkInfos &low_water) { 28 4 : low_water_ = low_water; 29 4 : } 30 : 31 1616 : void WaterMarkTuple::SetLowWaterMark(const WaterMarkInfo& lwm_info) { 32 1616 : low_water_.insert(lwm_info); 33 1616 : } 34 : 35 226374 : void WaterMarkTuple::ResetLowWaterMark() { 36 226374 : low_water_.clear(); 37 226374 : } 38 : 39 1 : WaterMarkInfos WaterMarkTuple::GetLowWaterMark() const { 40 1 : return low_water_; 41 : } 42 : 43 0 : void WaterMarkTuple::ProcessWaterMarks(size_t in_count, 44 : size_t curr_count) { 45 0 : if (in_count < curr_count) 46 0 : ProcessLowWaterMarks(in_count); 47 : else 48 0 : ProcessHighWaterMarks(in_count); 49 0 : } 50 : 51 21544520 : bool WaterMarkTuple::AreWaterMarksSet() const { 52 21544520 : return high_water_.size() != 0 || low_water_.size() != 0; 53 : } 54 : 55 9559863 : void WaterMarkTuple::ProcessHighWaterMarks(size_t count) { 56 9559863 : if (high_water_.size() != 0) { 57 112933 : WaterMarkInfos::const_iterator ubound = high_water_.upper_bound( 58 225866 : WaterMarkInfo(count, NULL)); 59 : 60 112933 : if (ubound != high_water_.begin()) { 61 : // ubound is the first one bigger than count, 62 : // so ubound-1 is the last one smaller or equal to count 63 89373 : --ubound; 64 89373 : assert(count >= ubound->count_); 65 : // we have to check if we actually crossed the watermark 66 89373 : if (last_count_ < ubound->count_) { 67 20 : ubound->cb_(count); 68 : } 69 : } 70 : } 71 9559563 : last_count_ = count; 72 9559563 : } 73 : 74 9558924 : void WaterMarkTuple::ProcessLowWaterMarks(size_t count) { 75 9558924 : if (low_water_.size() != 0) { 76 112931 : WaterMarkInfos::const_iterator lbound = low_water_.lower_bound( 77 225862 : WaterMarkInfo(count, NULL)); 78 : 79 112931 : if (lbound != low_water_.end()) { 80 : // we have to check if we actually crossed the watermark 81 112917 : if (last_count_ > lbound->count_) { 82 181 : lbound->cb_(count); 83 : } 84 : } 85 : } 86 9558225 : last_count_ = count; 87 9558225 : }