comparison data/fileio/MatrixFile.cpp @ 550:107d3f3705c9

* Auto-close write-mode matrix files when all columns written
author Chris Cannam
date Thu, 05 Feb 2009 14:17:11 +0000
parents 1469caaa8e67
children 60482f13e627
comparison
equal deleted inserted replaced
549:388afa99d537 550:107d3f3705c9
58 m_flags(0), 58 m_flags(0),
59 m_fmode(0), 59 m_fmode(0),
60 m_cellSize(cellSize), 60 m_cellSize(cellSize),
61 m_width(width), 61 m_width(width),
62 m_height(height), 62 m_height(height),
63 m_headerSize(2 * sizeof(size_t)) 63 m_headerSize(2 * sizeof(size_t)),
64 m_setColumns(0),
65 m_autoClose(false)
64 { 66 {
65 Profiler profiler("MatrixFile::MatrixFile", true); 67 Profiler profiler("MatrixFile::MatrixFile", true);
66 68
67 #ifdef DEBUG_MATRIX_FILE 69 #ifdef DEBUG_MATRIX_FILE
68 std::cerr << "MatrixFile::MatrixFile(" << fileBase.toStdString() << ", " << int(mode) << ", " << cellSize << ", " << width << ", " << height << ")" << std::endl; 70 std::cerr << "MatrixFile::MatrixFile(" << fileBase.toStdString() << ", " << int(mode) << ", " << cellSize << ", " << width << ", " << height << ")" << std::endl;
161 openCount --; 163 openCount --;
162 } 164 }
163 165
164 QMutexLocker locker(&m_createMutex); 166 QMutexLocker locker(&m_createMutex);
165 167
168 delete m_setColumns;
169
166 if (m_fileName != "") { 170 if (m_fileName != "") {
167 171
168 if (--m_refcount[m_fileName] == 0) { 172 if (--m_refcount[m_fileName] == 0) {
169 173
170 if (::unlink(m_fileName.toLocal8Bit())) { 174 if (::unlink(m_fileName.toLocal8Bit())) {
190 MatrixFile::initialise() 194 MatrixFile::initialise()
191 { 195 {
192 Profiler profiler("MatrixFile::initialise", true); 196 Profiler profiler("MatrixFile::initialise", true);
193 197
194 assert(m_mode == WriteOnly); 198 assert(m_mode == WriteOnly);
199
200 m_setColumns = new ResizeableBitset(m_width);
195 201
196 off_t off = m_headerSize + (m_width * m_height * m_cellSize) + m_width; 202 off_t off = m_headerSize + (m_width * m_height * m_cellSize) + m_width;
197 203
198 #ifdef DEBUG_MATRIX_FILE 204 #ifdef DEBUG_MATRIX_FILE
199 std::cerr << "MatrixFile[" << m_fd << "]::initialise(" << m_width << ", " << m_height << "): cell size " << m_cellSize << ", header size " << m_headerSize << ", resizing file" << std::endl; 205 std::cerr << "MatrixFile[" << m_fd << "]::initialise(" << m_width << ", " << m_height << "): cell size " << m_cellSize << ", header size " << m_headerSize << ", resizing file" << std::endl;
247 if (::close(m_fd) < 0) { 253 if (::close(m_fd) < 0) {
248 ::perror("MatrixFile::close: close failed"); 254 ::perror("MatrixFile::close: close failed");
249 } 255 }
250 m_fd = -1; 256 m_fd = -1;
251 -- openCount; 257 -- openCount;
258 #ifdef DEBUG_MATRIX_FILE
259 std::cerr << "MatrixFile: Now " << openCount << " open instances" << std::endl;
260 #endif
252 } 261 }
253 } 262 }
254 263
255 void 264 void
256 MatrixFile::getColumnAt(size_t x, void *data) 265 MatrixFile::getColumnAt(size_t x, void *data)
288 } 297 }
289 298
290 bool 299 bool
291 MatrixFile::haveSetColumnAt(size_t x) const 300 MatrixFile::haveSetColumnAt(size_t x) const
292 { 301 {
293 assert(m_mode == ReadOnly); 302 if (m_mode == WriteOnly) {
303 return m_setColumns->get(x);
304 }
294 305
295 Profiler profiler("MatrixFile::haveSetColumnAt"); 306 Profiler profiler("MatrixFile::haveSetColumnAt");
296 307
297 #ifdef DEBUG_MATRIX_FILE_READ_SET 308 #ifdef DEBUG_MATRIX_FILE_READ_SET
298 std::cerr << "MatrixFile[" << m_fd << "]::haveSetColumnAt(" << x << ")" << std::endl; 309 std::cerr << "MatrixFile[" << m_fd << "]::haveSetColumnAt(" << x << ")" << std::endl;
317 328
318 void 329 void
319 MatrixFile::setColumnAt(size_t x, const void *data) 330 MatrixFile::setColumnAt(size_t x, const void *data)
320 { 331 {
321 assert(m_mode == WriteOnly); 332 assert(m_mode == WriteOnly);
333 if (m_fd < 0) return; // closed
322 334
323 #ifdef DEBUG_MATRIX_FILE_READ_SET 335 #ifdef DEBUG_MATRIX_FILE_READ_SET
324 std::cerr << "MatrixFile[" << m_fd << "]::setColumnAt(" << x << ")" << std::endl; 336 std::cerr << "MatrixFile[" << m_fd << "]::setColumnAt(" << x << ")" << std::endl;
325 // std::cerr << "."; 337 // std::cerr << ".";
326 #endif 338 #endif
362 w = ::write(m_fd, &set, 1); 374 w = ::write(m_fd, &set, 1);
363 if (w != 1) { 375 if (w != 1) {
364 ::perror("WARNING: MatrixFile::setColumnAt: write failed (3)"); 376 ::perror("WARNING: MatrixFile::setColumnAt: write failed (3)");
365 throw FileOperationFailed(m_fileName, "write"); 377 throw FileOperationFailed(m_fileName, "write");
366 } 378 }
379
380 m_setColumns->set(x);
381 if (m_autoClose) {
382 if (m_setColumns->isAllOn()) {
383 std::cerr << "MatrixFile[" << m_fd << "]::setColumnAt(" << x << "): All columns set: auto-closing" << std::endl;
384 close();
385 /*
386 } else {
387 int set = 0;
388 for (int i = 0; i < m_width; ++i) {
389 if (m_setColumns->get(i)) ++set;
390 }
391 std::cerr << "MatrixFile[" << m_fd << "]::setColumnAt(" << x << "): Auto-close on, but not all columns set yet (" << set << " of " << m_width << ")" << std::endl;
392 */
393 }
394 }
367 } 395 }
368 396
369 bool 397 bool
370 MatrixFile::seekTo(size_t x, size_t y) const 398 MatrixFile::seekTo(size_t x, size_t y) const
371 { 399 {