NCBI C++ ToolKit
incr_time.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: incr_time.cpp 102575 2024-06-04 15:22:58Z grichenk $
2 * ===========================================================================
3 * PUBLIC DOMAIN NOTICE
4 * National Center for Biotechnology Information
5 *
6 * This software/database is a "United States Government Work" under the
7 * terms of the United States Copyright Act. It was written as part of
8 * the author's official duties as a United States Government employee and
9 * thus cannot be copyrighted. This software/database is freely available
10 * to the public for use. The National Library of Medicine and the U.S.
11 * Government have not placed any restriction on its use or reproduction.
12 *
13 * Although all reasonable efforts have been taken to ensure the accuracy
14 * and reliability of the software and data, the NLM and the U.S.
15 * Government do not and cannot warrant the performance or results that
16 * may be obtained by using this software or data. The NLM and the U.S.
17 * Government disclaim all warranties, express or implied, including
18 * warranties of performance, merchantability or fitness for any particular
19 * purpose.
20 *
21 * Please cite the author in any work or product based on this material.
22 * ===========================================================================
23 *
24 * Author: Eugene Vasilchenko
25 *
26 * File Description: Support for configurable increasing timeout
27 *
28 */
29 
30 #include <ncbi_pch.hpp>
31 #include <util/incr_time.hpp>
32 #include <corelib/ncbi_config.hpp>
33 #include <corelib/ncbireg.hpp>
34 #include <cmath>
35 
37 
38 /////////////////////////////////////////////////////////////////////////////
39 // CIncreasingTime
40 /////////////////////////////////////////////////////////////////////////////
41 
42 
44  : m_InitTime(params.m_Initial.m_DefaultValue),
45  m_MaxTime(params.m_Maximal.m_DefaultValue),
46  m_Multiplier(params.m_Multiplier.m_DefaultValue),
47  m_Increment(params.m_Increment.m_DefaultValue)
48 {
50 }
51 
52 
54 {
55  if (m_InitTime < 0) m_InitTime = 0;
56  if (m_MaxTime < 0) m_MaxTime = 0;
57  if (m_Multiplier < 1) m_Multiplier = 1;
58 }
59 
60 
62  const string& driver_name,
63  const SAllParams& params)
64 {
65  m_InitTime = x_GetDoubleParam(conf, driver_name, params.m_Initial);
66  m_MaxTime = x_GetDoubleParam(conf, driver_name, params.m_Maximal);
67  m_Multiplier = x_GetDoubleParam(conf, driver_name, params.m_Multiplier);
68  m_Increment = x_GetDoubleParam(conf, driver_name, params.m_Increment);
70 }
71 
72 
74  const string& section,
75  const SAllParams& params)
76 {
77  unique_ptr<CConfig::TParamTree> app_params(CConfig::ConvertRegToTree(reg));
78  const CConfig::TParamTree* param_tree = app_params->FindSubNode(section);
79  if ( !param_tree ) {
84  }
85  else {
86  CConfig conf(param_tree);
87  m_InitTime = x_GetDoubleParam(reg, section, params.m_Initial);
88  m_MaxTime = x_GetDoubleParam(reg, section, params.m_Maximal);
89  m_Multiplier = x_GetDoubleParam(reg, section, params.m_Multiplier);
90  m_Increment = x_GetDoubleParam(reg, section, params.m_Increment);
91  }
93 }
94 
95 
97  const string& driver_name,
98  const SParam& param)
99 {
100  string value = conf.GetString(driver_name,
101  param.m_ParamName,
103  "");
104  if ( value.empty() && param.m_ParamName2 ) {
105  value = conf.GetString(driver_name,
106  param.m_ParamName2,
108  "");
109  }
110  if ( value.empty() ) {
111  return param.m_DefaultValue;
112  }
114 }
115 
116 
118  const string& driver_name,
119  const SParam& param)
120 {
121  string value = reg.GetString(driver_name,
122  param.m_ParamName,
123  "");
124  if ( value.empty() && param.m_ParamName2 ) {
125  value = reg.GetString(driver_name,
126  param.m_ParamName2,
127  "");
128  }
129  if ( value.empty() ) {
130  return param.m_DefaultValue;
131  }
133 }
134 
135 
136 double CIncreasingTime::GetTime(int step) const
137 {
138  double time = m_InitTime;
139  if ( step > 0 ) {
140  if ( abs(m_Multiplier - 1) <= 1e-6 ) {
141  time += step * m_Increment;
142  }
143  else {
144  double p = pow(m_Multiplier, step);
145  time = time * p + m_Increment * (p - 1) / (m_Multiplier - 1);
146  }
147  }
148  return max(0.0, min(time, m_MaxTime));
149 }
150 
151 
double GetTime(int step) const
Definition: incr_time.cpp:136
double m_MaxTime
Definition: incr_time.hpp:80
static double x_GetDoubleParam(CConfig &conf, const string &driver_name, const SParam &param)
Definition: incr_time.cpp:96
double m_InitTime
Definition: incr_time.hpp:80
CIncreasingTime(const SAllParams &params)
Definition: incr_time.cpp:43
void Init(CConfig &conf, const string &driver_name, const SAllParams &params)
Definition: incr_time.cpp:61
double m_Multiplier
Definition: incr_time.hpp:80
void x_VerifyParams(void)
Definition: incr_time.cpp:53
double m_Increment
Definition: incr_time.hpp:80
CNcbiRegistry –.
Definition: ncbireg.hpp:913
definition of a Culling tree
Definition: ncbi_tree.hpp:100
static TParamTree * ConvertRegToTree(const IRegistry &reg, NStr::ECase use_case=NStr::eNocase)
Reconstruct param tree from the application registry.
string GetString(const string &driver_name, const string &param_name, EErrAction on_error, const string &default_value, const list< string > *synonyms=NULL)
Utility function to get an element of parameter tree Throws an exception when mandatory parameter is ...
@ eErr_NoThrow
Return default value on error.
virtual string GetString(const string &section, const string &name, const string &default_value, TFlags flags=0) const
Get the parameter string value.
Definition: ncbireg.cpp:321
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static double StringToDouble(const CTempStringEx str, TStringToNumFlags flags=0)
Convert string to double.
Definition: ncbistr.cpp:1381
@ fDecimalPosixOrLocal
StringToDouble*(): For decimal point, try both C and current locale.
Definition: ncbistr.hpp:301
const TTreeType * FindSubNode(const TKeyType &key) const
Non recursive linear scan of all subnodes, with key comparison.
Definition: ncbi_tree.hpp:940
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
Parameters initialization model.
#define abs(a)
Definition: ncbi_heapmgr.c:130
Process information in the NCBI Registry, including working with configuration files.
T max(T x_, T y_)
T min(T x_, T y_)
const char * m_ParamName2
Definition: incr_time.hpp:49
const char * m_ParamName
Definition: incr_time.hpp:48
Modified on Fri Sep 20 14:57:07 2024 by modify_doxy.py rev. 669887