Belle II Software development
FileHandler Class Reference

Public Member Functions

 FileHandler (DBInterface *db, const std::string &runtype, const char *host, const char *dbtmp)
 
int getDiskId ()
 
int getFileId ()
 
int open (const std::string &dir, int ndisks, int expno, int runno, int fileid)
 
void close ()
 
int write (char *evtbuf, int nbyte, bool isstreamer=false)
 
 operator bool ()
 
 FileHandler (DBInterface *db, const std::string &runtype, const char *host, const char *dbtmp)
 
int getDiskId ()
 
int getFileId ()
 
int open (const std::string &dir, int ndisks, int, int, int fileid)
 
void close ()
 
int write (char *evtbuf, int nbyte, bool isstreamer=false)
 
 operator bool ()
 

Private Attributes

DBInterfacem_db
 
std::string m_runtype
 
std::string m_host
 
std::string m_dbtmp
 
int m_id
 
std::string m_filename
 
std::string m_path
 
int m_diskid
 
int m_fileid
 
int m_file
 
std::string m_configname
 
int m_expno
 
int m_runno
 
unsigned long long m_filesize
 
unsigned long long m_chksum
 
unsigned long long m_nevents
 

Detailed Description

Definition at line 49 of file storagerecord.cc.

Constructor & Destructor Documentation

◆ FileHandler() [1/2]

FileHandler ( DBInterface db,
const std::string &  runtype,
const char *  host,
const char *  dbtmp 
)
inline

Definition at line 52 of file storagerecord.cc.

54 : m_db(db), m_runtype(runtype), m_host(host), m_dbtmp(dbtmp)
55 {
56 m_file = -1;
57 m_id = -1;
58 m_filesize = 0;
59 m_fileid = 0;
60 m_diskid = 1;
61 m_expno = -1;
62 m_runno = -1;
63 m_nevents = 0;
64 m_chksum = 0;
65 }

◆ ~FileHandler() [1/2]

~FileHandler ( )
throw (
)
inline

Definition at line 66 of file storagerecord.cc.

67 {
68 }

◆ FileHandler() [2/2]

FileHandler ( DBInterface db,
const std::string &  runtype,
const char *  host,
const char *  dbtmp 
)
inline

Definition at line 47 of file storagerecord_arich.cc.

49 : m_db(db), m_runtype(runtype), m_host(host), m_dbtmp(dbtmp)
50 {
51 m_file = -1;
52 m_filesize = 0;
53 m_fileid = 0;
54 m_diskid = 1;
55 m_id = -1;
56 m_expno = -1;
57 m_runno = -1;
58 m_nevents = 0;
59 m_chksum = 0;
60 }

◆ ~FileHandler() [2/2]

~FileHandler ( )
throw (
)
inline

Definition at line 61 of file storagerecord_arich.cc.

62 {
63 }

Member Function Documentation

◆ close() [1/2]

void close ( )
inline

Definition at line 169 of file storagerecord.cc.

170 {
171 if (m_file > 0) {
172 std::cout << "[DEBUG] File closed" << std::endl;
173 ::close(m_file);
174 try {
175 struct stat st;
176 stat(m_path.c_str(), &st);
177 std::string d = Date(st.st_mtime).toString();
178 m_db->connect();
179 m_db->execute("update %s set time_close = '%s', chksum = %lu, nevents = %lu, "
180 "size = %lu where name = '%s' and host = '%s';",
181 g_table, d.c_str(), m_chksum, m_nevents, m_filesize,
182 m_filename.c_str(), m_host.c_str());
183 } catch (const DBHandlerException& e) {
184 B2WARNING(e.what());
185 }
186 m_db->close();
187 }
188 }

◆ close() [2/2]

void close ( )
inline

Definition at line 145 of file storagerecord_arich.cc.

146 {
147 if (m_file > 0) {
148 std::cout << "[DEBUG] File closed" << std::endl;
149 ::close(m_file);
150 try {
151 struct stat st;
152 stat(m_path.c_str(), &st);
153 std::string d = Date(st.st_mtime).toString();
154 //if (m_fileid == 0) m_nevents--;//1 entry for StreamerInfo
155 m_db->connect();
156 m_db->execute("update %s set time_close = '%s', chksum = %lu, nevents = %lu, "
157 "size = %lu where name = '%s' and host = '%s';",
158 g_table, d.c_str(), m_chksum, m_nevents, m_filesize,
159 m_filename.c_str(), m_host.c_str());
160 } catch (const DBHandlerException& e) {
161 B2WARNING(e.what());
162 }
163 m_db->close();
164 }
165 }

◆ getDiskId() [1/2]

int getDiskId ( )
inline

Definition at line 71 of file storagerecord.cc.

71{ return m_diskid; }

◆ getDiskId() [2/2]

int getDiskId ( )
inline

Definition at line 66 of file storagerecord_arich.cc.

66{ return m_diskid; }

◆ getFileId() [1/2]

int getFileId ( )
inline

Definition at line 72 of file storagerecord.cc.

72{ return m_fileid; }

◆ getFileId() [2/2]

int getFileId ( )
inline

Definition at line 67 of file storagerecord_arich.cc.

67{ return m_fileid; }

◆ open() [1/2]

int open ( const std::string &  dir,
int  ndisks,
int  expno,
int  runno,
int  fileid 
)
inline

Definition at line 75 of file storagerecord.cc.

76 {
77 m_filesize = 0;
78 m_fileid = 0;
79 m_diskid = 1;
80 m_chksum = 1;
81 m_nevents = 0;
82 m_filesize = 0;
83 m_expno = (g_expno > 0) ? g_expno : expno;
84 m_runno = (g_runno > 0) ? g_runno : runno;
85 m_fileid = fileid;
86 bool available = false;
87 char filename[1024];
88 struct statvfs statfs;
89 if (g_diskid > 0) {
90 m_diskid = g_diskid;
91 available = true;
92 } else {
93 {
94 int diskid = 1;
95 std::ifstream fin(g_file_diskid.c_str());
96 fin >> diskid;
97 m_diskid = diskid;
98 }
99 for (int i = 0; i < ndisks; i++) {
100 sprintf(filename, "%s%02d", dir.c_str(), m_diskid);
101 statvfs(filename, &statfs);
102 float usage = 1 - ((float)statfs.f_bfree / statfs.f_blocks);
103 sprintf(filename, "%s%02d/storage/full_flag", dir.c_str(), m_diskid);
104 std::ifstream fin(filename);
105 int flag = 0;
106 fin >> flag;
107 fin.close();
108 /*
109 if (usage < 0.1) {
110 fin.close();
111 //::unlink(filename);
112 if (flag == 1) {
113 std::cout << "[DEBUG] disk : " << m_diskid << " is available again (full_flag removed)" << std::endl;
114 }
115 available = true;
116 std::cout << "[DEBUG] disk : " << m_diskid << " is available" << std::endl;
117 break;
118 } else
119 */
120 if (usage < 0.7) {
121 if (flag != 1) {
122 available = true;
123 std::cout << "[DEBUG] disk : " << m_diskid << " is available" << std::endl;
124 break;
125 }
126 std::cout << "[DEBUG] disk : " << m_diskid << " is with full_flag" << std::endl;
127 } else {
128 std::ofstream fout(filename);
129 fout << 1;
130 fout.close();
131 B2WARNING("disk-" << m_diskid << " is full " << usage);
132 }
133 m_diskid++;
134 if (m_diskid > ndisks) m_diskid = 1;
135 }
136 }
137 if (!available) {
138 B2FATAL("No disk available for writing " << __FILE__ << ":" << __LINE__);
139 exit(1);
140 }
141 std::string filedir = dir + StringUtil::form("%02d/storage/%4.4d/%5.5d/", m_diskid, expno, runno);
142 system(("mkdir -p " + filedir).c_str());
143 m_filename = StringUtil::form("%s.%4.4d.%5.5d.%s.f%5.5d.sroot",
144 m_runtype.c_str(), expno, runno, m_host.c_str(), m_fileid);
145 m_path = filedir + m_filename;
146 m_file = ::open(m_path.c_str(), O_WRONLY | O_CREAT | O_EXCL, 0664);
147 g_diskid = m_diskid;
148 std::ofstream fout(g_file_diskid.c_str());
149 fout << g_diskid;
150 if (m_file < 0) {
151 B2FATAL("Failed to open file : " << m_path);
152 exit(1);
153 }
154 try {
155 m_db->connect();
156 m_db->execute("insert into %s (name, path, host, label, expno, runno, fileno, nevents, chksum, size) "
157 "values ('%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0);",
158 g_table, m_filename.c_str(), m_path.c_str(), m_host.c_str(),
159 m_runtype.c_str(), m_expno, m_runno, m_fileid);
160 } catch (const DBHandlerException& e) {
161 B2WARNING(e.what());
162 }
163 write(g_streamerinfo, g_streamersize, true);
164 B2INFO("New file " << m_path << " is opened");
165
166 return m_id;
167 }

◆ open() [2/2]

int open ( const std::string &  dir,
int  ndisks,
int  ,
int  ,
int  fileid 
)
inline

Definition at line 70 of file storagerecord_arich.cc.

71 {
72 m_filesize = 0;
73 m_fileid = 0;
74 m_diskid = 1;
75 m_chksum = 1;
76 m_nevents = 0;
77 m_filesize = 0;
78 m_expno = g_expno;
79 m_runno = g_runno;
80 m_fileid = fileid;
81 bool available = false;
82 for (int i = 0; i < ndisks; i++) {
83 struct statvfs statfs;
84 char filename[1024];
85 sprintf(filename, "%s%02d", dir.c_str(), m_diskid);
86 statvfs(filename, &statfs);
87 float usage = 1 - ((float)statfs.f_bfree / statfs.f_blocks);
88 if (usage < 0.7) {
89 sprintf(filename, "%s%02d/storage/full_flag", dir.c_str(), m_diskid);
90 std::ifstream fin(filename);
91 int flag = 0;
92 fin >> flag;
93 if (flag != 1) {
94 available = true;
95 std::cout << "[DEBUG] disk : " << m_diskid << " is available" << std::endl;
96 break;
97 }
98 fin.close();
99 std::cout << "[DEBUG] disk : " << m_diskid << " is still full" << std::endl;
100 } else {
101 sprintf(filename, "%s%02d/storage/full_flag", dir.c_str(), m_diskid);
102 std::ofstream fout(filename);
103 fout << 1;
104 fout.close();
105 B2WARNING("disk-" << m_diskid << " is full " << usage);
106 }
107 m_diskid++;
108 if (m_diskid > ndisks) m_diskid = 1;
109 }
110 if (!available) {
111 B2FATAL("No disk available for writing");
112 exit(1);
113 }
114 std::string filedir = dir + StringUtil::form("%02d/storage/%4.4d/%5.5d/",
115 m_diskid, m_expno, m_runno);
116 system(("mkdir -p " + filedir).c_str());
117 m_filename = StringUtil::form("%s.%4.4d.%5.5d.%s.f%5.5d.sroot",
118 m_runtype.c_str(), m_expno, m_runno, m_host.c_str(), m_fileid);
119 m_path = filedir + m_filename;
120 m_file = ::open(m_path.c_str(), O_WRONLY | O_CREAT | O_EXCL, 0664);
121 if (g_diskid > 0 && g_diskid != m_diskid) {
122 B2FATAL("disk-" << m_diskid << " is already full! Terminating process..");
123 exit(1);
124 }
125 g_diskid = m_diskid;
126 if (m_file < 0) {
127 B2FATAL("Failed to open file : " << m_path);
128 exit(1);
129 }
130 try {
131 m_db->connect();
132 m_db->execute("insert into %s (name, path, host, label, expno, runno, fileno, nevents, chksum, size) "
133 "values ('%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0);",
134 g_table, m_filename.c_str(), m_path.c_str(), m_host.c_str(),
135 m_runtype.c_str(), m_expno, m_runno, m_fileid);
136 } catch (const DBHandlerException& e) {
137 B2WARNING(e.what());
138 }
139 write(g_streamerinfo, g_streamersize, true);
140 B2INFO("New file " << m_path << " is opened");
141
142 return m_id;
143 }

◆ operator bool() [1/2]

operator bool ( )
inline

Definition at line 201 of file storagerecord.cc.

202 {
203 return m_file > 0;
204 }

◆ operator bool() [2/2]

operator bool ( )
inline

Definition at line 178 of file storagerecord_arich.cc.

179 {
180 return m_file > 0;
181 }

◆ write() [1/2]

int write ( char *  evtbuf,
int  nbyte,
bool  isstreamer = false 
)
inline

Definition at line 190 of file storagerecord.cc.

191 {
192 m_chksum = adler32(m_chksum, (unsigned char*)evtbuf, nbyte);
193 int ret = ::write(m_file, evtbuf, nbyte);
194 m_filesize += nbyte;
195 if (!isstreamer) {
196 m_nevents++;
197 }
198 return ret;
199 }

◆ write() [2/2]

int write ( char *  evtbuf,
int  nbyte,
bool  isstreamer = false 
)
inline

Definition at line 167 of file storagerecord_arich.cc.

168 {
169 int ret = ::write(m_file, evtbuf, nbyte);
170 m_filesize += nbyte;
171 if (!isstreamer) {
172 m_nevents++;
173 }
174 m_chksum = adler32(m_chksum, (unsigned char*)evtbuf, nbyte);
175 return ret;
176 }

Member Data Documentation

◆ m_chksum

unsigned long long m_chksum
private

Definition at line 221 of file storagerecord.cc.

◆ m_configname

std::string m_configname
private

Definition at line 217 of file storagerecord.cc.

◆ m_db

DBInterface * m_db
private

Definition at line 207 of file storagerecord.cc.

◆ m_dbtmp

std::string m_dbtmp
private

Definition at line 210 of file storagerecord.cc.

◆ m_diskid

int m_diskid
private

Definition at line 214 of file storagerecord.cc.

◆ m_expno

int m_expno
private

Definition at line 218 of file storagerecord.cc.

◆ m_file

int m_file
private

Definition at line 216 of file storagerecord.cc.

◆ m_fileid

int m_fileid
private

Definition at line 215 of file storagerecord.cc.

◆ m_filename

std::string m_filename
private

Definition at line 212 of file storagerecord.cc.

◆ m_filesize

unsigned long long m_filesize
private

Definition at line 220 of file storagerecord.cc.

◆ m_host

std::string m_host
private

Definition at line 209 of file storagerecord.cc.

◆ m_id

int m_id
private

Definition at line 211 of file storagerecord.cc.

◆ m_nevents

unsigned long long m_nevents
private

Definition at line 222 of file storagerecord.cc.

◆ m_path

std::string m_path
private

Definition at line 213 of file storagerecord.cc.

◆ m_runno

int m_runno
private

Definition at line 219 of file storagerecord.cc.

◆ m_runtype

std::string m_runtype
private

Definition at line 208 of file storagerecord.cc.


The documentation for this class was generated from the following files: