annotate data/fileio/MatrixFile.h @ 316:3a6725f285d6

* Make RemoteFile far more pervasive, and use it for local files as well so that we can handle both transparently. Make it shallow copy with reference counting, so it can be used by value without having to worry about the cache file lifetime. Use RemoteFile for MainWindow file-open functions, etc
author Chris Cannam
date Thu, 18 Oct 2007 15:31:20 +0000
parents 1a42221a1522
children 3e0f1f7bec85
rev   line source
Chris@148 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@148 2
Chris@148 3 /*
Chris@148 4 Sonic Visualiser
Chris@148 5 An audio file viewer and annotation editor.
Chris@148 6 Centre for Digital Music, Queen Mary, University of London.
Chris@148 7 This file copyright 2006 Chris Cannam.
Chris@148 8
Chris@148 9 This program is free software; you can redistribute it and/or
Chris@148 10 modify it under the terms of the GNU General Public License as
Chris@148 11 published by the Free Software Foundation; either version 2 of the
Chris@148 12 License, or (at your option) any later version. See the file
Chris@148 13 COPYING included with this distribution for more information.
Chris@148 14 */
Chris@148 15
Chris@148 16 #ifndef _MATRIX_FILE_CACHE_H_
Chris@148 17 #define _MATRIX_FILE_CACHE_H_
Chris@148 18
Chris@148 19 #include "base/ResizeableBitset.h"
Chris@148 20
Chris@148 21 #include "FileReadThread.h"
Chris@148 22
Chris@148 23 #include <sys/types.h>
Chris@148 24 #include <QString>
Chris@148 25 #include <QMutex>
Chris@148 26 #include <map>
Chris@148 27
Chris@148 28 class MatrixFile : public QObject
Chris@148 29 {
Chris@148 30 Q_OBJECT
Chris@148 31
Chris@148 32 public:
Chris@148 33 enum Mode { ReadOnly, ReadWrite };
Chris@148 34
Chris@148 35 /**
Chris@148 36 * Construct a MatrixFile object reading from and/or writing to
Chris@148 37 * the matrix file with the given base name in the application's
Chris@148 38 * temporary directory.
Chris@148 39 *
Chris@148 40 * If mode is ReadOnly, the file must exist and be readable.
Chris@148 41 *
Chris@148 42 * If mode is ReadWrite and the file does not exist, it will be
Chris@148 43 * created. If mode is ReadWrite and the file does exist, the
Chris@148 44 * existing file will be used and the mode will be reset to
Chris@148 45 * ReadOnly. Call getMode() to check whether this has occurred
Chris@148 46 * after construction.
Chris@148 47 *
Chris@148 48 * cellSize specifies the size in bytes of the object type stored
Chris@148 49 * in the matrix. For example, use cellSize = sizeof(float) for a
Chris@148 50 * matrix of floats. The MatrixFile object doesn't care about the
Chris@148 51 * objects themselves, it just deals with raw data of a given size.
Chris@148 52 *
Chris@148 53 * If eagerCache is true, blocks from the file will be cached for
Chris@148 54 * read. If eagerCache is false, only columns that have been set
Chris@148 55 * by calling setColumnAt on this MatrixFile (i.e. columns for
Chris@148 56 * which haveSetColumnAt returns true) will be cached.
Chris@148 57 */
Chris@148 58 MatrixFile(QString fileBase, Mode mode, size_t cellSize, bool eagerCache);
Chris@148 59 virtual ~MatrixFile();
Chris@148 60
Chris@148 61 Mode getMode() const { return m_mode; }
Chris@148 62
Chris@148 63 size_t getWidth() const { return m_width; }
Chris@148 64 size_t getHeight() const { return m_height; }
Chris@148 65 size_t getCellSize() const { return m_cellSize; }
Chris@148 66
Chris@148 67 void resize(size_t width, size_t height);
Chris@148 68 void reset();
Chris@148 69
Chris@148 70 bool haveSetColumnAt(size_t x) const { return m_columnBitset->get(x); }
Chris@148 71 void getColumnAt(size_t x, void *data);
Chris@148 72 void setColumnAt(size_t x, const void *data);
Chris@148 73
Chris@148 74 void suspend();
Chris@148 75
Chris@148 76 protected:
Chris@148 77 int m_fd;
Chris@148 78 Mode m_mode;
Chris@148 79 int m_flags;
Chris@148 80 mode_t m_fmode;
Chris@148 81 size_t m_cellSize;
Chris@148 82 size_t m_width;
Chris@148 83 size_t m_height;
Chris@148 84 size_t m_headerSize;
Chris@148 85 QString m_fileName;
Chris@148 86 size_t m_defaultCacheWidth;
Chris@148 87 size_t m_prevX;
Chris@148 88
Chris@148 89 struct Cache {
Chris@148 90 size_t x;
Chris@148 91 size_t width;
Chris@148 92 char *data;
Chris@148 93 };
Chris@148 94
Chris@148 95 Cache m_cache;
Chris@148 96 bool m_eagerCache;
Chris@148 97
Chris@148 98 bool getFromCache(size_t x, size_t ystart, size_t ycount, void *data);
Chris@148 99 void primeCache(size_t x, bool left);
Chris@148 100
Chris@148 101 void resume();
Chris@148 102
Chris@148 103 bool seekTo(size_t x, size_t y);
Chris@148 104
Chris@148 105 static FileReadThread *m_readThread;
Chris@148 106 int m_requestToken;
Chris@148 107
Chris@148 108 size_t m_requestingX;
Chris@148 109 size_t m_requestingWidth;
Chris@148 110 char *m_spareData;
Chris@148 111
Chris@148 112 static std::map<QString, int> m_refcount;
Chris@148 113 static QMutex m_refcountMutex;
Chris@148 114 QMutex m_fdMutex;
Chris@148 115 QMutex m_cacheMutex;
Chris@148 116
Chris@148 117 typedef std::map<QString, ResizeableBitset *> ResizeableBitsetMap;
Chris@148 118 static ResizeableBitsetMap m_columnBitsets;
Chris@148 119 static QMutex m_columnBitsetWriteMutex;
Chris@148 120 ResizeableBitset *m_columnBitset;
Chris@148 121 };
Chris@148 122
Chris@148 123 #endif
Chris@148 124