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

Go to the SVN repository for this file.

1 /* $Id: netstorageobjectinfo.cpp 74868 2016-10-03 15:27:20Z 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 CNetStorageObjectInfo.
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
36 
37 
39 
41 
42 struct SData
43 {
45  string object_loc;
49 
50  SData() {}
52  const string& ol,
54  Uint8 fs,
56  : location(l),
57  object_loc(ol),
58  object_loc_info(li),
59  file_size(fs),
60  st_info(si)
61  {}
62 };
63 
65 {
68 
69  SLazyInitData(const SData& d)
70  : SData(d),
73  {
74  InitExtra();
75  }
76 
78  : json(j),
81  {
82  Clean();
83  }
84 
85  void Check()
86  {
87  if (!initialized) {
88  initialized = true;
89  if (data_source)
90  InitJson();
91  else
92  InitData();
93  }
94  }
95 
96 private:
97  template <TLocation> CTime GetTime();
98  void InitData();
99  void InitExtra();
100  void InitJson();
101  void Clean();
102 
105 };
106 
107 
109 {
111 
112  TLocation GetLocation() const { Check(); return m_Data.location; }
114  CTime GetCreationTime() const { Check(); return m_Data.time; }
115  Uint8 GetSize() const { Check(); return m_Data.file_size; }
117  CJsonNode GetJSON() const { Check(); return m_Data.json; }
118 
119 private:
120  void Check() const { m_Data.Check(); }
121 
123 };
124 
125 
126 template <>
127 CTime SLazyInitData::GetTime<eNFL_FileTrack>()
128 {
129  if (st_info) {
130  if (CJsonNode ctime = st_info.GetByKeyOrNull("ctime")) {
131  const string ctime_string = ctime.AsString();
132 
133  // TODO:
134  // Remove code related to old format after
135  // all NetStorage servers upgraded to contain CXX-8230.
136  try {
137  return CTime(ctime_string, "Y-M-DTh:m:s.rZ").ToLocalTime();
138  }
139  catch (CTimeException& ex) {
140  if (ex.GetErrCode() != CTimeException::eFormat) throw;
141 
142  // Try old format.
143  return CTime(ctime_string, "Y-M-DTh:m:s.ro").ToLocalTime();
144  }
145  }
146  }
147 
148  return CTime();
149 }
150 
151 template <>
152 CTime SLazyInitData::GetTime<eNFL_NetCache>()
153 {
154  const char* const kNCTimeFormat = "M/D/Y h:m:s.r";
155 
156  if (st_info) {
157  if (CJsonNode ctime = st_info.GetByKeyOrNull("Write time")) {
158  return CTime(ctime.AsString(), kNCTimeFormat);
159  }
160  }
161 
162  return CTime();
163 }
164 
166 {
167  // Init data from JSON
168 
169  const string l(json.GetString("Location"));
170  CJsonNode ol(json.GetByKeyOrNull("ObjectLoc"));
172 
173  location =
174  l == "NetCache" ? eNFL_NetCache :
175  l == "FileTrack" ? eNFL_FileTrack :
176  l == "NotFound" ? eNFL_NotFound : eNFL_Unknown;
177  object_loc = ol ? ol.AsString() : kEmptyStr;
178  object_loc_info = json.GetByKey("ObjectLocInfo");
179  file_size = size ? (Uint8) size.AsInteger() : 0;
180  st_info = json.GetByKeyOrNull("StorageSpecificInfo");
181  InitExtra();
182 }
183 
185 {
186  if (location == eNFL_FileTrack) {
187  time = GetTime<eNFL_FileTrack>();
188  } else if (location == eNFL_NetCache) {
189  time = GetTime<eNFL_NetCache>();
190  }
191 }
192 
194 {
195  // Remove server reply
196  json.DeleteByKey("Type");
197  json.DeleteByKey("Status");
198  json.DeleteByKey("RE");
199 }
200 
202 {
203  // Init JSON from data
204 
205  const char* const kOutputTimeFormat = "M/D/Y h:m:s";
207 
208  switch (location) {
209  case eNFL_NetCache:
210  json.SetByKey("CreationTime", CJsonNode::NewStringNode(
211  GetTime<eNFL_NetCache>().AsString(kOutputTimeFormat)));
212  json.SetString("Location", "NetCache");
213  json.SetInteger("Size", file_size);
214  break;
215  case eNFL_FileTrack:
216  json.SetByKey("CreationTime", CJsonNode::NewStringNode(
217  GetTime<eNFL_FileTrack>().AsString(kOutputTimeFormat)));
218  json.SetString("Location", "FileTrack");
219  json.SetInteger("Size", file_size);
220  break;
221  default:
222  json.SetString("Location", "NotFound");
223  }
224 
225  json.SetString("ObjectLoc", object_loc);
226 
227  if (object_loc_info)
228  json.SetByKey("ObjectLocInfo", object_loc_info);
229 
230  if (st_info)
231  json.SetByKey("StorageSpecificInfo", st_info);
232 }
233 
236  const CNetStorageObjectLoc* object_loc_struct,
237  Uint8 file_size, CJsonNode::TInstance storage_specific_info)
238 {
239  return new SNetStorageObjectInfoImpl(SData(location, object_loc,
240  object_loc_struct ? object_loc_struct->ToJSON() : CJsonNode(),
241  file_size, storage_specific_info));
242 }
243 
245  const CJsonNode& object_info_node)
246 {
247  return new SNetStorageObjectInfoImpl(object_info_node);
248 }
249 
251 {
252  return m_Impl->GetLocation();
253 }
254 
256 {
257  return m_Impl->GetObjectLocInfo();
258 }
259 
261 {
262  return m_Impl->GetCreationTime();
263 }
264 
266 {
267  return m_Impl->GetSize();
268 }
269 
271 {
272  return m_Impl->GetStorageSpecificInfo();
273 }
274 
276 {
277  return m_Impl->GetJSON();
278 }
279 
JSON node abstraction.
void DeleteByKey(const string &key)
Delete an element referred to by the specified key from a JSON object.
void SetString(const string &key, const string &value)
Set a JSON object element to the specified string value.
const string AsString() const
Provided that this is a string node, return the string value of this node.
string GetString(const string &key) const
For a JSON object node, return the string referred to by the specified key.
void SetInteger(const string &key, Int8 value)
Set a JSON object element to the specified integer value.
void SetByKey(const string &key, CJsonNode::TInstance value)
For a JSON object node, insert a new element or update an existing element.
static CJsonNode NewStringNode(const string &value)
Create a new JSON string node.
static CJsonNode NewObjectNode()
Create a new JSON object node.
CJsonNode GetByKey(const string &key) const
For a JSON object node, return the value associated with the specified key.
CJsonNode GetByKeyOrNull(const string &key) const
For a JSON object node, return the value associated with the specified key.
Detailed information about a CNetStorage object.
Definition: netstorage.hpp:96
void ToJSON(CJsonNode &root) const
CObject –.
Definition: ncbiobj.hpp:180
CTimeException –.
Definition: ncbitime.hpp:2075
CTime –.
Definition: ncbitime.hpp:296
static const char si[8][64]
Definition: des.c:146
#define true
Definition: bool.h:35
#define false
Definition: bool.h:36
static const char location[]
Definition: config.c:97
char data[12]
Definition: iconv.c:80
TErrCode GetErrCode(void) const
Definition: ncbiexpt.hpp:1493
Uint8 GetSize() const
Return object size in bytes.
CJsonNode GetObjectLocInfo() const
Return a JSON object containing the fields of the object ID.
CNetRef< SNetStorageObjectInfoImpl > m_Impl
Definition: netstorage.hpp:97
ENetStorageObjectLocation
Enumeration that indicates the current location of the object.
Definition: netstorage.hpp:86
CJsonNode ToJSON()
Pack the whole structure in a single JSON object.
ENetStorageObjectLocation GetLocation() const
Return a ENetStorageObjectLocation constant that corresponds to the storage back-end where the object...
CJsonNode GetStorageSpecificInfo() const
Return a JSON object containing storage-specific information about the object.
CTime GetCreationTime() const
Return object creation time reported by the storage back-end.
@ eNFL_FileTrack
Definition: netstorage.hpp:90
@ eNFL_NetCache
Definition: netstorage.hpp:89
@ eNFL_NotFound
Definition: netstorage.hpp:88
@ eNFL_Unknown
Definition: netstorage.hpp:87
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
#define kEmptyStr
Definition: ncbistr.hpp:123
CTime & ToLocalTime(void)
Convert the time into local time.
Definition: ncbitime.hpp:2464
string AsString(const CTimeFormat &format=kEmptyStr, TSeconds out_tz=eCurrentTimeZone) const
Transform time to string.
Definition: ncbitime.cpp:1512
@ eFormat
Incorrect format.
Definition: ncbitime.hpp:2082
const struct ncbi::grid::netcache::search::fields::SIZE size
ENetStorageObjectLocation TLocation
CNetStorageObjectInfo g_CreateNetStorageObjectInfo(const string &object_loc, ENetStorageObjectLocation location, const CNetStorageObjectLoc *object_loc_struct, Uint8 file_size, CJsonNode::TInstance storage_specific_info)
static SLJIT_INLINE sljit_ins l(sljit_gpr r, sljit_s32 d, sljit_gpr x, sljit_gpr b)
CJsonNode st_info
CJsonNode object_loc_info
SData(TLocation l, const string &ol, CJsonNode::TInstance li, Uint8 fs, CJsonNode::TInstance si)
TLocation location
SLazyInitData(const SData &d)
CTime GetTime()
SLazyInitData(const CJsonNode &j)
SNetStorageObjectInfoImpl(const SLazyInitData &data)
CJsonNode GetStorageSpecificInfo() const
Modified on Fri Sep 20 14:58:09 2024 by modify_doxy.py rev. 669887