MASA-Core
SpecialRow.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 SPECIALROW_HPP_
00023 #define SPECIALROW_HPP_
00024 
00025 #include <stdio.h>
00026 
00027 #include <string>
00028 using namespace std;
00029 
00030 #include "../io/SeekableCellsReader.hpp"
00031 #include "../io/CellsWriter.hpp"
00032 #include "../../libmasa/libmasa.hpp"
00033 
00034 /** @brief Abstract class that represents an Special Row.
00035  *
00036  * A Special Row is a row (or column) stored to be used in the further stages.
00037  * This class permit that subclasses store the special row in
00038  * any possible area (e.g.: disk, memory, database, etc.).
00039  *
00040  */
00041 class SpecialRow : public SeekableCellsReader, public CellsWriter {
00042 public:
00043         /**
00044          * Default Constructor.
00045          */
00046         SpecialRow();
00047 
00048         /**
00049          * Default Destructor.
00050          */
00051         virtual ~SpecialRow();
00052 
00053         /**
00054          * Function that sorts the special rows by ID.
00055          */
00056     static int sortById(SpecialRow* a, SpecialRow* b);
00057 
00058     /**
00059      * Opens the special rows for read-only or write-only.
00060      *
00061      * @param readOnly if true, indicates that the row will be used
00062      * only for read access. Otherwise, the row will only be used
00063      * for write access.
00064      * @param length the maximum length of this row.
00065      */
00066         void open(bool readOnly, int length=0);
00067 
00068         /**
00069          * Writes data in the row serially.
00070          *
00071          * @param buf vector containing the cells to be appended to the row.
00072          * @param len length of the vector.
00073          * @return The number of cells written.
00074          */
00075         virtual int write(const cell_t* buf, int len);
00076 
00077         /**
00078          * Reads data from the row serially. The read access is done in the
00079          * reverse direction, from the end to the beginning. Before reading,
00080          * the SpecialRow::setOffset must defines the start position to be read.
00081          *
00082          * @param buf vector that will receive the cells read from the row.
00083          * @param len length of the vector.
00084          * @return The number of cells read.
00085          */
00086         virtual int read(cell_t* buf, int len);
00087 
00088 
00089         virtual int getType();
00090 
00091         /**
00092          * Defines the next position to be read.
00093          * @param offset The reading position.
00094          */
00095         void seek(int offset);
00096 
00097         /**
00098          * Returns the current reading position.
00099          * @return the index of the next cell to be read.
00100          */
00101         int getOffset();
00102 
00103 
00104         /**
00105          * Returns the ID of the row.
00106          * @return the number of the row (i-coordinate).
00107          */
00108         int getId();
00109 
00110         /**
00111          * Closes the special row for writing.
00112          */
00113         virtual void close() = 0;
00114 
00115         /**
00116          * Truncate the size of the row. If the size is zero, the row must
00117          * be completely deleted. Each subclass must implement this
00118          * method with specific code.
00119          *
00120          * @param size the number of cells to keep after truncation.
00121          */
00122         virtual void truncateRow(int size) = 0;
00123 
00124 protected:
00125 
00126         /**
00127          * Defines the ID of the row, i.e., the number of the row it represents.
00128          * @param id the number of the row (i-coordinate).
00129          */
00130         void setId(int id);
00131 
00132 private:
00133         /** The number of the row that this object represents */
00134         int id;
00135 
00136         /** Current reading position */
00137         int offset;
00138 
00139         /** Indicates if the row is in reading or writing mode */
00140         bool readOnly;
00141 
00142         /**
00143          * Initializes the row with specific data.
00144          *
00145          * @param readOnly if true, indicates that the row will be used
00146      * only for read access. Otherwise, the row will only be used
00147      * for write access. Each subclass must implement this
00148          * method with specific code.
00149          * @param length the maximum length of this row. The subclass
00150          * may consider this as the initial size, for faster allocation.
00151          */
00152         virtual void initialize(bool readOnly, int length=0) = 0;
00153 
00154         /**
00155          * Stores the vector in a given offset. It is guaranteed that the
00156          * offset is serially incremented. Each subclass must implement this
00157          * method with specific code.
00158          *
00159          * @param buf vector containing the cells to be saved.
00160          * @param offset the position that the vector must be stored.
00161          * @param len length of the vector.
00162          * @return The number of cells stored.
00163          */
00164         virtual int write(const cell_t* buf, int offset, int len) = 0;
00165 
00166         /**
00167          * Stores the vector in a given offset. It is guaranteed that the
00168          * offset is serially decremented. Each subclass must implement this
00169          * method with specific code.
00170          *
00171          * @param buf vector containing the cells to be read. The data read will
00172          *      be stored in the first byte of this vector.
00173          * @param offset the position that the data must be read.
00174          * @param len length of the vector.
00175          * @return The number of cells read.
00176          */
00177         virtual int read(cell_t* buf, int offset, int len) = 0;
00178 
00179 };
00180 
00181 #endif /* SPECIALROW_HPP_ */