Shadowrun: Awakened 29 September 2011 - Build 871
DS_Table.h
Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 #ifndef __TABLE_H
00008 #define __TABLE_H
00009 
00010 #ifdef _MSC_VER
00011 #pragma warning( push )
00012 #endif
00013 
00014 #include "DS_List.h"
00015 #include "DS_BPlusTree.h"
00016 #include "RakMemoryOverride.h"
00017 #include "Export.h"
00018 #include "RakString.h"
00019 
00020 #define _TABLE_BPLUS_TREE_ORDER 16
00021 #define _TABLE_MAX_COLUMN_NAME_LENGTH 64
00022 
00025 namespace DataStructures
00026 {
00027 
00033     class RAK_DLL_EXPORT Table
00034     {
00035     public:
00036             
00037         enum ColumnType
00038         {
00039             // Cell::i used
00040             NUMERIC,
00041 
00042             // Cell::c used to hold a null terminated string.
00043             STRING,
00044 
00045             // Cell::c holds data.  Cell::i holds data length of c in bytes.
00046             BINARY,
00047 
00048             // Cell::c holds data.  Not deallocated. Set manually by assigning ptr.
00049             POINTER,
00050         };
00051         
00052         
00054         // Note: If this structure is changed the struct in the swig files need to be changed as well
00055         struct RAK_DLL_EXPORT Cell
00056         {
00057             Cell();
00058             ~Cell();
00059             Cell(double numericValue, char *charValue, void *ptr, ColumnType type);
00060             void SetByType(double numericValue, char *charValue, void *ptr, ColumnType type);
00061             void Clear(void);
00062             
00064             void Set(int input);
00065             void Set(unsigned int input);
00066             void Set(double input);
00067 
00069             void Set(const char *input);
00070 
00072             void Set(const char *input, int inputLength);
00073 
00075             void SetPtr(void* p);
00076 
00078             void Get(int *output);
00079             void Get(double *output);
00080 
00082             void Get(char *output);
00083 
00085             void Get(char *output, int *outputLength);
00086 
00087             RakNet::RakString ToString(ColumnType columnType);
00088 
00089             // assignment operator and copy constructor
00090             Cell& operator = ( const Cell& input );
00091             Cell( const Cell & input);
00092 
00093             ColumnType EstimateColumnType(void) const;
00094 
00095             bool isEmpty;
00096             double i;
00097             char *c;
00098             void *ptr;
00099         };
00100 
00103         // Note: If this structure is changed the struct in the swig files need to be changed as well
00104         struct RAK_DLL_EXPORT ColumnDescriptor
00105         {
00106             ColumnDescriptor();
00107             ~ColumnDescriptor();
00108             ColumnDescriptor(const char cn[_TABLE_MAX_COLUMN_NAME_LENGTH],ColumnType ct);
00109 
00110             char columnName[_TABLE_MAX_COLUMN_NAME_LENGTH];
00111             ColumnType columnType;
00112         };
00113 
00115         // Note: If this structure is changed the struct in the swig files need to be changed as well
00116         struct RAK_DLL_EXPORT Row
00117         {
00118             // list of cells
00119             DataStructures::List<Cell*> cells;
00120 
00122             void UpdateCell(unsigned columnIndex, double value);
00123 
00125             void UpdateCell(unsigned columnIndex, const char *str);
00126 
00128             void UpdateCell(unsigned columnIndex, int byteLength, const char *data);
00129         };
00130         
00131         // Operations to perform for cell comparison
00132         enum FilterQueryType
00133         {
00134             QF_EQUAL,
00135             QF_NOT_EQUAL,
00136             QF_GREATER_THAN,
00137             QF_GREATER_THAN_EQ,
00138             QF_LESS_THAN,
00139             QF_LESS_THAN_EQ,
00140             QF_IS_EMPTY,
00141             QF_NOT_EMPTY,
00142         };
00143 
00144         // Compare the cell value for a row at columnName to the cellValue using operation.
00145         // Note: If this structure is changed the struct in the swig files need to be changed as well
00146         struct RAK_DLL_EXPORT FilterQuery
00147         {
00148             FilterQuery();
00149             ~FilterQuery();
00150             FilterQuery(unsigned column, Cell *cell, FilterQueryType op);
00151 
00152             // If columnName is specified, columnIndex will be looked up using it.
00153             char columnName[_TABLE_MAX_COLUMN_NAME_LENGTH];
00154             unsigned columnIndex;
00155             Cell *cellValue;
00156             FilterQueryType operation;
00157         };
00158 
00160         enum SortQueryType
00161         {
00162             QS_INCREASING_ORDER,
00163             QS_DECREASING_ORDER,
00164         };
00165         
00166         // Sort on increasing or decreasing order for a particular column
00167         // Note: If this structure is changed the struct in the swig files need to be changed as well
00168         struct RAK_DLL_EXPORT SortQuery
00169         {
00171             unsigned columnIndex;
00172 
00174             SortQueryType operation;
00175         };
00176 
00177         // Constructor
00178         Table();
00179 
00180         // Destructor
00181         ~Table();
00182 
00187         unsigned AddColumn(const char columnName[_TABLE_MAX_COLUMN_NAME_LENGTH], ColumnType columnType);
00188 
00191         void RemoveColumn(unsigned columnIndex);
00192 
00197         unsigned ColumnIndex(char columnName[_TABLE_MAX_COLUMN_NAME_LENGTH]) const;
00198         unsigned ColumnIndex(const char *columnName) const;
00199 
00203         char* ColumnName(unsigned index) const;
00204 
00208         ColumnType GetColumnType(unsigned index) const;
00209 
00212         unsigned GetColumnCount(void) const;
00213 
00216         unsigned GetRowCount(void) const;
00217 
00227         Table::Row* AddRow(unsigned rowId);
00228         Table::Row* AddRow(unsigned rowId, DataStructures::List<Cell> &initialCellValues);
00229         Table::Row* AddRow(unsigned rowId, DataStructures::List<Cell*> &initialCellValues, bool copyCells=false);
00230 
00234         bool RemoveRow(unsigned rowId);
00235 
00238         void RemoveRows(Table *tableContainingRowIDs);
00239 
00246         bool UpdateCell(unsigned rowId, unsigned columnIndex, int value);
00247         bool UpdateCell(unsigned rowId, unsigned columnIndex, char *str);
00248         bool UpdateCell(unsigned rowId, unsigned columnIndex, int byteLength, char *data);
00249         bool UpdateCellByIndex(unsigned rowIndex, unsigned columnIndex, int value);
00250         bool UpdateCellByIndex(unsigned rowIndex, unsigned columnIndex, char *str);
00251         bool UpdateCellByIndex(unsigned rowIndex, unsigned columnIndex, int byteLength, char *data);
00252 
00255         void GetCellValueByIndex(unsigned rowIndex, unsigned columnIndex, int *output);
00256         void GetCellValueByIndex(unsigned rowIndex, unsigned columnIndex, char *output);
00257         void GetCellValueByIndex(unsigned rowIndex, unsigned columnIndex, char *output, int *outputLength);
00258 
00263         Row* GetRowByID(unsigned rowId) const;
00264 
00270         Row* GetRowByIndex(unsigned rowIndex, unsigned *key) const;
00271 
00280         void QueryTable(unsigned *columnIndicesSubset, unsigned numColumnSubset, FilterQuery *inclusionFilters, unsigned numInclusionFilters, unsigned *rowIds, unsigned numRowIDs, Table *result);
00281 
00289         void SortTable(Table::SortQuery *sortQueries, unsigned numSortQueries, Table::Row** out);
00290 
00292         void Clear(void);
00293 
00298         void PrintColumnHeaders(char *out, int outLength, char columnDelineator) const;
00299 
00306         void PrintRow(char *out, int outLength, char columnDelineator, bool printDelineatorForBinary, Table::Row* inputRow) const;
00307 
00309         const DataStructures::List<ColumnDescriptor>& GetColumns(void) const;
00310 
00312         const DataStructures::BPlusTree<unsigned, Row*, _TABLE_BPLUS_TREE_ORDER>& GetRows(void) const;
00313 
00315         DataStructures::Page<unsigned, Row*, _TABLE_BPLUS_TREE_ORDER> * GetListHead(void);
00316 
00319         unsigned GetAvailableRowId(void) const;
00320 
00321         Table& operator = ( const Table& input );
00322 
00323     protected:
00324         Table::Row* AddRowColumns(unsigned rowId, Row *row, DataStructures::List<unsigned> columnIndices);
00325 
00326         void DeleteRow(Row *row);
00327 
00328         void QueryRow(DataStructures::List<unsigned> &inclusionFilterColumnIndices, DataStructures::List<unsigned> &columnIndicesToReturn, unsigned key, Table::Row* row, FilterQuery *inclusionFilters, Table *result);
00329 
00330         // 16 is arbitrary and is the order of the BPlus tree.  Higher orders are better for searching while lower orders are better for
00331         // Insertions and deletions.
00332         DataStructures::BPlusTree<unsigned, Row*, _TABLE_BPLUS_TREE_ORDER> rows;
00333 
00334         // Columns in the table.
00335         DataStructures::List<ColumnDescriptor> columns;
00336     };
00337 }
00338 
00339 #ifdef _MSC_VER
00340 #pragma warning( pop )
00341 #endif
00342 
00343 #endif

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net