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

Go to the SVN repository for this file.

1 /* $Id: cached_taxon3.cpp 91382 2020-10-21 16:05:54Z grichenk $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Brad Holmes
27  *
28  * File Description:
29  * NCBI Taxonomy 3 service that caches a parameterized number of replies
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
34 #include <corelib/ncbistr.hpp>
39 #include <serial/serial.hpp>
40 #include <serial/enumvalues.hpp>
41 #include <serial/objistr.hpp>
42 #include <serial/objostr.hpp>
43 
44 #include <algorithm>
45 
46 
47 #define NCBI_USE_ERRCODE_X Objects_Taxonomy
48 
49 
50 
52 BEGIN_objects_SCOPE // namespace ncbi::objects::
53 
54 static const char* kInvalidReplyMsg = "Taxonomy service returned invalid reply";
55 
56 
58  : CCache<string, CRef<CTaxon3_reply> >(capacity),
59  m_taxon(taxon)
60 {
61 }
62 
63 
64 // The method to create the cache one-off
66  AutoPtr<ITaxon3> taxon, TSizeType capacity)
67 {
68  return AutoPtr<CCachedTaxon3>(CreateUnSafe(taxon, capacity));
69 }
70 
71 // This should only be used if it will be
72 // immediately wrapped into a safe pointer
74  AutoPtr<ITaxon3> taxon, TSizeType capacity)
75 {
76  CCachedTaxon3* c = new CCachedTaxon3(taxon, capacity);
77  return c;
78 }
79 
80 void
82 {
83  static const STimeout def_timeout = { 120, 0 };
85 }
86 
87 
88 void
89 CCachedTaxon3::Init(const STimeout* timeout, unsigned reconnect_attempts)
90 {
91  if ( timeout ) {
92  m_timeout_value = *timeout;
94  } else {
95  m_timeout = 0;
96  }
97 
98  m_nReconnectAttempts = reconnect_attempts;
99  m_pchService = "TaxService3";
100  const char* tmp;
101  if( ( (tmp=getenv("NI_TAXON3_SERVICE_NAME")) != NULL ) ||
102  ( (tmp=getenv("NI_SERVICE_NAME_TAXON3")) != NULL ) ) {
103  m_pchService = tmp;
104  }
105 
106 
107 #ifdef USE_TEXT_ASN
109 #else
111 #endif
112 
113 }
114 
115 
118 {
119  return m_taxon->SendRequest(request);
120 }
121 
122 
124  const TCacheKey& key,
125  const COrg_ref& org_ref)
126 {
127  //Build a request for the Org-ref
130  rq->SetOrg(*SerialClone(org_ref));
131  request->SetRequest().push_back(rq);
132 
133  //Send the request
134  CRef<CTaxon3_reply> reply = m_taxon->SendRequest(*request);
135 
136  //Check for valid reply
137  if (!reply->IsSetReply()) {
138 
139  //Do not cache, and return error.
140  reply = Ref(new CTaxon3_reply);
141  CRef<CT3Reply> t3reply = Ref(new CT3Reply);
144  reply->SetReply().push_back(t3reply);
145 
146  return reply;
147  }
148 
149  // Cache the reply we do have, and return it.
150  try {
151  static const TAddFlags addflags = fAdd_NoReplace;
152  const TOrder ord = Add(key, reply, 1, addflags);
153  if (ord < 0) {
154  ERR_POST(Warning << "Unable to add data for key: " << key);
155  }
156  static const int cache_getflags = fGet_NoTouch | fGet_NoCreate | fGet_NoInsert;
157  return Get(key, cache_getflags);
158  }
159  catch (const CException& e) {
160  ERR_POST(Error << "Unable to add reply to taxon cache: " << e.GetMsg());
161  }
162  return CRef<CTaxon3_reply>();
163 }
164 
166  const COrg_ref& org_ref)
167 {
168 
169  string key = NStr::IntToString(org_ref.GetTaxId());
170  if (org_ref.IsSetTaxname()) {
171  key += org_ref.GetTaxname();
172  }
173  //First, try to get the item from the cache
174  static const int cache_getflags = fGet_NoTouch | fGet_NoCreate | fGet_NoInsert;
175  try {
176  EGetResult retcode;
177  CRef<CTaxon3_reply> reply = Get(key, cache_getflags, &retcode);
178  if (retcode == eGet_Found) {
179  return reply;
180  }
181  }
182  catch (const CException& e) {
183  ERR_POST(Trace << "Taxon cache miss for key " << key);
184  }
185 
186  //Couldn't find it, so create a new one
187  return x_AddReplyToCache(key, org_ref);
188 }
189 
191 {
192 
194 
195  ITERATE(vector<CRef< COrg_ref> >, it, org_list)
196  {
197  const COrg_ref& org_ref = **it;
198  result->SetReply().push_back(x_GetReplyForOrgRef(org_ref)->SetReply().front());
199  }
200 
201  return result;
202 }
203 
204 
205 
206 END_objects_SCOPE
static const char * kInvalidReplyMsg
Cache template.
Definition: ncbi_cache.hpp:153
CCachedTaxon3(AutoPtr< ITaxon3 > taxon, TSizeType capacity)
CRef< CTaxon3_reply > x_GetReplyForOrgRef(const COrg_ref &org_ref)
virtual CRef< CTaxon3_reply > SendOrgRefList(const vector< CRef< COrg_ref > > &list)
ESerialDataFormat m_eDataFormat
STimeout * m_timeout
static AutoPtr< CCachedTaxon3 > Create(AutoPtr< ITaxon3 > taxon, TSizeType capacity=100000)
virtual CRef< CTaxon3_reply > SendRequest(const CTaxon3_request &request)
virtual void Init(void)
STimeout m_timeout_value
const char * m_pchService
static CCachedTaxon3 * CreateUnSafe(AutoPtr< ITaxon3 > taxon, TSizeType capacity=100000)
unsigned m_nReconnectAttempts
CRef< CTaxon3_reply > x_AddReplyToCache(const TCacheKey &key, const COrg_ref &org_ref)
AutoPtr< ITaxon3 > m_taxon
The cached taxon does not own the taxon.
TTaxId GetTaxId() const
Definition: Org_ref.cpp:72
CRef –.
Definition: ncbiobj.hpp:618
CT3Reply –.
Definition: T3Reply.hpp:66
CT3Request –.
Definition: T3Request.hpp:66
CTaxon3_reply –.
CTaxon3_request –.
virtual CRef< CTaxon3_reply > SendRequest(const CTaxon3_request &request)=0
The NCBI C++ standard methods for dealing with std::string.
static char tmp[3200]
Definition: utf8.c:42
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
string
Definition: cgiapp.hpp:687
#define NULL
Definition: ncbistd.hpp:225
int TAddFlags
bitwise OR of EAddFlags
Definition: ncbi_cache.hpp:196
TValueType Get(const TKeyType &key, TGetFlags get_flags=0, EGetResult *result=NULL)
Get an object from the cache by its key.
Definition: ncbi_cache.hpp:590
TOrder Add(const TKeyType &key, const TValueType &value, TWeight weight=1, TAddFlags add_flags=0, EAddResult *result=NULL)
Add new element to the cache or replace the existing value.
Definition: ncbi_cache.hpp:507
@ fAdd_NoReplace
Do not replace existing values if any.
Definition: ncbi_cache.hpp:194
@ fGet_NoTouch
Do not update the object's position.
Definition: ncbi_cache.hpp:227
@ fGet_NoInsert
Do not insert created values.
Definition: ncbi_cache.hpp:230
@ fGet_NoCreate
Do not create value if not found, throw an exception instead.
Definition: ncbi_cache.hpp:228
@ eGet_Found
The key was found in the cache.
Definition: ncbi_cache.hpp:236
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
void Error(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1197
const string & GetMsg(void) const
Get message string.
Definition: ncbiexpt.cpp:461
void Trace(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1179
void Warning(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1191
C * SerialClone(const C &src)
Create on heap a clone of the source object.
Definition: serialbase.hpp:512
@ eSerial_AsnText
ASN.1 text.
Definition: serialdef.hpp:73
@ eSerial_AsnBinary
ASN.1 binary.
Definition: serialdef.hpp:74
CRef< C > Ref(C *object)
Helper functions to get CRef<> and CConstRef<> objects.
Definition: ncbiobj.hpp:2015
#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 string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
Definition: ncbistr.hpp:5086
const TTaxname & GetTaxname(void) const
Get the Taxname member data.
Definition: Org_ref_.hpp:372
bool IsSetTaxname(void) const
preferred formal name Check if a value has been assigned to Taxname data member.
Definition: Org_ref_.hpp:360
void SetLevel(TLevel value)
Assign a value to Level data member.
Definition: T3Error_.hpp:363
void SetMessage(const TMessage &value)
Assign a value to Message data member.
Definition: T3Error_.hpp:403
TError & SetError(void)
Select the variant.
Definition: T3Reply_.cpp:108
Definition of all error codes used in objects libraries.
constexpr auto front(list< Head, As... >, T=T()) noexcept -> Head
const struct ncbi::grid::netcache::search::fields::KEY key
Timeout structure.
Definition: ncbi_types.h:76
static const STimeout def_timeout
Definition: taxon1.cpp:98
else result
Definition: token2.c:20
Modified on Mon Jul 22 05:03:10 2024 by modify_doxy.py rev. 669887