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