Table.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /* ---------------------------------------------------------------------------
  2. Nullsoft Database Engine
  3. --------------------
  4. codename: Near Death Experience
  5. --------------------------------------------------------------------------- */
  6. /* ---------------------------------------------------------------------------
  7. Table Class
  8. Apple Mac OS X implementation
  9. --------------------------------------------------------------------------- */
  10. #include "../nde.h"
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include "../CRC.H"
  14. const char *tSign="NDETABLE";
  15. //---------------------------------------------------------------------------
  16. Table::Table(const char *TableName, const char *Idx, BOOL Create, Database *_db, BOOL _Cached)
  17. : columns_cached(false), use_row_cache(false), Scanner(this)
  18. {
  19. Handle = 0;
  20. memset(column_ids, FIELD_UNKNOWN, 255);
  21. Cached = _Cached;
  22. db = _db;
  23. AutoCreate = Create;
  24. Name = strdup(TableName);
  25. IdxName = strdup(Idx);
  26. Init();
  27. }
  28. //---------------------------------------------------------------------------
  29. void Table::Init()
  30. {
  31. numErrors = 0;
  32. Scanners = new LinkedList();
  33. // benski> cut: Handle=NULL;
  34. IdxHandle=NULL;
  35. FieldsRecord=NULL;
  36. IndexList=NULL;
  37. GLocateUpToDate = FALSE;
  38. }
  39. //---------------------------------------------------------------------------
  40. Table::~Table()
  41. {
  42. Reset();
  43. if (Handle) // Reset doesn't completely destroy Handle
  44. Vfdestroy(Handle);
  45. Handle = 0;
  46. free(Name);
  47. free(IdxName);
  48. }
  49. //---------------------------------------------------------------------------
  50. void Table::Reset()
  51. {
  52. if (IndexList) IndexList->Release();
  53. IndexList=0;
  54. if (FieldsRecord) FieldsRecord->Release();
  55. FieldsRecord=0;
  56. delete Scanners;
  57. Scanners=0;
  58. if (Handle)
  59. Vfclose(Handle); // close (but don't destroy) to keep mutex open.
  60. if (IdxHandle)
  61. Vfdestroy(IdxHandle);
  62. IdxHandle = 0;
  63. for (RowCache::iterator itr=rowCache.begin();itr!=rowCache.end();itr++)
  64. {
  65. if (itr->second)
  66. itr->second->Release();
  67. }
  68. rowCache.clear();
  69. memset(column_ids, FIELD_UNKNOWN, 255);
  70. columns_cached=false;
  71. }
  72. struct IndexNewWalkerContext
  73. {
  74. IndexNewWalkerContext(Table *_table)
  75. {
  76. N = -1;
  77. table = _table;
  78. }
  79. int N;
  80. Table *table;
  81. };
  82. bool Table::IndexNewWalker(IndexRecord *record, Field *entry, void *context_in)
  83. {
  84. IndexNewWalkerContext *context = (IndexNewWalkerContext *)context_in;
  85. IndexField *p = (IndexField *)entry;
  86. p->index = new Index(context->table->IdxHandle, p->ID, context->N++, p->Type, FALSE, 0, context->table);
  87. return true;
  88. }
  89. //---------------------------------------------------------------------------
  90. BOOL Table::Open(void)
  91. {
  92. BOOL Valid;
  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. 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. int Ptr;
  165. char test1[9]={0,};
  166. char test2[9]={0,};
  167. Vfseek(Handle, 0, SEEK_SET);
  168. Vfread(test1, strlen(__TABLE_SIGNATURE__), Handle);
  169. Vfseek(IdxHandle, 0, SEEK_SET);
  170. Vfread(test2, strlen(__INDEX_SIGNATURE__), IdxHandle);
  171. test1[8]=0;
  172. test2[8]=0;
  173. if (strcmp(test1, __TABLE_SIGNATURE__) || strcmp(test2, __INDEX_SIGNATURE__))
  174. {
  175. if (Handle) Vfdestroy(Handle);
  176. Handle = 0;
  177. if (IdxHandle) Vfdestroy(IdxHandle);
  178. IdxHandle = 0;
  179. return FALSE;
  180. }
  181. // Load default index
  182. IndexField *field;
  183. field = new IndexField(PRIMARY_INDEX, -1, -1, CFSTR("None"));
  184. field->index = new Index(IdxHandle, PRIMARY_INDEX, -1, -1, FALSE, 0, this);
  185. // Get indexes
  186. Ptr = field->index->Get(INDEX_RECORD_NUM);
  187. IndexList = new IndexRecord(Ptr, INDEX_RECORD_NUM, Handle, this);
  188. if (!IndexList)
  189. {
  190. delete field;
  191. if (Handle) Vfdestroy(Handle);
  192. Handle = 0;
  193. if (IdxHandle) Vfdestroy(IdxHandle);
  194. IdxHandle = 0;
  195. return FALSE;
  196. }
  197. // Init them
  198. IndexNewWalkerContext newContext(this);
  199. IndexList->WalkFields(IndexNewWalker, &newContext);
  200. // Add default in case its not there (if it is it won't be added by addfield)
  201. IndexList->AddField(field);
  202. // Get the default index (whether loaded or preloaded)
  203. Scanner::index = ((IndexField*)IndexList->GetField(PRIMARY_INDEX))->index;
  204. // If it's different from preloaded, delete preloaded
  205. if (field->index != Scanner::index)
  206. {
  207. delete field;
  208. field=0;
  209. }
  210. // Set up colaboration
  211. IndexList->BuildCollaboration();
  212. // Get columns
  213. Ptr = Scanner::index->Get(FIELDS_RECORD_NUM);
  214. FieldsRecord = new Record(Ptr, FIELDS_RECORD_NUM, Handle, this);
  215. if (!FieldsRecord)
  216. {
  217. IndexList->Release();
  218. IndexList=0;
  219. if (Handle) Vfdestroy(Handle);
  220. Handle = 0;
  221. if (IdxHandle) Vfdestroy(IdxHandle);
  222. IdxHandle = 0;
  223. return FALSE;
  224. }
  225. // update the column cache
  226. FieldsRecord->WalkFields(BuildColumnCache, this);
  227. columns_cached=true;
  228. }
  229. #if 0 // TODO
  230. if (Valid && !justcreated)
  231. {
  232. if (IndexList->NeedFix())
  233. Compact();
  234. }
  235. #endif
  236. if (Valid) First();
  237. if (Handle)
  238. Vfunlock(Handle);
  239. return Valid;
  240. }
  241. //---------------------------------------------------------------------------
  242. void Table::Close(void)
  243. {
  244. int v=0;
  245. if (Handle && IndexList && Vflock(Handle, 0))
  246. {
  247. IndexList->WalkFields(IndexWriteWalker, 0);
  248. }
  249. delete Scanners;
  250. Scanners = NULL;
  251. Vsync(Handle);
  252. if (IdxHandle)
  253. {
  254. Vfdestroy(IdxHandle);
  255. IdxHandle = NULL;
  256. v |= 2;
  257. }
  258. if (Handle)
  259. {
  260. Vfdestroy(Handle);
  261. Handle = NULL;
  262. v |= 1;
  263. }
  264. if (v != 3)
  265. return;
  266. }
  267. bool Table::IndexWriteWalker(IndexRecord *record, Field *entry, void *context)
  268. {
  269. IndexField *field = (IndexField *)entry;
  270. field->index->WriteIndex();
  271. return true;
  272. }
  273. //---------------------------------------------------------------------------
  274. void Table::Sync(void)
  275. {
  276. if (!Vflock(Handle))
  277. return;
  278. if (IndexList)
  279. IndexList->WalkFields(IndexWriteWalker, 0);
  280. int err=0;
  281. if (!err && Handle) err|=Vsync(Handle);
  282. if (!err && IdxHandle) err|=Vsync(IdxHandle);
  283. Vfunlock(Handle);
  284. }
  285. //---------------------------------------------------------------------------
  286. ColumnField *Table::NewColumn(unsigned char FieldID, CFStringRef FieldName, unsigned char FieldType, BOOL indexUnique)
  287. {
  288. columns_cached=false; // if they start writing new columns, kill the columns cache until they PostColumns()
  289. ColumnField *f = GetColumnById(FieldID);
  290. if (f) {
  291. int t = f->GetDataType();
  292. if (t != FieldType) {
  293. if (CompatibleFields(t, FieldType))
  294. {
  295. f->SetDataType(FieldType);
  296. goto aok;
  297. }
  298. }
  299. return NULL;
  300. }
  301. aok:
  302. if (GetColumnByName(FieldName))
  303. return NULL;
  304. ColumnField *field = new ColumnField(FieldID, FieldName, FieldType, this);
  305. column_ids[FieldID]=FieldType;
  306. FieldsRecord->AddField(field);
  307. return field;
  308. }
  309. void Table::SetFieldSearchableById(unsigned char field_id, bool searchable)
  310. {
  311. ColumnField *column = GetColumnById(field_id);
  312. if (column)
  313. column->SetSearchable(searchable);
  314. if (searchable)
  315. {
  316. search_fields.insert(field_id);
  317. }
  318. else
  319. {
  320. search_fields.erase(field_id);
  321. }
  322. }
  323. //---------------------------------------------------------------------------
  324. bool Table::BuildColumnCache(Record *record, Field *entry, void *context)
  325. {
  326. Table *table = (Table *)context;
  327. ColumnField *column = (ColumnField *)entry;
  328. unsigned char field_id=column->GetFieldId();
  329. table->column_ids[field_id] = column->GetDataType();
  330. if (column->IsSearchableField())
  331. {
  332. table->search_fields.insert(field_id);
  333. }
  334. else
  335. {
  336. table->search_fields.erase(field_id);
  337. }
  338. return true;
  339. }
  340. //---------------------------------------------------------------------------
  341. void Table::PostColumns(void)
  342. {
  343. FieldsRecord->WriteFields(this, FIELDS_RECORD_NUM);
  344. memset(column_ids, FIELD_UNKNOWN, 255);
  345. FieldsRecord->WalkFields(BuildColumnCache, this);
  346. columns_cached=true;
  347. }
  348. unsigned char Table::GetColumnType(unsigned char Id)
  349. {
  350. if (columns_cached)
  351. {
  352. return column_ids[Id];
  353. }
  354. else
  355. {
  356. return GetColumnById(Id)->GetDataType();
  357. }
  358. }
  359. //---------------------------------------------------------------------------
  360. IndexField *Table::GetIndexByName(CFStringRef name)
  361. {
  362. if (!IndexList)
  363. return NULL;
  364. return IndexList->GetIndexByName(name);
  365. }
  366. //---------------------------------------------------------------------------
  367. IndexField *Table::GetIndexById(unsigned char Id)
  368. {
  369. if (!IndexList)
  370. return NULL;
  371. return (IndexField *)IndexList->GetField(Id);
  372. }
  373. //---------------------------------------------------------------------------
  374. void Table::AddIndexByName(CFStringRef name, CFStringRef desc)
  375. {
  376. ColumnField *header = GetColumnByName(name);
  377. if (header)
  378. {
  379. unsigned char Idx = header->ID;
  380. AddIndexById(Idx, desc);
  381. }
  382. }
  383. //---------------------------------------------------------------------------
  384. void Table::AddIndexById(unsigned char Id, CFStringRef desc)
  385. {
  386. if (GetIndexById(Id)) return;
  387. ColumnField *col = GetColumnById(Id);
  388. if (!col)
  389. return;
  390. IndexField *newindex = new IndexField(Id, IndexList->GetColumnCount(), col->GetDataType(), desc);
  391. newindex->index = new Index(IdxHandle, Id, IndexList->GetColumnCount(), col->GetDataType(), TRUE, Scanner::index->NEntries, this);
  392. IndexList->AddField(newindex);
  393. IndexField *previous = (IndexField *)newindex->prev;
  394. previous->index->Colaborate(newindex);
  395. IndexField *primary_index = (IndexField *)IndexList->GetField(PRIMARY_INDEX);
  396. newindex->index->Colaborate(primary_index);
  397. previous->index->Propagate();
  398. IndexList->WriteFields(this);
  399. }
  400. //---------------------------------------------------------------------------
  401. BOOL Table::CheckIndexing(void)
  402. {
  403. if (IndexList->GetColumnCount() == 0) return TRUE;
  404. for (int i=0;i<Scanner::index->NEntries;i++)
  405. {
  406. if (!IndexList->CheckIndexing(i))
  407. return FALSE;
  408. }
  409. return TRUE;
  410. }
  411. struct IndexWalkerThunkContext
  412. {
  413. void *context;
  414. Table *_this;
  415. Table::IndexWalker callback;
  416. };
  417. bool Table::IndexWalkerThunk(IndexRecord *record, Field *entry, void *context_in)
  418. {
  419. IndexWalkerThunkContext *context = (IndexWalkerThunkContext *)context_in;
  420. return context->callback(context->_this, (IndexField *)entry, context->context);
  421. }
  422. //---------------------------------------------------------------------------
  423. void Table::WalkIndices(IndexWalker callback, void *context)
  424. {
  425. if (IndexList && callback)
  426. {
  427. IndexWalkerThunkContext walkerContext = { context, this, callback };
  428. IndexList->WalkFields(IndexWalkerThunk, &walkerContext);
  429. }
  430. }
  431. //---------------------------------------------------------------------------
  432. void Table::DropIndex(IndexField *Ptr)
  433. {
  434. if (!Ptr || Ptr->Type != FIELD_INDEX) return;
  435. if (Scanner::index == Ptr->index)
  436. {
  437. Scanner::index = ((IndexField*)IndexList->GetField(PRIMARY_INDEX))->index;
  438. IndexList->BuildCollaboration();
  439. }
  440. IndexList->RemoveField(Ptr);
  441. if (Scanner::index->SecIndex == Ptr)
  442. Scanner::index->SecIndex = 0;
  443. IndexList->SetModified(TRUE);
  444. IndexList->WriteFields(this);
  445. }
  446. //---------------------------------------------------------------------------
  447. void Table::DropIndexByName(CFStringRef desc)
  448. {
  449. IndexField *indx = GetIndexByName(desc);
  450. if (CFStringCompare(desc, CFSTR("None"), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
  451. return;
  452. if (indx)
  453. DropIndex(indx);
  454. }
  455. //---------------------------------------------------------------------------
  456. void Table::DropIndexById(unsigned char Id)
  457. {
  458. if (!IndexList)
  459. return;
  460. if (Id == (unsigned char)PRIMARY_INDEX) return;
  461. IndexField *indx=(IndexField *)IndexList->GetField(Id);
  462. if (indx)
  463. DropIndex(indx);
  464. }
  465. //---------------------------------------------------------------------------
  466. BOOL Table::LocateByIdEx(int Id, int From, Field *field, int comp_mode)
  467. {
  468. return Scanner::LocateByIdEx(Id, From, field, NULL, comp_mode);
  469. }
  470. //---------------------------------------------------------------------------
  471. Record *Table::GetColumns(void)
  472. {
  473. if (!FieldsRecord)
  474. return NULL;
  475. return FieldsRecord;
  476. }
  477. //---------------------------------------------------------------------------
  478. Scanner *Table::NewScanner()
  479. {
  480. Scanner *s = new Scanner(this);
  481. /*if (Scanners->GetNElements() > 0)*/
  482. s->index = Scanner::index;
  483. Scanners->AddEntry(s, TRUE);
  484. return s;
  485. }
  486. //---------------------------------------------------------------------------
  487. Scanner *Table::GetDefaultScanner(void)
  488. {
  489. return this;
  490. }
  491. //---------------------------------------------------------------------------
  492. void Table::DeleteScanner(Scanner *scan)
  493. {
  494. if (!scan) return;
  495. Scanners->RemoveEntry(scan);
  496. }
  497. //---------------------------------------------------------------------------
  498. void Table::IndexModified(void)
  499. {
  500. Scanner *s = (Scanner *)Scanners->GetHead();
  501. while (s)
  502. {
  503. s->IndexModified();
  504. s = (Scanner *)s->GetNext();
  505. }
  506. }
  507. //---------------------------------------------------------------------------
  508. void Table::SetGlobalLocateUpToDate(BOOL is) {
  509. GLocateUpToDate = is;
  510. }
  511. ColumnField *Table::GetColumnById(unsigned char Idx)
  512. {
  513. if (!FieldsRecord)
  514. return NULL;
  515. return (ColumnField *)FieldsRecord->GetField(Idx);
  516. }
  517. ColumnField *Table::GetColumnByName(CFStringRef FieldName)
  518. {
  519. return FieldsRecord->GetColumnByName(FieldName);
  520. }
  521. void Table::RowCache_Delete(int position)
  522. {
  523. if (use_row_cache)
  524. {
  525. RowCache::iterator found = rowCache.find(position);
  526. if (found != rowCache.end())
  527. {
  528. if (found->second)
  529. found->second->Release();
  530. rowCache.erase(found);
  531. }
  532. }
  533. }
  534. void Table::RowCache_Remove(int position)
  535. {
  536. if (use_row_cache)
  537. {
  538. Record *&row = rowCache[position];
  539. if (row)
  540. {
  541. row->Release();
  542. }
  543. row = 0;
  544. }
  545. }
  546. void Table::RowCache_Add(Record *record, int position)
  547. {
  548. if (use_row_cache)
  549. {
  550. record->Retain();
  551. Record *&row = rowCache[position];
  552. if (row)
  553. {
  554. row->Release();
  555. }
  556. row = record;
  557. }
  558. }
  559. Record *Table::RowCache_Get(int position)
  560. {
  561. if (!use_row_cache)
  562. return 0;
  563. Record *row = rowCache[position];
  564. if (row)
  565. row->Retain();
  566. return row;
  567. }
  568. void Table::EnableRowCache()
  569. {
  570. use_row_cache=true;
  571. }