DS_Table.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. /*
  2. * 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. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "DS_Table.h"
  11. #include "DS_OrderedList.h"
  12. #include <string.h>
  13. #include "RakAssert.h"
  14. #include "RakAssert.h"
  15. #include "Itoa.h"
  16. using namespace DataStructures;
  17. #ifdef _MSC_VER
  18. #pragma warning( push )
  19. #endif
  20. void ExtendRows(Table::Row* input, int index)
  21. {
  22. (void) index;
  23. input->cells.Insert(RakNet::OP_NEW<Table::Cell>(_FILE_AND_LINE_), _FILE_AND_LINE_ );
  24. }
  25. void FreeRow(Table::Row* input, int index)
  26. {
  27. (void) index;
  28. unsigned i;
  29. for (i=0; i < input->cells.Size(); i++)
  30. {
  31. RakNet::OP_DELETE(input->cells[i], _FILE_AND_LINE_);
  32. }
  33. RakNet::OP_DELETE(input, _FILE_AND_LINE_);
  34. }
  35. Table::Cell::Cell()
  36. {
  37. isEmpty=true;
  38. c=0;
  39. ptr=0;
  40. i=0.0;
  41. }
  42. Table::Cell::~Cell()
  43. {
  44. Clear();
  45. }
  46. Table::Cell& Table::Cell::operator = ( const Table::Cell& input )
  47. {
  48. isEmpty=input.isEmpty;
  49. i=input.i;
  50. ptr=input.ptr;
  51. if (c)
  52. rakFree_Ex(c, _FILE_AND_LINE_);
  53. if (input.c)
  54. {
  55. c = (char*) rakMalloc_Ex( (int) i, _FILE_AND_LINE_ );
  56. memcpy(c, input.c, (int) i);
  57. }
  58. else
  59. c=0;
  60. return *this;
  61. }
  62. Table::Cell::Cell( const Table::Cell & input)
  63. {
  64. isEmpty=input.isEmpty;
  65. i=input.i;
  66. ptr=input.ptr;
  67. if (input.c)
  68. {
  69. if (c)
  70. rakFree_Ex(c, _FILE_AND_LINE_);
  71. c = (char*) rakMalloc_Ex( (int) i, _FILE_AND_LINE_ );
  72. memcpy(c, input.c, (int) i);
  73. }
  74. }
  75. void Table::Cell::Set(double input)
  76. {
  77. Clear();
  78. i=input;
  79. c=0;
  80. ptr=0;
  81. isEmpty=false;
  82. }
  83. void Table::Cell::Set(unsigned int input)
  84. {
  85. Set((int) input);
  86. }
  87. void Table::Cell::Set(int input)
  88. {
  89. Clear();
  90. i=(double) input;
  91. c=0;
  92. ptr=0;
  93. isEmpty=false;
  94. }
  95. void Table::Cell::Set(const char *input)
  96. {
  97. Clear();
  98. if (input)
  99. {
  100. i=(int)strlen(input)+1;
  101. c = (char*) rakMalloc_Ex( (int) i, _FILE_AND_LINE_ );
  102. strcpy(c, input);
  103. }
  104. else
  105. {
  106. c=0;
  107. i=0;
  108. }
  109. ptr=0;
  110. isEmpty=false;
  111. }
  112. void Table::Cell::Set(const char *input, int inputLength)
  113. {
  114. Clear();
  115. if (input)
  116. {
  117. c = (char*) rakMalloc_Ex( inputLength, _FILE_AND_LINE_ );
  118. i=inputLength;
  119. memcpy(c, input, inputLength);
  120. }
  121. else
  122. {
  123. c=0;
  124. i=0;
  125. }
  126. ptr=0;
  127. isEmpty=false;
  128. }
  129. void Table::Cell::SetPtr(void* p)
  130. {
  131. Clear();
  132. c=0;
  133. ptr=p;
  134. isEmpty=false;
  135. }
  136. void Table::Cell::Get(int *output)
  137. {
  138. RakAssert(isEmpty==false);
  139. int o = (int) i;
  140. *output=o;
  141. }
  142. void Table::Cell::Get(double *output)
  143. {
  144. RakAssert(isEmpty==false);
  145. *output=i;
  146. }
  147. void Table::Cell::Get(char *output)
  148. {
  149. RakAssert(isEmpty==false);
  150. strcpy(output, c);
  151. }
  152. void Table::Cell::Get(char *output, int *outputLength)
  153. {
  154. RakAssert(isEmpty==false);
  155. memcpy(output, c, (int) i);
  156. if (outputLength)
  157. *outputLength=(int) i;
  158. }
  159. RakNet::RakString Table::Cell::ToString(ColumnType columnType)
  160. {
  161. if (isEmpty)
  162. return RakNet::RakString();
  163. if (columnType==NUMERIC)
  164. {
  165. return RakNet::RakString("%f", i);
  166. }
  167. else if (columnType==STRING)
  168. {
  169. return RakNet::RakString(c);
  170. }
  171. else if (columnType==BINARY)
  172. {
  173. return RakNet::RakString("<Binary>");
  174. }
  175. else if (columnType==POINTER)
  176. {
  177. return RakNet::RakString("%p", ptr);
  178. }
  179. return RakNet::RakString();
  180. }
  181. Table::Cell::Cell(double numericValue, char *charValue, void *ptr, ColumnType type)
  182. {
  183. SetByType(numericValue,charValue,ptr,type);
  184. }
  185. void Table::Cell::SetByType(double numericValue, char *charValue, void *ptr, ColumnType type)
  186. {
  187. isEmpty=true;
  188. if (type==NUMERIC)
  189. {
  190. Set(numericValue);
  191. }
  192. else if (type==STRING)
  193. {
  194. Set(charValue);
  195. }
  196. else if (type==BINARY)
  197. {
  198. Set(charValue, (int) numericValue);
  199. }
  200. else if (type==POINTER)
  201. {
  202. SetPtr(ptr);
  203. }
  204. else
  205. {
  206. ptr=(void*) charValue;
  207. }
  208. }
  209. Table::ColumnType Table::Cell::EstimateColumnType(void) const
  210. {
  211. if (c)
  212. {
  213. if (i!=0.0f)
  214. return BINARY;
  215. else
  216. return STRING;
  217. }
  218. if (ptr)
  219. return POINTER;
  220. return NUMERIC;
  221. }
  222. void Table::Cell::Clear(void)
  223. {
  224. if (isEmpty==false && c)
  225. {
  226. rakFree_Ex(c, _FILE_AND_LINE_);
  227. c=0;
  228. }
  229. isEmpty=true;
  230. }
  231. Table::ColumnDescriptor::ColumnDescriptor()
  232. {
  233. }
  234. Table::ColumnDescriptor::~ColumnDescriptor()
  235. {
  236. }
  237. Table::ColumnDescriptor::ColumnDescriptor(const char cn[_TABLE_MAX_COLUMN_NAME_LENGTH], ColumnType ct)
  238. {
  239. columnType=ct;
  240. strcpy(columnName, cn);
  241. }
  242. void Table::Row::UpdateCell(unsigned columnIndex, double value)
  243. {
  244. cells[columnIndex]->Clear();
  245. cells[columnIndex]->Set(value);
  246. // cells[columnIndex]->i=value;
  247. // cells[columnIndex]->c=0;
  248. // cells[columnIndex]->isEmpty=false;
  249. }
  250. void Table::Row::UpdateCell(unsigned columnIndex, const char *str)
  251. {
  252. cells[columnIndex]->Clear();
  253. cells[columnIndex]->Set(str);
  254. }
  255. void Table::Row::UpdateCell(unsigned columnIndex, int byteLength, const char *data)
  256. {
  257. cells[columnIndex]->Clear();
  258. cells[columnIndex]->Set(data,byteLength);
  259. }
  260. Table::Table()
  261. {
  262. }
  263. Table::~Table()
  264. {
  265. Clear();
  266. }
  267. unsigned Table::AddColumn(const char columnName[_TABLE_MAX_COLUMN_NAME_LENGTH], ColumnType columnType)
  268. {
  269. if (columnName[0]==0)
  270. return (unsigned) -1;
  271. // Add this column.
  272. columns.Insert(Table::ColumnDescriptor(columnName, columnType), _FILE_AND_LINE_);
  273. // Extend the rows by one
  274. rows.ForEachData(ExtendRows);
  275. return columns.Size()-1;
  276. }
  277. void Table::RemoveColumn(unsigned columnIndex)
  278. {
  279. if (columnIndex >= columns.Size())
  280. return;
  281. columns.RemoveAtIndex(columnIndex);
  282. // Remove this index from each row.
  283. int i;
  284. DataStructures::Page<unsigned, Row*, _TABLE_BPLUS_TREE_ORDER> *cur = rows.GetListHead();
  285. while (cur)
  286. {
  287. for (i=0; i < cur->size; i++)
  288. {
  289. RakNet::OP_DELETE(cur->data[i]->cells[columnIndex], _FILE_AND_LINE_);
  290. cur->data[i]->cells.RemoveAtIndex(columnIndex);
  291. }
  292. cur=cur->next;
  293. }
  294. }
  295. unsigned Table::ColumnIndex(const char *columnName) const
  296. {
  297. unsigned columnIndex;
  298. for (columnIndex=0; columnIndex<columns.Size(); columnIndex++)
  299. if (strcmp(columnName, columns[columnIndex].columnName)==0)
  300. return columnIndex;
  301. return (unsigned)-1;
  302. }
  303. unsigned Table::ColumnIndex(char columnName[_TABLE_MAX_COLUMN_NAME_LENGTH]) const
  304. {
  305. return ColumnIndex((const char *) columnName);
  306. }
  307. char* Table::ColumnName(unsigned index) const
  308. {
  309. if (index >= columns.Size())
  310. return 0;
  311. else
  312. return (char*)columns[index].columnName;
  313. }
  314. Table::ColumnType Table::GetColumnType(unsigned index) const
  315. {
  316. if (index >= columns.Size())
  317. return (Table::ColumnType) 0;
  318. else
  319. return columns[index].columnType;
  320. }
  321. unsigned Table::GetColumnCount(void) const
  322. {
  323. return columns.Size();
  324. }
  325. unsigned Table::GetRowCount(void) const
  326. {
  327. return rows.Size();
  328. }
  329. Table::Row* Table::AddRow(unsigned rowId)
  330. {
  331. Row *newRow;
  332. newRow = RakNet::OP_NEW<Row>( _FILE_AND_LINE_ );
  333. if (rows.Insert(rowId, newRow)==false)
  334. {
  335. RakNet::OP_DELETE(newRow, _FILE_AND_LINE_);
  336. return 0; // Already exists
  337. }
  338. unsigned rowIndex;
  339. for (rowIndex=0; rowIndex < columns.Size(); rowIndex++)
  340. newRow->cells.Insert( RakNet::OP_NEW<Table::Cell>(_FILE_AND_LINE_), _FILE_AND_LINE_ );
  341. return newRow;
  342. }
  343. Table::Row* Table::AddRow(unsigned rowId, DataStructures::List<Cell> &initialCellValues)
  344. {
  345. Row *newRow = RakNet::OP_NEW<Row>( _FILE_AND_LINE_ );
  346. unsigned rowIndex;
  347. for (rowIndex=0; rowIndex < columns.Size(); rowIndex++)
  348. {
  349. if (rowIndex < initialCellValues.Size() && initialCellValues[rowIndex].isEmpty==false)
  350. {
  351. Table::Cell *c;
  352. c = RakNet::OP_NEW<Table::Cell>(_FILE_AND_LINE_);
  353. c->SetByType(initialCellValues[rowIndex].i,initialCellValues[rowIndex].c,initialCellValues[rowIndex].ptr,columns[rowIndex].columnType);
  354. newRow->cells.Insert(c, _FILE_AND_LINE_ );
  355. }
  356. else
  357. newRow->cells.Insert(RakNet::OP_NEW<Table::Cell>(_FILE_AND_LINE_), _FILE_AND_LINE_ );
  358. }
  359. rows.Insert(rowId, newRow);
  360. return newRow;
  361. }
  362. Table::Row* Table::AddRow(unsigned rowId, DataStructures::List<Cell*> &initialCellValues, bool copyCells)
  363. {
  364. Row *newRow = RakNet::OP_NEW<Row>( _FILE_AND_LINE_ );
  365. unsigned rowIndex;
  366. for (rowIndex=0; rowIndex < columns.Size(); rowIndex++)
  367. {
  368. if (rowIndex < initialCellValues.Size() && initialCellValues[rowIndex] && initialCellValues[rowIndex]->isEmpty==false)
  369. {
  370. if (copyCells==false)
  371. newRow->cells.Insert(RakNet::OP_NEW_4<Table::Cell>( _FILE_AND_LINE_, initialCellValues[rowIndex]->i, initialCellValues[rowIndex]->c, initialCellValues[rowIndex]->ptr, columns[rowIndex].columnType), _FILE_AND_LINE_);
  372. else
  373. {
  374. Table::Cell *c = RakNet::OP_NEW<Table::Cell>( _FILE_AND_LINE_ );
  375. newRow->cells.Insert(c, _FILE_AND_LINE_);
  376. *c=*(initialCellValues[rowIndex]);
  377. }
  378. }
  379. else
  380. newRow->cells.Insert(RakNet::OP_NEW<Table::Cell>(_FILE_AND_LINE_), _FILE_AND_LINE_);
  381. }
  382. rows.Insert(rowId, newRow);
  383. return newRow;
  384. }
  385. Table::Row* Table::AddRowColumns(unsigned rowId, Row *row, DataStructures::List<unsigned> columnIndices)
  386. {
  387. Row *newRow = RakNet::OP_NEW<Row>( _FILE_AND_LINE_ );
  388. unsigned columnIndex;
  389. for (columnIndex=0; columnIndex < columnIndices.Size(); columnIndex++)
  390. {
  391. if (row->cells[columnIndices[columnIndex]]->isEmpty==false)
  392. {
  393. newRow->cells.Insert(RakNet::OP_NEW_4<Table::Cell>( _FILE_AND_LINE_,
  394. row->cells[columnIndices[columnIndex]]->i,
  395. row->cells[columnIndices[columnIndex]]->c,
  396. row->cells[columnIndices[columnIndex]]->ptr,
  397. columns[columnIndex].columnType
  398. ), _FILE_AND_LINE_);
  399. }
  400. else
  401. {
  402. newRow->cells.Insert(RakNet::OP_NEW<Table::Cell>(_FILE_AND_LINE_), _FILE_AND_LINE_);
  403. }
  404. }
  405. rows.Insert(rowId, newRow);
  406. return newRow;
  407. }
  408. bool Table::RemoveRow(unsigned rowId)
  409. {
  410. Row *out;
  411. if (rows.Delete(rowId, out))
  412. {
  413. DeleteRow(out);
  414. return true;
  415. }
  416. return false;
  417. }
  418. void Table::RemoveRows(Table *tableContainingRowIDs)
  419. {
  420. unsigned i;
  421. DataStructures::Page<unsigned, Row*, _TABLE_BPLUS_TREE_ORDER> *cur = tableContainingRowIDs->GetRows().GetListHead();
  422. while (cur)
  423. {
  424. for (i=0; i < (unsigned)cur->size; i++)
  425. {
  426. rows.Delete(cur->keys[i]);
  427. }
  428. cur=cur->next;
  429. }
  430. return;
  431. }
  432. bool Table::UpdateCell(unsigned rowId, unsigned columnIndex, int value)
  433. {
  434. RakAssert(columns[columnIndex].columnType==NUMERIC);
  435. Row *row = GetRowByID(rowId);
  436. if (row)
  437. {
  438. row->UpdateCell(columnIndex, value);
  439. return true;
  440. }
  441. return false;
  442. }
  443. bool Table::UpdateCell(unsigned rowId, unsigned columnIndex, char *str)
  444. {
  445. RakAssert(columns[columnIndex].columnType==STRING);
  446. Row *row = GetRowByID(rowId);
  447. if (row)
  448. {
  449. row->UpdateCell(columnIndex, str);
  450. return true;
  451. }
  452. return false;
  453. }
  454. bool Table::UpdateCell(unsigned rowId, unsigned columnIndex, int byteLength, char *data)
  455. {
  456. RakAssert(columns[columnIndex].columnType==BINARY);
  457. Row *row = GetRowByID(rowId);
  458. if (row)
  459. {
  460. row->UpdateCell(columnIndex, byteLength, data);
  461. return true;
  462. }
  463. return false;
  464. }
  465. bool Table::UpdateCellByIndex(unsigned rowIndex, unsigned columnIndex, int value)
  466. {
  467. RakAssert(columns[columnIndex].columnType==NUMERIC);
  468. Row *row = GetRowByIndex(rowIndex,0);
  469. if (row)
  470. {
  471. row->UpdateCell(columnIndex, value);
  472. return true;
  473. }
  474. return false;
  475. }
  476. bool Table::UpdateCellByIndex(unsigned rowIndex, unsigned columnIndex, char *str)
  477. {
  478. RakAssert(columns[columnIndex].columnType==STRING);
  479. Row *row = GetRowByIndex(rowIndex,0);
  480. if (row)
  481. {
  482. row->UpdateCell(columnIndex, str);
  483. return true;
  484. }
  485. return false;
  486. }
  487. bool Table::UpdateCellByIndex(unsigned rowIndex, unsigned columnIndex, int byteLength, char *data)
  488. {
  489. RakAssert(columns[columnIndex].columnType==BINARY);
  490. Row *row = GetRowByIndex(rowIndex,0);
  491. if (row)
  492. {
  493. row->UpdateCell(columnIndex, byteLength, data);
  494. return true;
  495. }
  496. return false;
  497. }
  498. void Table::GetCellValueByIndex(unsigned rowIndex, unsigned columnIndex, int *output)
  499. {
  500. RakAssert(columns[columnIndex].columnType==NUMERIC);
  501. Row *row = GetRowByIndex(rowIndex,0);
  502. if (row)
  503. {
  504. row->cells[columnIndex]->Get(output);
  505. }
  506. }
  507. void Table::GetCellValueByIndex(unsigned rowIndex, unsigned columnIndex, char *output)
  508. {
  509. RakAssert(columns[columnIndex].columnType==STRING);
  510. Row *row = GetRowByIndex(rowIndex,0);
  511. if (row)
  512. {
  513. row->cells[columnIndex]->Get(output);
  514. }
  515. }
  516. void Table::GetCellValueByIndex(unsigned rowIndex, unsigned columnIndex, char *output, int *outputLength)
  517. {
  518. RakAssert(columns[columnIndex].columnType==BINARY);
  519. Row *row = GetRowByIndex(rowIndex,0);
  520. if (row)
  521. {
  522. row->cells[columnIndex]->Get(output, outputLength);
  523. }
  524. }
  525. Table::FilterQuery::FilterQuery()
  526. {
  527. columnName[0]=0;
  528. }
  529. Table::FilterQuery::~FilterQuery()
  530. {
  531. }
  532. Table::FilterQuery::FilterQuery(unsigned column, Cell *cell, FilterQueryType op)
  533. {
  534. columnIndex=column;
  535. cellValue=cell;
  536. operation=op;
  537. }
  538. Table::Row* Table::GetRowByID(unsigned rowId) const
  539. {
  540. Row *row;
  541. if (rows.Get(rowId, row))
  542. return row;
  543. return 0;
  544. }
  545. Table::Row* Table::GetRowByIndex(unsigned rowIndex, unsigned *key) const
  546. {
  547. DataStructures::Page<unsigned, Row*, _TABLE_BPLUS_TREE_ORDER> *cur = rows.GetListHead();
  548. while (cur)
  549. {
  550. if (rowIndex < (unsigned)cur->size)
  551. {
  552. if (key)
  553. *key=cur->keys[rowIndex];
  554. return cur->data[rowIndex];
  555. }
  556. if (rowIndex <= (unsigned)cur->size)
  557. rowIndex-=cur->size;
  558. else
  559. return 0;
  560. cur=cur->next;
  561. }
  562. return 0;
  563. }
  564. void Table::QueryTable(unsigned *columnIndicesSubset, unsigned numColumnSubset, FilterQuery *inclusionFilters, unsigned numInclusionFilters, unsigned *rowIds, unsigned numRowIDs, Table *result)
  565. {
  566. unsigned i;
  567. DataStructures::List<unsigned> columnIndicesToReturn;
  568. // Clear the result table.
  569. result->Clear();
  570. if (columnIndicesSubset && numColumnSubset>0)
  571. {
  572. for (i=0; i < numColumnSubset; i++)
  573. {
  574. if (columnIndicesSubset[i]<columns.Size())
  575. columnIndicesToReturn.Insert(columnIndicesSubset[i], _FILE_AND_LINE_);
  576. }
  577. }
  578. else
  579. {
  580. for (i=0; i < columns.Size(); i++)
  581. columnIndicesToReturn.Insert(i, _FILE_AND_LINE_);
  582. }
  583. if (columnIndicesToReturn.Size()==0)
  584. return; // No valid columns specified
  585. for (i=0; i < columnIndicesToReturn.Size(); i++)
  586. {
  587. result->AddColumn(columns[columnIndicesToReturn[i]].columnName,columns[columnIndicesToReturn[i]].columnType);
  588. }
  589. // Get the column indices of the filter queries.
  590. DataStructures::List<unsigned> inclusionFilterColumnIndices;
  591. if (inclusionFilters && numInclusionFilters>0)
  592. {
  593. for (i=0; i < numInclusionFilters; i++)
  594. {
  595. if (inclusionFilters[i].columnName[0])
  596. inclusionFilters[i].columnIndex=ColumnIndex(inclusionFilters[i].columnName);
  597. if (inclusionFilters[i].columnIndex<columns.Size())
  598. inclusionFilterColumnIndices.Insert(inclusionFilters[i].columnIndex, _FILE_AND_LINE_);
  599. else
  600. inclusionFilterColumnIndices.Insert((unsigned)-1, _FILE_AND_LINE_);
  601. }
  602. }
  603. if (rowIds==0 || numRowIDs==0)
  604. {
  605. // All rows
  606. DataStructures::Page<unsigned, Row*, _TABLE_BPLUS_TREE_ORDER> *cur = rows.GetListHead();
  607. while (cur)
  608. {
  609. for (i=0; i < (unsigned)cur->size; i++)
  610. {
  611. QueryRow(inclusionFilterColumnIndices, columnIndicesToReturn, cur->keys[i], cur->data[i], inclusionFilters, result);
  612. }
  613. cur=cur->next;
  614. }
  615. }
  616. else
  617. {
  618. // Specific rows
  619. Row *row;
  620. for (i=0; i < numRowIDs; i++)
  621. {
  622. if (rows.Get(rowIds[i], row))
  623. {
  624. QueryRow(inclusionFilterColumnIndices, columnIndicesToReturn, rowIds[i], row, inclusionFilters, result);
  625. }
  626. }
  627. }
  628. }
  629. void Table::QueryRow(DataStructures::List<unsigned> &inclusionFilterColumnIndices, DataStructures::List<unsigned> &columnIndicesToReturn, unsigned key, Table::Row* row, FilterQuery *inclusionFilters, Table *result)
  630. {
  631. bool pass=false;
  632. unsigned columnIndex;
  633. unsigned j;
  634. // If no inclusion filters, just add the row
  635. if (inclusionFilterColumnIndices.Size()==0)
  636. {
  637. result->AddRowColumns(key, row, columnIndicesToReturn);
  638. }
  639. else
  640. {
  641. // Go through all inclusion filters. Only add this row if all filters pass.
  642. for (j=0; j<inclusionFilterColumnIndices.Size(); j++)
  643. {
  644. columnIndex=inclusionFilterColumnIndices[j];
  645. if (columnIndex!=(unsigned)-1 && row->cells[columnIndex]->isEmpty==false )
  646. {
  647. if (columns[inclusionFilterColumnIndices[j]].columnType==STRING &&
  648. (row->cells[columnIndex]->c==0 ||
  649. inclusionFilters[j].cellValue->c==0) )
  650. continue;
  651. switch (inclusionFilters[j].operation)
  652. {
  653. case QF_EQUAL:
  654. switch(columns[inclusionFilterColumnIndices[j]].columnType)
  655. {
  656. case NUMERIC:
  657. pass=row->cells[columnIndex]->i==inclusionFilters[j].cellValue->i;
  658. break;
  659. case STRING:
  660. pass=strcmp(row->cells[columnIndex]->c,inclusionFilters[j].cellValue->c)==0;
  661. break;
  662. case BINARY:
  663. pass=row->cells[columnIndex]->i==inclusionFilters[j].cellValue->i &&
  664. memcmp(row->cells[columnIndex]->c,inclusionFilters[j].cellValue->c, (int) row->cells[columnIndex]->i)==0;
  665. break;
  666. case POINTER:
  667. pass=row->cells[columnIndex]->ptr==inclusionFilters[j].cellValue->ptr;
  668. break;
  669. }
  670. break;
  671. case QF_NOT_EQUAL:
  672. switch(columns[inclusionFilterColumnIndices[j]].columnType)
  673. {
  674. case NUMERIC:
  675. pass=row->cells[columnIndex]->i!=inclusionFilters[j].cellValue->i;
  676. break;
  677. case STRING:
  678. pass=strcmp(row->cells[columnIndex]->c,inclusionFilters[j].cellValue->c)!=0;
  679. break;
  680. case BINARY:
  681. pass=row->cells[columnIndex]->i==inclusionFilters[j].cellValue->i &&
  682. memcmp(row->cells[columnIndex]->c,inclusionFilters[j].cellValue->c, (int) row->cells[columnIndex]->i)==0;
  683. break;
  684. case POINTER:
  685. pass=row->cells[columnIndex]->ptr!=inclusionFilters[j].cellValue->ptr;
  686. break;
  687. }
  688. break;
  689. case QF_GREATER_THAN:
  690. switch(columns[inclusionFilterColumnIndices[j]].columnType)
  691. {
  692. case NUMERIC:
  693. pass=row->cells[columnIndex]->i>inclusionFilters[j].cellValue->i;
  694. break;
  695. case STRING:
  696. pass=strcmp(row->cells[columnIndex]->c,inclusionFilters[j].cellValue->c)>0;
  697. break;
  698. case BINARY:
  699. break;
  700. case POINTER:
  701. pass=row->cells[columnIndex]->ptr>inclusionFilters[j].cellValue->ptr;
  702. break;
  703. }
  704. break;
  705. case QF_GREATER_THAN_EQ:
  706. switch(columns[inclusionFilterColumnIndices[j]].columnType)
  707. {
  708. case NUMERIC:
  709. pass=row->cells[columnIndex]->i>=inclusionFilters[j].cellValue->i;
  710. break;
  711. case STRING:
  712. pass=strcmp(row->cells[columnIndex]->c,inclusionFilters[j].cellValue->c)>=0;
  713. break;
  714. case BINARY:
  715. break;
  716. case POINTER:
  717. pass=row->cells[columnIndex]->ptr>=inclusionFilters[j].cellValue->ptr;
  718. break;
  719. }
  720. break;
  721. case QF_LESS_THAN:
  722. switch(columns[inclusionFilterColumnIndices[j]].columnType)
  723. {
  724. case NUMERIC:
  725. pass=row->cells[columnIndex]->i<inclusionFilters[j].cellValue->i;
  726. break;
  727. case STRING:
  728. pass=strcmp(row->cells[columnIndex]->c,inclusionFilters[j].cellValue->c)<0;
  729. break;
  730. case BINARY:
  731. break;
  732. case POINTER:
  733. pass=row->cells[columnIndex]->ptr<inclusionFilters[j].cellValue->ptr;
  734. break;
  735. }
  736. break;
  737. case QF_LESS_THAN_EQ:
  738. switch(columns[inclusionFilterColumnIndices[j]].columnType)
  739. {
  740. case NUMERIC:
  741. pass=row->cells[columnIndex]->i<=inclusionFilters[j].cellValue->i;
  742. break;
  743. case STRING:
  744. pass=strcmp(row->cells[columnIndex]->c,inclusionFilters[j].cellValue->c)<=0;
  745. break;
  746. case BINARY:
  747. break;
  748. case POINTER:
  749. pass=row->cells[columnIndex]->ptr<=inclusionFilters[j].cellValue->ptr;
  750. break;
  751. }
  752. break;
  753. case QF_IS_EMPTY:
  754. pass=false;
  755. break;
  756. case QF_NOT_EMPTY:
  757. pass=true;
  758. break;
  759. default:
  760. pass=false;
  761. RakAssert(0);
  762. break;
  763. }
  764. }
  765. else
  766. {
  767. if (inclusionFilters[j].operation==QF_IS_EMPTY)
  768. pass=true;
  769. else
  770. pass=false; // No value for this cell
  771. }
  772. if (pass==false)
  773. break;
  774. }
  775. if (pass)
  776. {
  777. result->AddRowColumns(key, row, columnIndicesToReturn);
  778. }
  779. }
  780. }
  781. static Table::SortQuery *_sortQueries;
  782. static unsigned _numSortQueries;
  783. static DataStructures::List<unsigned> *_columnIndices;
  784. static DataStructures::List<Table::ColumnDescriptor> *_columns;
  785. int RowSort(Table::Row* const &first, Table::Row* const &second) // first is the one inserting, second is the one already there.
  786. {
  787. unsigned i, columnIndex;
  788. for (i=0; i<_numSortQueries; i++)
  789. {
  790. columnIndex=(*_columnIndices)[i];
  791. if (columnIndex==(unsigned)-1)
  792. continue;
  793. if (first->cells[columnIndex]->isEmpty==true && second->cells[columnIndex]->isEmpty==false)
  794. return 1; // Empty cells always go at the end
  795. if (first->cells[columnIndex]->isEmpty==false && second->cells[columnIndex]->isEmpty==true)
  796. return -1; // Empty cells always go at the end
  797. if (_sortQueries[i].operation==Table::QS_INCREASING_ORDER)
  798. {
  799. if ((*_columns)[columnIndex].columnType==Table::NUMERIC)
  800. {
  801. if (first->cells[columnIndex]->i>second->cells[columnIndex]->i)
  802. return 1;
  803. if (first->cells[columnIndex]->i<second->cells[columnIndex]->i)
  804. return -1;
  805. }
  806. else
  807. {
  808. // String
  809. if (strcmp(first->cells[columnIndex]->c,second->cells[columnIndex]->c)>0)
  810. return 1;
  811. if (strcmp(first->cells[columnIndex]->c,second->cells[columnIndex]->c)<0)
  812. return -1;
  813. }
  814. }
  815. else
  816. {
  817. if ((*_columns)[columnIndex].columnType==Table::NUMERIC)
  818. {
  819. if (first->cells[columnIndex]->i<second->cells[columnIndex]->i)
  820. return 1;
  821. if (first->cells[columnIndex]->i>second->cells[columnIndex]->i)
  822. return -1;
  823. }
  824. else
  825. {
  826. // String
  827. if (strcmp(first->cells[columnIndex]->c,second->cells[columnIndex]->c)<0)
  828. return 1;
  829. if (strcmp(first->cells[columnIndex]->c,second->cells[columnIndex]->c)>0)
  830. return -1;
  831. }
  832. }
  833. }
  834. return 0;
  835. }
  836. void Table::SortTable(Table::SortQuery *sortQueries, unsigned numSortQueries, Table::Row** out)
  837. {
  838. unsigned i;
  839. unsigned outLength;
  840. DataStructures::List<unsigned> columnIndices;
  841. _sortQueries=sortQueries;
  842. _numSortQueries=numSortQueries;
  843. _columnIndices=&columnIndices;
  844. _columns=&columns;
  845. bool anyValid=false;
  846. for (i=0; i < numSortQueries; i++)
  847. {
  848. if (sortQueries[i].columnIndex<columns.Size() && columns[sortQueries[i].columnIndex].columnType!=BINARY)
  849. {
  850. columnIndices.Insert(sortQueries[i].columnIndex, _FILE_AND_LINE_);
  851. anyValid=true;
  852. }
  853. else
  854. columnIndices.Insert((unsigned)-1, _FILE_AND_LINE_); // Means don't check this column
  855. }
  856. DataStructures::Page<unsigned, Row*, _TABLE_BPLUS_TREE_ORDER> *cur;
  857. cur = rows.GetListHead();
  858. if (anyValid==false)
  859. {
  860. outLength=0;
  861. while (cur)
  862. {
  863. for (i=0; i < (unsigned)cur->size; i++)
  864. {
  865. out[(outLength)++]=cur->data[i];
  866. }
  867. cur=cur->next;
  868. }
  869. return;
  870. }
  871. // Start adding to ordered list.
  872. DataStructures::OrderedList<Row*, Row*, RowSort> orderedList;
  873. while (cur)
  874. {
  875. for (i=0; i < (unsigned)cur->size; i++)
  876. {
  877. RakAssert(cur->data[i]);
  878. orderedList.Insert(cur->data[i],cur->data[i], true, _FILE_AND_LINE_);
  879. }
  880. cur=cur->next;
  881. }
  882. outLength=0;
  883. for (i=0; i < orderedList.Size(); i++)
  884. out[(outLength)++]=orderedList[i];
  885. }
  886. void Table::PrintColumnHeaders(char *out, int outLength, char columnDelineator) const
  887. {
  888. if (outLength<=0)
  889. return;
  890. if (outLength==1)
  891. {
  892. *out=0;
  893. return;
  894. }
  895. unsigned i;
  896. out[0]=0;
  897. int len;
  898. for (i=0; i < columns.Size(); i++)
  899. {
  900. if (i!=0)
  901. {
  902. len = (int) strlen(out);
  903. if (len < outLength-1)
  904. sprintf(out+len, "%c", columnDelineator);
  905. else
  906. return;
  907. }
  908. len = (int) strlen(out);
  909. if (len < outLength-(int) strlen(columns[i].columnName))
  910. sprintf(out+len, "%s", columns[i].columnName);
  911. else
  912. return;
  913. }
  914. }
  915. void Table::PrintRow(char *out, int outLength, char columnDelineator, bool printDelineatorForBinary, Table::Row* inputRow) const
  916. {
  917. if (outLength<=0)
  918. return;
  919. if (outLength==1)
  920. {
  921. *out=0;
  922. return;
  923. }
  924. if (inputRow->cells.Size()!=columns.Size())
  925. {
  926. strncpy(out, "Cell width does not match column width.\n", outLength);
  927. out[outLength-1]=0;
  928. return;
  929. }
  930. char buff[512];
  931. unsigned i;
  932. int len;
  933. out[0]=0;
  934. for (i=0; i < columns.Size(); i++)
  935. {
  936. if (columns[i].columnType==NUMERIC)
  937. {
  938. if (inputRow->cells[i]->isEmpty==false)
  939. {
  940. sprintf(buff, "%f", inputRow->cells[i]->i);
  941. len=(int)strlen(buff);
  942. }
  943. else
  944. len=0;
  945. if (i+1!=columns.Size())
  946. buff[len++]=columnDelineator;
  947. buff[len]=0;
  948. }
  949. else if (columns[i].columnType==STRING)
  950. {
  951. if (inputRow->cells[i]->isEmpty==false && inputRow->cells[i]->c)
  952. {
  953. strncpy(buff, inputRow->cells[i]->c, 512-2);
  954. buff[512-2]=0;
  955. len=(int)strlen(buff);
  956. }
  957. else
  958. len=0;
  959. if (i+1!=columns.Size())
  960. buff[len++]=columnDelineator;
  961. buff[len]=0;
  962. }
  963. else if (columns[i].columnType==POINTER)
  964. {
  965. if (inputRow->cells[i]->isEmpty==false && inputRow->cells[i]->ptr)
  966. {
  967. sprintf(buff, "%p", inputRow->cells[i]->ptr);
  968. len=(int)strlen(buff);
  969. }
  970. else
  971. len=0;
  972. if (i+1!=columns.Size())
  973. buff[len++]=columnDelineator;
  974. buff[len]=0;
  975. }
  976. else
  977. {
  978. if (printDelineatorForBinary)
  979. {
  980. if (i+1!=columns.Size())
  981. buff[0]=columnDelineator;
  982. buff[1]=0;
  983. }
  984. else
  985. buff[0]=0;
  986. }
  987. len=(int)strlen(out);
  988. if (outLength==len+1)
  989. break;
  990. strncpy(out+len, buff, outLength-len);
  991. out[outLength-1]=0;
  992. }
  993. }
  994. void Table::Clear(void)
  995. {
  996. rows.ForEachData(FreeRow);
  997. rows.Clear();
  998. columns.Clear(true, _FILE_AND_LINE_);
  999. }
  1000. const List<Table::ColumnDescriptor>& Table::GetColumns(void) const
  1001. {
  1002. return columns;
  1003. }
  1004. const DataStructures::BPlusTree<unsigned, Table::Row*, _TABLE_BPLUS_TREE_ORDER>& Table::GetRows(void) const
  1005. {
  1006. return rows;
  1007. }
  1008. DataStructures::Page<unsigned, DataStructures::Table::Row*, _TABLE_BPLUS_TREE_ORDER> * Table::GetListHead(void)
  1009. {
  1010. return rows.GetListHead();
  1011. }
  1012. unsigned Table::GetAvailableRowId(void) const
  1013. {
  1014. bool setKey=false;
  1015. unsigned key=0;
  1016. int i;
  1017. DataStructures::Page<unsigned, Row*, _TABLE_BPLUS_TREE_ORDER> *cur = rows.GetListHead();
  1018. while (cur)
  1019. {
  1020. for (i=0; i < cur->size; i++)
  1021. {
  1022. if (setKey==false)
  1023. {
  1024. key=cur->keys[i]+1;
  1025. setKey=true;
  1026. }
  1027. else
  1028. {
  1029. if (key!=cur->keys[i])
  1030. return key;
  1031. key++;
  1032. }
  1033. }
  1034. cur=cur->next;
  1035. }
  1036. return key;
  1037. }
  1038. void Table::DeleteRow(Table::Row *row)
  1039. {
  1040. unsigned rowIndex;
  1041. for (rowIndex=0; rowIndex < row->cells.Size(); rowIndex++)
  1042. {
  1043. RakNet::OP_DELETE(row->cells[rowIndex], _FILE_AND_LINE_);
  1044. }
  1045. RakNet::OP_DELETE(row, _FILE_AND_LINE_);
  1046. }
  1047. Table& Table::operator = ( const Table& input )
  1048. {
  1049. Clear();
  1050. unsigned int i;
  1051. for (i=0; i < input.GetColumnCount(); i++)
  1052. AddColumn(input.ColumnName(i), input.GetColumnType(i));
  1053. DataStructures::Page<unsigned, Row*, _TABLE_BPLUS_TREE_ORDER> *cur = input.GetRows().GetListHead();
  1054. while (cur)
  1055. {
  1056. for (i=0; i < (unsigned int) cur->size; i++)
  1057. {
  1058. AddRow(cur->keys[i], cur->data[i]->cells, false);
  1059. }
  1060. cur=cur->next;
  1061. }
  1062. return *this;
  1063. }
  1064. #ifdef _MSC_VER
  1065. #pragma warning( pop )
  1066. #endif
粤ICP备19079148号