diff 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
line wrap: on
line diff
--- a/data/fileio/MatrixFile.cpp	Thu Feb 05 12:53:19 2009 +0000
+++ b/data/fileio/MatrixFile.cpp	Thu Feb 05 14:17:11 2009 +0000
@@ -60,7 +60,9 @@
     m_cellSize(cellSize),
     m_width(width),
     m_height(height),
-    m_headerSize(2 * sizeof(size_t))
+    m_headerSize(2 * sizeof(size_t)),
+    m_setColumns(0),
+    m_autoClose(false)
 {
     Profiler profiler("MatrixFile::MatrixFile", true);
 
@@ -163,6 +165,8 @@
 
     QMutexLocker locker(&m_createMutex);
 
+    delete m_setColumns;
+
     if (m_fileName != "") {
 
         if (--m_refcount[m_fileName] == 0) {
@@ -192,6 +196,8 @@
     Profiler profiler("MatrixFile::initialise", true);
 
     assert(m_mode == WriteOnly);
+
+    m_setColumns = new ResizeableBitset(m_width);
     
     off_t off = m_headerSize + (m_width * m_height * m_cellSize) + m_width;
 
@@ -249,6 +255,9 @@
         }
         m_fd = -1;
         -- openCount;
+#ifdef DEBUG_MATRIX_FILE
+        std::cerr << "MatrixFile: Now " << openCount << " open instances" << std::endl;
+#endif
     }
 }
 
@@ -290,7 +299,9 @@
 bool
 MatrixFile::haveSetColumnAt(size_t x) const
 {
-    assert(m_mode == ReadOnly);
+    if (m_mode == WriteOnly) {
+        return m_setColumns->get(x);
+    }
 
     Profiler profiler("MatrixFile::haveSetColumnAt");
 
@@ -319,6 +330,7 @@
 MatrixFile::setColumnAt(size_t x, const void *data)
 {
     assert(m_mode == WriteOnly);
+    if (m_fd < 0) return; // closed
 
 #ifdef DEBUG_MATRIX_FILE_READ_SET
     std::cerr << "MatrixFile[" << m_fd << "]::setColumnAt(" << x << ")" << std::endl;
@@ -364,6 +376,22 @@
         ::perror("WARNING: MatrixFile::setColumnAt: write failed (3)");
         throw FileOperationFailed(m_fileName, "write");
     }
+
+    m_setColumns->set(x);
+    if (m_autoClose) {
+        if (m_setColumns->isAllOn()) {
+            std::cerr << "MatrixFile[" << m_fd << "]::setColumnAt(" << x << "): All columns set: auto-closing" << std::endl;
+            close();
+/*
+        } else {
+            int set = 0;
+            for (int i = 0; i < m_width; ++i) {
+                if (m_setColumns->get(i)) ++set;
+            }
+            std::cerr << "MatrixFile[" << m_fd << "]::setColumnAt(" << x << "): Auto-close on, but not all columns set yet (" << set << " of " << m_width << ")" << std::endl;
+*/
+        }
+    }
 }
 
 bool