MASA-Core
SpecialRowFile.hpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2010-2015   Edans Sandes
00004  *
00005  * This file is part of MASA-Core.
00006  * 
00007  * MASA-Core is free software: you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation, either version 3 of the License, or
00010  * (at your option) any later version.
00011  * 
00012  * MASA-Core is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with MASA-Core.  If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  ******************************************************************************/
00021 
00022 #ifndef SPECIALROWFILE_HPP_
00023 #define SPECIALROWFILE_HPP_
00024 
00025 #include "SpecialRow.hpp"
00026 
00027 /** @brief Class that reads and stores an Special Row in the disk.
00028  *
00029  * This class is the implementation of a SpecialRow that stores the cells
00030  * in a file saved in the disk.
00031  *
00032  * While the special row is in write mode, a temporary file (*.tmp) store the
00033  * cells. As soon as the row turn to read mode, the temporary file is closed
00034  * and renamed to the definitive name.
00035  */
00036 class SpecialRowFile : public SpecialRow {
00037 public:
00038         /**
00039          * Creates a new Special Row associated with a given file.
00040          *
00041          * @param path the path to save this file.
00042          * @param filename the name of this file. The rowId is extracted from this
00043          *   name. If the filename is not a valid row name, rowId is set to -1.
00044          */
00045         SpecialRowFile(string* path, string filename);
00046 
00047         /**
00048          * Creates a new Special Row associated with a given rowId.
00049          *
00050          * @param path the path to save this file.
00051          * @param id the rowId of this file. The filename is associated to this id.
00052          *      The rowId must be relative to the partition, so the rowId 0 is the first
00053          *      row of the partition.
00054          */
00055         SpecialRowFile(string* path, int id);
00056 
00057         /**
00058          * Destroys any allocated resource previously allocated by this object.
00059          */
00060         virtual ~SpecialRowFile();
00061 
00062         /**
00063          * Closes the file.
00064          */
00065         virtual void close();
00066 
00067         /**
00068          * Truncates the file.
00069          *
00070          * @param size the number of cells to keep in the file.
00071          */
00072         virtual void truncateRow(int size);
00073 
00074 private:
00075         /** Dynamic path name of the partition. */
00076         string* path; // this string changes when the partition changes its name.
00077 
00078         /** File name basic prefix*/
00079         string filename;
00080 
00081         /** Opened file descriptor */
00082         FILE* file;
00083 
00084         /**
00085          * Opens the file for read or write mode.
00086          * @param readOnly true if it must be opened for read mode, false otherwise.
00087          * @param length the initial size of the file.
00088          */
00089         virtual void initialize(bool readOnly, int length);
00090 
00091         /*
00092          * @see description in superclass header.
00093          */
00094         virtual int write(const cell_t* buf, int offset, int len);
00095 
00096         /*
00097          * @see description in superclass header.
00098          */
00099         virtual int read(cell_t* buf, int offset, int len);
00100 
00101         /**
00102          * Returns the complete filename (with path) of the special row.
00103          * @param temp indicates if the filename is temporary or definitive.
00104          */
00105         string getFullFilename(bool temp);
00106 };
00107 
00108 #endif /* SPECIALROWFILE_HPP_ */