1 #ifndef NETSTORAGE_SERVICE_PARAMETERS__HPP
2 #define NETSTORAGE_SERVICE_PARAMETERS__HPP
3 
4 /*  $Id: nst_service_parameters.hpp 505775 2016-06-28 18:55:51Z satskyse $
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  * Authors:  Sergey Satskiy
30  *
31  * File Description:
32  *   NetStorage services and their properties
33  *
34  */
35 
36 #include <corelib/ncbimtx.hpp>
37 #include <corelib/ncbistr.hpp>
38 #include <connect/services/json_over_uttp.hpp>
39 
40 #include "nst_database.hpp"
41 
42 
43 BEGIN_NCBI_SCOPE
44 
45 
46 // The latest requirement is to make the service name case insensitive.
47 // So this constant can use any letters - the test statements are case
48 // insensitive throughout the code
49 const string    k_LBSMDNSTTestService = "LBSMDNSTTestService";
50 const double    k_ProlongInTTLsNotConfigured = -1.0;
51 
52 const CTimeSpan::TSmartStringFlags   kTimeSpanFlags =
53                                                 CTimeSpan::fSS_Nanosecond |
54                                                 CTimeSpan::fSS_SkipZero |
55                                                 CTimeSpan::fSS_Short;
56 
57 
58 // Note: access to the instances of this class is possible only via the service
59 // registry and the corresponding container access is always done under a lock.
60 // So it is safe to do any modifications in the members without any locks here.
61 class CNSTServiceProperties
62 {
63     public:
64         CNSTServiceProperties();
CNSTServiceProperties(const TNSTDBValue<CTimeSpan> & ttl,const CTimeSpan & prolong_on_read,const CTimeSpan & prolong_on_write,const CTimeSpan & prolong_on_relocate)65         CNSTServiceProperties(const TNSTDBValue<CTimeSpan> &  ttl,
66                               const CTimeSpan &  prolong_on_read,
67                               const CTimeSpan &  prolong_on_write,
68                               const CTimeSpan &  prolong_on_relocate) :
69             m_TTL(ttl), m_ProlongOnRead(prolong_on_read),
70             m_ProlongOnReadInTTLs(k_ProlongInTTLsNotConfigured),
71             m_ProlongOnWrite(prolong_on_write),
72             m_ProlongOnWriteInTTLs(k_ProlongInTTLsNotConfigured),
73             m_ProlongOnRelocate(prolong_on_relocate),
74             m_ProlongOnRelocateInTTLs(k_ProlongInTTLsNotConfigured)
75         {}
76 
GetTTL(void) const77         TNSTDBValue<CTimeSpan>  GetTTL(void) const
78         { return m_TTL; }
79         string  GetTTLAsString(void) const;
GetProlongOnReadAsString(void) const80         string  GetProlongOnReadAsString(void) const
81         { return x_GetProlongAsString(m_ProlongOnRead,
82                                       m_ProlongOnReadInTTLs); }
GetProlongOnWriteAsString(void) const83         string  GetProlongOnWriteAsString(void) const
84         { return x_GetProlongAsString(m_ProlongOnWrite,
85                                       m_ProlongOnWriteInTTLs); }
GetProlongOnRelocateAsString(void) const86         string  GetProlongOnRelocateAsString(void) const
87         { return x_GetProlongAsString(m_ProlongOnRelocate,
88                                       m_ProlongOnRelocateInTTLs); }
89 
90         TNSTDBValue<CTimeSpan>  GetProlongOnRead(
91                         const TNSTDBValue<Int8> &  individual_obj_ttl) const;
92         TNSTDBValue<CTimeSpan>  GetProlongOnWrite(
93                         const TNSTDBValue<Int8> &  individual_obj_ttl) const;
94         TNSTDBValue<CTimeSpan>  GetProlongOnRelocate(
95                         const TNSTDBValue<Int8> &  individual_obj_ttl) const;
96 
SetTTL(const TNSTDBValue<CTimeSpan> & new_val)97         void SetTTL(const TNSTDBValue<CTimeSpan> & new_val)
98         { m_TTL = new_val; }
SetProlongOnRead(const CTimeSpan & new_val)99         void SetProlongOnRead(const CTimeSpan &  new_val)
100         { m_ProlongOnRead = new_val;
101           m_ProlongOnReadInTTLs = k_ProlongInTTLsNotConfigured; }
SetProlongOnRead(double inTTLs)102         void SetProlongOnRead(double  inTTLs)
103         { m_ProlongOnRead = CTimeSpan(0L);
104           m_ProlongOnReadInTTLs = inTTLs; }
SetProlongOnWrite(const CTimeSpan & new_val)105         void SetProlongOnWrite(const CTimeSpan &  new_val)
106         { m_ProlongOnWrite = new_val;
107           m_ProlongOnWriteInTTLs = k_ProlongInTTLsNotConfigured; }
SetProlongOnWrite(double inTTLs)108         void SetProlongOnWrite(double  inTTLs)
109         { m_ProlongOnWrite = CTimeSpan(0L);
110           m_ProlongOnWriteInTTLs = inTTLs; }
SetProlongOnRelocate(const CTimeSpan & new_val)111         void SetProlongOnRelocate(const CTimeSpan &  new_val)
112         { m_ProlongOnRelocate = new_val;
113           m_ProlongOnRelocateInTTLs = k_ProlongInTTLsNotConfigured; }
SetProlongOnRelocate(double inTTLs)114         void SetProlongOnRelocate(double  inTTLs)
115         { m_ProlongOnRelocate = CTimeSpan(0L);
116           m_ProlongOnRelocateInTTLs = inTTLs; }
117 
118         CJsonNode  Serialize(void) const;
119 
operator ==(const CNSTServiceProperties & other) const120         bool operator==(const CNSTServiceProperties &  other) const
121         { return
122             (m_TTL == other.m_TTL) &&
123             (m_ProlongOnRead == other.m_ProlongOnRead) &&
124             (m_ProlongOnReadInTTLs == other.m_ProlongOnReadInTTLs) &&
125             (m_ProlongOnWrite == other.m_ProlongOnWrite) &&
126             (m_ProlongOnWriteInTTLs == other.m_ProlongOnWriteInTTLs) &&
127             (m_ProlongOnRelocate == other.m_ProlongOnRelocate) &&
128             (m_ProlongOnRelocateInTTLs == other.m_ProlongOnRelocateInTTLs); }
operator !=(const CNSTServiceProperties & other) const129         bool operator!=(const CNSTServiceProperties &  other) const
130         { return !this->operator==(other); }
131 
132     private:
133         TNSTDBValue<CTimeSpan>      m_TTL;
134         CTimeSpan                   m_ProlongOnRead;
135         double                      m_ProlongOnReadInTTLs;
136         CTimeSpan                   m_ProlongOnWrite;
137         double                      m_ProlongOnWriteInTTLs;
138         CTimeSpan                   m_ProlongOnRelocate;
139         double                      m_ProlongOnRelocateInTTLs;
140 
141     private:
142         string  x_GetProlongAsString(const CTimeSpan &  as_time_span,
143                                      double  in_ttls) const;
144         TNSTDBValue<CTimeSpan>
145         x_GetProlongInTTLs(double  multiplier,
146                            const TNSTDBValue<Int8> &  individual_obj_ttl) const;
147 };
148 
149 
150 // Specific case for a service info
151 enum EServiceMetadataPresence
152 {
153     eUnknownService,        // [service_XXX] section is not in the .ini file
154                             // at all.
155     eMetadataOn,            // [service_XXX] section is in the .ini file and
156                             // 'metadata' is set to true
157     eMetadataExplicitOff,   // [service_XXX] section is in the .ini file and
158                             // 'metadata' is set to false
159     eMetadataDefaultOff     // [service_XXX] section is in the .ini file and
160                             // 'metadata' is not found i.e. false by default
161 };
162 
163 
164 class CNSTServiceRegistry
165 {
166     public:
167         CNSTServiceRegistry();
168 
169         size_t  Size(void) const;
170         CJsonNode  ReadConfiguration(const IRegistry &  reg);
171         CJsonNode  Serialize(void) const;
172         EServiceMetadataPresence  IsKnown(const string &  service) const;
173         bool  GetTTL(const string &            service,
174                      TNSTDBValue<CTimeSpan> &  ttl) const;
175         bool  GetProlongOnRead(
176                    const string &  service,
177                    const TNSTDBValue<Int8> &  individual_obj_ttl,
178                    TNSTDBValue<CTimeSpan> &  prolong_on_read) const;
179         bool  GetProlongOnWrite(
180                    const string &  service,
181                    const TNSTDBValue<Int8> &  individual_obj_ttl,
182                    TNSTDBValue<CTimeSpan> &  prolong_on_write) const;
183         bool  GetProlongOnRelocate(
184                    const string &  service,
185                    const TNSTDBValue<Int8> &  individual_obj_ttl,
186                    TNSTDBValue<CTimeSpan> &  prolong_on_relocate) const;
187         bool  GetServiceProperties(const string &  service,
188                                    CNSTServiceProperties &  props) const;
189         CNSTServiceProperties  GetDefaultProperties(void) const;
190 
191     private:
192         CNSTServiceProperties
193             x_ReadServiceProperties(const IRegistry &  reg,
194                                     const string &  section,
195                                     const CNSTServiceProperties &  defaults);
196         CTimeSpan  x_ReadProlongProperty(const string &  value);
197         double  x_ReadProlongInTTLs(string &  value) const;
198         list<string>  x_GetMetadataServices(const IRegistry &  reg);
199 
200     private:
201         CNSTServiceProperties           m_LBSMDTestServiceProperties;
202         CNSTServiceProperties           m_DefaultProperties;
203 
204         // The latest requirement is to make the service case insensitive
205         typedef map< string,
206                      CNSTServiceProperties,
207                      PNocase >          TServiceProperties;
208         TServiceProperties              m_Services; // All the services
209                                                     // netstorage knows about
210 
211         // Auxiliary data to provide a better log messages
212         set<string, PNocase>            m_ServicesExplicitNoMetadata;
213         set<string, PNocase>            m_ServicesDefaultNoMetadata;
214 
215         mutable CMutex                  m_Lock;     // Lock for the map
216 };
217 
218 
219 END_NCBI_SCOPE
220 
221 #endif /* NETSTORAGE_SERVICE_PARAMETERS__HPP */
222 
223