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

Go to the SVN repository for this file.

1 /* $Id: ns_start_ids.cpp 90070 2020-05-07 19:04:09Z satskyse $
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: Sergey Satskiy
27  *
28  * File Description:
29  * NetSchedule queue job start identifiers
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
34 #include <corelib/ncbistd.hpp>
35 #include <corelib/ncbifile.hpp>
36 
37 #include "ns_start_ids.hpp"
38 #include "ns_types.hpp"
39 
40 
42 
43 
44 
45 CNSStartIDs::CNSStartIDs(const string & data_dir_name,
46  bool diskless) :
47  m_Diskless(diskless)
48 {
50 }
51 
52 
54 {}
55 
56 
57 void CNSStartIDs::Set(const string & qname, unsigned int value)
58 {
59  if (!m_Diskless) {
60  TStartIDs::iterator k;
61  CFastMutexGuard guard(m_Lock);
62 
63  k = m_IDs.find(qname);
64  if (k != m_IDs.end()) {
65  if (k->second != value) {
66  k->second = value;
68  }
69  } else {
70  // Create a new entry and serialize the file
71  m_IDs[ qname ] = value;
73  }
74  }
75 }
76 
77 
78 unsigned int CNSStartIDs::Get(const string & qname)
79 {
80  if (!m_Diskless) {
81  TStartIDs::const_iterator k;
82  CFastMutexGuard guard(m_Lock);
83 
84  k = m_IDs.find(qname);
85  if (k != m_IDs.end())
86  return k->second;
87 
88  // Create a new entry and serialize the file
89  m_IDs[ qname ] = 1;
91  }
92  return 1;
93 }
94 
95 
96 void CNSStartIDs::Serialize(void) const
97 {
98  if (!m_Diskless) {
99  CFastMutexGuard guard(m_Lock);
101  }
102 }
103 
104 
106 {
107  if (m_Diskless)
108  return;
109 
110  // Loading is done at the startup time when there is no threaded access
111  FILE * f = fopen(m_FileName.c_str(), "r");
112  if (f == NULL)
113  return;
114 
115  char * raw_line = NULL;
116  size_t raw_length = 0;
117  ssize_t read_size = 0;
118 
119  while ((read_size = getline(&raw_line, &raw_length, f)) != -1) {
120  if (read_size == 0)
121  continue;
122 
123  if (raw_line[read_size - 1] == '\n')
124  --read_size;
125 
126  string s(raw_line, read_size);
127  s = NStr::TruncateSpaces(s);
128  if (s.length() == 0)
129  continue;
130  if (s[0] == ';' || s[0] == '#')
131  continue;
132 
133  // This is a meaningfull line
134  list<string> tokens;
135  NStr::Split(s, "=", tokens);
136  if (tokens.size() != 2) {
137  ERR_POST("Poor format of the " + kStartJobIDsFileName +
138  " file. Line: " + s + ". Ignore and continue.");
139  continue;
140  }
141  list<string>::const_iterator item = tokens.begin();
142  string qname = *item;
143  ++item;
144  m_IDs[qname] = NStr::StringToUInt(*item);
145  }
146 
147  if (raw_line != NULL)
148  free(raw_line);
149  fclose(f);
150 }
151 
152 
154 {
155  if (!m_Diskless) {
156  TStartIDs::const_iterator k;
157  FILE * f = fopen(m_FileName.c_str(), "w");
158 
159  if (f != NULL) {
160  for (k = m_IDs.begin(); k != m_IDs.end(); ++k)
161  fprintf(f, "%s=%u\n", k->first.c_str(), k->second);
162  fclose(f);
163  } else
164  ERR_POST("Cannot serialize start queue job ids into file " +
165  m_FileName);
166  }
167 }
168 
170 
unsigned int Get(const string &qname)
void Set(const string &qname, unsigned int value)
TStartIDs m_IDs
void Load(void)
CFastMutex m_Lock
string m_FileName
void Serialize(void) const
void x_SerializeNoLock(void) const
CNSStartIDs(const string &data_dir_name, bool diskless)
Include a standard set of the NCBI C++ Toolkit most basic headers.
#define NULL
Definition: ncbistd.hpp:225
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
static string MakePath(const string &dir=kEmptyStr, const string &base=kEmptyStr, const string &ext=kEmptyStr)
Assemble a path from basic components.
Definition: ncbifile.cpp:413
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
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:3461
static unsigned int StringToUInt(const CTempString str, TStringToNumFlags flags=0, int base=10)
Convert string to unsigned int.
Definition: ncbistr.cpp:642
static string TruncateSpaces(const string &str, ETrunc where=eTrunc_Both)
Truncate spaces in a string.
Definition: ncbistr.cpp:3186
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
int ssize_t
Definition: ncbiconf_msvc.h:93
Defines classes: CDirEntry, CFile, CDir, CSymLink, CMemoryFile, CFileUtil, CFileLock,...
double f(double x_, const double &y_)
Definition: njn_root.hpp:188
const string kStartJobIDsFileName("STARTJOBIDS")
static size_t read_size(CNcbiIstream &stream, const char *name)
Definition: reader_snp.cpp:404
void free(voidpf ptr)
Modified on Wed May 15 15:09:47 2024 by modify_doxy.py rev. 669887