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

Go to the SVN repository for this file.

1 /* $Id: cgi_session_netcache.cpp 74384 2016-08-30 11:40:05Z ivanov $
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: Maxim Didenko
27  *
28  *
29  */
30 #include <ncbi_pch.hpp>
31 
32 #include <corelib/ncbireg.hpp>
33 #include <corelib/ncbistr.hpp>
34 
35 #include <cgi/ncbicgi.hpp>
36 #include <cgi/cgi_exception.hpp>
37 
39 
40 
42 
44  m_Storage(CNetCacheAPI(conf)),
45  m_Dirty(false),
46  m_Loaded(false)
47 {
48 }
49 
51 {
52  try {
53  Reset();
54  }
55  catch (std::exception& e) {
56  ERR_POST("Could not flush session data: " << e.what());
57  }
58  catch (...) {
59  ERR_POST("Could not flush session data");
60  }
61 }
62 
64 {
65  m_Blobs.clear();
66  m_SessionId.erase();
67  Reset();
69  m_Loaded = true;
70  return m_SessionId;
71 }
72 
73 void CCgiSession_NetCache::ModifySessionId(const string& /*new_id*/)
74 {
75  NCBI_THROW(CCgiSessionException, eImplNotSet,
76  "This method is not implemented for NetCache service.");
77 }
78 
79 
80 bool CCgiSession_NetCache::LoadSession(const string& sessionid)
81 {
82  m_Blobs.clear();
83  m_SessionId.erase();
84  Reset();
85  m_Loaded = false;
86  string master_value;
87  try {
88  master_value = m_Storage.GetBlobAsString(sessionid);
89  }
90  catch(...) {
91  return false;
92  }
93  m_SessionId = sessionid;
94  list<string> pairs;
95  NStr::Split(master_value, ";", pairs,
97  ITERATE(list<string>, it, pairs) {
98  string blobid, blobname;
99  NStr::SplitInTwo(*it, "|", blobname, blobid);
100  m_Blobs[blobname] = blobid;
101  }
102  m_Loaded = true;
103  return m_Loaded;
104 }
106 {
107  TNames names;
108  x_CheckStatus();
109  ITERATE(TBlobs, it, m_Blobs) {
110  names.push_back(it->first);
111  }
112  return names;
113 }
114 
116  size_t* size)
117 {
118  x_CheckStatus();
119 
120  Reset();
122  if (i == m_Blobs.end()) {
123  NCBI_THROW(CCgiSessionException, eAttrNotFound, " : " + name);
124  }
125  return m_Storage.GetIStream(i->second, size);
126 }
128 {
129  x_CheckStatus();
130  Reset();
131  m_Dirty = true;
132  string& blobid = m_Blobs[name];
133  return m_Storage.CreateOStream(blobid);
134 }
135 
136 void CCgiSession_NetCache::SetAttribute(const string& name, const string& value)
137 {
138  GetAttrOStream(name) << value;
139  Reset();
140 }
141 string CCgiSession_NetCache::GetAttribute(const string& name) const
142 {
143  x_CheckStatus();
144  const_cast<CCgiSession_NetCache*>(this)->Reset();
146  if (i == m_Blobs.end()) {
147  NCBI_THROW(CCgiSessionException, eAttrNotFound, " : " + name);
148  }
149  try {
150  return const_cast<CBlobStorage_NetCache&>(m_Storage).GetBlobAsString(
151  i->second);
152  }
153  catch (CException& e) {
154  NCBI_RETHROW(e, CCgiSessionException, eImplException,
155  "; causal exception attached");
156  }
157 }
158 
160 {
161  x_CheckStatus();
162  TBlobs::iterator i = m_Blobs.find(name);
163  if (i == m_Blobs.end())
164  return;
165  Reset();
166  string blobid = i->second;
167  m_Blobs.erase(i);
168  m_Storage.DeleteBlob(blobid);
169  m_Dirty = true;
170  Reset();
171 }
173 {
174  x_CheckStatus();
175  Reset();
176  ITERATE(TBlobs, it, m_Blobs) {
177  m_Storage.DeleteBlob(it->second);
178  }
180  m_Loaded = false;
181 }
182 
183 
185 {
186  if (!m_Loaded) return;
187  m_Storage.Reset();
188  if (m_Dirty) {
189  string master_value;
190  ITERATE(TBlobs, it, m_Blobs) {
191  if (it != m_Blobs.begin())
192  master_value += ";";
193  master_value += it->first + "|" + it->second;
194  }
196  os << master_value;
197  m_Storage.Reset();
198  m_Dirty = false;
199  }
200 }
201 
202 
204 {
205  if (!m_Loaded)
206  NCBI_THROW(CCgiSessionException, eNotLoaded, "");
207 }
208 
210 
211 /* @} */
Exception classes used by the NCBI CGI framework.
CBlobStorage_NetCache – NetCache-based implementation of IBlobStorage.
CCgiSessionException –.
virtual void SetAttribute(const string &name, const string &value)
Set attribute data as a string.
virtual CNcbiIstream & GetAttrIStream(const string &name, size_t *size=0)
Get input stream to read an attribute data from.
virtual void ModifySessionId(const string &new_id)
Modify session id.
virtual void DeleteSession()
Delete current session.
virtual TNames GetAttributeNames(void) const
Retrieve names of all attributes attached to this session.
virtual void Reset()
Close all open connections.
virtual string GetAttribute(const string &name) const
Get attribute data as string.
virtual bool LoadSession(const string &sessionid)
Load the session.
virtual CNcbiOstream & GetAttrOStream(const string &name)
Get output stream to write an attribute data to.
CBlobStorage_NetCache m_Storage
virtual void RemoveAttribute(const string &name)
Remove attribute from the session.
CCgiSession_NetCache(const IRegistry &)
Create Session Storage from the registry.
virtual string CreateNewSession()
Create a new empty session.
Client API for NetCache server.
IRegistry –.
Definition: ncbireg.hpp:73
void erase(iterator pos)
Definition: map.hpp:167
container_type::const_iterator const_iterator
Definition: map.hpp:53
const_iterator begin() const
Definition: map.hpp:151
const_iterator end() const
Definition: map.hpp:152
void clear()
Definition: map.hpp:169
const_iterator find(const key_type &key) const
Definition: map.hpp:153
The NCBI C++ standard methods for dealing with std::string.
static const struct name_t names[]
#define false
Definition: bool.h:36
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
CCgiSession::TNames TNames
list< string > TNames
Definition: cgi_session.hpp:64
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
#define NCBI_RETHROW(prev_exception, exception_class, err_code, message)
Generic macro to re-throw an exception.
Definition: ncbiexpt.hpp:737
virtual void Reset()
Close all streams and connections.
virtual void DeleteBlob(const string &data_id)
Delete a blob.
virtual string GetBlobAsString(const string &data_id)
Get a blob content as a string.
virtual CNcbiOstream & CreateOStream(string &data_id, ELockMode lock_mode=eLockNoWait)
Get an output stream to a blob.
virtual CNcbiIstream & GetIStream(const string &data_id, size_t *blob_size_ptr=0, ELockMode lock_mode=eLockWait)
Get an input stream to a blob.
virtual string CreateEmptyBlob()
Create an new blob.
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
IO_PREFIX::istream CNcbiIstream
Portable alias for istream.
Definition: ncbistre.hpp:146
static list< string > & Split(const CTempString str, const CTempString delim, list< string > &arr, TSplitFlags flags=0, vector< SIZE_TYPE > *token_pos=NULL)
Split a string using specified delimiters.
Definition: ncbistr.cpp:3452
static bool SplitInTwo(const CTempString str, const CTempString delim, string &str1, string &str2, TSplitFlags flags=0)
Split a string into two pieces using the specified delimiters.
Definition: ncbistr.cpp:3545
@ fSplit_Truncate
Definition: ncbistr.hpp:2503
@ fSplit_MergeDelimiters
Merge adjacent delimiters.
Definition: ncbistr.hpp:2500
int i
const struct ncbi::grid::netcache::search::fields::SIZE size
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
Process information in the NCBI Registry, including working with configuration files.
Modified on Fri Sep 20 14:58:19 2024 by modify_doxy.py rev. 669887