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

Go to the SVN repository for this file.

1 #ifndef CONNECT_SERVICES_IMPL__NETSTORAGE_IMPL__HPP
2 #define CONNECT_SERVICES_IMPL__NETSTORAGE_IMPL__HPP
3 
4 /* $Id: netstorage_impl.hpp 76845 2017-03-09 16:11:36Z sadyrovr $
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: Dmitry Kazimirov
30  *
31  * File Description:
32  * NetStorage implementation declarations.
33  *
34  */
35 
36 #include <corelib/ncbi_url.hpp>
39 
40 #include <algorithm>
41 
43 
45 
46 /// @internal
48 {
49  virtual string GetLoc() const = 0;
50  virtual bool Eof() = 0;
51  virtual Uint8 GetSize() = 0;
52  virtual list<string> GetAttributeList() const = 0;
53  virtual string GetAttribute(const string& name) const = 0;
54  virtual void SetAttribute(const string& name, const string& value) = 0;
56  virtual void SetExpiration(const CTimeout& ttl) = 0;
57  virtual string FileTrack_Path() = 0;
59  virtual bool Exists() = 0;
61 
62  virtual pair<string, string> GetUserInfo()
63  {
64  NCBI_THROW_FMT(CNetStorageException, eNotSupported, "INetStorageObjectState::GetUserInfo()");
65  }
66 
68  {
69  NCBI_THROW_FMT(CNetStorageException, eNotSupported, "INetStorageObjectState::Locator()");
70  }
71 
72  virtual void CancelRelocate()
73  {
74  NCBI_THROW_FMT(CNetStorageException, eNotSupported, "INetStorageObjectState::CancelRelocate()");
75  }
76 
77 protected:
78  void EnterState(INetStorageObjectState* state);
79  void ExitState();
80 
81 private:
82  virtual SNetStorageObjectImpl& Fsm() = 0;
83 };
84 
85 /// @internal
86 template <class TBase>
87 struct SNetStorageObjectState : TBase
88 {
89  template <class... TArgs>
91  TBase(std::forward<TArgs>(args)...),
92  m_Fsm(fsm)
93  {
94  }
95 
96 private:
97  SNetStorageObjectImpl& Fsm() final { return m_Fsm; }
98 
100 };
101 
102 /// @internal
104 {
105  Uint8 GetSize() final;
106  list<string> GetAttributeList() const final;
107  string GetAttribute(const string& name) const final;
108  void SetAttribute(const string& name, const string& value) final;
109  CNetStorageObjectInfo GetInfo() final;
110  void SetExpiration(const CTimeout& ttl) final;
111  string FileTrack_Path() final;
112  string Relocate(TNetStorageFlags flags, TNetStorageProgressCb cb) final;
113  bool Exists() final;
114  ENetStorageRemoveResult Remove() final;
115 };
116 
117 /// @internal
119 {
120  ERW_Result Write(const void* buf, size_t count, size_t* written) final;
121  ERW_Result Flush() final;
122 };
123 
124 /// @internal
126 {
127  ERW_Result Read(void* buf, size_t count, size_t* read) final;
128  ERW_Result PendingCount(size_t* count) final;
129  bool Eof() final;
130 };
131 
132 /// @internal
134 {
135  enum EApi { eAnyApi, eBuffer, eIoStream, eIReaderIWriter, eString };
136  enum EMth { eAnyMth, eRead, eWrite, eEof };
137 
138  bool Set(EApi api, EMth mth)
139  {
140  if (m_Api != eAnyApi && m_Api != api) return false;
141 
142  m_Api = api;
143  m_Mth = mth;
144  return true;
145  }
146 
147  void Reset() { m_Api = eAnyApi; }
148  bool IoStream() const { return m_Api == eIoStream; }
149  void Throw(EApi api, EMth mth, string object_loc);
150 
151 private:
152  static string ToString(EApi api, EMth mth);
153 
154  EApi m_Api = eAnyApi;
155  EMth m_Mth = eAnyMth;
156 };
157 
158 /// @internal
160 {
162 
163  void SetStartState(INetStorageObjectState* state);
164 
166  {
167  if (!m_IoMode.Set(api, mth)) m_IoMode.Throw(api, mth, m_Current->GetLoc());
168  }
169 
170  IEmbeddedStreamReaderWriter& GetReaderWriter();
171  CNcbiIostream* GetRWStream();
172 
173  INetStorageObjectState* operator->() { _ASSERT(m_Current); return m_Current; }
174  const INetStorageObjectState* operator->() const { _ASSERT(m_Current); return m_Current; }
175 
176  void Close();
177 
178  template <class TState, class... TArgs>
179  static SNetStorageObjectImpl* Create(TArgs&&... args)
180  {
181  return CreateAndStart<TState>([](TState&){}, std::forward<TArgs>(args)...);
182  }
183 
184  template <class TState, class TStarter, class... TArgs>
185  static SNetStorageObjectImpl* CreateAndStart(TStarter starter, TArgs&&... args)
186  {
187  unique_ptr<SNetStorageObjectImpl> fsm(new SNetStorageObjectImpl());
188  auto state = new SNetStorageObjectState<TState>(*fsm, *fsm, std::forward<TArgs>(args)...);
189  fsm->SetStartState(state);
190  starter(*state);
191  return fsm.release();
192  }
193 
194 private:
195  void EnterState(INetStorageObjectState* state);
196  void ExitState();
197 
198  unique_ptr<IEmbeddedStreamReaderWriter> m_ReaderWriter;
199  unique_ptr<IEmbeddedStreamReaderWriter> m_IoStreamReaderWriter;
200  unique_ptr<INetStorageObjectState> m_Start;
201  INetStorageObjectState* m_Previous = nullptr;
202  INetStorageObjectState* m_Current = nullptr;
204 
205  friend struct INetStorageObjectState;
206 };
207 
209 {
210  _ASSERT(state);
211  _ASSERT(!m_Start);
212  m_Start.reset(state);
213  m_Current = state;
214 }
215 
217 {
218  _ASSERT(state);
220  m_Current = state;
221 }
222 
224 {
227  m_Previous = nullptr;
228 }
229 
231 {
232  Fsm().EnterState(state);
233 }
234 
236 {
237  Fsm().ExitState();
238 }
239 
240 /// @internal
242 {
243  struct SConfig;
244  struct SLimits;
245 
246  static SNetStorageImpl* CreateImpl(const SConfig&, TNetStorageFlags);
247  static SNetStorageByKeyImpl* CreateByKeyImpl(const SConfig&, TNetStorageFlags);
248 };
249 
250 /// @internal
252 {
258  };
259 
260  enum EErrMode {
264  };
265 
266  string service;
267  string nc_service;
268  string app_domain;
269  string client_name;
270  string metadata;
273  string ticket;
275 
276  SConfig() : default_storage(eUndefined), err_mode(eLog) {}
277  void ParseArg(const string&, const string&);
278  void Validate(const string&);
279 
280  static SConfig Build(const string& init_string)
281  {
282  return BuildImpl<SConfig>(init_string);
283  }
284 
285 protected:
286  template <class TConfig>
287  static TConfig BuildImpl(const string& init_string)
288  {
289  CUrlArgs url_parser(init_string);
290  TConfig cfg;
291 
292  ITERATE(CUrlArgs::TArgs, field, url_parser.GetArgs()) {
293  if (!field->name.empty() && !field->value.empty()) {
294  cfg.ParseArg(field->name, field->value);
295  }
296  }
297 
298  cfg.Validate(init_string);
299  return cfg;
300  }
301 
302 private:
303  static EDefaultStorage GetDefaultStorage(const string&);
304  static EErrMode GetErrMode(const string&);
305 };
306 
307 /// @internal
309 {
310  struct SNamespace
311  {
312  static string Name() { return "Namespace"; }
313  static size_t MaxLength() { return 32; }
314  static bool IsValid(char c) { return isalnum(c) || c == '_'; };
315  };
316 
317  struct SUserKey
318  {
319  static string Name() { return "User key"; }
320  static size_t MaxLength() { return 256; }
321  static bool IsValid(char c) { return ::isprint(c); };
322  };
323 
324  struct SAttrName
325  {
326  static string Name() { return "Attribute name"; }
327  static size_t MaxLength() { return 64; }
328  static bool IsValid(char c) { return isalnum(c) || c == '_'; };
329  };
330 
331  struct SAttrValue
332  {
333  static string Name() { return "Attribute value"; }
334  static size_t MaxLength() { return 900; }
335  static bool IsValid(char) { return true; };
336  };
337 
338  struct SClientName
339  {
340  static string Name() { return "Client name"; }
341  static size_t MaxLength() { return 256; }
342  static bool IsValid(char c) { return ::isprint(c); };
343  };
344 
346  {
347  static string Name() { return "User namespace"; }
348  static size_t MaxLength() { return 64; }
349  static bool IsValid(char c) { return isalnum(c) || c == '_'; };
350  };
351 
352  struct SUserName
353  {
354  static string Name() { return "User name"; }
355  static size_t MaxLength() { return 64; }
356  static bool IsValid(char c) { return isalnum(c) || c == '_'; };
357  };
358 
359  template <class TValue>
360  static void Check(const string& value)
361  {
362  if (value.length() > TValue::MaxLength()) {
363  ThrowTooLong(TValue::Name(), TValue::MaxLength());
364  }
365 
366  if (!all_of(value.begin(), value.end(), TValue::IsValid)) {
367  ThrowIllegalChars(TValue::Name(), value);
368  }
369  }
370 
371 private:
372  static void ThrowTooLong(const string&, size_t);
373  static void ThrowIllegalChars(const string&, const string&);
374 };
375 
376 /// @internal
378 {
380 
382  virtual SNetStorageObjectImpl* Open(const string& object_loc) = 0;
383 };
384 
385 /// @internal
387 {
389 
390  virtual SNetStorageObjectImpl* Open(const string& unique_key, TNetStorageFlags flags) = 0;
391 };
392 
393 #define NETSTORAGE_CONVERT_NETCACHEEXCEPTION(message) \
394  catch (CNetCacheException& e) { \
395  g_ThrowNetStorageException(DIAG_COMPILE_INFO, e, FORMAT(message)); \
396  }
398 void g_ThrowNetStorageException(const CDiagCompileInfo& compile_info,
399  const CNetCacheException& prev_exception, const string& message);
400 
402 CNetStorageObjectInfo g_CreateNetStorageObjectInfo(const string& object_loc,
404  const CNetStorageObjectLoc* object_loc_struct,
405  Uint8 file_size, CJsonNode::TInstance storage_specific_info);
406 
409  const CJsonNode& object_info_node);
410 
412 
413 #endif /* CONNECT_SERVICES_IMPL__NETSTORAGE_IMPL__HPP */
Incapsulate compile time information such as __FILE__, __LINE__, NCBI_MODULE, current function.
Definition: ncbidiag.hpp:65
JSON node abstraction.
NetCache internal exception.
Exception class for use by CNetStorage, CNetStorageByKey, and CNetStorageObject.
Definition: netstorage.hpp:67
Detailed information about a CNetStorage object.
Definition: netstorage.hpp:96
CObject –.
Definition: ncbiobj.hpp:180
CTimeout – Timeout interval.
Definition: ncbitime.hpp:1693
CUrlArgs::
Definition: ncbi_url.hpp:240
virtual void Close()=0
URL parsing classes.
static uch flags
static const char location[]
Definition: config.c:97
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#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
unsigned TNetStorageFlags
Bitwise OR of ENetStorageFlags.
Definition: netstorage.hpp:147
ENetStorageObjectLocation
Enumeration that indicates the current location of the object.
Definition: netstorage.hpp:86
function< void(CJsonNode)> TNetStorageProgressCb
Progress callback.
Definition: netstorage.hpp:348
ENetStorageRemoveResult
Result returned by Remove() methods.
Definition: netstorage.hpp:356
bool IsValid(const CSeq_point &pt, CScope *scope)
Checks that point >= 0 and point < length of Bioseq.
uint64_t Uint8
8-byte (64-bit) unsigned integer
Definition: ncbitype.h:105
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
ERW_Result
Result codes for I/O operations.
virtual ERW_Result Flush(void)=0
Flush pending data (if any) down to the output device.
virtual ERW_Result PendingCount(size_t *count)=0
Via parameter "count" (which is guaranteed to be supplied non-NULL) return the number of bytes that a...
IO_PREFIX::iostream CNcbiIostream
Portable alias for iostream.
Definition: ncbistre.hpp:152
virtual ERW_Result Write(const void *buf, size_t count, size_t *bytes_written=0)=0
Write up to "count" bytes from the buffer pointed to by the "buf" argument onto the output device.
virtual ERW_Result Read(void *buf, size_t count, size_t *bytes_read=0)=0
Read as many as "count" bytes into a buffer pointed to by the "buf" argument.
const TArgs & GetArgs(void) const
Get the const list of arguments.
Definition: ncbi_url.hpp:300
list< TArg > TArgs
Definition: ncbi_url.hpp:276
#define NCBI_XCONNECT_EXPORT
char * buf
static int Eof
Definition: mdb_load.c:35
SNetStorageObjectState< SNetStorageObjectDirectState< TBase > > TState
Definition: state.hpp:107
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
int isalnum(Uchar c)
Definition: ncbictype.hpp:62
int isprint(Uchar c)
Definition: ncbictype.hpp:67
NetCache API exception declarations.
CNetStorageObjectInfo g_CreateNetStorageObjectInfo(const string &object_loc, ENetStorageObjectLocation location, const CNetStorageObjectLoc *object_loc_struct, Uint8 file_size, CJsonNode::TInstance storage_specific_info)
void g_ThrowNetStorageException(const CDiagCompileInfo &compile_info, const CNetCacheException &prev_exception, const string &message)
Definition: netstorage.cpp:425
@ eRead
Definition: ns_types.hpp:56
virtual list< string > GetAttributeList() const =0
virtual string Relocate(TNetStorageFlags flags, TNetStorageProgressCb cb)=0
virtual void SetAttribute(const string &name, const string &value)=0
virtual Uint8 GetSize()=0
virtual void SetExpiration(const CTimeout &ttl)=0
void EnterState(INetStorageObjectState *state)
virtual bool Eof()=0
virtual pair< string, string > GetUserInfo()
virtual SNetStorageObjectImpl & Fsm()=0
virtual bool Exists()=0
virtual ENetStorageRemoveResult Remove()=0
virtual string GetAttribute(const string &name) const =0
virtual string FileTrack_Path()=0
virtual CNetStorageObjectLoc & Locator()
virtual string GetLoc() const =0
virtual void CancelRelocate()
virtual CNetStorageObjectInfo GetInfo()=0
virtual SNetStorageObjectImpl * Open(const string &unique_key, TNetStorageFlags flags)=0
SNetStorage::SConfig TConfig
virtual SNetStorageObjectImpl * Create(TNetStorageFlags flags)=0
virtual SNetStorageObjectImpl * Open(const string &object_loc)=0
SNetStorage::SConfig TConfig
INetStorageObjectState * m_Previous
INetStorageObjectState * m_Current
const INetStorageObjectState * operator->() const
SNetStorageObjectIoMode m_IoMode
unique_ptr< INetStorageObjectState > m_Start
unique_ptr< IEmbeddedStreamReaderWriter > m_IoStreamReaderWriter
INetStorageObjectState * operator->()
unique_ptr< IEmbeddedStreamReaderWriter > m_ReaderWriter
static SNetStorageObjectImpl * Create(TArgs &&... args)
static SNetStorageObjectImpl * CreateAndStart(TStarter starter, TArgs &&... args)
void SetStartState(INetStorageObjectState *state)
void SetIoMode(SNetStorageObjectIoMode::EApi api, SNetStorageObjectIoMode::EMth mth)
void EnterState(INetStorageObjectState *state)
bool Set(EApi api, EMth mth)
SNetStorageObjectImpl & Fsm() final
SNetStorageObjectState(SNetStorageObjectImpl &fsm, TArgs &&... args)
SNetStorageObjectImpl & m_Fsm
static TConfig BuildImpl(const string &init_string)
static SConfig Build(const string &init_string)
EDefaultStorage default_storage
static void Check(const string &value)
#define _ASSERT
string ToString(const wxRect &rc)
Definition: wx_utils.cpp:773
Modified on Mon Apr 22 04:07:06 2024 by modify_doxy.py rev. 669887