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

Go to the SVN repository for this file.

1 /* $Id: netstorage.cpp 91249 2020-09-30 19:12:51Z sadyrovr $
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: Dmitry Kazimirov, Rafael Sadyrov
27  *
28  * File Description:
29  * Implementation of the unified network blob storage API.
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
34 #include "object.hpp"
35 
36 
38 
39 
40 using namespace NDirectNetStorageImpl;
41 
42 
43 template <class TDerived, class TBaseRef>
44 TDerived* Impl(TBaseRef& base_ref)
45 {
46  return static_cast<TDerived*>(base_ref.GetNonNullPointer());
47 }
48 
49 
52 {
53 }
54 
55 
58 {
59  return m_Impl--->Relocate(flags, cb);
60 }
61 
62 
64 {
65  return m_Impl--->CancelRelocate();
66 }
67 
68 
70 {
71  return m_Impl--->Remove();
72 }
73 
74 
76 {
77  return m_Impl--->Locator();
78 }
79 
80 
82 {
83  return m_Impl--->FileTrack_Path();
84 }
85 
86 
88 {
89  return m_Impl--->GetUserInfo();
90 }
91 
92 
95 {}
96 
97 
99 {
101 
102  static SNetStorageImpl* CreateImpl(const string& init_string,
103  TNetStorageFlags default_flags);
104 
105  static SNetStorageByKeyImpl* CreateByKeyImpl(const string& init_string,
106  TNetStorageFlags default_flags);
107 };
108 
109 
111 {
113  TNetStorageFlags default_flags)
114  : m_Context(new SContext(config, default_flags))
115  {
116  }
117 
119  SNetStorageObjectImpl* Open(const string&);
120 
121  // For direct NetStorage API only
123  const IRegistry&);
125  bool Exists(const string& db_loc, const string& client_loc);
126  CJsonNode ReportConfig() const;
127 
128 private:
130 };
131 
132 
134 {
135  if (!flags) flags = m_Context->default_flags;
136 
137  return SNetStorageObjectImpl::Create<CObj>(m_Context, m_Context->Create(flags), flags);
138 }
139 
140 
142 {
143  CNetStorageObjectLoc loc(m_Context->compound_id_pool, object_loc);
144 
145  return SNetStorageObjectImpl::Create<CObj>(m_Context, loc, loc.GetStorageAttrFlags(), true, loc.GetLocation());
146 }
147 
148 
149 void s_SetServiceName(CNetStorageObjectLoc& loc, const string& service)
150 {
151  // Do not set fake service name used by NST health check script
152  if (NStr::CompareNocase(service, "LBSMDNSTTestService")) {
153  loc.SetServiceName(service);
154  }
155 }
156 
157 
159 {
160  if (!flags) flags = m_Context->default_flags;
161 
162  CNetStorageObjectLoc loc = m_Context->Create(flags);
163  s_SetServiceName(loc, service);
164 
165  // Server reports locator to the client before writing anything
166  // So, object must choose location for writing here to make locator valid
167  auto l = [&](CObj& state) { state.SetLocator(); };
168 
169  return SNetStorageObjectImpl::CreateAndStart<CObj>(l, m_Context, loc, flags);
170 }
171 
172 
174  CCompoundIDPool::TInstance compound_id_pool,
175  const IRegistry& registry)
176  // NetStorage server does not get app_domain from clients
177  // if object locators are used (opposed to object keys).
178  // Instead of introducing new configuration parameter
179  // we notify the context that app_domain not available (thus kEmptyStr).
180  : m_Context(new SContext(service_name, kEmptyStr,
181  compound_id_pool, registry))
182 {
183 }
184 
185 
186 bool s_TrustOldLoc(CCompoundIDPool& pool, const string& old_loc,
187  TNetStorageFlags new_flags)
188 {
189  if (old_loc.empty()) return false;
190 
191  auto old_location = CNetStorageObjectLoc(pool, old_loc).GetLocation();
192 
193  // Old location is unknown/not found
194  if (old_location == eNFL_Unknown) return false;
195  if (old_location == eNFL_NotFound) return false;
196 
197  // New location is unknown
198  if (!(new_flags & fNST_AnyLoc)) return true;
199 
200  // Even if new location is wrong,
201  // the object should be still accessible (due to "movable")
202  if (new_flags & fNST_Movable) return true;
203 
204  // Both locations are known, check for discrepancies
205 
206  // Old location is NetCache
207  if (old_location == eNFL_NetCache) {
208  return new_flags & fNST_NetCache;
209  }
210 
211  // Old location is FileTrack
212  return new_flags & fNST_FileTrack;
213 }
214 
215 bool SDirectNetStorageImpl::Exists(const string& db_loc, const string& client_loc)
216 {
217  CNetStorageObjectLoc new_loc(m_Context->compound_id_pool, client_loc);
219 
220  if (new_loc.GetLocation() == eNFL_NetCache) {
221  flags |= fNST_NetCache;
222  } else if (new_loc.GetLocation() == eNFL_FileTrack) {
224  }
225 
226  if (s_TrustOldLoc(m_Context->compound_id_pool, db_loc, flags)) {
227  return true;
228  }
229 
230  CNetStorageObject object = Open(client_loc);
231  return object--->Exists();
232 }
233 
234 
236 {
239 
240  if (m_Context->icache_client) result.SetByKey("netcache", empty);
241  if (m_Context->filetrack_api) result.SetByKey("filetrack", empty);
242 
243  return result;
244 }
245 
246 
248 {
250  TNetStorageFlags default_flags)
251  : m_Context(new SContext(config, default_flags))
252  {
253  }
254 
256 
257  // For direct NetStorage API only
258  SDirectNetStorageByKeyImpl(const string&, const string&,
261  bool Exists(const string& db_loc, const string& key, TNetStorageFlags flags);
262 
263 private:
265  const string m_ServiceName;
266 };
267 
268 
271 {
272  if (!flags) flags = m_Context->default_flags;
273 
274  return SNetStorageObjectImpl::Create<CObj>(m_Context, m_Context->Create(key, flags), flags, true);
275 }
276 
277 
279  const string& service_name,
280  const string& app_domain,
281  CCompoundIDPool::TInstance compound_id_pool,
282  const IRegistry& registry)
283  : m_Context(new SContext(service_name, app_domain,
284  compound_id_pool, registry)),
285  m_ServiceName(service_name)
286 {
287  if (app_domain.empty()) {
289  "Domain name cannot be empty.");
290  }
291 }
292 
293 
295 {
296  if (!flags) flags = m_Context->default_flags;
297 
298  CNetStorageObjectLoc loc = m_Context->Create(key, flags);
300 
301  return SNetStorageObjectImpl::Create<CObj>(m_Context, loc, flags, true);
302 }
303 
304 
305 bool SDirectNetStorageByKeyImpl::Exists(const string& db_loc,
306  const string& key, TNetStorageFlags flags)
307 {
308  if (s_TrustOldLoc(m_Context->compound_id_pool, db_loc, flags)) {
309  return true;
310  }
311 
312  CNetStorageObject object = Open(key, flags);
313  return object--->Exists();
314 }
315 
316 
318  const IRegistry& registry,
319  const string& service_name,
320  CCompoundIDPool::TInstance compound_id_pool)
321  : CNetStorage(
322  new SDirectNetStorageImpl(service_name, compound_id_pool, registry))
323 {
324 }
325 
326 
329 {
330  return Impl<SDirectNetStorageImpl>(m_Impl)->Create(flags, service_name);
331 }
332 
333 
335 {
336  return m_Impl->Open(object_loc);
337 }
338 
339 
340 bool CDirectNetStorage::Exists(const string& db_loc, const string& client_loc)
341 {
342  return Impl<SDirectNetStorageImpl>(m_Impl)->Exists(db_loc, client_loc);
343 }
344 
345 
347 {
348  return Impl<const SDirectNetStorageImpl>(m_Impl)->ReportConfig();
349 }
350 
351 
353  const IRegistry& registry,
354  const string& service_name,
355  CCompoundIDPool::TInstance compound_id_pool,
356  const string& app_domain)
358  new SDirectNetStorageByKeyImpl(service_name, app_domain,
359  compound_id_pool, registry))
360 {
361 }
362 
363 
366 {
367  SNetStorage::SLimits::Check<SNetStorage::SLimits::SUserKey>(key);
368  return Impl<SDirectNetStorageByKeyImpl>(m_Impl)->Open(flags, key);
369 }
370 
371 
372 bool CDirectNetStorageByKey::Exists(const string& db_loc,
373  const string& key, TNetStorageFlags flags)
374 {
375  return Impl<SDirectNetStorageByKeyImpl>(m_Impl)->Exists(db_loc, key, flags);
376 }
377 
378 
380  const string& init_string, TNetStorageFlags default_flags)
381 {
382  TConfig config(TConfig::Build(init_string));
383 
384  return config.mode == TConfig::eDefault ?
385  SNetStorage::CreateImpl(config, default_flags) :
386  new SDirectNetStorageImpl(config, default_flags);
387 }
388 
389 
391  TNetStorageFlags default_flags) :
392  CNetStorage(
393  SCombinedNetStorage::CreateImpl(init_string, default_flags))
394 {
395 }
396 
397 
399  const string& init_string, TNetStorageFlags default_flags)
400 {
401  TConfig config(TConfig::Build(init_string));
402 
403  return config.mode == TConfig::eDefault ?
404  SNetStorage::CreateByKeyImpl(config, default_flags) :
405  new SDirectNetStorageByKeyImpl(config, default_flags);
406 }
407 
408 
410  TNetStorageFlags default_flags) :
412  SCombinedNetStorage::CreateByKeyImpl(init_string, default_flags))
413 {
414 }
415 
416 
CCombinedNetStorageByKey(const string &init_string, TNetStorageFlags default_flags=0)
Definition: netstorage.cpp:409
CCombinedNetStorage(const string &init_string, TNetStorageFlags default_flags=0)
Definition: netstorage.cpp:390
Pool of recycled CCompoundID objects.
CDirectNetStorageObject Open(const string &unique_key, TNetStorageFlags flags=0)
Definition: netstorage.cpp:364
bool Exists(const string &db_loc, const string &key, TNetStorageFlags flags)
Definition: netstorage.cpp:372
CDirectNetStorageByKey(const IRegistry &registry, const string &service_name, CCompoundIDPool::TInstance compound_id_pool, const string &app_domain)
Definition: netstorage.cpp:352
ENetStorageRemoveResult Remove()
Definition: netstorage.cpp:69
const CNetStorageObjectLoc & Locator()
Definition: netstorage.cpp:75
string Relocate(TNetStorageFlags flags, TNetStorageProgressCb cb=TNetStorageProgressCb())
Definition: netstorage.cpp:56
pair< string, string > GetUserInfo()
Definition: netstorage.cpp:87
CDirectNetStorageObject Open(const string &object_loc)
Definition: netstorage.cpp:334
CDirectNetStorage(const IRegistry &registry, const string &service_name, CCompoundIDPool::TInstance compound_id_pool)
Definition: netstorage.cpp:317
CDirectNetStorageObject Create(const string &service_name, TNetStorageFlags flags)
Definition: netstorage.cpp:327
bool Exists(const string &db_loc, const string &client_loc)
Definition: netstorage.cpp:340
CJsonNode ReportConfig() const
Definition: netstorage.cpp:346
JSON node abstraction.
static CJsonNode NewObjectNode()
Create a new JSON object node.
Network BLOB storage API – with access by user-defined keys.
Definition: netstorage.hpp:471
Exception class for use by CNetStorage, CNetStorageByKey, and CNetStorageObject.
Definition: netstorage.hpp:67
TNetStorageAttrFlags GetStorageAttrFlags() const
ENetStorageObjectLocation GetLocation() const
void SetServiceName(const string &service_name)
Basic network-based data object I/O.
Definition: netstorage.hpp:167
Network BLOB storage API.
Definition: netstorage.hpp:368
CRef –.
Definition: ncbiobj.hpp:618
IRegistry –.
Definition: ncbireg.hpp:73
static CMemoryRegistry registry
Definition: cn3d_tools.cpp:81
struct config config
static uch flags
#define NCBI_THROW_FMT(exception_class, err_code, message)
The same as NCBI_THROW but with message processed as output to ostream.
Definition: ncbiexpt.hpp:719
CNetRef< SNetStorageImpl > m_Impl
Definition: netstorage.hpp:369
unsigned TNetStorageFlags
Bitwise OR of ENetStorageFlags.
Definition: netstorage.hpp:147
CNetRef< SNetStorageObjectImpl > m_Impl
Definition: netstorage.hpp:168
function< void(CJsonNode)> TNetStorageProgressCb
Progress callback.
Definition: netstorage.hpp:348
CNetRef< SNetStorageByKeyImpl > m_Impl
Definition: netstorage.hpp:472
ENetStorageRemoveResult
Result returned by Remove() methods.
Definition: netstorage.hpp:356
@ fNST_AnyLoc
Any location (all location bits are set)
Definition: netstorage.hpp:136
@ fNST_NetCache
Use NetCache as the primary storage.
Definition: netstorage.hpp:130
@ fNST_FileTrack
Use FileTrack as the primary storage.
Definition: netstorage.hpp:131
@ fNST_Movable
Allow the object to move between storages.
Definition: netstorage.hpp:138
@ eNFL_FileTrack
Definition: netstorage.hpp:90
@ eNFL_NetCache
Definition: netstorage.hpp:89
@ eNFL_NotFound
Definition: netstorage.hpp:88
@ eNFL_Unknown
Definition: netstorage.hpp:87
#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
void s_SetServiceName(CNetStorageObjectLoc &loc, const string &service)
Definition: netstorage.cpp:149
TDerived * Impl(TBaseRef &base_ref)
Definition: netstorage.cpp:44
bool s_TrustOldLoc(CCompoundIDPool &pool, const string &old_loc, TNetStorageFlags new_flags)
Definition: netstorage.cpp:186
constexpr bool empty(list< Ts... >) noexcept
const struct ncbi::grid::netcache::search::fields::KEY key
EVoid
To create a void (uninitialized) component instance.
@ eVoid
To create a void (uninitialized) instance of a component.
static SCombinedNetStorageConfig Build(const string &init_string)
Definition: state.hpp:55
static SNetStorageByKeyImpl * CreateByKeyImpl(const string &init_string, TNetStorageFlags default_flags)
Definition: netstorage.cpp:398
SCombinedNetStorageConfig TConfig
Definition: netstorage.cpp:100
static SNetStorageImpl * CreateImpl(const string &init_string, TNetStorageFlags default_flags)
Definition: netstorage.cpp:379
CRef< SContext > m_Context
Definition: netstorage.cpp:264
SDirectNetStorageByKeyImpl(const SCombinedNetStorageConfig &config, TNetStorageFlags default_flags)
Definition: netstorage.cpp:249
bool Exists(const string &db_loc, const string &key, TNetStorageFlags flags)
Definition: netstorage.cpp:305
SNetStorageObjectImpl * Open(const string &, TNetStorageFlags)
Definition: netstorage.cpp:269
SNetStorageObjectImpl * Open(const string &)
Definition: netstorage.cpp:141
bool Exists(const string &db_loc, const string &client_loc)
Definition: netstorage.cpp:215
SDirectNetStorageImpl(const SCombinedNetStorageConfig &config, TNetStorageFlags default_flags)
Definition: netstorage.cpp:112
CJsonNode ReportConfig() const
Definition: netstorage.cpp:235
SNetStorageObjectImpl * Create(TNetStorageFlags)
Definition: netstorage.cpp:133
CRef< SContext > m_Context
Definition: netstorage.cpp:129
static SNetStorageImpl * CreateImpl(const SConfig &, TNetStorageFlags)
static SNetStorageByKeyImpl * CreateByKeyImpl(const SConfig &, TNetStorageFlags)
else result
Definition: token2.c:20
Modified on Wed Apr 17 13:10:40 2024 by modify_doxy.py rev. 669887