NCBI C++ ToolKit
icache_cf.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef UTIL_ICACHE_CF__HPP
2 #define UTIL_ICACHE_CF__HPP
3 
4 /* $Id: icache_cf.hpp 91363 2020-10-19 14:54:50Z grichenk $
5 * ===========================================================================
6 *
7 * public DOMAIN NOTICE
8 * National Center for Biotechnology Information
9 *
10 * This software/database is a "United States Government Work" under the
11 * terms of the United States Copyright Act. It was written as part of
12 * the author's official duties as a United States Government employee and
13 * thus cannot be copyrighted. This software/database is freely available
14 * to the public for use. The National Library of Medicine and the U.S.
15 * Government have not placed any restriction on its use or reproduction.
16 *
17 * Although all reasonable efforts have been taken to ensure the accuracy
18 * and reliability of the software and data, the NLM and the U.S.
19 * Government do not and cannot warrant the performance or results that
20 * may be obtained by using this software or data. The NLM and the U.S.
21 * Government disclaim all warranties, express or implied, including
22 * warranties of performance, merchantability or fitness for any particular
23 * purpose.
24 *
25 * Please cite the author in any work or product based on this material.
26 *
27 * ===========================================================================
28 *
29 * Author: Anatoliy Kuznetsov
30 *
31 * File Description:
32 * Util library ICache class factory assistance functions
33 *
34 */
35 
36 #include <corelib/ncbistd.hpp>
37 #include <corelib/ncbistr.hpp>
40 #include <util/cache/icache.hpp>
42 #include <util/error_codes.hpp>
43 
44 
46 
47 /// Utility class for ICache class factories
48 ///
49 /// @internal
50 ///
51 template<class TDriver>
52 class CICacheCF : public CSimpleClassFactoryImpl<ICache, TDriver>
53 {
54 public:
55  typedef
57 
58 public:
59  CICacheCF(const string& driver_name, int patch_level = -1)
60  : TParent(driver_name, patch_level)
61  {}
62 
63  /// Utility function, configures common ICache parameters
64  void ConfigureICache(ICache* icache,
65  const TPluginManagerParamTree* params) const
66  {
67  if (!params) return;
68 
69  // Timestamp configuration
70  {{
71  static const char* kCFParam_timestamp = "timestamp";
72 
73  const string& ts_flags_str =
74  this->GetParam(params, kCFParam_timestamp, false);
75 
76  if (!ts_flags_str.empty()) {
77  ConfigureTimeStamp(icache, params, ts_flags_str);
78  }
79 
80  }}
81 
82 
83  static const char* kCFParam_keep_versions = "keep_versions";
84 
85  const string& keep_versions_str =
86  this->GetParam(params, kCFParam_keep_versions, false);
87  if (!keep_versions_str.empty()) {
88  static const char* kCFParam_keep_versions_all = "all";
89  static const char* kCFParam_keep_versions_drop_old = "drop_old";
90  static const char* kCFParam_keep_versions_drop_all = "drop_all";
91 
93  if (NStr::CompareNocase(keep_versions_str,
94  kCFParam_keep_versions_all)==0) {
95  kv_policy = ICache::eKeepAll;
96  } else
97  if (NStr::CompareNocase(keep_versions_str,
98  kCFParam_keep_versions_drop_old)==0) {
99  kv_policy = ICache::eDropOlder;
100  } else
101  if (NStr::CompareNocase(keep_versions_str,
102  kCFParam_keep_versions_drop_all)==0) {
103  kv_policy = ICache::eDropAll;
104  } else {
105  ERR_POST_XX(Util_Cache, 1, Warning
106  << "ICache::ClassFactory: Unknown keep_versions"
107  " policy parameter: "
108  << keep_versions_str);
109  }
110 
111  icache->SetVersionRetention(kv_policy);
112  }
113 
114  }
115 
117  const TPluginManagerParamTree* params,
118  const string& options) const
119  {
120  static
121  const char* kCFParam_timeout = "timeout";
122  static
123  const char* kCFParam_max_timeout = "max_timeout";
124 
125  static
126  const char* kCFParam_timestamp_onread = "onread";
127  static
128  const char* kCFParam_timestamp_subkey = "subkey";
129  static
130  const char* kCFParam_timestamp_expire_not_used = "expire_not_used";
131  static
132  const char* kCFParam_timestamp_purge_on_startup = "purge_on_startup";
133  static
134  const char* kCFParam_timestamp_check_expiration = "check_expiration";
135 
136  list<string> opt;
137  NStr::Split(options, " \t", opt, NStr::fSplit_Tokenize);
138  ICache::TTimeStampFlags ts_flag = 0;
139  ITERATE(list<string>, it, opt) {
140  const string& opt_value = *it;
141  if (NStr::CompareNocase(opt_value,
142  kCFParam_timestamp_onread)==0) {
143  ts_flag |= ICache::fTimeStampOnRead;
144  continue;
145  }
146  if (NStr::CompareNocase(opt_value,
147  kCFParam_timestamp_subkey)==0) {
148  ts_flag |= ICache::fTrackSubKey;
149  continue;
150  }
151  if (NStr::CompareNocase(opt_value,
152  kCFParam_timestamp_expire_not_used)==0) {
154  continue;
155  }
156  if (NStr::CompareNocase(opt_value,
157  kCFParam_timestamp_purge_on_startup)==0) {
158  ts_flag |= ICache::fPurgeOnStartup;
159  continue;
160  }
161  if (NStr::CompareNocase(opt_value,
162  kCFParam_timestamp_check_expiration)==0) {
164  continue;
165  }
166  ERR_POST_XX(Util_Cache, 2, Warning
167  << "ICache::ClassFactory: Unknown timeout policy parameter: "
168  << opt_value);
169  } // ITERATE
170 
171 
172  unsigned int timeout = (unsigned int)
173  this->GetParamInt(params, kCFParam_timeout, false, 60 * 60);
174  unsigned int max_timeout = (unsigned int)
175  this->GetParamInt(params, kCFParam_max_timeout, false, 0);
176 
177  if (max_timeout && max_timeout < timeout)
178  max_timeout = timeout;
179 
180  if (ts_flag) {
181  icache->SetTimeStampPolicy(ts_flag, timeout, max_timeout);
182  }
183 
184  }
185 
186  /// Create instance of TDriver
187  typename TParent::TInterface*
188  CreateInstance(const string& driver = kEmptyStr,
190  const TPluginManagerParamTree* params = 0) const final
191  {
192  if (auto main = x_CreateInstance(driver, version, params)) {
193 
194  if (TParent::GetParamBool(params, "cache_write_async", false, false)) {
195 
196  if (auto writer = x_CreateInstance(driver, version, params)) {
197 
198  auto gp = TParent::GetParamDouble(params, "cache_write_async_grace_period", false, 0.0);
199  return new CAsyncWriteCache(main, writer, gp);
200  }
201  }
202 
203  return main;
204  }
205 
206  return nullptr;
207  }
208 
209 private:
210  virtual typename TParent::TInterface*
211  x_CreateInstance(const string& driver = kEmptyStr,
213  const TPluginManagerParamTree* params = 0) const = 0;
214 };
215 
217 
218 #endif /* UTIL_EXCEPTION__HPP */
Class for deferred asynchronous writes in a separate thread.
Definition: cache_async.hpp:49
Utility class for ICache class factories.
Definition: icache_cf.hpp:53
TParent::TInterface * CreateInstance(const string &driver=kEmptyStr, CVersionInfo version=TParent::GetDefaultDrvVers(), const TPluginManagerParamTree *params=0) const final
Create instance of TDriver.
Definition: icache_cf.hpp:188
virtual TParent::TInterface * x_CreateInstance(const string &driver=kEmptyStr, CVersionInfo version=TParent::GetDefaultDrvVers(), const TPluginManagerParamTree *params=0) const =0
void ConfigureTimeStamp(ICache *icache, const TPluginManagerParamTree *params, const string &options) const
Definition: icache_cf.hpp:116
CSimpleClassFactoryImpl< ICache, TDriver > TParent
Definition: icache_cf.hpp:56
void ConfigureICache(ICache *icache, const TPluginManagerParamTree *params) const
Utility function, configures common ICache parameters.
Definition: icache_cf.hpp:64
CICacheCF(const string &driver_name, int patch_level=-1)
Definition: icache_cf.hpp:59
Template class helps to implement one driver class factory.
definition of a Culling tree
Definition: ncbi_tree.hpp:100
CVersionInfo –.
BLOB cache read/write/maintenance interface.
Definition: icache.hpp:64
EKeepVersions
If to keep already cached versions of the BLOB when storing another version of it (not necessarily a ...
Definition: icache.hpp:162
@ eDropOlder
Delete the earlier (than the one being stored) versions of the BLOB.
Definition: icache.hpp:167
@ eDropAll
Delete all versions of the BLOB, even those which are newer than the one being stored.
Definition: icache.hpp:170
@ eKeepAll
Do not delete other versions of cache entries.
Definition: icache.hpp:164
virtual void SetVersionRetention(EKeepVersions policy)=0
Set version retention policy.
virtual void SetTimeStampPolicy(TTimeStampFlags policy, unsigned int timeout, unsigned int max_timeout=0)=0
Set timestamp update policy.
@ fTimeStampOnRead
Timestamp is updated every on every access (read or write)
Definition: icache.hpp:110
@ fExpireLeastFrequentlyUsed
Expire objects older than a certain time frame Example: If object is not accessed within a week it is...
Definition: icache.hpp:117
@ fTrackSubKey
Timestamp full key-subkey pair.
Definition: icache.hpp:113
@ fPurgeOnStartup
Expired objects should be deleted on cache mount (Open)
Definition: icache.hpp:119
@ fCheckExpirationAlways
Expiration timeout is checked on any access to cache element.
Definition: icache.hpp:121
int TTimeStampFlags
Holds a bitwise OR of "ETimeStampFlags".
Definition: icache.hpp:128
Include a standard set of the NCBI C++ Toolkit most basic headers.
The NCBI C++ standard methods for dealing with std::string.
int main(int argc, const char *argv[])
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define ERR_POST_XX(error_name, err_subcode, message)
Error posting with error code having given name and with given error subcode.
Definition: ncbidiag.hpp:564
void Warning(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1191
double GetParamDouble(const TPluginManagerParamTree *params, const string &param_name, bool, double default_value) const
Utility function to get a double of parameter tree Throws an exception when mandatory parameter is mi...
static const CVersionInfo & GetDefaultDrvVers(void)
int GetParamInt(const TPluginManagerParamTree *params, const string &param_name, bool, int default_value) const
Utility function to get an integer of parameter tree Throws an exception when mandatory parameter is ...
bool GetParamBool(const TPluginManagerParamTree *params, const string &param_name, bool, bool default_value) const
Utility function to get an bool of parameter tree Throws an exception when mandatory parameter is mis...
string GetParam(const TPluginManagerParamTree *params, const string &param_name, bool mandatory, const string &default_value) const
Utility function to get an element of parameter tree Throws an exception when mandatory parameter is ...
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define kEmptyStr
Definition: ncbistr.hpp:123
static int CompareNocase(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n, const char *s2)
Case-insensitive compare of a substring with another string.
Definition: ncbistr.cpp:219
static list< string > & Split(const CTempString str, const CTempString delim, list< string > &arr, TSplitFlags flags=0, vector< SIZE_TYPE > *token_pos=NULL)
Split a string using specified delimiters.
Definition: ncbistr.cpp:3461
@ fSplit_Tokenize
All delimiters are merged and trimmed, to get non-empty tokens only.
Definition: ncbistr.hpp:2508
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
Interfaces for a local cache of versioned binary large objects (BLOBS).
Definition of all error codes used in util (xutil.lib).
static int version
Definition: mdb_load.c:29
Plugin manager (using class factory paradigm).
Helper classes and templates to implement plugins.
#define const
Definition: zconf.h:232
Modified on Wed May 15 15:09:12 2024 by modify_doxy.py rev. 669887