-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.pas
executable file
·2801 lines (2430 loc) · 74.6 KB
/
database.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
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{ $Id$
OpenXP data base unit
Copyright (C) 1991-2001 Peter Mandrella
Copyright (C) 2000-2002 OpenXP team (www.openxp.de)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
}
{$I xpdefine.inc }
{$R-}
unit database;
interface
uses
xpglobal,
datadef;
{------------------------------------------------------- Allgemeines ---}
procedure dbSetICP(p:dbIndexCProc);
procedure dbICproc(var icr:dbIndexCRec); { Default-ICP }
procedure dbAllocateFL(var flp:dbFLP; feldanz:xpWord);
procedure dbReleaseFL(var flp:dbFLP);
function dbIOerror:integer;
procedure dbSetindexcache(pages:xpWord); { 1..MaxCache }
procedure dbReleasecache;
procedure dbEnableIndexCache;
procedure dbDisableIndexCache;
procedure dbGetFrag(dbp:DB; typ:byte; var fsize,anz,gsize:longint);
{------------------------------------------------------- Datenbanken ---}
function dbHasField(const filename:string; const feldname:dbFeldStr):boolean;
procedure dbOpen(var dbp:DB; name:dbFileName; flags:xpWord);
procedure dbClose(var dbp:DB);
procedure dbFlushClose(var dbp:DB);
procedure dbTempClose(var dbp:DB);
procedure dbTempOpen(var dbp:DB);
function dbRecCount(dbp:DB):longint;
function dbPhysRecs(dbp:DB):longint;
procedure dbSetNextIntnr(dbp:DB; newnr:longint);
procedure dbSetIndexVersion(version:byte);
function dbGetIndexVersion(filename:dbFileName):byte;
{----------------------------------------------------- Satz wechseln ---}
procedure dbSkip(dbp:DB; n:longint);
procedure dbNext(dbp:DB); { skip(1) }
function dbRecNo(dbp:DB):longint;
procedure dbGo(dbp:DB; no:longint);
function dbBOF(dbp:DB):boolean;
function dbEOF(dbp:DB):boolean;
procedure dbGoTop(dbp:DB);
procedure dbGoEnd(dbp:DB);
{---------------------------------------------------- Suchen & Index ---}
procedure dbSetIndex(dbp:DB; indnr:xpWord);
function dbGetIndex(dbp:DB):xpWord;
procedure dbSeek(dbp:DB; indnr:xpWord; const key:string);
function dbFound:boolean;
function dbIntStr(i:integer16):string;
function dbLongStr(l:longint):string;
{--------------------------------------------- Daten lesen/schreiben ---}
procedure dbAppend(dbp:DB);
procedure dbDelete(dbp:DB);
function dbDeleted(dbp:DB; adr:longint):boolean;
function dbGetFeldNr(dbp:DB; const feldname: string):integer; { -1=unbekannt }
procedure dbRead (dbp:DB; const feld:dbFeldStr; var data);
procedure dbReadN (dbp:DB; feldnr:integer; var data);
procedure dbWrite (dbp:DB; const feld:dbFeldStr; const data);
procedure dbWriteN(dbp:DB; feldnr:integer; const data);
function dbReadStr(dbp:DB; const feld:dbFeldStr):string;
function dbReadStrN(dbp:DB; feldnr:integer):string;
function dbReadInt(dbp:DB; const feld:dbFeldStr):longint;
function dbReadIntN(dbp:DB; feldnr:integer):longint;
function dbReadByte(dbp:DB; const feld:dbFeldStr):byte;
function dbReadByteN(dbp:DB; feldnr:integer):byte;
function dbReadChar(dbp:DB; const feld:dbFeldStr):char;
function dbReadCharN(dbp:DB; feldnr:integer):char;
function dbXsize (dbp:DB; const feld:dbFeldStr):longint;
procedure dbReadX (dbp:DB; const feld:dbFeldStr; var size:integer; var data);
procedure dbReadXX (dbp:DB; const feld:dbFeldStr; var size:longint; const datei:string;
append:boolean);
procedure dbReadXF (dbp:DB; const feld:dbFeldStr; ofs:longint; var size:longint;
var datei:file);
procedure dbWriteX (dbp:DB; const feld:dbFeldStr; size:xpWord; var data);
procedure dbWriteXX(dbp:DB; const feld:dbFeldStr; const datei:string);
procedure dbFlush(dbp:DB);
procedure dbStopHU(dbp:DB);
procedure dbRestartHU(dbp:DB);
function dbReadUserflag(dbp:DB; nr:byte):xpWord; { nr=1..8 }
procedure dbWriteUserflag(dbp:DB; nr:byte; value:xpWord);
{ Neue Funktionen wg. AnsiString }
function dbReadNStr(dbp:DB; feldnr: integer): string;
function dbReadXStr(dbp: DB; const feld: dbFeldStr; var size: integer): string; overload;
function dbReadXStr(dbp: DB; const feld: dbFeldStr): string; overload;
procedure dbWriteNStr(dbp:DB; feldnr:integer; const s: string);
procedure dbWriteStr(dbp:DB; const feld:dbFeldStr; const s: string);
procedure dbWriteXStr (dbp:DB; const feld:dbFeldStr; size:xpWord; const s: string);
{--------------------------------------------- interne Routinen --------}
procedure OpenIndex(dbp:DB); { intern }
procedure InitDataBaseUnit;
implementation {=======================================================}
uses
sysutils,
{$IFDEF unix}
baseunix,unix,
xpunix,
{$ENDIF }
{$IFDEF debug}
debug,
{$ENDIF}
fileio, // fsplit
typeform,
inout, // only: ticker
datadef1;
procedure dbSetICP(p:dbIndexCProc);
begin
ICP:=p;
end;
{ Platz fuer feldanz Felder belegen }
procedure dbAllocateFL(var flp:dbFLP; feldanz:xpWord);
begin
getmem(flp,sizeof(flp^.felder)++sizeof(dbFeldTyp)*(feldanz+1)); { +1 wg. INT_NR }
flp^.felder:=feldanz;
end;
{ Feldliste freigeben }
procedure dbReleaseFL(var flp:dbFLP);
begin
if flp <> nil then
begin
freemem(flp,sizeof(flp^.felder)+sizeof(dbFeldTyp)*(flp^.felder+1));
flp := nil;
end;
end;
{ letzen I/O-Fehler abfragen
von dbCreate,dbOpen, dbAppendField, }
function dbIOerror:integer;
begin
dbIOerror:=lastioerror;
end;
procedure getkey(dbp:DB; indnr:xpWord; old:boolean; var key:string); forward;
procedure insertkey(dbp:DB; indnr:xpWord; const key:string); forward;
procedure deletekey(dbp:DB; indnr:xpWord; const key:string); forward;
{ Datensatz schreiben }
procedure dbFlush(dbp:DB);
var i : integer; { MK 12/99 }
k1,k2 : string;
begin
with dp(dbp)^ do begin
if flushed then exit;
{$ifdef debug} Debug.DebugLog('database','dbFlush '+fname+' - Write('+strs(recno)+')', dlTrace); {$endif}
seek(f1,hd.hdsize+(recno-1)*hd.recsize);
blockwrite(f1,recbuf^,hd.recsize);
if flindex then begin
for i:=1 to ixhd.indizes do begin
getkey(dbp,i,false,k2);
if newrec then
insertkey(dbp,i,k2)
else begin
getkey(dbp,i,true,k1);
if k1<>k2 then begin
deletekey(dbp,i,k1);
insertkey(dbp,i,k2);
end;
end;
end;
move(recbuf^,orecbuf^,hd.recsize);
end;
flushed:=true; newrec:=false;
end;
end;
procedure dbStopHU(dbp:DB);
begin
dp(dbp)^.hdupdate:=false;
end;
procedure dbRestartHU(dbp:DB);
begin
dp(dbp)^.hdupdate:=true;
writehd(dbp);
end;
{===== Satz wechseln =================================================}
procedure recRead(dbp:DB; testdel:boolean);
begin
with dp(dbp)^ do begin
{$ifdef debug} Debug.DebugLog('database','recRead '+fname+' - Read('+strs(recno)+')', dlTrace); {$endif}
seek(f1,hd.hdsize+(recno-1)*hd.recsize);
blockread(f1,recbuf^,hd.recsize);
if inoutres<>0 then
begin
(*
writeln;
writeln('<DB> interner Fehler '+strs(inoutres)+' beim Lesen aus '+fname+dbext);
writeinf(dbp);
*)
if flindex and (ioresult=100) then begin
(*
writeln(sp(79));
writeln('Indexdatei ist fehlerhaft und wird bei naechstem Programmstart neu angelegt. ');
*)
close(f1); close(fi);
erase(fi);
raise EXPDatabase.Create(1,'<DB> interner Fehler '+strs(inoutres)+' beim Lesen aus '+fname+dbext
+#13#10'Indexdatei ist fehlerhaft und wird bei naechstem Programmstart neu angelegt.');
end
else begin
if dbInterrProc<>nil then
proctype(dbInterrProc);
raise EXPDatabase.Create(1,'<DB> interner Fehler '+strs(inoutres)+' beim Lesen aus '+fname+dbext+'.');
end;
halt(1);
end;
if flindex then move(recbuf^,orecbuf^,hd.recsize);
if testdel and (recbuf^[0] and 1 <>0) then
write(#7'Fehlerhafte Indexdatei: '+FileUpperCase(fname)+dbIxExt+#7);
end;
end;
procedure findkey(dbp:DB; indnr:xpWord; searchkey:string; rec:boolean;
var data:longint); forward;
procedure AllocNode(dbp:DB; indnr:xpWord; var np:inodep); forward;
procedure FreeNode(var np:inodep); forward;
procedure ReadNode(offs:longint; var np:inodep); forward;
procedure korr_actindex(dbp:DB);
begin
with dp(dbp)^ do
if lastindex<>actindex then begin
tiefe:=0;
lastindex:=actindex;
end;
end;
{ Skip(0) bewirkt ein Neueinlesen des aktuellen Datensatzes }
{ (wird nach dbDelete verwendet) }
{ Nach positivem Skip ist nur EOF definiert, nach negativem }
{ nur BOF. }
procedure dbSkip(dbp:DB; n:longint);
var i : integer;
key : string;
l : longint;
bf : inodep;
procedure testOF;
begin
with dp(dbp)^ do begin
if dBOF then error('Skip at BOF');
if dEOF then error('Skip at EOF');
end;
end;
begin
korr_actindex(dbp);
with dp(dbp)^ do begin
{$ifdef debug} Debug.DebugLog('database','dbSkip '+fname+' - Skip('+strs(n)+')', dlTrace); {$endif}
dbFlush(dbp);
if (n<0) and dBOF then exit;
if (n>0) and dEOF then exit;
i:=0;
if flindex and (actindex<>0) and (tiefe=0) then begin
getkey(dbp,actindex,false,key);
l:=recno;
findkey(dbp,actindex,key,true,l);
if not found then
if mustfind then
error('Ha! Fataler Fehler! Satz futsch!')
else
recno:=l
else
if not mustfind then
error('Huch! berfluessiger Datensatz!');
end;
if n<0 then begin
testOF;
dEOF:=false;
while not dBOF and (i>n) do
if flindex and (actindex<>0) then begin { Skip -1 mit Index }
allocnode(dbp,actindex,bf);
readnode(vpos[tiefe],bf);
if bf^.key[vx[tiefe]-1].ref=0 then
if vx[tiefe]>1 then dec(vx[tiefe]) { 1. Fall: eins links }
else begin
repeat { 2. Fall }
dec(tiefe);
if tiefe=0 then dBOF:=true;
until dBOF or (vx[tiefe]>0);
if not dBOF then
readnode(vpos[tiefe],bf);
end
else begin
dec(vx[tiefe]);
repeat { 3. Fall: den groessten }
inc(tiefe); { Schluessl im linken }
vpos[tiefe]:=bf^.key[vx[tiefe-1]].ref; { Teilbaum suchen }
readnode(vpos[tiefe],bf);
vx[tiefe]:=bf^.anzahl;
until bf^.key[vx[tiefe]].ref=0;
end;
if not dBOF then begin
recno:=bf^.key[vx[tiefe]].data;
recRead(dbp,true);
dec(i);
end;
freenode(bf);
end
else begin { Skip -1 ohne Index }
dec(recno);
{ !F! } if recno<1 then dBOF:=true
else begin
recRead(dbp,false);
if recbuf^[0] and rflagDeleted=0 then dec(i);
end;
end;
end
else if n>0 then begin
testOF;
dBOF:=false;
while not dEOF and (i<n) do
if flindex and (actindex<>0) then begin { Skip +1 mit Index }
allocnode(dbp,actindex,bf);
readnode(vpos[tiefe],bf);
if bf^.key[vx[tiefe]].ref=0 then
if vx[tiefe]<bf^.anzahl then inc(vx[tiefe]) { 1. Fall: eins r. }
else
repeat { 2. Fall }
dec(tiefe);
if tiefe=0 then
dEOF:=true
else begin
inc(vx[tiefe]);
readnode(vpos[tiefe],bf);
end;
until dEOF or (vx[tiefe]<=bf^.anzahl)
else begin
repeat { 3. Fall: den kleinsten }
inc(tiefe); { Schluessl im rechten }
vpos[tiefe]:=bf^.key[vx[tiefe-1]].ref; { Teilbaum suchen }
readnode(vpos[tiefe],bf);
vx[tiefe]:=0;
until bf^.key[0].ref=0;
inc(vx[tiefe]);
end;
if not dEOF then begin
recno:=bf^.key[vx[tiefe]].data;
recRead(dbp,true);
inc(i);
end;
freenode(bf);
end
else begin
inc(recno); { Skip +1 ohne Index }
{ !F! } if recno>hd.recs then dEOF:=true
else begin
recRead(dbp,false);
if recbuf^[0] and rflagDeleted=0 then inc(i);
end;
end
end
else { n = 0 }
if not dEOF and not dBOF then
recRead(dbp,false);
end;
end;
procedure dbNext(dbp:DB);
begin
dbSkip(dbp,1);
end;
{ aktueller Datensatz - liefert 0 bei BOF / >recno bei EOF }
function dbRecNo(dbp:DB):longint;
begin
with dp(dbp)^ do
if dBOF then dbRecNo:=0
else if dEOF then dbRecNo:=hd.recs+1
else dbRecNo:=dp(dbp)^.recno;
end;
procedure GoRec(dbp:DB; no:longint);
begin
with dp(dbp)^ do begin
recno:=no;
recRead(dbp,false);
if recbuf^[0] and rFlagDeleted<>0 then
error('dbGo auf geloeschten Datensatz!');
dBOF:=false; dEOF:=false;
end;
end;
{ Satz positinieren - fuehrt zu Fehler, falls Satz geloescht ist! }
procedure dbGo(dbp:DB; no:longint);
begin
dbFlush(dbp);
with dp(dbp)^ do begin
{$ifdef debug} Debug.DebugLog('database','dbGo '+fname+' - Go('+strs(no)+')', dlTrace); {$endif}
if no>hd.recs then dEOF:=true
else if no<1 then dBOF:=true
else
GoRec(dbp,no);
tiefe:=0;
end;
end;
function dbBOF(dbp:DB):boolean;
begin
dbBOF:=dp(dbp)^.dBOF;
end;
function dbEOF(dbp:DB):boolean;
begin
dbEOF:=dp(dbp)^.dEOF;
end;
procedure dbGoTop(dbp:DB);
begin
with dp(dbp)^ do begin
{$ifdef debug} Debug.DebugLog('database','dbGoTop '+fname, dlTrace); {$endif}
if flindex and (actindex>0) then
dbSeek(dbp,actindex,'')
else begin
recno:=0;
dBOF:=false; dEOF:=false;
dbSkip(dbp,1);
end;
end;
end;
procedure dbGoEnd(dbp:DB);
var bf : inodep;
begin
korr_actindex(dbp);
with dp(dbp)^ do begin
{$ifdef debug} Debug.DebugLog('database','dbGoEnd '+fname, dlTrace); {$endif}
if flindex and (actindex>0) then
with index^[actindex] do begin
dbflush(dbp);
if rootrec=0 then begin
dBOF:=true; dEOF:=true; end
else begin
dBOF:=false; dEOF:=false;
allocnode(dbp,actindex,bf);
tiefe:=1;
vpos[tiefe]:=rootrec;
repeat
readnode(vpos[tiefe],bf);
vx[tiefe]:=bf^.anzahl;
inc(tiefe);
vpos[tiefe]:=bf^.key[bf^.anzahl].ref;
until vpos[tiefe]=0;
dec(tiefe);
GoRec(dbp,bf^.key[vx[tiefe]].data);
freenode(bf);
end;
end
else begin
recno:=hd.recs+1;
dBOF:=false;
dbSkip(dbp,-1);
end;
end;
end;
{===== Indizierung ==================================================}
{.$I databas1.inc} { Index-Routinen 1 }
{ Cache-Seiten allokieren }
procedure dbSetindexcache(pages:xpWord);
begin
cacheanz:=pages;
getmem(cache,pages*sizeof(cachepage));
fillchar(cache^,pages*sizeof(cachepage),0);
end;
procedure dbReleasecache;
begin
if cacheanz>0 then
freemem(cache,cacheanz*sizeof(cachepage));
cacheanz:=0;
end;
procedure dbEnableIndexCache;
begin
dbSetIndexCache(OldCacheAnz);
end;
procedure dbDisableIndexCache;
begin
OldCacheAnz := CacheAnz;
dbReleaseCache;
end;
procedure cache_read(dbp:DB; irsize:xpWord; offs:longint; var data);
var
s: TDateTime;
i,sp : integer;
TempCachePage: PCachepage;
begin
with dp(dbp)^ do
if cacheanz=0 then begin
seek(fi,offs);
blockread(fi,data,irsize);
end
else
begin
i:=cacheanz-1; // we can safely assume that cacheanz>=1 => i>=0
TempCachePage := @cache^[i]; // MUCH faster!
while ((TempCachePage.dbp<>dbp) or (TempCachePage.ofs<>offs) or not TempCachePage.used) do
begin
Dec(i);
if i<0 then break;
TempCachePage := @cache^[i];
end;
if i>=0 then
begin
Move(cache^[i].page,data,irsize);
cache^[i].lasttick:=ticker;
end
else begin
seek(fi,offs);
blockread(fi,data,irsize);
s:=maxlongint;
sp:=0;
i:=cacheanz-1; // we can safely assume that cacheanz>=1 => i>=0
TempCachePage := @cache^[i];
while TempCachePage.used do
begin
with TempCachePage^ do
if lasttick<s then
begin
s:=lasttick;
sp:=i;
end;
Dec(i);
if i<0 then break;
TempCachePage := @cache^[i];
end;
if i>=0 then sp:=i;
cache^[sp].used:=true;
cache^[sp].lasttick:=ticker;
cache^[sp].dbp:=dbp;
cache^[sp].ofs:=offs;
Move(data,cache^[sp].page,irsize);
end;
end;
end;
procedure cache_write(dbp:DB; irsize:xpWord; offs:longint; var data);
var i,sp : integer;
s : TDateTime;
begin
with dp(dbp)^ do
begin
seek(fi,offs);
blockwrite(fi,data,irsize);
if cacheanz>0 then
begin
i:=0;
sp:=0; s:=maxlongint;
while (i<cacheanz) and (not cache^[i].used or (cache^[i].dbp<>dbp) or
(cache^[i].ofs<>offs)) do begin
if not cache^[i].used then begin
sp:=i; s:=0;
end
else if cache^[i].lasttick<s then begin
sp:=i; s:=cache^[i].lasttick;
end;
inc(i);
end;
if i<cacheanz then { Seite schon im Cache vorhanden }
Move(data,cache^[i].page,irsize)
else
begin
cache^[sp].lasttick:=ticker;
cache^[sp].dbp:=dbp;
cache^[sp].ofs:=offs;
Move(data,cache^[sp].page,irsize);
i:=sp;
end;
cache^[i].used:=true;
end;
end;
end;
{ Platz fuer Index-Knoten auf Heap belegen }
procedure AllocNode(dbp:DB; indnr:xpWord; var np:inodep);
var size: xpWord;
begin
with dp(dbp)^.index^[indnr] do begin
size:=16+(nn+1)*sizeof(inodekey);
getmem(np,size);
with np^ do begin
memsize:=size;
ksize:=keysize;
irsize:=irecsize;
db_p:=dbp;
nk:=nn;
end;
end;
end;
{ Index-Knoten auf Heap freigeben }
procedure FreeNode(var np:inodep);
begin
freemem(np,np^.memsize);
end;
{ Index-Knoten einlesen }
procedure ReadNode(offs:longint; var np:inodep);
var rbuf : barrp;
wp : ^smallword absolute rbuf;
i,o: integer;
begin
with np^,dp(db_p)^ do
begin
getmem(rbuf,irsize);
filepos:=offs;
cache_read(db_p,irsize,offs,rbuf^);
{ !! Hier muss noch was getan werden, denn so klappt das unter
32 Bit einfach nicht... }
// if wp^>nk then
// error('fehlerhafte Indexseite in '+fname+dbIxExt);
anzahl:=wp^;
Move(rbuf^[2],key[0].data,8);
o:=10;
for i:=1 to anzahl do begin
Move(rbuf^[o],key[i],9+ksize);
inc(o,9+ksize);
end;
freemem(rbuf,irsize);
end;
end;
{ Index-Knoten schreiben }
procedure WriteNode(var np:inodep);
var rbuf : barrp;
wp : ^smallword absolute rbuf;
i,o : xpWord;
begin
with np^, dp(db_p)^ do begin
getmem(rbuf,irsize);
wp^:=anzahl;
Move(key[0].data,rbuf^[2],8);
o:=10;
for i:=1 to anzahl do begin
Move(key[i],rbuf^[o],9+ksize);
inc(o,9+ksize);
end;
cache_write(db_p,irsize,filepos,rbuf^);
freemem(rbuf,irsize);
end;
end;
{ einzelnen Index in Header schreiben }
procedure writeindf(dbp:DB; indnr:xpWord);
begin
with dp(dbp)^ do begin
seek(fi,32*indnr);
blockwrite(fi,index^[indnr],32);
end;
end;
{ Datensatz in Indexdatei belegen }
procedure AllocateIrec(dbp:DB; indnr:xpWord; var adr:longint);
begin
with dp(dbp)^,index^[indnr] do
if firstfree=0 then adr:=filesize(fi)
else begin
adr:=firstfree;
seek(fi,adr);
blockread(fi,firstfree,4);
writeindf(dbp,indnr);
end;
end;
{ Datensatz in Indexdatei freigeben }
procedure ReleaseIrec(dbp:DB; indnr:xpWord; adr:longint);
var l : longint;
begin
with dp(dbp)^ , index^[indnr] do begin
l:=firstfree;
firstfree:=adr;
writeindf(dbp,indnr);
seek(fi,adr);
blockwrite(fi,l,4);
end;
end;
{.$I database.inc} { B-Tree-Routinen }
{ B-Tree-Routinen }
{ Falls der gesuchte Schluessel nicht im Knoten bf^ enthalten ist, }
{ liefert searchpage (in x) die Nummer des naechstkleineren Keys. }
{ Das kann auch die Nummer 0 sein! }
procedure searchpage(bf:inodep; const searchkey:string; searchrec:longint;
var x:integer);
var r,l : integer;
ke : boolean;
TempSearchKey: String[127]; // performance critical, due to comparing ansistring with shortstring
begin
l:=0;
r:=succ(bf^.anzahl);
found:=false;
TempSearchKey := SearchKey;
while (l+1<r) and not found do begin
x:=(l+r) div 2;
ke:=bf^.key[x].keystr=Tempsearchkey;
if ke and ((searchrec=0) or (bf^.key[x].data=searchrec)) then
found:=true
else
if (ke and (searchrec<bf^.key[x].data)) or (Tempsearchkey<bf^.key[x].keystr)
then r:=x
else l:=x;
end;
if not found then
x:=l
else
if searchrec=0 then
while (x>1) and (bf^.key[x-1].keystr=TempSearchkey) do dec(x);
end;
{ Key zusammensetzen }
procedure getkey(dbp:DB; indnr:xpWord; old:boolean; var key:string);
var i,j : byte;
s : string;
r : real;
rb : barrp;
s2: ShortString;
begin
with dp(dbp)^ do
with index^[indnr] do
if feldanz and $80<>0 then
if old then begin
rb:=recbuf; recbuf:=orecbuf;
key:=ifunc(dbp);
recbuf:=rb;
end else
key:=ifunc(dbp)
else begin
key:='';
if old then
rb:=orecbuf
else
rb:=recbuf;
for i:=1 to feldanz do
with feldp^.feld[ifeldnr[i] and $fff] do
case ftyp of
dbTypeString:
begin
Move(rb^[fofs],s2,rb^[fofs]+1);
if length(s2)+1>fsize then SetLength(s2, fsize-1);
s := s2;
if ifeldnr[i] and $8000<>0 then
s:=UpperCase(s); //really ASCII???
if feldanz=i then
key:=key+s
else
key:=key+forms(s,fsize-1);
end;
dbTypeInt:
for j:=1 to fsize do
key := Key + char(rb^[fofs+fsize-j]);
dbTypeReal:
begin
Move(rb^[fofs],r,6);
str(r:20:3,s);
key:=key+s;
end;
dbTypeDatum:
for j:=1 to 4 do
key := key + char(rb^[fofs+4-j]);
end;
end;
end;
{ Index-Schluessel 'key' in Index Nr. 'indnr' von Datenbank 'dbp' einfuegen }
procedure insertkey(dbp:DB; indnr:xpWord; const key:string);
var bf : inodep;
risen : boolean;
rootsplit : boolean;
rootitem : inodekey;
newroot : inodep;
srecno : longint; { gesuchte Adresse (data) }
procedure split(bf:inodep; var item:inodekey; x:integer);
var splititem : inodekey;
splitbf : inodep;
z,n : integer;
begin
AllocNode(dbp,indnr,splitbf);
allocateIrec(dbp,indnr,splitbf^.filepos);
with dp(dbp)^.index^[indnr] do begin
n:=nn div 2;
if x<n then begin
splititem:=bf^.key[n];
for z:=n-1 downto x+1 do
bf^.key[z+1]:=bf^.key[z];
bf^.key[x+1]:=item;
end else if x>n then begin
splititem:=bf^.key[n+1];
for z:=n+2 to x do
bf^.key[z-1]:=bf^.key[z];
bf^.key[x]:=item;
end else
splititem:=item;
splitbf^.key[0].ref:=splititem.ref;
splititem.ref:=splitbf^.filepos;
item:=splititem;
for z:=n+1 to nn do
splitbf^.key[z-n]:=bf^.key[z];
bf^.anzahl:=n;
splitbf^.anzahl:=nn-n;
end;
writenode(splitbf);
freenode(splitbf);
end;
procedure update(node:longint; var rise:boolean; var risenitem:inodekey);
var x,z : integer;
begin
if node=0 then begin
rise:=true;
risenitem.keystr:=key;
risenitem.data:=srecno;
risenitem.ref:=0;
end else begin
readnode(node,bf);
searchpage(bf,key,srecno,x);
risen:=false;
update(bf^.key[x].ref,risen,risenitem);
if risen then begin
readnode(node,bf);
if bf^.anzahl<dp(dbp)^.index^[indnr].nn then
with bf^ do begin
inc(anzahl);
for z:=anzahl-1 downto x+1 do
key[z+1]:=key[z];
key[x+1]:=risenitem;
rise:=false;
end
else begin
split(bf,risenitem,x);
rise:=true;
end;
writenode(bf);
end;
end;
end;
begin { insertkey }
with dp(dbp)^ do
with index^[indnr] do begin
srecno:=recno;
allocnode(dbp,indnr,bf);
allocnode(dbp,indnr,newroot);
rootsplit:=false;
update(rootrec,rootsplit,rootitem);
if rootsplit then begin
allocateIrec(dbp,indnr,newroot^.filepos);
newroot^.anzahl:=1;
newroot^.key[0].ref:=rootrec;
newroot^.key[1]:=rootitem;
writenode(newroot);
rootrec:=newroot^.filepos;
writeindf(dbp,indnr);
end;
if indnr=actindex then tiefe:=0;
end;
freenode(newroot);
freenode(bf);
end;
{ Index-Schluessel 'key' aus Index Nr. 'indnr' von Datenbank 'dbp' loeschen }
procedure deletekey(dbp:DB; indnr:xpWord; const key:string);
var z : longint;
underflow : boolean;
bf : inodep;
delrec : longint; { Datensatz-Nr. }
n : integer;
procedure del(node:longint; var underflow:boolean);
var x,z : integer;
y : longint;
procedure compensate(precedent,node:longint; path:integer;
var underflow:boolean);
var neighbour : longint;
numbf2,numbf3 : integer;
x,z : integer;
bf1,bf2,bf3 : inodep;
begin
allocnode(dbp,indnr,bf1);
allocnode(dbp,indnr,bf2);
allocnode(dbp,indnr,bf3);
readnode(node,bf1);
readnode(precedent,bf3);
numbf3:=bf3^.anzahl;
if path<numbf3 then begin
inc(path);
neighbour:=bf3^.key[path].ref;