Line data Source code
1 : // 2 : // Copyright (c) 2016 Juniper Networks, Inc. All rights reserved. 3 : // 4 : 5 : #include "watermark.h" 6 : 7 250248 : WaterMarkTuple::WaterMarkTuple() : 8 250248 : last_count_(0) { 9 250248 : } 10 : 11 1 : void WaterMarkTuple::SetHighWaterMark(const WaterMarkInfos &high_water) { 12 1 : high_water_ = high_water; 13 1 : } 14 : 15 1623 : void WaterMarkTuple::SetHighWaterMark(const WaterMarkInfo& hwm_info) { 16 1623 : high_water_.insert(hwm_info); 17 1623 : } 18 : 19 226340 : void WaterMarkTuple::ResetHighWaterMark() { 20 226340 : high_water_.clear(); 21 226340 : } 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 1608 : void WaterMarkTuple::SetLowWaterMark(const WaterMarkInfo& lwm_info) { 32 1608 : low_water_.insert(lwm_info); 33 1608 : } 34 : 35 226339 : void WaterMarkTuple::ResetLowWaterMark() { 36 226339 : low_water_.clear(); 37 226339 : } 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 21471129 : bool WaterMarkTuple::AreWaterMarksSet() const { 52 21471129 : return high_water_.size() != 0 || low_water_.size() != 0; 53 : } 54 : 55 9528657 : void WaterMarkTuple::ProcessHighWaterMarks(size_t count) { 56 9528657 : if (high_water_.size() != 0) { 57 112951 : WaterMarkInfos::const_iterator ubound = high_water_.upper_bound( 58 225902 : WaterMarkInfo(count, NULL)); 59 : 60 112951 : 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 88315 : --ubound; 64 88315 : assert(count >= ubound->count_); 65 : // we have to check if we actually crossed the watermark 66 88315 : if (last_count_ < ubound->count_) { 67 19 : ubound->cb_(count); 68 : } 69 : } 70 : } 71 9528454 : last_count_ = count; 72 9528454 : } 73 : 74 9527697 : void WaterMarkTuple::ProcessLowWaterMarks(size_t count) { 75 9527697 : if (low_water_.size() != 0) { 76 112949 : WaterMarkInfos::const_iterator lbound = low_water_.lower_bound( 77 225898 : WaterMarkInfo(count, NULL)); 78 : 79 112949 : if (lbound != low_water_.end()) { 80 : // we have to check if we actually crossed the watermark 81 103924 : if (last_count_ > lbound->count_) { 82 175 : lbound->cb_(count); 83 : } 84 : } 85 : } 86 9527603 : last_count_ = count; 87 9527603 : }