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