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

Go to the SVN repository for this file.

1 #ifndef DBAPI_SVC_MAPPER_HPP
2 #define DBAPI_SVC_MAPPER_HPP
3 
4 /* $Id: dbapi_svc_mapper.hpp 100781 2023-09-12 15:55:38Z ucko $
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: Sergey Sikorskiy
30 *
31 * File Description:
32 *
33 *============================================================================
34 */
35 
37 #include <corelib/ncbimtx.hpp>
38 
40 
41 #include <vector>
42 #include <set>
43 #include <map>
44 #include <random>
45 
47 
48 
49 ///////////////////////////////////////////////////////////////////////////////
50 /// CDBDefaultServiceMapper
51 ///
52 
54 {
55 public:
57  virtual ~CDBDefaultServiceMapper(void);
58 
59  virtual string GetName (void) const;
60  virtual void Configure (const IRegistry* registry = NULL);
61  virtual TSvrRef GetServer (const string& service);
62  virtual void SetPreference(const string& service,
63  const TSvrRef& preferred_server,
64  double preference = 100.0);
65 };
66 
67 ///////////////////////////////////////////////////////////////////////////////
68 /// CDBServiceMapperCoR
69 ///
70 /// IDBServiceMapper adaptor which implements the chain of responsibility
71 /// pattern
72 ///
73 
75 {
76 public:
77  CDBServiceMapperCoR(void);
78  virtual ~CDBServiceMapperCoR(void);
79 
80  virtual string GetName (void) const;
81  virtual void Configure (const IRegistry* registry = NULL);
82  virtual TSvrRef GetServer (const string& service);
83  virtual void Exclude (const string& service,
84  const TSvrRef& server);
85  virtual void CleanExcluded(const string& service);
86  virtual bool HasExclusions(const string& service) const;
87  virtual void SetPreference(const string& service,
88  const TSvrRef& preferred_server,
89  double preference = 100.0);
90  virtual void GetServersList(const string& service,
91  list<string>* serv_list) const;
92  virtual void GetServerOptions(const string& service, TOptions* options);
93  virtual bool RecordServer(I_ConnectionExtra& extra) const;
94 
95  void Push(const CRef<IDBServiceMapper>& mapper);
96  void Pop(void);
97  CRef<IDBServiceMapper> Top(void) const;
98  bool Empty(void) const;
99 
100 protected:
101  void ConfigureFromRegistry(const IRegistry* registry = NULL);
102 
103  typedef vector<CRef<IDBServiceMapper> > TDelegates;
104 
106 };
107 
108 
109 
110 ///////////////////////////////////////////////////////////////////////////////
111 /// CDBUDRandomMapper
112 ///
113 
115 {
116 public:
118  virtual ~CDBUDRandomMapper(void);
119 
120 public:
121  virtual string GetName (void) const;
122  virtual void Configure (const IRegistry* registry = NULL);
123  virtual TSvrRef GetServer (const string& service);
124  virtual void Exclude (const string& service,
125  const TSvrRef& server);
126  virtual void CleanExcluded(const string& service);
127  virtual bool HasExclusions(const string& service) const;
128  virtual void SetPreference(const string& service,
129  const TSvrRef& preferred_server,
130  double preference = 100.0);
131  virtual void GetServerOptions(const string& service, TOptions* options);
132 
133  void Add (const string& service,
134  const TSvrRef& server,
135  double preference = 0.0);
136 
137  static IDBServiceMapper* Factory(const IRegistry* registry);
138 
139 protected:
140  void ConfigureFromRegistry(const IRegistry* registry = NULL);
141 
142 private:
143  void x_RecalculatePreferences(const string& service);
144 
145  struct SPreferences {
146  vector<CRef<CDBServerOption> > servers;
147  unique_ptr<discrete_distribution<> > distribution;
148  };
149 
153 
158  default_random_engine m_RandomEngine;
159 };
160 
161 
162 
163 ///////////////////////////////////////////////////////////////////////////////
164 /// CDBUDPriorityMapper
165 ///
166 /// @note When listing a single server multiple times with different
167 /// (possibly implicit) degrees of preference, each instance starts
168 /// out with a handicap corresponding to its immediately associated
169 /// preference, but subsequent deprioritization to give other servers
170 /// turns is based on the first-listed instance's degree of preference
171 /// (or the most recent SetPreference call's, if any).
172 
174 {
175 public:
177  virtual ~CDBUDPriorityMapper(void);
178 
179 public:
180  virtual string GetName (void) const;
181  virtual void Configure (const IRegistry* registry = NULL);
182  virtual TSvrRef GetServer (const string& service);
183  virtual void Exclude (const string& service,
184  const TSvrRef& server);
185  virtual void CleanExcluded(const string& service);
186  virtual void SetPreference(const string& service,
187  const TSvrRef& preferred_server,
188  double preference = 100.0);
189  virtual void GetServerOptions(const string& service, TOptions* options);
190  void Add (const string& service,
191  const TSvrRef& server,
192  double preference = 0.0);
193 
194  static IDBServiceMapper* Factory(const IRegistry* registry);
195 
196 protected:
197  void ConfigureFromRegistry(const IRegistry* registry = NULL);
198 
199 private:
205 
210 };
211 
213 {
214 public:
215  typedef pair<string, TFactory> TMapperConf;
216 
217  CDBUniversalMapper(const IRegistry* registry = nullptr,
218  const TMapperConf& ext_mapper
219  = TMapperConf(kEmptyStr, (TFactory)nullptr));
220  virtual ~CDBUniversalMapper(void);
221 
222  virtual string GetName(void) const;
223  virtual void Configure(const IRegistry* registry = NULL);
224 
225 protected:
226  void ConfigureFromRegistry(const IRegistry* registry = NULL);
227 
228 private:
230 };
231 
232 ///////////////////////////////////////////////////////////////////////////////
233 // Type traits
234 //
235 
236 template <>
238 {
239 public:
240  static string GetName(void);
241 };
242 
243 template <>
245 {
246 public:
247  static string GetName(void);
248 };
249 
250 template <>
252 {
253 public:
254  static string GetName(void);
255 };
256 
257 template <>
259 {
260 public:
261  static string GetName(void);
262 };
263 
264 template <>
266 {
267 public:
268  static string GetName(void);
269 };
270 
271 
272 ////////////////////////////////////////////////////////////////////////////////
273 inline
276 {
277  return new CDBUniversalMapper(
278  registry,
282  )
283  );
284 }
285 
286 
287 /// Easy-to-use macro to install the default DBAPI service mapper and
288 /// a user-defined connection factory, with control over what to do
289 /// if a connection factory has already been explicitly registered.
290 /// @sa DBLB_INSTALL_DEFAULT_EX, DBLB_INSTALL_FACTORY
291 #define DBLB_INSTALL_FACTORY_EX(factory_name, if_set) \
292  ncbi::CDbapiConnMgr::Instance().SetConnectionFactory( \
293  new factory_name(ncbi::MakeCDBUniversalMapper), \
294  ncbi::CDbapiConnMgr::if_set)
295 
296 /// Easy-to-use macro to install the default DBAPI service mapper
297 /// and a user-defined connection factory.
298 /// @sa DBLB_INSTALL_DEFAULT, DBLB_INSTALL_FACTORY_EX
299 #define DBLB_INSTALL_FACTORY(factory_name) \
300  DBLB_INSTALL_FACTORY_EX(factory_name, eIfSet_Replace)
301 
302 /// Easy-to-use macro to install the default DBAPI service mapper,
303 /// with control over what to do if a connection factory has already
304 /// been explicitly registered.
305 /// @sa DBLB_INSTALL_DEFAULT, DBLB_INSTALL_FACTORY_EX
306 #define DBLB_INSTALL_DEFAULT_EX(if_set) \
307  DBLB_INSTALL_FACTORY_EX(ncbi::CDBConnectionFactory, if_set)
308 
309 /// Easy-to-use macro to install the default DBAPI service mapper.
310 /// @sa DBLB_INSTALL_DEFAULT_EX, DBLB_INSTALL_FACTORY
311 #define DBLB_INSTALL_DEFAULT() DBLB_INSTALL_DEFAULT_EX(eIfSet_Replace)
312 
313 
314 
316 
317 #endif // DBAPI_SVC_MAPPER_HPP
CDBDefaultServiceMapper.
static IDBServiceMapper * Factory(const IRegistry *registry)
CDBServiceMapperCoR.
vector< CRef< IDBServiceMapper > > TDelegates
DBServiceMapperTraits IDBServiceMapper traits.
CDBUDPriorityMapper.
map< TSvrRef, double, SDereferenceLess > TSvrMap
map< string, bool > TLBNameMap
TServiceUsageMap m_OrigServiceUsageMap
TServiceUsageMap m_ServiceUsageMap
multimap< double, TSvrRef > TServerUsageMap
map< string, TServerUsageMap > TServiceUsageMap
map< string, TSvrMap > TServiceMap
CDBUDRandomMapper.
default_random_engine m_RandomEngine
map< string, bool > TLBNameMap
TPreferenceMap m_PreferenceMap
map< string, TOptions > TServiceMap
TServiceMap m_FavoritesMap
map< string, SPreferences > TPreferenceMap
pair< string, TFactory > TMapperConf
IDBServiceMapper.
virtual void GetServerOptions(const string &service, TOptions *options)
Get an annotated list of all servers for the given service.
virtual TSvrRef GetServer(const string &service)=0
Map a service to a server.
virtual bool RecordServer(I_ConnectionExtra &) const
Given a connection that succeeded even though this service mapper was unable to identify a good serve...
virtual void SetPreference(const string &service, const TSvrRef &preferred_server, double preference=100)=0
Set up mapping preferences for a service preference - value between 0 and 100 (0 means *no particular...
virtual void CleanExcluded(const string &service)
Clean the list of excluded servers for the given service.
virtual void Configure(const IRegistry *registry=NULL)=0
list< CRef< CDBServerOption > > TOptions
virtual void GetServersList(const string &, list< string > *serv_list) const
Get list of all servers for the given service disregarding any exclusions.
virtual string GetName(void) const
virtual bool HasExclusions(const string &service) const
virtual void Exclude(const string &service, const TSvrRef &server)
Exclude a server from the mapping for a service.
IRegistry –.
Definition: ncbireg.hpp:73
I_ConnectionExtra.
static CMemoryRegistry registry
Definition: cn3d_tools.cpp:81
static struct tagNamedScoreFactory Factory[]
IDBServiceMapper * MakeCDBUniversalMapper(const IRegistry *registry)
bool Empty(const CNcbiOstrstream &src)
Definition: fileutil.cpp:523
#define NULL
Definition: ncbistd.hpp:225
#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
#define NCBI_DBAPIDRIVER_EXPORT
Definition: ncbi_export.h:392
Multi-threading – mutexes; rw-locks; semaphore.
vector< CRef< CDBServerOption > > servers
unique_ptr< discrete_distribution<> > distribution
Modified on Thu Apr 25 08:16:26 2024 by modify_doxy.py rev. 669887