Shadowrun: Awakened 29 September 2011 - Build 871
TableSerializer.h
Go to the documentation of this file.
00001 #ifndef __TABLE_SERIALIZER_H
00002 #define __TABLE_SERIALIZER_H
00003 
00004 #include "RakMemoryOverride.h"
00005 #include "DS_Table.h"
00006 #include "Export.h"
00007 
00008 namespace RakNet
00009 {
00010     class BitStream;
00011 }
00012 
00013 namespace RakNet
00014 {
00015 
00016 class RAK_DLL_EXPORT TableSerializer
00017 {
00018 public:
00019     static void SerializeTable(DataStructures::Table *in, RakNet::BitStream *out);
00020     static bool DeserializeTable(unsigned char *serializedTable, unsigned int dataLength, DataStructures::Table *out);
00021     static bool DeserializeTable(RakNet::BitStream *in, DataStructures::Table *out);
00022     static void SerializeColumns(DataStructures::Table *in, RakNet::BitStream *out);
00023     static void SerializeColumns(DataStructures::Table *in, RakNet::BitStream *out, DataStructures::List<int> &skipColumnIndices);
00024     static bool DeserializeColumns(RakNet::BitStream *in, DataStructures::Table *out);  
00025     static void SerializeRow(DataStructures::Table::Row *in, unsigned keyIn, const DataStructures::List<DataStructures::Table::ColumnDescriptor> &columns, RakNet::BitStream *out);
00026     static void SerializeRow(DataStructures::Table::Row *in, unsigned keyIn, const DataStructures::List<DataStructures::Table::ColumnDescriptor> &columns, RakNet::BitStream *out, DataStructures::List<int> &skipColumnIndices);
00027     static bool DeserializeRow(RakNet::BitStream *in, DataStructures::Table *out);
00028     static void SerializeCell(RakNet::BitStream *out, DataStructures::Table::Cell *cell, DataStructures::Table::ColumnType columnType);
00029     static bool DeserializeCell(RakNet::BitStream *in, DataStructures::Table::Cell *cell, DataStructures::Table::ColumnType columnType);
00030     static void SerializeFilterQuery(RakNet::BitStream *in, DataStructures::Table::FilterQuery *query);
00031     // Note that this allocates query->cell->c!
00032     static bool DeserializeFilterQuery(RakNet::BitStream *out, DataStructures::Table::FilterQuery *query);
00033     static void SerializeFilterQueryList(RakNet::BitStream *in, DataStructures::Table::FilterQuery *query, unsigned int numQueries, unsigned int maxQueries);
00034     // Note that this allocates queries, cells, and query->cell->c!. Use DeallocateQueryList to free.
00035     static bool DeserializeFilterQueryList(RakNet::BitStream *out, DataStructures::Table::FilterQuery **query, unsigned int *numQueries, unsigned int maxQueries, int allocateExtraQueries=0);
00036     static void DeallocateQueryList(DataStructures::Table::FilterQuery *query, unsigned int numQueries);
00037 };
00038 
00039 } // namespace RakNet
00040 
00041 #endif
00042 
00043 // Test code for the table
00044 /*
00045 #include "LightweightDatabaseServer.h"
00046 #include "LightweightDatabaseClient.h"
00047 #include "TableSerializer.h"
00048 #include "BitStream.h"
00049 #include "StringCompressor.h"
00050 #include "DS_Table.h"
00051 void main(void)
00052 {
00053     DataStructures::Table table;
00054     DataStructures::Table::Row *row;
00055     unsigned int dummydata=12345;
00056 
00057     // Add columns Name (string), IP (binary), score (int), and players (int).
00058     table.AddColumn("Name", DataStructures::Table::STRING);
00059     table.AddColumn("IP", DataStructures::Table::BINARY);
00060     table.AddColumn("Score", DataStructures::Table::NUMERIC);
00061     table.AddColumn("Players", DataStructures::Table::NUMERIC);
00062     table.AddColumn("Empty Test Column", DataStructures::Table::STRING);
00063     RakAssert(table.GetColumnCount()==5);
00064     row=table.AddRow(0);
00065     RakAssert(row);
00066     row->UpdateCell(0,"Kevin Jenkins");
00067     row->UpdateCell(1,sizeof(dummydata), (char*)&dummydata);
00068     row->UpdateCell(2,5);
00069     row->UpdateCell(3,10);
00070     //row->UpdateCell(4,"should be unique");
00071 
00072     row=table.AddRow(1);
00073     row->UpdateCell(0,"Kevin Jenkins");
00074     row->UpdateCell(1,sizeof(dummydata), (char*)&dummydata);
00075     row->UpdateCell(2,5);
00076     row->UpdateCell(3,15);
00077 
00078     row=table.AddRow(2);
00079     row->UpdateCell(0,"Kevin Jenkins");
00080     row->UpdateCell(1,sizeof(dummydata), (char*)&dummydata);
00081     row->UpdateCell(2,5);
00082     row->UpdateCell(3,20);
00083 
00084     row=table.AddRow(3);
00085     RakAssert(row);
00086     row->UpdateCell(0,"Kevin Jenkins");
00087     row->UpdateCell(1,sizeof(dummydata), (char*)&dummydata);
00088     row->UpdateCell(2,15);
00089     row->UpdateCell(3,5);
00090     row->UpdateCell(4,"col index 4");
00091 
00092     row=table.AddRow(4);
00093     RakAssert(row);
00094     row->UpdateCell(0,"Kevin Jenkins");
00095     row->UpdateCell(1,sizeof(dummydata), (char*)&dummydata);
00096     //row->UpdateCell(2,25);
00097     row->UpdateCell(3,30);
00098     //row->UpdateCell(4,"should be unique");
00099 
00100     row=table.AddRow(5);
00101     RakAssert(row);
00102     row->UpdateCell(0,"Kevin Jenkins");
00103     row->UpdateCell(1,sizeof(dummydata), (char*)&dummydata);
00104     //row->UpdateCell(2,25);
00105     row->UpdateCell(3,5);
00106     //row->UpdateCell(4,"should be unique");
00107 
00108     row=table.AddRow(6);
00109     RakAssert(row);
00110     row->UpdateCell(0,"Kevin Jenkins");
00111     row->UpdateCell(1,sizeof(dummydata), (char*)&dummydata);
00112     row->UpdateCell(2,35);
00113     //row->UpdateCell(3,40);
00114     //row->UpdateCell(4,"should be unique");
00115 
00116     row=table.AddRow(7);
00117     RakAssert(row);
00118     row->UpdateCell(0,"Bob Jenkins");
00119 
00120     row=table.AddRow(8);
00121     RakAssert(row);
00122     row->UpdateCell(0,"Zack Jenkins");
00123 
00124     // Test multi-column sorting
00125     DataStructures::Table::Row *rows[30];
00126     DataStructures::Table::SortQuery queries[4];
00127     queries[0].columnIndex=0;
00128     queries[0].operation=DataStructures::Table::QS_INCREASING_ORDER;
00129     queries[1].columnIndex=1;
00130     queries[1].operation=DataStructures::Table::QS_INCREASING_ORDER;
00131     queries[2].columnIndex=2;
00132     queries[2].operation=DataStructures::Table::QS_INCREASING_ORDER;
00133     queries[3].columnIndex=3;
00134     queries[3].operation=DataStructures::Table::QS_DECREASING_ORDER;
00135     table.SortTable(queries, 4, rows);
00136     unsigned i;
00137     char out[256];
00138     RAKNET_DEBUG_PRINTF("Sort: Ascending except for column index 3\n");
00139     for (i=0; i < table.GetRowCount(); i++)
00140     {
00141         table.PrintRow(out,256,',',true, rows[i]);
00142         RAKNET_DEBUG_PRINTF("%s\n", out);
00143     }
00144 
00145     // Test query:
00146     // Don't return column 3, and swap columns 0 and 2
00147     unsigned columnsToReturn[4];
00148     columnsToReturn[0]=2;
00149     columnsToReturn[1]=1;
00150     columnsToReturn[2]=0;
00151     columnsToReturn[3]=4;
00152     DataStructures::Table resultsTable;
00153     table.QueryTable(columnsToReturn,4,0,0,&resultsTable);
00154     RAKNET_DEBUG_PRINTF("Query: Don't return column 3, and swap columns 0 and 2:\n");
00155     for (i=0; i < resultsTable.GetRowCount(); i++)
00156     {
00157         resultsTable.PrintRow(out,256,',',true, resultsTable.GetRowByIndex(i));
00158         RAKNET_DEBUG_PRINTF("%s\n", out);
00159     }
00160 
00161     // Test filter:
00162     // Only return rows with column index 4 empty
00163     DataStructures::Table::FilterQuery inclusionFilters[3];
00164     inclusionFilters[0].columnIndex=4;
00165     inclusionFilters[0].operation=DataStructures::Table::QF_IS_EMPTY;
00166     // inclusionFilters[0].cellValue; // Unused for IS_EMPTY
00167     table.QueryTable(0,0,inclusionFilters,1,&resultsTable);
00168     RAKNET_DEBUG_PRINTF("Filter: Only return rows with column index 4 empty:\n");
00169     for (i=0; i < resultsTable.GetRowCount(); i++)
00170     {
00171         resultsTable.PrintRow(out,256,',',true, resultsTable.GetRowByIndex(i));
00172         RAKNET_DEBUG_PRINTF("%s\n", out);
00173     }
00174 
00175     // Column 5 empty and column 0 == Kevin Jenkins
00176     inclusionFilters[0].columnIndex=4;
00177     inclusionFilters[0].operation=DataStructures::Table::QF_IS_EMPTY;
00178     inclusionFilters[1].columnIndex=0;
00179     inclusionFilters[1].operation=DataStructures::Table::QF_EQUAL;
00180     inclusionFilters[1].cellValue.Set("Kevin Jenkins");
00181     table.QueryTable(0,0,inclusionFilters,2,&resultsTable);
00182     RAKNET_DEBUG_PRINTF("Filter: Column 5 empty and column 0 == Kevin Jenkins:\n");
00183     for (i=0; i < resultsTable.GetRowCount(); i++)
00184     {
00185         resultsTable.PrintRow(out,256,',',true, resultsTable.GetRowByIndex(i));
00186         RAKNET_DEBUG_PRINTF("%s\n", out);
00187     }
00188 
00189     RakNet::BitStream bs;
00190     RAKNET_DEBUG_PRINTF("PreSerialize:\n");
00191     for (i=0; i < table.GetRowCount(); i++)
00192     {
00193         table.PrintRow(out,256,',',true, table.GetRowByIndex(i));
00194         RAKNET_DEBUG_PRINTF("%s\n", out);
00195     }
00196     StringCompressor::AddReference();
00197     TableSerializer::Serialize(&table, &bs);
00198     TableSerializer::Deserialize(&bs, &table);
00199     StringCompressor::RemoveReference();
00200     RAKNET_DEBUG_PRINTF("PostDeserialize:\n");
00201     for (i=0; i < table.GetRowCount(); i++)
00202     {
00203         table.PrintRow(out,256,',',true, table.GetRowByIndex(i));
00204         RAKNET_DEBUG_PRINTF("%s\n", out);
00205     }
00206     int a=5;
00207 }
00208 */

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