-
Notifications
You must be signed in to change notification settings - Fork 10
/
TDL_INDE.PAS
494 lines (442 loc) · 12.3 KB
/
TDL_INDE.PAS
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
{$I tdl_dire.inc}
unit tdl_inde;
{
Indexing routines for working with the files we've copied over.
Also contains structures for working with title metadata, such as favorites.
}
interface
uses
DOS,
objects,
tdl_glob,
tdl_conf;
const
{Metadata bitflags:}
numMetaFlags=2;
m_favorite=1;
m_unpacked=2;
m_reserved=4;
type
{base class for indexes in general}
PIndex=^TIndex;
TIndex=object(TObject)
PUBLIC
entries:word;
{cached:boolean;}
data:PStream;
Constructor Init(fpath:PathStr;caching:boolean); {caching=are we allowed to}
Destructor Done; VIRTUAL;
Function verify:boolean; VIRTUAL;
end;
PFileIndex=^TFileIndex;
TFileIndex=object(TIndex)
Constructor Init(fpath:PathStr;caching:boolean);
Destructor Done; VIRTUAL;
{retrieval functions return a pointer because we might have the data
cached in memory already}
Function retrieve(i:word;var d:pFileStruct):boolean;
Function verify:boolean; VIRTUAL;
PRIVATE
tmpdata:PFileStruct;
end;
PTitleIndex=^TTitleIndex;
TTitleIndex=object(TIndex)
Constructor Init(fpath:PathStr;caching:boolean);
Destructor Done; VIRTUAL;
Function retrieve(i:word;var d:pTitleStruct):boolean;
Function retrieve1c(i:word):char; {first char of title returned}
Function verify:boolean; VIRTUAL;
PRIVATE
tmpdata:PTitleStruct;
headerCached:boolean;
headerCache:PTitleOffsets;
headerCacheSize:word;
end;
{
Not yet written:
Mapping index (contains what titles contain which search words)
}
PMetadataArray=^TMetadataArray;
TMetadataArray=array[0..maxTitles-1] of byte;
PMetadata=^TMetadata;
TMetadata=object(TObject)
PUBLIC
numEntries:word;
metaFlags:pMetadataArray;
changed:boolean;
Constructor Init(favcache:PathStr;ne:word);
Destructor Done; VIRTUAL;
(*
Procedure FileExport(fname:PathStr);
Procedure FileImport(fname:PathStr);
*)
function Flush:boolean;
function Used:word; {returns how entries are in use}
function getFlag(tid:word;flag:byte):boolean; {returns if a title has a certain flag set}
function setFlag(tid:word;flag:byte):boolean; {sets a flag for a title}
function clearFlag(tid:word;flag:byte):boolean; {sets a flag for a title}
function toggleFlag(tid:word;flag:byte):boolean; {flips a flag bit}
function countFlag(flag:byte):word; {counts how many titles have a certain set}
PRIVATE
fname:string;
fhandle:file;
end;
procedure stCheck(st:PStream);
var
Files:PFileIndex; {holds all fileIDs and their 8.4 filenames}
Titles:PTitleIndex; {holds all titleIDs and their full title strings}
implementation
uses
support,
streams;
const
{Leave these values alone. Reducing these values may cause breakage.
Increasing these values will not significantly speed anything up.}
minReadSize=sizeof(TTitleStruct); {minimum size of bytes to read from title stream}
indexMinRAM=4096; {minimum size of RAM bytes to preload a stream}
procedure stCheck(st:PStream);
begin
case st^.status of
stError :fatalerror(21,'Access error');
stInitError :fatalerror(22,'Cannot initialize stream');
stReadError :fatalerror(23,'Read beyond end of stream');
stWriteError:fatalerror(24,'Cannot expand stream');
stGetError :fatalerror(25,'Get of unregistered object type');
stPutError :fatalerror(26,'Put of unregistered object type');
end;
end;
Constructor TIndex.Init;
var
odata:PBufStream;
l:longint;
begin
if not Inherited Init then fail;
if config=nil then fail;
if not fileexists(fpath) then fail;
{register index as a stream on disk}
odata:=new(pbufstream,init(fpath,stOpenRead,indexMinRAM));
{If we have no EMS or XMS, and if stream is > than RAM,
disable caching to speed up program initialization.}
if (ems_memavail=0)
and (xms_memavail=0)
and (odata^.getsize > maxavail)
then caching:=false;
if caching then begin
{create a new RAM-based stream and copy index into it}
l:=indexMinRAM;
{if index is tiny, just load entire thing}
if l > odata^.getsize then l:=odata^.getsize;
data:=TempStream(l,odata^.getsize,ForSpeed);
if data=nil
then fatalerror(16,'Caching '+fpath+' failed during init');
{Copy disk-based stream into memory-based stream.
Do size-efficient copy if index is loaded into lower DOS RAM,
as we might not have enough low RAM for FastCopy's 64K temp buffer.}
if typeof(data^) = typeof(TMemoryStream)
then data^.copyfrom(odata^,odata^.getsize)
else FastCopy(odata^,data^,odata^.GetSize);
if data^.status <> stOK
then fatalerror(16,'Caching '+fpath+' failed during copy');
dispose(odata,done);
end else begin
{Just use the stream on disk as-is. Slow, but low RAM usage.}
data:=odata;
end;
data^.seek(0);
end;
Destructor TIndex.Done;
begin
{close our index file handle}
dispose(data,done);
Inherited Done;
end;
Function TIndex.verify:boolean;
begin
if data=nil then fatalerror(3,'Index not initialized');
end;
Constructor TFileIndex.Init;
begin
if not Inherited Init(fpath,caching) then fail;
data^.read(entries,2);
config^.NumTitles:=entries;
getmem(tmpdata,sizeof(tfilestruct));
end;
Destructor TFileIndex.done;
begin
freemem(tmpdata,sizeof(tfilestruct));
Inherited Done;
end;
Function TFileIndex.Retrieve(i:word;var d:pFileStruct):boolean;
begin
{find offset in stream header, then grab the data into temporary buffer}
data^.seek(2+longint(i)*sizeof(TFileStruct));
data^.read(tmpdata^,sizeof(TFileStruct));
d:=tmpdata;
retrieve:=(data^.status=stOK);
end;
Function TFileIndex.Verify:boolean;
const
err_lengthuneven=1;
err_wronglength=2;
err_outoforder=4;
var
calcedEntries,w,numf:word;
mangled:word;
begin
Verify:=false;
mangled:=0;
{verify length is correct}
if ((data^.GetSize-2) mod (2+12)) <> 0
then mangled:=mangled OR err_lengthuneven;
{verify Entries is correct}
calcedEntries:=((data^.GetSize-2) div (2+12));
if calcedEntries<>entries
then mangled:=mangled OR err_wronglength;
{verify all fileIDs are correct}
data^.seek(2);
for numf:=0 to Entries-1 do begin
data^.read(w,2);
if w<>numf then begin
mangled:=mangled OR err_outoforder;
break;
end;
data^.seek(data^.GetPos+12);
end;
if mangled<>0 then begin
writeln('File index errors found: ');
if (mangled AND err_lengthuneven)=err_lengthuneven
then writeln('Index wrong length; expected ',(14*entries),' but length is ',data^.GetSize-2);
if (mangled AND err_wronglength)=err_wronglength
then writeln('Expected ',entries,' entries but index appears to contain ',calcedEntries);
if (mangled AND err_outoforder)=err_outoforder
then writeln('Wrong fileID found; expected ',numf,', found ',w);
writeln('Hit enter when done reviewing');
readln;
end else begin
{if we get here, we didn't detect anything wrong}
Verify:=true;
end
end;
Constructor TTitleIndex.Init;
begin
if not Inherited Init(fpath,caching) then fail;
data^.read(entries,2);
headerCached:=false;
{We can eliminate nearly half of all seeks if we cache the header offsets in RAM}
if caching and (entries<maxCacheableTitles) then begin
headerCacheSize:=entries*4;
if maxavail>headerCacheSize then begin;
getmem(headerCache,headerCacheSize);
data^.read(headerCache^,headerCacheSize);
headerCached:=true;
end;
end;
getmem(tmpdata,minReadSize);
end;
Destructor TTitleIndex.done;
begin
freemem(tmpdata,minReadSize);
if headerCached
then freemem(headerCache,headerCacheSize);
Inherited Done;
end;
Function TTitleIndex.retrieve(i:word;var d:pTitleStruct):boolean;
{
Seeks are costly on slow I/O systems. Normally we would seek to the title
length, read it, then read only the title characters we need. To remove
one seek per retrieval, we are just going to read 256 bytes. This will
always dip into the next record, but the extra junk won't be visible
to the calling program.
}
var
l:longint;
status:integer;
b:byte;
begin
{find offset in stream header, then grab the data into temporary buffer}
if headerCached then begin
l:=headerCache^[i];
end else begin
data^.seek(2+(longint(i)*sizeof(longint)));
data^.read(l,sizeof(longint));
end;
data^.seek(l);
data^.read(tmpdata^,minReadSize);
{Our speed optimization of reading minReadSize can go past the end of the
stream. If we do this, we'll fall back to slower but 100% accurate reading.}
if data^.status<>stOK then begin
data^.status:=stOK;
data^.seek(l);
data^.read(tmpdata^.id,sizeof(tmpdata^.id)+sizeof(tmpdata^.hash));
{get exact length of title}
data^.read(b,1);
data^.seek(data^.getpos-1);
data^.read(tmpdata^.title,b+1);
end;
{$IFDEF DEBUG}
{If still getting an error, fatal abort with the reason}
if data^.status<>stOK then stCheck(data);
{$ENDIF}
d:=tmpdata;
retrieve:=(data^.status=stOK);
end;
Function TTitleIndex.retrieve1c(i:word):char;
{
Retrieves only the first character of the title. Used for title binary search.
}
const
tcofs=2+16+1;
var
l:longint;
c:char;
begin
{find offset in stream header, then grab the data into temporary buffer}
if headerCached then begin
{l:=headerCache^[i];
data^.seek(l+tcofs);}
data^.seek(headerCache^[i]+tcofs);
end else begin
data^.seek(2+(longint(i)*sizeof(longint)));
data^.read(l,sizeof(longint));
data^.seek(l+tcofs);
end;
data^.read(c,1);
retrieve1c:=c;
end;
Function TTItleIndex.Verify:boolean;
var
w,tmpid:word;
l,oldpos:longint;
begin
Verify:=false;
{check to see if we have same number of entries as filenames}
if entries <> config^.numTitles then exit;
{verify title offsets and titleIDs are correct}
data^.seek(2);
for w:=0 to config^.numTitles-1 do begin
if headerCached then begin
data^.seek(headerCache^[w]);
data^.read(tmpid,2);
end else begin
oldpos:=data^.GetPos;
data^.read(l,4);
data^.seek(l);
data^.read(tmpid,2);
data^.seek(oldpos+4);
end;
if tmpid <> w
then exit;
end;
{Verifying md5 hash is beyond the scope of slow vintage computers.}
{Verifying title strings isn't possible without end-of-string markers.}
{if we get here, we didn't detect anything wrong}
Verify:=true;
end;
Constructor TMetadata.init;
var
makeNewCache:boolean;
begin
Inherited Init;
numEntries:=ne;
fname:=favcache;
changed:=false;
getmem(metaFlags,numEntries);
fillchar(metaFlags^,numEntries,0);
if fname<>'' then begin {if '' then read-only filesystem, can't work with files}
makeNewCache:=false;
if not fileexists(fname)
then begin
makeNewCache:=true
end else begin
if sizeoffile(fname)<>ne {if size doesn't match our index files, invalidate the cache}
then makeNewCache:=true;
end;
assign(fhandle,fname);
if makeNewCache then begin
rewrite(fhandle,1);
blockwrite(fhandle,metaFlags^,numEntries);
end else begin
reset(fhandle,1);
blockread(fhandle,metaFlags^,numEntries);
end;
close(fhandle);
end;
end;
Destructor TMetadata.done;
begin
freemem(metaFlags,numEntries);
Inherited Done;
end;
function TMetadata.Used;
var
w,count:word;
begin
count:=0;
for w:=0 to numEntries-1 do
if metaFlags^[w]<>0
then inc(count);
Used:=count;
end;
function TMetadata.countFlag;
var
w,count:word;
begin
count:=0;
for w:=0 to numEntries-1 do
if (metaFlags^[w] AND flag)=flag
then inc(count);
countFlag:=count;
end;
function TMetadata.setFlag;
var
rc:boolean;
begin
rc:=false;
if tid<numEntries then begin
metaFlags^[tid] := metaFlags^[tid] OR flag;
rc:=true;
end;
setFlag:=rc;
end;
function TMetadata.clearFlag;
var
rc:boolean;
begin
rc:=false;
if tid<numEntries then begin
metaFlags^[tid] := metaFlags^[tid] AND NOT flag;
rc:=true;
end;
clearFlag:=rc;
end;
function TMetadata.toggleFlag;
var
rc:boolean;
begin
rc:=false;
if tid<numEntries then begin
metaFlags^[tid] := metaFlags^[tid] XOR flag;
rc:=true;
end;
toggleFlag:=rc;
end;
function TMetadata.getFlag;
var
rc:boolean;
begin
rc:=false;
if (metaFlags^[tid] AND flag) = flag
then rc:=true;
getFlag:=rc;
end;
function TMetadata.Flush;
begin
if (fname<>'') and changed then begin
assign(fhandle,fname);
rewrite(fhandle,1);
blockwrite(fhandle,metaFlags^,numEntries);
close(fhandle);
changed:=false;
end;
end;
end.