SLikeNet 0.1.3

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DS_Table.h
Go to the documentation of this file.
1 /*
2 * Original work: Copyright (c) 2014, Oculus VR, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7 * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8 *
9 *
10 * Modified work: Copyright (c) 2016-2018, SLikeSoft UG (haftungsbeschränkt)
11 *
12 * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13 * license found in the license.txt file in the root directory of this source tree.
14 */
15
18
19
20 #ifndef __TABLE_H
21 #define __TABLE_H
22
23 #include "DS_List.h"
24 #include "DS_BPlusTree.h"
25 #include "memoryoverride.h"
26 #include "Export.h"
27 #include "string.h"
28
29 #define _TABLE_BPLUS_TREE_ORDER 16
30 #define _TABLE_MAX_COLUMN_NAME_LENGTH 64
31
34 namespace DataStructures
35 {
36
42 class RAK_DLL_EXPORT Table
43 {
44 public:
45
46 enum ColumnType
47 {
48 // Cell::i used
49 NUMERIC,
50
51 // Cell::c used to hold a null terminated string.
52 STRING,
53
54 // Cell::c holds data. Cell::i holds data length of c in bytes.
55 BINARY,
56
57 // Cell::c holds data. Not deallocated. Set manually by assigning ptr.
58 POINTER,
59 };
60
61
63 // Note: If this structure is changed the struct in the swig files need to be changed as well
64 struct RAK_DLL_EXPORT Cell
65 {
66 Cell();
67 ~Cell();
68 Cell(double numericValue, char *charValue, void *ptr, ColumnType type);
69 void SetByType(double numericValue, char *charValue, void *inPtr, ColumnType type);
70 void Clear(void);
71
73 void Set(int input);
74 void Set(unsigned int input);
75 void Set(double input);
76
78 void Set(const char *input);
79
81 void Set(const char *input, int inputLength);
82
84 void SetPtr(void* p);
85
87 void Get(int *output);
88 void Get(double *output);
89
91 void Get(char *output);
92 void Get(char *output, size_t outputLength);
93
95 void Get(char *output, int *outputLength);
96
97 SLNet::RakString ToString(ColumnType columnType);
98
99 // assignment operator and copy constructor
100 Cell& operator = ( const Cell& input );
101 Cell( const Cell & input);
102
103 ColumnType EstimateColumnType(void) const;
104
105 bool isEmpty;
106 double i;
107 char *c;
108 void *ptr;
109 };
110
113 // Note: If this structure is changed the struct in the swig files need to be changed as well
114 struct RAK_DLL_EXPORT ColumnDescriptor
115 {
116 ColumnDescriptor();
117 ~ColumnDescriptor();
118 ColumnDescriptor(const char cn[_TABLE_MAX_COLUMN_NAME_LENGTH],ColumnType ct);
119
120 char columnName[_TABLE_MAX_COLUMN_NAME_LENGTH];
121 ColumnType columnType;
122 };
123
125 // Note: If this structure is changed the struct in the swig files need to be changed as well
126 struct RAK_DLL_EXPORT Row
127 {
128 // list of cells
129 DataStructures::List<Cell*> cells;
130
132 void UpdateCell(unsigned columnIndex, double value);
133
135 void UpdateCell(unsigned columnIndex, const char *str);
136
138 void UpdateCell(unsigned columnIndex, int byteLength, const char *data);
139 };
140
141 // Operations to perform for cell comparison
142 enum FilterQueryType
143 {
144 QF_EQUAL,
145 QF_NOT_EQUAL,
146 QF_GREATER_THAN,
147 QF_GREATER_THAN_EQ,
148 QF_LESS_THAN,
149 QF_LESS_THAN_EQ,
150 QF_IS_EMPTY,
151 QF_NOT_EMPTY,
152 };
153
154 // Compare the cell value for a row at columnName to the cellValue using operation.
155 // Note: If this structure is changed the struct in the swig files need to be changed as well
156 struct RAK_DLL_EXPORT FilterQuery
157 {
158 FilterQuery();
159 ~FilterQuery();
160 FilterQuery(unsigned column, Cell *cell, FilterQueryType op);
161
162 // If columnName is specified, columnIndex will be looked up using it.
163 char columnName[_TABLE_MAX_COLUMN_NAME_LENGTH];
164 unsigned columnIndex;
165 Cell *cellValue;
166 FilterQueryType operation;
167 };
168
170 enum SortQueryType
171 {
172 QS_INCREASING_ORDER,
173 QS_DECREASING_ORDER,
174 };
175
176 // Sort on increasing or decreasing order for a particular column
177 // Note: If this structure is changed the struct in the swig files need to be changed as well
178 struct RAK_DLL_EXPORT SortQuery
179 {
181 unsigned columnIndex;
182
184 SortQueryType operation;
185 };
186
187 // Constructor
188 Table();
189
190 // Destructor
191 ~Table();
192
197 unsigned AddColumn(const char columnName[_TABLE_MAX_COLUMN_NAME_LENGTH], ColumnType columnType);
198
201 void RemoveColumn(unsigned columnIndex);
202
207 unsigned ColumnIndex(char columnName[_TABLE_MAX_COLUMN_NAME_LENGTH]) const;
208 unsigned ColumnIndex(const char *columnName) const;
209
213 char* ColumnName(unsigned index) const;
214
218 ColumnType GetColumnType(unsigned index) const;
219
222 unsigned GetColumnCount(void) const;
223
226 unsigned GetRowCount(void) const;
227
237 Table::Row* AddRow(unsigned rowId);
238 Table::Row* AddRow(unsigned rowId, DataStructures::List<Cell> &initialCellValues);
239 Table::Row* AddRow(unsigned rowId, DataStructures::List<Cell*> &initialCellValues, bool copyCells=false);
240
244 bool RemoveRow(unsigned rowId);
245
248 void RemoveRows(Table *tableContainingRowIDs);
249
256 bool UpdateCell(unsigned rowId, unsigned columnIndex, int value);
257 bool UpdateCell(unsigned rowId, unsigned columnIndex, char *str);
258 bool UpdateCell(unsigned rowId, unsigned columnIndex, int byteLength, char *data);
259 bool UpdateCellByIndex(unsigned rowIndex, unsigned columnIndex, int value);
260 bool UpdateCellByIndex(unsigned rowIndex, unsigned columnIndex, char *str);
261 bool UpdateCellByIndex(unsigned rowIndex, unsigned columnIndex, int byteLength, char *data);
262
265 void GetCellValueByIndex(unsigned rowIndex, unsigned columnIndex, int *output);
266 void GetCellValueByIndex(unsigned rowIndex, unsigned columnIndex, char *output);
267 void GetCellValueByIndex(unsigned rowIndex, unsigned columnIndex, char *output, size_t outputLength);
268 void GetCellValueByIndex(unsigned rowIndex, unsigned columnIndex, char *output, int *outputLength);
269
274 Row* GetRowByID(unsigned rowId) const;
275
281 Row* GetRowByIndex(unsigned rowIndex, unsigned *key) const;
282
291 void QueryTable(unsigned *columnIndicesSubset, unsigned numColumnSubset, FilterQuery *inclusionFilters, unsigned numInclusionFilters, unsigned *rowIds, unsigned numRowIDs, Table *result);
292
300 void SortTable(Table::SortQuery *sortQueries, unsigned numSortQueries, Table::Row** out);
301
303 void Clear(void);
304
309 void PrintColumnHeaders(char *out, int outLength, char columnDelineator) const;
310
317 void PrintRow(char *out, int outLength, char columnDelineator, bool printDelineatorForBinary, Table::Row* inputRow) const;
318
320 const DataStructures::List<ColumnDescriptor>& GetColumns(void) const;
321
323 const DataStructures::BPlusTree<unsigned, Row*, _TABLE_BPLUS_TREE_ORDER>& GetRows(void) const;
324
326 DataStructures::Page<unsigned, Row*, _TABLE_BPLUS_TREE_ORDER> * GetListHead(void);
327
330 unsigned GetAvailableRowId(void) const;
331
332 Table& operator = ( const Table& input );
333
334 protected:
335 Table::Row* AddRowColumns(unsigned rowId, Row *row, DataStructures::List<unsigned> columnIndices);
336
337 void DeleteRow(Row *row);
338
339 void QueryRow(DataStructures::List<unsigned> &inclusionFilterColumnIndices, DataStructures::List<unsigned> &columnIndicesToReturn, unsigned key, Table::Row* row, FilterQuery *inclusionFilters, Table *result);
340
341 // 16 is arbitrary and is the order of the BPlus tree. Higher orders are better for searching while lower orders are better for
342 // Insertions and deletions.
343 DataStructures::BPlusTree<unsigned, Row*, _TABLE_BPLUS_TREE_ORDER> rows;
344
345 // Columns in the table.
346 DataStructures::List<ColumnDescriptor> columns;
347 };
348 }
349
350 #endif