23 #include <sys/types.h> 41 #ifdef DEBUG_MATRIX_FILE_READ_SET 42 #ifndef DEBUG_MATRIX_FILE 43 #define DEBUG_MATRIX_FILE 1 55 int cellSize,
int width,
int height) :
63 m_headerSize(2 * sizeof(int)),
66 m_readyToReadColumn(-1)
68 Profiler profiler(
"MatrixFile::MatrixFile",
true);
70 #ifdef DEBUG_MATRIX_FILE 71 SVDEBUG <<
"MatrixFile::MatrixFile(" << fileBase <<
", " << int(mode) <<
", " << cellSize <<
", " << width <<
", " << height <<
")" << endl;
77 QString fileName(tempDir.filePath(QString(
"%1.mfc").arg(fileBase)));
78 bool newFile = !QFileInfo(fileName).exists();
81 cerr <<
"ERROR: MatrixFile::MatrixFile: Read-only mode " 82 <<
"specified, but cache file does not exist" << endl;
87 cerr <<
"ERROR: MatrixFile::MatrixFile: Write-only mode " 88 <<
"specified, but file already exists" << endl;
105 #ifdef DEBUG_MATRIX_FILE 106 cerr <<
"MatrixFile(" <<
this <<
")::MatrixFile: opening " << fileName <<
"..." << endl;
110 ::perror(
"Open failed");
111 cerr <<
"ERROR: MatrixFile::MatrixFile: " 112 <<
"Failed to open cache file \"" 121 #ifdef DEBUG_MATRIX_FILE 122 cerr <<
"MatrixFile(" <<
this <<
")::MatrixFile: fd is " <<
m_fd << endl;
129 if (::read(
m_fd, header, 2 *
sizeof(
int)) < 0) {
130 ::perror(
"MatrixFile::MatrixFile: read failed");
131 cerr <<
"ERROR: MatrixFile::MatrixFile: " 132 <<
"Failed to read header (fd " <<
m_fd <<
", file \"" 133 << fileName <<
"\")" << endl;
137 cerr <<
"ERROR: MatrixFile::MatrixFile: " 138 <<
"Dimensions in file header (" << header[0] <<
"x" 139 << header[1] <<
") differ from expected dimensions " 148 #ifdef DEBUG_MATRIX_FILE 149 cerr <<
"MatrixFile[" <<
m_fd <<
"]::MatrixFile: File " << fileName <<
", ref " <<
m_refcount[fileName] << endl;
151 cerr <<
"MatrixFile[" <<
m_fd <<
"]::MatrixFile: Done, size is " <<
"(" <<
m_width <<
", " <<
m_height <<
")" << endl;
162 ::perror(
"MatrixFile::~MatrixFile: close failed");
176 cerr <<
"WARNING: MatrixFile::~MatrixFile: reference count reached 0, but failed to unlink file \"" <<
m_fileName <<
"\"" << endl;
188 #ifdef DEBUG_MATRIX_FILE 189 cerr <<
"MatrixFile[" <<
m_fd <<
"]::~MatrixFile: " << endl;
197 Profiler profiler(
"MatrixFile::initialise",
true);
205 #ifdef DEBUG_MATRIX_FILE 209 if (::lseek(
m_fd, off - 1, SEEK_SET) < 0) {
210 ::perror(
"ERROR: MatrixFile::initialise: seek to end failed");
214 unsigned char byte = 0;
215 if (::write(
m_fd, &
byte, 1) != 1) {
216 ::perror(
"ERROR: MatrixFile::initialise: write at end failed");
220 if (::lseek(
m_fd, 0, SEEK_SET) < 0) {
221 ::perror(
"ERROR: MatrixFile::initialise: Seek to write header failed");
228 if (::write(
m_fd, header, 2 *
sizeof(
int)) != 2 *
sizeof(
int)) {
229 ::perror(
"ERROR: MatrixFile::initialise: Failed to write header");
237 #ifdef DEBUG_MATRIX_FILE 238 cerr <<
"MatrixFile[" <<
m_fd <<
"]::initialise(" <<
m_width <<
", " <<
m_height <<
"): storage " 241 cerr <<
"MatrixFile: Total storage " <<
totalStorage/1024 <<
"K" << endl;
250 #ifdef DEBUG_MATRIX_FILE 251 SVDEBUG <<
"MatrixFile::close()" << endl;
255 ::perror(
"MatrixFile::close: close failed");
259 #ifdef DEBUG_MATRIX_FILE 260 cerr <<
"MatrixFile: Now " <<
openCount <<
" open instances" << endl;
270 #ifdef DEBUG_MATRIX_FILE_READ_SET 271 cerr <<
"MatrixFile[" <<
m_fd <<
"]::getColumnAt(" << x <<
")" << endl;
274 Profiler profiler(
"MatrixFile::getColumnAt");
281 unsigned char set = 0;
283 cerr <<
"ERROR: MatrixFile::getColumnAt(" << x <<
"): Seek failed" << endl;
287 r = ::read(
m_fd, &set, 1);
289 ::perror(
"MatrixFile::getColumnAt: read failed");
293 cerr <<
"MatrixFile[" <<
m_fd <<
"]::getColumnAt(" << x <<
"): Column has not been set" << endl;
300 ::perror(
"MatrixFile::getColumnAt: read failed");
315 Profiler profiler(
"MatrixFile::haveSetColumnAt");
317 #ifdef DEBUG_MATRIX_FILE_READ_SET 318 cerr <<
"MatrixFile[" <<
m_fd <<
"]::haveSetColumnAt(" << x <<
")" << endl;
322 unsigned char set = 0;
324 cerr <<
"ERROR: MatrixFile::haveSetColumnAt(" << x <<
"): Seek failed" << endl;
329 r = ::read(
m_fd, &set, 1);
331 ::perror(
"MatrixFile::haveSetColumnAt: read failed");
344 if (
m_fd < 0)
return;
346 #ifdef DEBUG_MATRIX_FILE_READ_SET 347 cerr <<
"MatrixFile[" <<
m_fd <<
"]::setColumnAt(" << x <<
")" << endl;
354 cerr <<
"ERROR: MatrixFile::setColumnAt(" << x <<
"): Seek failed" << endl;
358 unsigned char set = 0;
359 w = ::write(
m_fd, &set, 1);
361 ::perror(
"WARNING: MatrixFile::setColumnAt: write failed (1)");
367 ::perror(
"WARNING: MatrixFile::setColumnAt: write failed (2)");
380 cerr <<
"MatrixFile[" <<
m_fd <<
"]::setColumnAt(" << x <<
"): Seek failed" << endl;
385 w = ::write(
m_fd, &set, 1);
387 ::perror(
"WARNING: MatrixFile::setColumnAt: write failed (3)");
394 #ifdef DEBUG_MATRIX_FILE 395 cerr <<
"MatrixFile[" <<
m_fd <<
"]::setColumnAt(" << x <<
"): All columns set: auto-closing" << endl;
414 cerr <<
"ERROR: MatrixFile::seekTo: File not open" << endl;
422 #ifdef DEBUG_MATRIX_FILE_READ_SET 424 cerr <<
"MatrixFile[" <<
m_fd <<
"]::seekTo(" << x <<
"): off = " << off << endl;
428 #ifdef DEBUG_MATRIX_FILE_READ_SET 429 cerr <<
"MatrixFile[" <<
m_fd <<
"]::seekTo(" << x <<
"): off = " << off << endl;
432 if (::lseek(
m_fd, off, SEEK_SET) == (off_t)-1) {
433 ::perror(
"Seek failed");
434 cerr <<
"ERROR: MatrixFile::seekTo(" << x
435 <<
") = " << off <<
" failed" << endl;
void setColumnAt(int x, const void *data)
ResizeableBitset * m_setColumns
bool haveSetColumnAt(int x) const
static std::map< QString, int > m_refcount
static TempDirectory * getInstance()
bool seekTo(int col) const
bool get(size_t column) const
MatrixFile(QString fileBase, Mode mode, int cellSize, int width, int height)
Construct a MatrixFile object reading from and/or writing to the matrix file with the given base name...
static QMutex m_createMutex
void getColumnAt(int x, void *data)
static size_t totalStorage
Profile point instance class.