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

Go to the SVN repository for this file.

1 /* $Id: psg_cache.cpp 99744 2023-05-05 15:04:59Z saprykin $
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  * Authors: Dmitri Dmitrienko
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 
35 
36 #include <set>
37 #include <sstream>
38 #include <string>
39 #include <utility>
40 #include <vector>
41 
42 #include <util/lmdbxx/lmdb++.h>
43 
45 #include "psg_cache_si2csi.hpp"
46 #include "psg_cache_blob_prop.hpp"
47 
49 
53 
55  CPubseqGatewayCache::TRuntimeErrorList& error_list,
56  CPubseqGatewayCache::TRuntimeError error
57 )
58 {
59  ERR_POST(Warning << error.message);
61  while (error_list.size() >= CPubseqGatewayCache::kRuntimeErrorLimit) {
62  error_list.pop_front();
63  }
64  }
65  error_list.emplace_back(error);
66 }
67 
69 {
70  if (cache) {
73  request.SetAccession(record.GetAccession()).SetSeqIdType(record.GetSeqIdType());
74  CPubseqGatewayCache::TBioseqInfoResponse response = cache->Fetch(request);
75  if (!response.empty()) {
76  auto& latest_record = response[0];
77  if (latest_record.GetState() == ncbi::psg::retrieval::SEQ_STATE_LIVE) {
78  set<int16_t> seq_id_types;
79  for (auto const & seq_id : record.GetSeqIds()) {
80  seq_id_types.insert(get<0>(seq_id));
81  }
82  for (auto const & seq_id : latest_record.GetSeqIds()) {
83  if (seq_id_types.count(get<0>(seq_id)) == 0) {
84  record.GetSeqIds().insert(seq_id);
85  }
86  }
87  }
88  }
89  }
90  }
91 }
92 
93 END_SCOPE()
94 
97 
98 const size_t CPubseqGatewayCache::kRuntimeErrorLimit = 10;
99 
101  const string& bioseq_info_file_name,
102  const string& si2csi_file_name,
103  const string& blob_prop_file_name
104 )
105  : m_BioseqInfoPath(bioseq_info_file_name)
106  , m_Si2CsiPath(si2csi_file_name)
107  , m_BlobPropPath(blob_prop_file_name)
108 {
109 }
110 
112 {
113 }
114 
116 {
117  if (!m_BioseqInfoPath.empty()) {
118  m_BioseqInfoCache = make_unique<CPubseqGatewayCacheBioseqInfo>(m_BioseqInfoPath);
119  try {
120  m_BioseqInfoCache->Open();
121  } catch (const lmdb::error& e) {
122  stringstream s;
123  s << "Failed to open '" << m_BioseqInfoPath << "' cache: "
124  << e.what() << ", bioseq_info cache will not be used.";
125  TRuntimeError error(s.str());
127  m_BioseqInfoCache.reset();
128  }
129  }
130  if (!m_Si2CsiPath.empty()) {
131  m_Si2CsiCache = make_unique<CPubseqGatewayCacheSi2Csi>(m_Si2CsiPath);
132  try {
133  m_Si2CsiCache->Open();
134  } catch (const lmdb::error& e) {
135  stringstream s;
136  s << "Failed to open '" << m_Si2CsiPath << "' cache: " << e.what() << ", si2csi cache will not be used.";
137  TRuntimeError error(s.str());
139  m_Si2CsiCache.reset();
140  }
141  }
142  if (!m_BlobPropPath.empty()) {
143  m_BlobPropCache = make_unique<CPubseqGatewayCacheBlobProp>(m_BlobPropPath);
144  try {
145  m_BlobPropCache->Open(sat_ids);
146  } catch (const lmdb::error& e) {
147  stringstream s;
148  s << "Failed to open '" << m_BlobPropPath
149  << "' cache: " << e.what() << ", blob prop cache will not be used.";
150  TRuntimeError error(s.str());
152  m_BlobPropCache.reset();
153  }
154  }
155 }
156 
158 {
159  m_RuntimeErrors.clear();
160 }
161 
163 {
165  TBioseqInfoResponse response = m_BioseqInfoCache->Fetch(request);
166  for (auto & record : response) {
168  }
169  return response;
170  }
171  return TBioseqInfoResponse();
172 }
173 
175 {
176  if (m_BioseqInfoCache) {
177  TBioseqInfoResponse response = m_BioseqInfoCache->FetchLast();
178  for (auto & record : response) {
180  }
181  return response;
182  }
183  return TBioseqInfoResponse();
184 }
185 
187 {
188  if (m_BlobPropCache) {
189  return m_BlobPropCache->Fetch(request);
190  }
191 
192  return TBlobPropResponse();
193 }
194 
196 {
197  if (m_BlobPropCache) {
198  return m_BlobPropCache->FetchLast(request);
199  }
200 
201  return TBlobPropResponse();
202 }
203 
205 {
206  if (m_BlobPropCache) {
207  m_BlobPropCache->EnumerateBlobProp(sat, move(fn));
208  }
209 }
210 
212 {
214  return m_Si2CsiCache->Fetch(request);
215  }
216 
217  return TSi2CsiResponse();
218 }
219 
221 {
222  if (m_Si2CsiCache) {
223  return m_Si2CsiCache->FetchLast();
224  }
225 
226  return TSi2CsiResponse();
227 }
228 
229 string CPubseqGatewayCache::PackBioseqInfoKey(const string& accession, int version)
230 {
232 }
233 
234 string CPubseqGatewayCache::PackBioseqInfoKey(const string& accession, int version, int seq_id_type)
235 {
236  return CPubseqGatewayCacheBioseqInfo::PackKey(accession, version, seq_id_type);
237 }
238 
239 string CPubseqGatewayCache::PackBioseqInfoKey(const string& accession, int version, int seq_id_type, int64_t gi)
240 {
241  return CPubseqGatewayCacheBioseqInfo::PackKey(accession, version, seq_id_type, gi);
242 }
243 
245  const char* key, size_t key_sz, int& version, int& seq_id_type, int64_t& gi)
246 {
247  return CPubseqGatewayCacheBioseqInfo::UnpackKey(key, key_sz, version, seq_id_type, gi);
248 }
249 
251  const char* key, size_t key_sz, string& accession, int& version, int& seq_id_type, int64_t& gi)
252 {
253  return CPubseqGatewayCacheBioseqInfo::UnpackKey(key, key_sz, accession, version, seq_id_type, gi);
254 }
255 
256 string CPubseqGatewayCache::PackSiKey(const string& sec_seqid, int sec_seq_id_type)
257 {
258  return CPubseqGatewayCacheSi2Csi::PackKey(sec_seqid, sec_seq_id_type);
259 }
260 
261 bool CPubseqGatewayCache::UnpackSiKey(const char* key, size_t key_sz, int& sec_seq_id_type)
262 {
263  return CPubseqGatewayCacheSi2Csi::UnpackKey(key, key_sz, sec_seq_id_type);
264 }
265 
266 bool CPubseqGatewayCache::UnpackSiKey(const char* key, size_t key_sz, string& sec_seq_id, int& sec_seq_id_type)
267 {
268  return CPubseqGatewayCacheSi2Csi::UnpackKey(key, key_sz, sec_seq_id, sec_seq_id_type);
269 }
270 
272 {
273  return CPubseqGatewayCacheBlobProp::PackKey(sat_key);
274 }
275 
277 {
278  return CPubseqGatewayCacheBlobProp::PackKey(sat_key, last_modified);
279 }
280 
281 bool CPubseqGatewayCache::UnpackBlobPropKey(const char* key, size_t key_sz, int64_t& last_modified)
282 {
283  return CPubseqGatewayCacheBlobProp::UnpackKey(key, key_sz, last_modified);
284 }
285 
286 bool CPubseqGatewayCache::UnpackBlobPropKey(const char* key, size_t key_sz, int64_t& last_modified, int32_t& sat_key)
287 {
288  return CPubseqGatewayCacheBlobProp::UnpackKey(key, key_sz, last_modified, sat_key);
289 }
290 
#define END_IDBLOB_SCOPE
Definition: IdCassScope.hpp:40
#define BEGIN_IDBLOB_SCOPE
Definition: IdCassScope.hpp:39
CBioseqInfoFetchRequest & SetSeqIdType(CBioseqInfoRecord::TSeqIdType value)
Definition: request.hpp:80
bool HasField(EFields field) const
Definition: request.hpp:126
CBioseqInfoFetchRequest & SetAccession(CBioseqInfoRecord::TAccession const &value)
Definition: request.hpp:64
TSeqIdType GetSeqIdType() const
Definition: record.hpp:208
TSeqIds & GetSeqIds()
Definition: record.hpp:248
TState GetState() const
Definition: record.hpp:263
TAccession const & GetAccession() const
Definition: record.hpp:198
vector< CBioseqInfoRecord > Fetch(CBioseqInfoFetchRequest const &request)
static string PackKey(const string &accession, int version)
static bool UnpackKey(const char *key, size_t key_sz, int &version, int &seq_id_type, int64_t &gi)
static bool UnpackKey(const char *key, size_t key_sz, int64_t &last_modified)
static string PackKey(int32_t sat_key)
static string PackKey(const string &sec_seqid, int sec_seq_id_type)
static bool UnpackKey(const char *key, size_t key_sz, int &sec_seq_id_type)
TBioseqInfoResponse FetchBioseqInfo(TBioseqInfoRequest const &request)
Definition: psg_cache.cpp:162
TBioseqInfoResponse FetchBioseqInfoLast()
Definition: psg_cache.cpp:174
static bool UnpackBlobPropKey(const char *key, size_t key_sz, int64_t &last_modified)
Definition: psg_cache.cpp:281
unique_ptr< CPubseqGatewayCacheBlobProp > m_BlobPropCache
Definition: psg_cache.hpp:125
static bool UnpackSiKey(const char *key, size_t key_sz, int &sec_seq_id_type)
Definition: psg_cache.cpp:261
vector< CBlobRecord > TBlobPropResponse
Definition: psg_cache.hpp:74
TSi2CsiResponse FetchSi2CsiLast()
Definition: psg_cache.cpp:220
TBlobPropResponse FetchBlobPropLast(TBlobPropRequest const &request)
Definition: psg_cache.cpp:195
TRuntimeErrorList m_RuntimeErrors
Definition: psg_cache.hpp:126
vector< CBioseqInfoRecord > TBioseqInfoResponse
Definition: psg_cache.hpp:71
static string PackSiKey(const string &sec_seqid, int sec_seq_id_type)
Definition: psg_cache.cpp:256
void Open(const set< int > &sat_ids)
Definition: psg_cache.cpp:115
void EnumerateBlobProp(int32_t sat, TBlobPropEnumerateFn fn)
Definition: psg_cache.cpp:204
vector< CSI2CSIRecord > TSi2CsiResponse
Definition: psg_cache.hpp:77
virtual ~CPubseqGatewayCache()
Definition: psg_cache.cpp:111
static string PackBlobPropKey(int32_t sat_key)
Definition: psg_cache.cpp:271
function< bool(int32_t, int64_t)> TBlobPropEnumerateFn
Definition: psg_cache.hpp:65
unique_ptr< CPubseqGatewayCacheBioseqInfo > m_BioseqInfoCache
Definition: psg_cache.hpp:123
static bool UnpackBioseqInfoKey(const char *key, size_t key_sz, int &version, int &seq_id_type, int64_t &gi)
Definition: psg_cache.cpp:244
TSi2CsiResponse FetchSi2Csi(CSi2CsiFetchRequest const &request)
Definition: psg_cache.cpp:211
static string PackBioseqInfoKey(const string &accession, int version)
Definition: psg_cache.cpp:229
unique_ptr< CPubseqGatewayCacheSi2Csi > m_Si2CsiCache
Definition: psg_cache.hpp:124
TBlobPropResponse FetchBlobProp(TBlobPropRequest const &request)
Definition: psg_cache.cpp:186
static const size_t kRuntimeErrorLimit
Definition: psg_cache.hpp:80
bool HasField(EFields field) const
Definition: request.hpp:343
Base class for LMDB exception conditions.
Definition: lmdb++.h:63
virtual const char * what() const noexcept
Returns the underlying LMDB error code.
Definition: lmdb++.h:98
Definition: set.hpp:45
iterator_bool insert(const value_type &val)
Definition: set.hpp:149
Int4 int32_t
Int8 int64_t
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
void Warning(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1191
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:75
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
static int version
Definition: mdb_load.c:29
const struct ncbi::grid::netcache::search::fields::KEY key
void sAddRuntimeError(CPubseqGatewayCache::TRuntimeErrorList &error_list, CPubseqGatewayCache::TRuntimeError error)
Definition: psg_cache.cpp:54
void ApplyInheritedSeqIds(CPubseqGatewayCacheBioseqInfo *cache, CBioseqInfoRecord &record)
Definition: psg_cache.cpp:68
USING_IDBLOB_SCOPE
Definition: psg_cache.cpp:51
USING_NCBI_SCOPE
Definition: psg_cache.cpp:52
#define const
Definition: zconf.h:232
Modified on Fri May 24 14:54:38 2024 by modify_doxy.py rev. 669887