1
0

Table.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /* ---------------------------------------------------------------------------
  2. Nullsoft Database Engine
  3. --------------------
  4. codename: Near Death Experience
  5. --------------------------------------------------------------------------- */
  6. /* ---------------------------------------------------------------------------
  7. Table Class
  8. --------------------------------------------------------------------------- */
  9. #include "../nde.h"
  10. //#include <direct.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include "../CRC.H"
  14. #include <strsafe.h>
  15. const char *tSign="NDETABLE";
  16. //---------------------------------------------------------------------------
  17. Table::Table( const wchar_t *TableName, const wchar_t *Idx, BOOL Create, Database *_db, BOOL _Cached ) : Scanner( this )
  18. {
  19. memset( column_ids, FIELD_UNKNOWN, 255 );
  20. Cached = _Cached;
  21. db = _db;
  22. AutoCreate = Create;
  23. Name = ndestring_wcsdup( TableName );
  24. IdxName = ndestring_wcsdup( Idx );
  25. Init();
  26. }
  27. //---------------------------------------------------------------------------
  28. void Table::Init()
  29. {
  30. numErrors = 0;
  31. Scanners = new LinkedList();
  32. // benski> cut: Handle=NULL;
  33. IdxHandle = NULL;
  34. FieldsRecord = NULL;
  35. IndexList = NULL;
  36. GLocateUpToDate = FALSE;
  37. }
  38. //---------------------------------------------------------------------------
  39. Table::~Table()
  40. {
  41. Reset();
  42. if (Handle) // Reset doesn'l_data_type completely destroy Handle
  43. Vfdestroy(Handle);
  44. Handle = 0;
  45. ndestring_release(Name);
  46. ndestring_release(IdxName);
  47. }
  48. //---------------------------------------------------------------------------
  49. void Table::Reset()
  50. {
  51. if ( IndexList )
  52. IndexList->Release();
  53. IndexList = 0;
  54. if ( FieldsRecord )
  55. FieldsRecord->Release();
  56. FieldsRecord = 0;
  57. delete Scanners;
  58. Scanners = NULL;
  59. if ( Handle )
  60. Vfclose( Handle ); // close (but don'l_data_type destroy) to keep mutex open.
  61. if ( IdxHandle )
  62. Vfdestroy( IdxHandle );
  63. IdxHandle = NULL;
  64. for ( RowCache::iterator itr = rowCache.begin(); itr != rowCache.end(); ++itr )
  65. {
  66. if ( itr->second )
  67. itr->second->Release();
  68. }
  69. rowCache.clear();
  70. memset( column_ids, FIELD_UNKNOWN, 255 );
  71. columns_cached = false;
  72. }
  73. struct IndexNewWalkerContext
  74. {
  75. IndexNewWalkerContext(Table *_table)
  76. {
  77. N = -1;
  78. table = _table;
  79. }
  80. int N;
  81. Table *table;
  82. };
  83. bool Table::IndexNewWalker(IndexRecord *record, Field *entry, void *context_in)
  84. {
  85. IndexNewWalkerContext *context = (IndexNewWalkerContext *)context_in;
  86. IndexField *p = (IndexField *)entry;
  87. p->index = new Index(context->table->IdxHandle, p->ID, context->N++, p->Type, FALSE, 0, context->table);
  88. return true;
  89. }
  90. //---------------------------------------------------------------------------
  91. BOOL Table::Open(void)
  92. {
  93. int justcreated = 0;
  94. if (!Handle)
  95. Handle = Vfnew(Name, "r+b", Cached);
  96. if (!Handle) return FALSE;
  97. if (!Vflock(Handle))
  98. {
  99. Vfdestroy(Handle);
  100. Handle = 0;
  101. return FALSE;
  102. }
  103. Handle = Vfopen(Handle, Name, "r+b", Cached);
  104. IdxHandle = Vfopen(0, IdxName, "r+b", TRUE);
  105. BOOL Valid = (Handle && IdxHandle);
  106. // unlock
  107. if (Valid || !AutoCreate)
  108. {
  109. //if (Handle)
  110. //Vfunlock(Handle);
  111. }
  112. else
  113. {
  114. if (Handle)
  115. {
  116. Vfclose(Handle);
  117. if (IdxHandle)
  118. Vfdestroy(IdxHandle);
  119. IdxHandle = 0;
  120. }
  121. else
  122. {
  123. if (IdxHandle)
  124. Vfdestroy(IdxHandle);
  125. IdxHandle = 0;
  126. Handle = Vfnew(Name, "w+b", Cached);
  127. if (!Vflock(Handle))
  128. {
  129. Vfdestroy(Handle);
  130. return FALSE;
  131. }
  132. }
  133. Handle = Vfopen(Handle, Name, "w+b", Cached);
  134. IdxHandle = Vfopen(0, IdxName, "w+b", TRUE);
  135. Valid = (Handle && IdxHandle);
  136. if (Valid)
  137. {
  138. Vfwrite(__TABLE_SIGNATURE__, strlen(__TABLE_SIGNATURE__), Handle);
  139. Vfwrite(__INDEX_SIGNATURE__, strlen(__TABLE_SIGNATURE__), IdxHandle);
  140. // TODO bensk> change if NUM_SPECIAL_RECORDS ever increases
  141. int v=NUM_SPECIAL_RECORDS;//strlen(__TABLE_SIGNATURE__);
  142. Vfwrite(&v, sizeof(v), IdxHandle);
  143. // v = 0; fwrite(&v, sizeof(v), 1, IdxHandle);
  144. v = -1; Vfwrite(&v, sizeof(v), IdxHandle); // write ID
  145. v = 0;
  146. for (int i=0;i<NUM_SPECIAL_RECORDS;i++)
  147. {
  148. Vfwrite(&v, sizeof(v), IdxHandle);
  149. Vfwrite(&v, sizeof(v), IdxHandle);
  150. }
  151. Sync();
  152. justcreated = 1;
  153. }
  154. }
  155. if (!Valid)
  156. {
  157. if (Handle) Vfdestroy(Handle);
  158. if (IdxHandle) Vfdestroy(IdxHandle);
  159. Handle = NULL;
  160. IdxHandle = NULL;
  161. }
  162. else
  163. {
  164. char test1[9]={0};
  165. char test2[9]={0};
  166. Vfseek(Handle, 0, SEEK_SET);
  167. Vfread(test1, strlen(__TABLE_SIGNATURE__), Handle);
  168. Vfseek(IdxHandle, 0, SEEK_SET);
  169. Vfread(test2, strlen(__INDEX_SIGNATURE__), IdxHandle);
  170. test1[8]=0;
  171. test2[8]=0;
  172. if (strcmp(test1, __TABLE_SIGNATURE__) || strcmp(test2, __INDEX_SIGNATURE__))
  173. {
  174. if (Handle) Vfdestroy(Handle);
  175. Handle = 0;
  176. if (IdxHandle) Vfdestroy(IdxHandle);
  177. IdxHandle = 0;
  178. return FALSE;
  179. }
  180. // Load default index
  181. IndexField *l_index_field = new IndexField(PRIMARY_INDEX, -1, -1, L"None");
  182. l_index_field->index = new Index(IdxHandle, PRIMARY_INDEX, -1, -1, FALSE, 0, this);
  183. // Get indexes
  184. int Ptr = l_index_field->index->Get(INDEX_RECORD_NUM);
  185. IndexList = new IndexRecord(Ptr, INDEX_RECORD_NUM, Handle, this);
  186. if (!IndexList)
  187. {
  188. delete l_index_field;
  189. if (Handle) Vfdestroy(Handle);
  190. Handle = 0;
  191. if (IdxHandle) Vfdestroy(IdxHandle);
  192. IdxHandle = 0;
  193. return FALSE;
  194. }
  195. // Init them
  196. IndexNewWalkerContext newContext(this);
  197. IndexList->WalkFields(IndexNewWalker, &newContext);
  198. // Add default in case its not there (if it is it won'l_data_type be added by addfield)
  199. IndexList->AddField(l_index_field);
  200. // Get the default index (whether loaded or preloaded)
  201. Scanner::index = ((IndexField*)IndexList->GetField(PRIMARY_INDEX))->index;
  202. // If it's different from preloaded, delete preloaded
  203. if (l_index_field->index != Scanner::index)
  204. {
  205. delete l_index_field;
  206. l_index_field = NULL;
  207. }
  208. // Set up colaboration
  209. IndexList->BuildCollaboration();
  210. // Get columns
  211. Ptr = Scanner::index->Get(FIELDS_RECORD_NUM);
  212. FieldsRecord = new Record(Ptr, FIELDS_RECORD_NUM, Handle, this);
  213. if (!FieldsRecord)
  214. {
  215. IndexList->Release();
  216. IndexList=0;
  217. if (Handle) Vfdestroy(Handle);
  218. Handle = 0;
  219. if (IdxHandle) Vfdestroy(IdxHandle);
  220. IdxHandle = 0;
  221. return FALSE;
  222. }
  223. // update the column cache
  224. FieldsRecord->WalkFields(BuildColumnCache, this);
  225. columns_cached=true;
  226. }
  227. if (Valid && !justcreated)
  228. {
  229. if (IndexList->NeedFix())
  230. Compact();
  231. }
  232. if (Valid) First();
  233. if (Handle)
  234. Vfunlock(Handle);
  235. return Valid;
  236. }
  237. //---------------------------------------------------------------------------
  238. void Table::Close(void)
  239. {
  240. int v=0;
  241. if (Handle && IndexList && Vflock(Handle, 0))
  242. {
  243. IndexList->WalkFields(IndexWriteWalker, 0);
  244. }
  245. delete Scanners;
  246. Scanners = NULL;
  247. Vsync(Handle);
  248. if (IdxHandle)
  249. {
  250. Vfdestroy(IdxHandle);
  251. IdxHandle = NULL;
  252. v |= 2;
  253. }
  254. if (Handle)
  255. {
  256. Vfdestroy(Handle);
  257. Handle = NULL;
  258. v |= 1;
  259. }
  260. if (v != 3)
  261. return;
  262. }
  263. bool Table::IndexWriteWalker(IndexRecord *record, Field *entry, void *context)
  264. {
  265. IndexField *field = (IndexField *)entry;
  266. field->index->WriteIndex();
  267. return true;
  268. }
  269. //---------------------------------------------------------------------------
  270. void Table::Sync(void)
  271. {
  272. if (!Vflock(Handle))
  273. return;
  274. if (IndexList)
  275. IndexList->WalkFields(IndexWriteWalker, 0);
  276. int err=0;
  277. if (!err && Handle) err|=Vsync(Handle);
  278. if (!err && IdxHandle) Vsync(IdxHandle);
  279. Vfunlock(Handle);
  280. }
  281. //---------------------------------------------------------------------------
  282. ColumnField *Table::NewColumn( unsigned char FieldID, const wchar_t *FieldName, unsigned char FieldType, BOOL indexUnique )
  283. {
  284. columns_cached = false; // if they start writing new columns, kill the columns cache until they PostColumns()
  285. ColumnField *l_column_field = GetColumnById( FieldID );
  286. if ( l_column_field )
  287. {
  288. int l_data_type = l_column_field->GetDataType();
  289. if ( l_data_type != FieldType )
  290. {
  291. OutputDebugStringW( L"column " );
  292. OutputDebugStringW( FieldName );
  293. OutputDebugStringW( L" already exists but is of the wrong type\n" );
  294. if ( CompatibleFields( l_data_type, FieldType ) )
  295. {
  296. OutputDebugStringW( L"going from one equivalent type to another, converting column\n" );
  297. l_column_field->SetDataType( FieldType );
  298. goto aok;
  299. }
  300. }
  301. l_column_field->SetFieldName( (wchar_t *)FieldName );
  302. return NULL;
  303. }
  304. aok:
  305. if ( GetColumnByName( FieldName ) )
  306. return NULL;
  307. ColumnField *l_new_column_field = new ColumnField( FieldID, FieldName, FieldType, this );
  308. column_ids[ FieldID ] = FieldType;
  309. FieldsRecord->AddField( l_new_column_field );
  310. return l_new_column_field;
  311. }
  312. void Table::SetFieldSearchableById(unsigned char field_id, bool searchable)
  313. {
  314. ColumnField *column = GetColumnById(field_id);
  315. if (column)
  316. column->SetSearchable(searchable);
  317. if (searchable)
  318. {
  319. search_fields.insert(field_id);
  320. }
  321. else
  322. {
  323. search_fields.erase(field_id);
  324. }
  325. }
  326. //---------------------------------------------------------------------------
  327. bool Table::BuildColumnCache(Record *record, Field *entry, void *context)
  328. {
  329. Table *table = (Table *)context;
  330. ColumnField *column = (ColumnField *)entry;
  331. unsigned char field_id=column->GetFieldId();
  332. table->column_ids[field_id] = column->GetDataType();
  333. if (column->IsSearchableField())
  334. {
  335. table->search_fields.insert(field_id);
  336. }
  337. else
  338. {
  339. table->search_fields.erase(field_id);
  340. }
  341. return true;
  342. }
  343. //---------------------------------------------------------------------------
  344. void Table::PostColumns(void)
  345. {
  346. FieldsRecord->WriteFields(this, FIELDS_RECORD_NUM);
  347. memset(column_ids, FIELD_UNKNOWN, 255);
  348. FieldsRecord->WalkFields(BuildColumnCache, this);
  349. columns_cached=true;
  350. }
  351. unsigned char Table::GetColumnType(unsigned char Id)
  352. {
  353. if (columns_cached)
  354. {
  355. return column_ids[Id];
  356. }
  357. else
  358. {
  359. return GetColumnById(Id)->GetDataType();
  360. }
  361. }
  362. //---------------------------------------------------------------------------
  363. IndexField *Table::GetIndexByName(const wchar_t *name)
  364. {
  365. if (!IndexList)
  366. return NULL;
  367. return IndexList->GetIndexByName(name);
  368. }
  369. //---------------------------------------------------------------------------
  370. IndexField *Table::GetIndexById(unsigned char Id)
  371. {
  372. if (!IndexList)
  373. return NULL;
  374. return (IndexField *)IndexList->GetField(Id);
  375. }
  376. //---------------------------------------------------------------------------
  377. void Table::AddIndexByName(const wchar_t *name, const wchar_t *desc)
  378. {
  379. ColumnField *header = GetColumnByName(name);
  380. if (header)
  381. {
  382. unsigned char Idx = header->ID;
  383. AddIndexById(Idx, desc);
  384. }
  385. }
  386. //---------------------------------------------------------------------------
  387. void Table::AddIndexById( unsigned char Id, const wchar_t *desc )
  388. {
  389. if ( GetIndexById( Id ) )
  390. return;
  391. ColumnField *l_column_field = GetColumnById( Id );
  392. if ( !l_column_field )
  393. return;
  394. IndexField *l_new_index_field = new IndexField( Id, IndexList->GetColumnCount(), l_column_field->GetDataType(), desc );
  395. l_new_index_field->index = new Index( IdxHandle, Id, IndexList->GetColumnCount(), l_column_field->GetDataType(), TRUE, Scanner::index->NEntries, this );
  396. IndexField* l_previous_index_field = dynamic_cast<IndexField*>(IndexList->GetLastField());
  397. IndexList->AddField( l_new_index_field );
  398. //IndexField* l_previous_index_field = (IndexField*)l_new_index_field->prev;
  399. if (l_previous_index_field)
  400. {
  401. l_previous_index_field->index->Colaborate(l_new_index_field);
  402. }
  403. IndexField *l_primary_index_field = (IndexField *)IndexList->GetField( PRIMARY_INDEX );
  404. if (l_primary_index_field)
  405. {
  406. l_new_index_field->index->Colaborate(l_primary_index_field);
  407. }
  408. if (l_previous_index_field)
  409. {
  410. l_previous_index_field->index->Propagate();
  411. }
  412. IndexList->WriteFields( this );
  413. }
  414. //---------------------------------------------------------------------------
  415. BOOL Table::CheckIndexing(void)
  416. {
  417. if (IndexList->GetColumnCount() == 0) return TRUE;
  418. for (int i=0;i<Scanner::index->NEntries;i++)
  419. {
  420. if (!IndexList->CheckIndexing(i))
  421. return FALSE;
  422. }
  423. return TRUE;
  424. }
  425. struct IndexWalkerThunkContext
  426. {
  427. void *context;
  428. Table *_this;
  429. Table::IndexWalker callback;
  430. };
  431. bool Table::IndexWalkerThunk(IndexRecord *record, Field *entry, void *context_in)
  432. {
  433. IndexWalkerThunkContext *context = (IndexWalkerThunkContext *)context_in;
  434. return context->callback(context->_this, (IndexField *)entry, context->context);
  435. }
  436. //---------------------------------------------------------------------------
  437. void Table::WalkIndices(IndexWalker callback, void *context)
  438. {
  439. if (IndexList && callback)
  440. {
  441. IndexWalkerThunkContext walkerContext = { context, this, callback };
  442. IndexList->WalkFields(IndexWalkerThunk, &walkerContext);
  443. }
  444. }
  445. //---------------------------------------------------------------------------
  446. void Table::DropIndex(IndexField *Ptr)
  447. {
  448. if (!Ptr || Ptr->Type != FIELD_INDEX) return;
  449. if (Scanner::index == Ptr->index)
  450. {
  451. Scanner::index = ((IndexField*)IndexList->GetField(PRIMARY_INDEX))->index;
  452. IndexList->BuildCollaboration();
  453. }
  454. IndexList->RemoveField(Ptr);
  455. if (Scanner::index->SecIndex == Ptr)
  456. Scanner::index->SecIndex = 0;
  457. IndexList->WriteFields(this);
  458. }
  459. //---------------------------------------------------------------------------
  460. void Table::DropIndexByName(const wchar_t *desc)
  461. {
  462. IndexField *indx = GetIndexByName(desc);
  463. if (!_wcsicmp(desc, L"None")) return;
  464. if (indx)
  465. DropIndex(indx);
  466. }
  467. //---------------------------------------------------------------------------
  468. void Table::DropIndexById(unsigned char Id)
  469. {
  470. if (!IndexList)
  471. return;
  472. if (Id == (unsigned char)PRIMARY_INDEX) return;
  473. IndexField *indx=(IndexField *)IndexList->GetField(Id);
  474. if (indx)
  475. DropIndex(indx);
  476. }
  477. //---------------------------------------------------------------------------
  478. BOOL Table::LocateByIdEx(int Id, int From, Field *field, int comp_mode)
  479. {
  480. return Scanner::LocateByIdEx(Id, From, field, NULL, comp_mode);
  481. }
  482. //---------------------------------------------------------------------------
  483. Record *Table::GetColumns(void)
  484. {
  485. if (!FieldsRecord)
  486. return NULL;
  487. return FieldsRecord;
  488. }
  489. //---------------------------------------------------------------------------
  490. Scanner *Table::NewScanner()
  491. {
  492. Scanner *s = new Scanner(this);
  493. /*if (Scanners->GetNElements() > 0)*/
  494. s->index = Scanner::index;
  495. Scanners->AddEntry(s, TRUE);
  496. return s;
  497. }
  498. //---------------------------------------------------------------------------
  499. Scanner *Table::GetDefaultScanner()
  500. {
  501. return this;
  502. }
  503. //---------------------------------------------------------------------------
  504. void Table::DeleteScanner(Scanner *scan)
  505. {
  506. if (!scan) return;
  507. Scanners->RemoveEntry(scan);
  508. }
  509. //---------------------------------------------------------------------------
  510. void Table::IndexModified(void)
  511. {
  512. Scanner *s = (Scanner *)Scanners->GetHead();
  513. while (s)
  514. {
  515. s->IndexModified();
  516. s = (Scanner *)s->GetNext();
  517. }
  518. }
  519. //---------------------------------------------------------------------------
  520. void Table::SetGlobalLocateUpToDate(BOOL is) {
  521. GLocateUpToDate = is;
  522. }
  523. struct ColumnWalkContext
  524. {
  525. Table *ctable;
  526. };
  527. bool Table::Compact_ColumnWalk(Record *record, Field *entry, void *context_in)
  528. {
  529. ColumnField *field = static_cast<ColumnField *>(entry);
  530. ColumnWalkContext *context = (ColumnWalkContext *)context_in;
  531. Table *ctable = context->ctable;
  532. ctable->NewColumn(field->GetFieldId(), field->GetFieldName(), field->GetDataType(), FALSE);
  533. return true;
  534. }
  535. struct ColumnWalk2Context
  536. {
  537. Table *ctable;
  538. Table *thisTable;
  539. uint8_t *data;
  540. size_t data_size;
  541. int gotstuff;
  542. };
  543. bool Table::Compact_ColumnWalk2(Record *record, Field *entry, void *context_in)
  544. {
  545. ColumnField *colfield = (ColumnField *)entry;
  546. ColumnWalk2Context *context = (ColumnWalk2Context *)context_in;
  547. unsigned char fieldid = colfield->GetFieldId();
  548. //char *fieldname = colfield->GetFieldName();
  549. Field *mfield = context->thisTable->GetFieldById(fieldid);
  550. //Field *mfield = GetFieldByName(fieldname);
  551. if (mfield != NULL) {
  552. if (!context->gotstuff) {
  553. context->ctable->New();
  554. context->gotstuff = 1;
  555. }
  556. Field *cfield = context->ctable->NewFieldById(fieldid, 0);
  557. //Field *cfield = ctable->NewFieldByName(fieldname, mfield->GetPerm());
  558. size_t len = mfield->GetDataSize();
  559. if (len > context->data_size)
  560. {
  561. size_t old_data_size = context->data_size;
  562. context->data_size = len;
  563. uint8_t *new_data = (uint8_t *)realloc(context->data, context->data_size);
  564. if (new_data)
  565. {
  566. context->data = new_data;
  567. }
  568. else
  569. {
  570. new_data = (uint8_t *)malloc(context->data_size);
  571. if (new_data)
  572. {
  573. memcpy(new_data, context->data, old_data_size);
  574. free(context->data);
  575. context->data = new_data;
  576. }
  577. else
  578. {
  579. context->data_size = old_data_size;
  580. return false;
  581. }
  582. }
  583. }
  584. mfield->WriteTypedData(context->data, len);
  585. cfield->ReadTypedData(context->data, len);
  586. }
  587. return true;
  588. }
  589. bool Table::Compact_IndexWalk(Table *table, IndexField *field, void *context)
  590. {
  591. Table *ctable = (Table *)context;
  592. if (_wcsicmp(field->GetIndexName(), L"None"))
  593. ctable->AddIndexById(field->GetFieldId(), field->GetIndexName());
  594. return true;
  595. }
  596. //---------------------------------------------------------------------------
  597. void Table::Compact(int *progress) {
  598. // ok so we're gonna be cheating a bit, instead of figuring out how to safely modify all those
  599. // nifty indexes that cross reference themselves and blablabla, we're just gonna duplicate the
  600. // whole table from scratch, overwrite ourselves, and reopen the table. duh.
  601. if (!Vflock(Handle))
  602. {
  603. if (progress != NULL) *progress = 100;
  604. return;
  605. }
  606. // create a temporary table in windows temp dir
  607. wchar_t temp_table[MAX_PATH+12] = {0};
  608. wchar_t temp_index[MAX_PATH+12] = {0};
  609. wchar_t old_table[MAX_PATH+12] = {0};
  610. wchar_t old_index[MAX_PATH+12] = {0};
  611. DWORD pid=GetCurrentProcessId();
  612. StringCbPrintfW(temp_table, sizeof(temp_table), L"%s.new%08X", Name,pid);
  613. StringCbPrintfW(temp_index, sizeof(temp_index),L"%s.new%08X", IdxName,pid);
  614. StringCbPrintfW(old_table, sizeof(old_table),L"%s.old%08X", Name,pid);
  615. StringCbPrintfW(old_index, sizeof(old_index),L"%s.old%08X", IdxName,pid);
  616. // delete them, in case we crashed while packing
  617. DeleteFileW(temp_table);
  618. DeleteFileW(temp_index);
  619. DeleteFileW(old_table);
  620. DeleteFileW(old_index);
  621. // create a brand new db and a brand new table
  622. Table *ctable = db->OpenTable(temp_table, temp_index, NDE_OPEN_ALWAYS, Cached);
  623. // duplicate the columns
  624. Record *record = GetColumns();
  625. if (record != NULL)
  626. {
  627. ColumnWalkContext context;
  628. context.ctable = ctable;
  629. record->WalkFields(Compact_ColumnWalk, &context);
  630. }
  631. ctable->PostColumns();
  632. // duplicate the indexes
  633. WalkIndices(Compact_IndexWalk, (void *)ctable);
  634. // duplicate the data
  635. int reccount = GetRecordsCount();
  636. int count = 0;
  637. First();
  638. ColumnWalk2Context context;
  639. context.data_size = 65536;
  640. context.data = (uint8_t *)calloc(65536, sizeof(uint8_t));
  641. context.ctable = ctable;
  642. context.thisTable = this;
  643. while (1) {
  644. int lasterr = NumErrors();
  645. GetDefaultScanner()->GetRecordById(count, FALSE);
  646. count++;
  647. if (Eof() || count > reccount) break;
  648. if (NumErrors() > lasterr)
  649. continue;
  650. Index *idx = GetDefaultScanner()->GetIndex();
  651. int pos = idx->Get(GetDefaultScanner()->GetRecordId());
  652. if (pos == 0) continue;
  653. int pr = (int)((float)GetRecordId()/(float)reccount*100.0f);
  654. if (progress != NULL) *progress = pr;
  655. context.gotstuff = 0;
  656. if (record != NULL)
  657. record->WalkFields(Compact_ColumnWalk2, &context);
  658. if (context.gotstuff)
  659. ctable->Post();
  660. }
  661. free(context.data);
  662. // done creating temp table
  663. db->CloseTable(ctable);
  664. // reset the data structures and close underlying file handles
  665. Reset();
  666. if (MoveFileW(Name,old_table))
  667. {
  668. if (MoveFileW(IdxName,old_index))
  669. {
  670. if (!MoveFileW(temp_table,Name) || !MoveFileW(temp_index,IdxName))
  671. {
  672. // failed, try to copy back
  673. DeleteFileW(Name);
  674. DeleteFileW(IdxName);
  675. MoveFileW(old_table,Name); // restore old file
  676. MoveFileW(old_index,IdxName); // restore old file
  677. }
  678. }
  679. else
  680. {
  681. MoveFileW(old_table,Name); // restore old file
  682. }
  683. }
  684. // clean up our temp files
  685. DeleteFileW(temp_table);
  686. DeleteFileW(temp_index);
  687. DeleteFileW(old_table);
  688. DeleteFileW(old_index);
  689. if (progress != NULL) *progress = 100;
  690. // reopen our table
  691. Init();
  692. Open();
  693. }
  694. ColumnField *Table::GetColumnById(unsigned char Idx)
  695. {
  696. if (!FieldsRecord)
  697. return NULL;
  698. return (ColumnField *)FieldsRecord->GetField(Idx);
  699. }
  700. ColumnField *Table::GetColumnByName(const wchar_t *FieldName)
  701. {
  702. return FieldsRecord->GetColumnByName(FieldName);
  703. }
  704. void Table::RowCache_Delete(int position)
  705. {
  706. if (use_row_cache)
  707. {
  708. RowCache::iterator found = rowCache.find(position);
  709. if (found != rowCache.end())
  710. {
  711. if (found->second)
  712. found->second->Release();
  713. rowCache.erase(found);
  714. }
  715. }
  716. }
  717. void Table::RowCache_Remove(int position)
  718. {
  719. if (use_row_cache)
  720. {
  721. Record *&row = rowCache[position];
  722. if (row)
  723. {
  724. row->Release();
  725. }
  726. rowCache[position] = 0;
  727. }
  728. }
  729. void Table::RowCache_Add(Record *record, int position)
  730. {
  731. if (use_row_cache)
  732. {
  733. record->Retain();
  734. Record *&row = rowCache[position];
  735. if (row)
  736. {
  737. row->Release();
  738. }
  739. rowCache[position] = record;
  740. }
  741. }
  742. Record *Table::RowCache_Get(int position)
  743. {
  744. if (!use_row_cache || 0 == rowCache.count(position))
  745. return 0;
  746. Record *row = rowCache[position];
  747. if (row)
  748. row->Retain();
  749. return row;
  750. }
  751. void Table::EnableRowCache()
  752. {
  753. use_row_cache=true;
  754. }