forked from madorin/fibplus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FIBCacheManage.pas
1332 lines (1189 loc) · 35.6 KB
/
FIBCacheManage.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
{***************************************************************}
{ FIBPlus - component library for direct access to Firebird and }
{ InterBase databases }
{ }
{ FIBPlus is based in part on the product }
{ Free IB Components, written by Gregory H. Deatz for }
{ Hoagland, Longo, Moran, Dunst & Doukas Company. }
{ mailto:[email protected] }
{ }
{ Copyright (c) 1998-2013 Devrace Ltd. }
{ Written by Serge Buzadzhy ([email protected]) }
{ }
{ ------------------------------------------------------------- }
{ FIBPlus home page: http://www.fibplus.com/ }
{ FIBPlus support : http://www.devrace.com/support/ }
{ ------------------------------------------------------------- }
{ }
{ Please see the file License.txt for full license information }
{***************************************************************}
unit FIBCacheManage;
interface
uses SysUtils,pFIBLists,Classes,FIBPlatforms;
{$I FIBPlus.inc}
type
TBlockArray =array[0..0] of PByte;
PBlockArray =^TBlockArray;
TIntArray =array[1..1] of integer;
PIntArray =^TIntArray;
TRecordPosition = record
RecordNo :integer;
InternalNo:integer;
end;
PRecordPosition=^TRecordPosition;
TRecPositions = array[1..1] of TRecordPosition;
PRecPositions =^TRecPositions;
TRecordsCache = class
private
FMapRecords :TFIBList;
FStringData :TStringCollection;
FLogStringData :TStringCollection;
FBlocks :TFIBList;
FChangeLog :TFIBList;
FChangesPositions: PRecPositions;
FOldBufRecordNumber:integer;
FOldBuffer :TDataBuffer;
FBlockSize :integer;
FRecordSize :Integer;
FInOutRecordSize:integer;
FRecordCount :integer;
FChangesCount:integer;
FStringFieldOffsets :PIntArray;
FStringFieldSize :PIntArray;
FRecInBlock :Integer;
FStrFieldCount :Word;
FSaveChangeLog :boolean;
procedure SetBlockCount(NewCount: Integer);
function GetBlockCount:integer;
procedure SetChangeLogBlockCount(NewCount: Integer);
function InternalRecordNo(const RecordNo: integer):integer;
function GetSize: integer;
function GetChangePosition(aRecordNo: integer; Force:boolean):PRecordPosition;
// procedure ReadRecordBuffer(RecordNo:integer;var Dest:PChar;Old:boolean);
function FindChangeRecNo(const ARecno:integer; var Index:integer):boolean ;
procedure InitializeMap;
function GetChangesBlockCount: integer;
public
constructor Create(aBlockRecCount,aRecordSize,aBlockReadSize,aStrCount:integer);
destructor Destroy; override;
procedure Assign(SourceCache:TRecordsCache);
function CreateNewBlock:integer;
function PrepareMemory(RecordNo:integer):TDataBuffer;
function MemoryPrepared(RecordNo:integer):boolean;
function PRecBuffer(const RecordNo:integer; Old: boolean):TDataBuffer;
function RecordPosition(const RecordNo:integer;IsInternalRecno:boolean=False):integer;
function RecordBlock(const RecordNo:integer;ForceAllocMem: boolean;IsInternalRecno:boolean=False):integer;
procedure SetStrOffset(Index:integer;Offset,DataSize:integer);
function Capacity:integer;
procedure ReadRecord(const RecordNo:integer;var Dest:TDataBuffer);
procedure ReadRecordBuffer(RecordNo:integer;var Dest:TDataBuffer;Old:boolean);
function GetFieldData(RecordNo,FieldOffSet,StrIndex:integer;Var RecordData:Pointer):Pointer;
function GetStringFieldData(RecordNo,StrIndex:integer):Pointer;
function GetNonStringFieldData(RecordNo,FieldOffSet:integer;Var RecordData:Pointer):Pointer;
procedure ShiftMapValues(Distance:integer);
procedure WriteRecord(RecordNo:integer;const Source:TDataBuffer;ForceAllocMem: boolean);
procedure WriteField(RecordNo,FieldOffSet:integer;const Data:TDataBuffer;SizeData:integer);
procedure SetStringValue(Index,RecordNo:integer; const Value:string);
procedure SetStringFromPChar(Index,RecordNo:integer; const Value:PAnsiChar; Len:integer;Triming:boolean);
procedure SaveOldBuffer (RecordNo:integer);
procedure ClearOldBuffer;
procedure SaveToChangeLog(aRecordNo:integer);
function OldBuffer(RecordNo:integer):TDataBuffer;
function pRecBuff(RecordNo:integer):TDataBuffer;
procedure RevertRecord(aRecordNo:integer);
procedure ClearLog;
procedure SwapRecords(OldRecordNo,NewRecordNo:integer);
procedure MoveRecord(OldRecno,NewRecno:integer);
procedure SaveToStream(Stream:TStream; SeekBegin :boolean;CacheSize:integer=32*1024);
procedure LoadFromStream(Stream:TStream; SeekBegin :boolean );
procedure Insert(RecordNo:integer);
procedure CancelInsert(RecordNo:integer);
function BookMarkByRecord(RecordNo:integer):integer;
function RecordByBookMark(BookMark:integer):integer;
function BookMarkValid(BookMark:integer):boolean;
property ChangesBlockCount:integer read GetChangesBlockCount write SetChangeLogBlockCount;
public
property RecordSize:Integer read FRecordSize;
property BlockCount:integer read GetBlockCount write SetBlockCount;
property RecordCount:integer read FRecordCount;
property Size:integer read GetSize;
property SaveChangeLog :boolean read FSaveChangeLog write FSaveChangeLog;
end;
EMemManagerError=class(Exception);
const
MinBlockSize=1024;
implementation
uses StdFuncs,StrUtil;
{ TRecordsCache }
{$IFNDEF D6+}
type
PInteger=^Integer;
{$ENDIF}
constructor TRecordsCache.Create(aBlockRecCount,aRecordSize,aBlockReadSize,aStrCount:integer);
var
aBlockSize:integer;
begin
FOldBufRecordNumber:=-1;
FRecordSize :=aBlockReadSize;
FInOutRecordSize :=aRecordSize;
aBlockSize :=aBlockRecCount*FRecordSize ;
if aBlockSize<MinBlockSize then
aBlockSize:=MinBlockSize+ aRecordSize-(MinBlockSize mod aRecordSize);
if (aBlockSize < FRecordSize) then
raise EMemManagerError.Create('Incompatible Sizes');
FBlocks :=nil;
FBlocks :=TFIBList.Create;
FBlockSize :=aBlockSize;
FRecordCount:=0;
FRecInBlock :=FBlockSize div FRecordSize;
FLogStringData :=nil;
if aStrCount>0 then
begin
FStrFieldCount :=aStrCount;
FStringData :=TStringCollection.Create(aStrCount);
GetMem(FStringFieldOffsets , aStrCount * SizeOf(Integer));
GetMem(FStringFieldSize , aStrCount * SizeOf(Integer));
end
else
begin
FStrFieldCount :=0;
FStringData :=nil;
FStringFieldOffsets:=nil;
end;
GetMem(FOldBuffer , FInOutRecordSize);
if FInOutRecordSize>0 then
FillChar(FOldBuffer[0],FInOutRecordSize,1); // NullValues
FChangeLog :=nil;
FChangesPositions:=nil;
FChangesCount :=0;
FSaveChangeLog:=False;
FMapRecords :=nil;
end;
destructor TRecordsCache.Destroy;
var
i:integer;
begin
for i := 0 to BlockCount - 1 do
FreeMem(FBlocks.List^[i]);
for i := 0 to ChangesBlockCount - 1 do
FreeMem(FChangeLog.List^[i]);
ReallocMem(FStringFieldOffsets,0);
ReallocMem(FChangesPositions,0);
ReallocMem(FStringFieldSize,0);
FreeMem(FOldBuffer);
FStringData .Free;
FLogStringData .Free;
FBlocks .Free;
if FChangeLog<>nil then
FChangeLog .Free;
FMapRecords.Free;
inherited;
end;
procedure TRecordsCache.Assign(SourceCache: TRecordsCache);
var
i,j:integer;
ExistingBlockCount:integer;
ExistingCheckBlockCount:integer;
p:Pointer;
begin
if FBlockSize<>SourceCache.FBlockSize then
begin
for i := 0 to BlockCount - 1 do
begin
FreeMem(FBlocks.List^[i]);
end;
for i := 0 to ChangesBlockCount - 1 do
begin
FreeMem(FChangeLog.List^[i]);
end;
FBlocks.Clear;
if FChangeLog<>nil then
FChangeLog.Clear;
end;
if BlockCount>=SourceCache.BlockCount then
begin
for i := BlockCount-1 downto SourceCache.BlockCount do
FreeMem(FBlocks.List^[i]);
FBlocks.Count:=SourceCache.BlockCount;
ExistingBlockCount:=SourceCache.BlockCount
end
else
ExistingBlockCount:=BlockCount;
if ChangesBlockCount>=SourceCache.ChangesBlockCount then
begin
for i := ChangesBlockCount-1 downto SourceCache.ChangesBlockCount do
FreeMem(FChangeLog.List^[i]);
ExistingCheckBlockCount:=SourceCache.ChangesBlockCount
end
else
ExistingCheckBlockCount:=ChangesBlockCount;
FBlockSize:=SourceCache.FBlockSize;
FBlocks.Count:=SourceCache.BlockCount;
ChangesBlockCount:=SourceCache.ChangesBlockCount;
for i := 0 to BlockCount - 1 do
begin
if i>=ExistingBlockCount then
FBlocks.List^[i]:=nil;
if Assigned(FBlocks.List^[i]) then
begin
p:=FBlocks.List^[i];
ReallocMem(p,FBlockSize);
FBlocks.List^[i]:=p;
end
else
FBlocks.List^[i]:=AllocMem(FBlockSize);
Move(SourceCache.FBlocks.List^[i]^,FBlocks.List^[i]^,FBlockSize);
end;
for i := 0 to ChangesBlockCount - 1 do
begin
if i>=ExistingCheckBlockCount then FChangeLog.List^[i]:=nil;
p:=FChangeLog.List^[i];
ReallocMem(p,FBlockSize);
FChangeLog.List^[i]:=p;
Move(SourceCache.FChangeLog.List^[i]^,FChangeLog.List^[i]^,FBlockSize);
end;
FChangesCount:=SourceCache.FChangesCount;
ReallocMem(FChangesPositions,FChangesCount* SizeOf(TRecordPosition));
for i := 1 to FChangesCount do
begin
FChangesPositions^[i].RecordNo:=SourceCache.FChangesPositions^[i].RecordNo;
FChangesPositions^[i].InternalNo:=SourceCache.FChangesPositions^[i].InternalNo;
end;
FStrFieldCount :=SourceCache.FStrFieldCount;
ReallocMem(FStringFieldOffsets , FStrFieldCount * SizeOf(Integer));
ReallocMem(FStringFieldSize , FStrFieldCount * SizeOf(Integer));
FStringData.Free;
FLogStringData .Free;
if FStrFieldCount>0 then
begin
for i := 1 to FStrFieldCount do
begin
FStringFieldOffsets^[i]:=SourceCache.FStringFieldOffsets^[i];
FStringFieldSize^[i] :=SourceCache.FStringFieldSize^[i];
end;
FStringData :=TStringCollection.Create(FStrFieldCount);
FStringData.Capacity:=SourceCache.FStringData.Capacity;
for i := 0 to Pred(SourceCache.FStringData.HighX) do
for j := 0 to Pred(SourceCache.FStringData.CountY) do
begin
if i=0 then FStringData.Add;
FStringData[i,j]:=SourceCache.FStringData[i,j];
end;
if Assigned(SourceCache.FLogStringData) then
begin
FLogStringData:=TStringCollection.Create(FStrFieldCount);
FLogStringData.Capacity:=SourceCache.FLogStringData.Capacity;
for i := 0 to Pred(SourceCache.FLogStringData.HighX) do
for j := 0 to Pred(SourceCache.FLogStringData.CountY) do
begin
if i=0 then FLogStringData.Add;
FLogStringData[i,j]:=SourceCache.FLogStringData[i,j];
end;
end;
end
else
begin
FStringData:=nil;
FLogStringData:=nil;
end;
FInOutRecordSize:=SourceCache.FInOutRecordSize;
FRecordSize :=SourceCache.FRecordSize ;
FRecInBlock :=SourceCache.FRecInBlock ;
FRecordCount :=SourceCache.FRecordCount ;
ReallocMem(FOldBuffer , FInOutRecordSize);
end;
procedure TRecordsCache.SetBlockCount(NewCount: Integer);
begin
if NewCount>=FBlocks.Capacity then
FBlocks.Capacity:=NewCount+10;
FBlocks.Count:=NewCount
end;
function TRecordsCache.GetBlockCount:integer;
begin
Result:=FBlocks.Count
end;
function TRecordsCache.CreateNewBlock:integer;
begin
SetBlockCount(BlockCount+1);
FBlocks.List^[BlockCount-1]:=AllocMem(FBlockSize);
Result:=BlockCount;
end;
function TRecordsCache.MemoryPrepared(RecordNo:integer):boolean;
begin
if Assigned(FMapRecords) then
Result := (RecordNo<FMapRecords.Count)
else
Result := RecordNo<FRecordCount;
end;
function TRecordsCache.PrepareMemory(RecordNo:integer):TDataBuffer;
var
BlockNo:integer;
IRecordNo,i,c :integer;
begin
if Assigned(FMapRecords) and (RecordNo>FMapRecords.Count) then
begin
c:=FMapRecords.Count;
for i:=c to RecordNo-1 do
FMapRecords.Add(Pointer(FRecordCount+1+i-c));
FRecordCount:=FMapRecords.Count ;
if Assigned(FStringData) then
while (FStringData.CountY<FMapRecords.Count) do
FStringData.Add
end
else
if FRecordCount<RecordNo then FRecordCount:=RecordNo;
IRecordNo:=InternalRecordNo(RecordNo);
BlockNo:=(IRecordNo-1) div FRecInBlock;
if BlockNo=BlockCount then
CreateNewBlock;
Result:=TDataBuffer(FBlocks.List^[BlockNo])+RecordPosition(IRecordNo,True);
end;
function TRecordsCache.RecordPosition(const RecordNo: integer;IsInternalRecno:boolean=False): integer;
begin
if IsInternalRecno then
Result := ((RecordNo-1) mod FRecInBlock)*FRecordSize
else
Result := ((InternalRecordNo(RecordNo)-1) mod FRecInBlock)*FRecordSize
end;
function TRecordsCache.Capacity:integer;
begin
Result:= (FBlockSize div FRecordSize)*BlockCount;
end;
function TRecordsCache.RecordBlock(const RecordNo: integer;ForceAllocMem: boolean;IsInternalRecno:boolean=False): integer;
begin
if ForceAllocMem then
begin
PrepareMemory(RecordNo);
if (RecordNo>FRecordCount) then
FRecordCount:=RecordNo;
end;
if IsInternalRecno then
Result := (RecordNo-1) div FRecInBlock
else
Result := (InternalRecordNo(RecordNo)-1) div FRecInBlock;
end;
procedure TRecordsCache.ReadRecord(const RecordNo: integer; var Dest: TDataBuffer);
begin
ReadRecordBuffer(RecordNo, Dest,False);
end;
function TRecordsCache.PRecBuffer(const RecordNo:integer; Old: boolean):TDataBuffer;
var
BlockIndex:integer;
PosInBlock:integer;
IRecordNo:integer;
begin
IRecordNo:=InternalRecordNo(RecordNo);
BlockIndex:=RecordBlock(IRecordNo,False,True);
if BlockIndex>=BlockCount then
raise EMemManagerError.Create('Can''t read Buffer.Incorrect RecordNo');
PosInBlock:=RecordPosition(IRecordNo,True);
Result :=TDataBuffer(FBlocks.List^[BlockIndex])+PosInBlock;
end;
procedure TRecordsCache.ReadRecordBuffer(RecordNo: integer;
var Dest: TDataBuffer; Old: boolean);
var
BlockIndex:integer;
PosInBlock:integer;
Cache:TDataBuffer;
i,L:integer;
IRecordNo:integer;
p:PRecordPosition;
ss:TStringCollection;
ps:PFIBByteString;
begin
IRecordNo:=InternalRecordNo(RecordNo);
if Old then
begin
p:=GetChangePosition(IRecordNo,False);
if not Assigned(p) then
begin
ReadRecordBuffer(RecordNo, Dest,False);
Exit;
end;
IRecordNo:=p^.InternalNo;
ss:=FLogStringData;
BlockIndex:=(IRecordNo-1) div FRecInBlock;
if BlockIndex>=ChangesBlockCount then
raise EMemManagerError.Create('Can''t read LogBuffer.Incorrect RecordNo');
PosInBlock:= ((IRecordNo-1) mod FRecInBlock)*FRecordSize;
end
else
begin
ss:=FStringData;
BlockIndex:=RecordBlock(IRecordNo,False,True);
if BlockIndex>=BlockCount then
raise EMemManagerError.Create('Can''t read Buffer.Incorrect RecordNo');
PosInBlock:=RecordPosition(IRecordNo,True);
end;
if Old then
Cache:=TDataBuffer(FChangeLog.List^[BlockIndex])+PosInBlock
else
Cache:=TDataBuffer(FBlocks.List^[BlockIndex])+PosInBlock;
if (FStrFieldCount=0) then
Move(Cache^,Dest^,FRecordSize)
else
begin
Move(Cache[0],Dest[0],FStringFieldOffsets^[1]);
for i := 1 to FStrFieldCount do
begin
ps:=ss.PValue[i-1,IRecordNo-1];
if ps<>nil then
begin
L:=Length(ps^);
if L>FStringFieldSize^[i] then
begin
L:=FStringFieldSize^[i];
SetLength(ps^,L);
end;
PInteger(@Dest[FStringFieldOffsets^[i]])^:=L; // LengthExp
if (L>0) then
begin
Move(ps^[1],Dest[FStringFieldOffsets^[i]+SizeOf(Integer)],L);
end;
if L<FStringFieldSize^[i] then
{$IFDEF D2009+}
Dest[FStringFieldOffsets^[i]+L+SizeOf(Integer)]:=0;
{$ELSE}
Dest[FStringFieldOffsets^[i]+L+SizeOf(Integer)]:=#0;
{$ENDIF}
end;
end;
if FRecordSize>FStringFieldOffsets^[1] then //Blobs
Move(Cache[FStringFieldOffsets^[1]],
Dest[FStringFieldOffsets^[FStrFieldCount]+SizeOf(Integer)+FStringFieldSize^[FStrFieldCount]],
FRecordSize-FStringFieldOffsets^[1]
);
end;
end;
procedure TRecordsCache.WriteRecord(RecordNo: integer;
const Source: TDataBuffer; ForceAllocMem: boolean );
var
BlockIndex:integer;
PosInBlock:integer;
Cache:TDataBuffer;
L,i:integer;
ps:PFIBByteString;
begin
BlockIndex:=RecordBlock(RecordNo,ForceAllocMem);
if BlockIndex<BlockCount then
begin
PosInBlock:=RecordPosition(RecordNo);
Cache:=TDataBuffer(FBlocks.List^[BlockIndex])+PosInBlock;
if Source=nil then
FillChar(Cache^,FRecordSize,0)
else
begin
if (FStrFieldCount=0) then
Move(Source[0],Cache[0],FRecordSize)
else
begin
Move(Source[0],Cache[0],FStringFieldOffsets^[1]);
Move(Source[FStringFieldOffsets^[FStrFieldCount]+FStringFieldSize^[FStrFieldCount]+SizeOf(Integer)],
Cache[FStringFieldOffsets^[1]], FRecordSize-FStringFieldOffsets^[1]
);
end;
if Assigned(FStringData) then
begin
RecordNo:=InternalRecordNo(RecordNo);
for i := 1 to FStrFieldCount do
begin
if FStringData.CountY<=RecordNo then
FStringData.Add;
ps:=FStringData.PValue[i-1,RecordNo-1];
L:=PInteger(@Source[FStringFieldOffsets^[i]])^;
if L<>Length(ps^) then
SetLength(ps^,L);
if L>0 then
Move(Source[FStringFieldOffsets^[i]+SizeOf(Integer)],ps^[1],L);
end;
end
end;
end
else
raise EMemManagerError.Create('Can''t write to Buffer.Incorrect RecordNo');
end;
function TRecordsCache.InternalRecordNo(const RecordNo: integer): Integer;
begin
if Assigned(FMapRecords) then
if RecordNo<=FMapRecords.Count then
Result :=Integer(FMapRecords.List^[RecordNo-1])
else
Result := -1
else
Result :=RecordNo
end;
function TRecordsCache.GetSize: integer;
begin
Result:=BlockCount*FBlockSize;
end;
procedure TRecordsCache.SetStrOffset(Index, Offset,DataSize: integer);
begin
if Assigned(FStringFieldOffsets) and Assigned(FStringData )then
if (Index<=FStringData.HighX) and (Index>0) then
begin
FStringFieldOffsets^[Index]:=Offset;
FStringFieldSize^[Index] :=DataSize;
end;
end;
procedure TRecordsCache.WriteField(RecordNo, FieldOffSet: integer;
const Data: TDataBuffer; SizeData: integer);
var
BlockIndex:integer;
PosInBlock:integer;
Cache:TDataBuffer;
begin
BlockIndex:=RecordBlock(RecordNo,False);
if BlockIndex<BlockCount then
begin
PosInBlock:=RecordPosition(RecordNo);
Cache:=TDataBuffer(FBlocks.List^[BlockIndex])+PosInBlock+FieldOffSet;
if Data=nil then
FillChar(Cache^,SizeData,0)
else
Move(Data[0],Cache^,SizeData);
end
else
raise EMemManagerError.Create('Can''t write to Field to Buffer.Incorrect RecordNo');
end;
function TRecordsCache.GetStringFieldData(RecordNo,StrIndex:integer):Pointer;
begin
// used in new Locate
if Assigned(FMapRecords) then
RecordNo :=InternalRecordNo(RecordNo+1)-1;
if RecordNo>FStringData.Capacity then
raise EMemManagerError.Create('Can''t read FieldData .Incorrect RecordNo');
Result:=FStringData.PValue[StrIndex,RecordNo];
end;
function TRecordsCache.GetFieldData( RecordNo,FieldOffSet,StrIndex:integer;
var RecordData:Pointer ):Pointer;
var
BlockIndex:integer;
PosInBlock:integer;
begin
// Used in Locate
if Assigned(FMapRecords) then
RecordNo :=InternalRecordNo(RecordNo);
BlockIndex:=(RecordNo-1) div FRecInBlock;
if BlockIndex<FBlocks.Count then
begin
PosInBlock:=(RecordNo-1-BlockIndex*FRecInBlock)*FRecordSize;
RecordData:=FBlocks.List^[BlockIndex];
Inc(PByte(RecordData),PosInBlock);
end
else
raise EMemManagerError.Create('Can''t read FieldData .Incorrect RecordNo');
if (StrIndex=-1) or (FStrFieldCount=0) then
begin
Result:=RecordData;
Inc(PByte(Result),FieldOffSet);
end
else
begin
if RecordNo>FStringData.Capacity then
raise EMemManagerError.Create('Can''t read FieldData .Incorrect RecordNo');
Result:=FStringData.PValue[StrIndex,RecordNo-1];
end;
end;
function TRecordsCache.GetNonStringFieldData(RecordNo,FieldOffSet:integer;Var RecordData:Pointer):Pointer;
var
BlockIndex:integer;
PosInBlock:integer;
begin
// used in new Locate
if Assigned(FMapRecords) then
RecordNo :=InternalRecordNo(RecordNo+1)-1;
BlockIndex:=RecordNo div FRecInBlock;
if BlockIndex<FBlocks.Count then
begin
// PosInBlock:=(RecordNo mod FRecInBlock)*FRecordSize;
PosInBlock:=(RecordNo-BlockIndex*FRecInBlock)*FRecordSize;
RecordData:=FBlocks.List^[BlockIndex];
Inc(TDataBuffer(RecordData),PosInBlock)
end
else
raise EMemManagerError.Create('Can''t read FieldData .Incorrect RecordNo');
;
Result:=RecordData;
Inc(TDataBuffer(Result),FieldOffSet);
end;
procedure TRecordsCache.SetStringValue(Index, RecordNo: integer;
const Value: string);
var
i,L:integer;
Added:Boolean;
begin
if Assigned(FStringFieldOffsets) and Assigned(FStringData)then
begin
if Assigned(FMapRecords) then
RecordNo:=InternalRecordNo(RecordNo+1)-1;
Added:=FStringData.CountY<=RecordNo;
while FStringData.CountY<=RecordNo do
FStringData.Add;
L:= Length(Value);
if L=0 then
begin
if not Added then
FStringData.SetPCharValue(Index,RecordNo,'',0)
// FStringData.Value[Index,RecordNo]:=''
end
else
if FStringFieldSize^[Index+1]<L then
begin
// For char Fields
i:=FStringFieldSize^[Index+1];
while (i>0) and (Value[i]<=' ') do Dec(i);
{ if i<FStringFieldSize^[Index+1] then
begin
DoCopy(Value,FStringData.PValue[Index,RecordNo]^,1,i)
end
else}
FStringData.Value[Index,RecordNo]:=Value;
end
else
if (Value[L]=' ') then
FStringData.Value[Index,RecordNo]:=TrimRight(Value)
else
FStringData.Value[Index,RecordNo]:=Value
end;
end;
procedure TRecordsCache.SetStringFromPChar(Index,RecordNo:integer; const Value:PAnsiChar;
Len:integer; Triming:boolean
);
begin
if Assigned(FMapRecords) then
RecordNo:=InternalRecordNo(RecordNo+1)-1;
while FStringData.CountY<=RecordNo do
FStringData.Add;
if Triming then
begin
if Len<0 then
Len:=FStringFieldSize^[Index+1];
while (Len>0) and (Value[Len-1]=' ') do
Dec(Len);
if (Len<FStringFieldSize^[Index+1]) then
begin
Value[Len]:=#0;
end;
end;
FStringData.SetPCharValue(Index,RecordNo,Value,Len)
end;
function TRecordsCache.OldBuffer(RecordNo:integer): TDataBuffer;
begin
if SaveChangeLog then
begin
if RecordNo<=FRecordCount then
ReadRecordBuffer(RecordNo,FOldBuffer,True)
else
ClearOldBuffer
end
else
begin
if FOldBufRecordNumber<>RecordNo then
if RecordNo<=FRecordCount then
SaveOldBuffer(RecordNo)
else
ClearOldBuffer
end;
Result:=FOldBuffer
end;
procedure TRecordsCache.SaveOldBuffer(RecordNo: integer);
begin
if SaveChangeLog then
SaveToChangeLog(RecordNo)
else
begin
if RecordNo<=FRecordCount then
ReadRecord(RecordNo,FOldBuffer)
else
ClearOldBuffer;
FOldBufRecordNumber:=RecordNo;
end;
end;
procedure TRecordsCache.ClearOldBuffer;
begin
FillChar(FOldBuffer[0],FInOutRecordSize,1); // NullValues
end;
procedure TRecordsCache.SaveToChangeLog(aRecordNo: integer);
var
p:PRecordPosition;
BlockIndex:integer;
PosInBlock:integer;
ChangeBlockIndex:integer;
ChangePosInBlock:integer;
i:integer;
IRecordNo:integer;
begin
// for CachedUpdates
if FChangeLog=nil then
FChangeLog:=TFIBList.Create;
IRecordNo:=InternalRecordNo(aRecordNo);
BlockIndex:=RecordBlock(IRecordNo,False,True);
PosInBlock:=RecordPosition(IRecordNo,True);
p:=GetChangePosition(IRecordNo,True);
ChangeBlockIndex:=(p^.InternalNo-1) div FRecInBlock;
ChangePosInBlock:=((p^.InternalNo-1) mod FRecInBlock)*FRecordSize;
// if ((p^.InternalNo mod FRecInBlock)=1) and (BlockIndex>=ChangesBlockCount) then
if (ChangeBlockIndex>=ChangesBlockCount) then
begin
SetChangeLogBlockCount(ChangesBlockCount+1);
FChangeLog.List^[ChangesBlockCount-1]:=AllocMem(FBlockSize);
if Assigned(FStringData) and not Assigned(FLogStringData) then
FLogStringData :=TStringCollection.Create(FStrFieldCount);
end;
Move(TDataBuffer(FBlocks.List^[BlockIndex])[PosInBlock],TDataBuffer(FChangeLog.List^[ChangeBlockIndex])[ChangePosInBlock],FRecordSize);
if Assigned(FStringData) then
begin
if p^.InternalNo>=FLogStringData.CountY then FLogStringData.Add;
for I := 0 to FStringData.HighX-1 do
begin
FLogStringData.Value[i,p^.InternalNo-1]:=FStringData.Value[i,IRecordNo-1];
end;
end;
end;
procedure TRecordsCache.SetChangeLogBlockCount(NewCount: Integer);
begin
if FChangeLog=nil then FChangeLog:=TFIBList.Create;
FChangeLog.Count:=NewCount
end;
function TRecordsCache.GetChangePosition(aRecordNo: integer; Force:boolean): PRecordPosition;
var
i:integer;
begin
if FindChangeRecNo(aRecordNo,i) then
Result:=@FChangesPositions^[i]
else
if not Force then
Result := nil
else
begin
Inc(FChangesCount);
ReallocMem(FChangesPositions , FChangesCount * SizeOf(TRecordPosition));
Move(FChangesPositions[i],FChangesPositions[i+1],SizeOf(TRecordPosition)*(FChangesCount-i));
with FChangesPositions^[i] do
begin
RecordNo :=aRecordNo;
InternalNo:=FChangesCount;
Result :=@FChangesPositions^[i];
end;
end;
end;
procedure TRecordsCache.ClearLog;
var i:integer;
begin
if FChangeLog=nil then Exit;
for i := 0 to ChangesBlockCount - 1 do
begin
FreeMem(FChangeLog[i]);
end;
FChangeLog.Clear;
ReallocMem(FChangesPositions,0);
FChangesCount :=0
end;
function TRecordsCache.FindChangeRecNo(const ARecno:integer;
var Index: integer): boolean;
var
L, H, I, C: Integer;
function Compare(I,I1:integer):integer;
begin
if I=I1 then Result:=0 else
if I>I1 then Result:=1 else Result:=-1;
end;
begin
Result := False;
L := 1;
H := FChangesCount;
while L <= H do
begin
I := (L + H) shr 1;
C:=Compare(FChangesPositions[I].RecordNo,ARecno);
if C < 0 then
L := I + 1
else
begin
H := I - 1;
if C = 0 then
Result := True;
end;
end;
Index := L;
end;
procedure TRecordsCache.RevertRecord(aRecordNo: integer);
var
Buf:TDataBuffer;
begin
Buf:=OldBuffer(aRecordNo);
WriteRecord(aRecordNo,Buf,False)
end;
function TRecordsCache.pRecBuff(RecordNo: integer): TDataBuffer;
var
BlockIndex:integer;
PosInBlock:integer;
begin
BlockIndex:=RecordBlock(RecordNo,False);
if BlockIndex<BlockCount then
begin
PosInBlock:=RecordPosition(RecordNo);
Result := TDataBuffer(FBlocks.List^[BlockIndex])+PosInBlock;
end
else
Result := nil;
end;
procedure TRecordsCache.SwapRecords(OldRecordNo, NewRecordNo: integer);
var
c:Integer;
begin
InitializeMap;
c:=Integer(FMapRecords.List^[OldRecordNo]);
FMapRecords.List^[OldRecordNo]:=FMapRecords.List^[NewRecordNo];
FMapRecords.List^[NewRecordNo]:=Pointer(c);
end;
procedure TRecordsCache.MoveRecord(OldRecno,NewRecno:integer);
var
Position:integer;
begin
if OldRecno=NewRecno then
Exit;
if NewRecno>OldRecno then
Inc(NewRecno);
InitializeMap;
Position:=Integer(FMapRecords.List^[OldRecNo]);
FMapRecords.Insert(NewRecNo,Pointer(Position));
if OldRecNo<NewRecNo then
FMapRecords.Delete(OldRecno)
else
FMapRecords.Delete(OldRecno+1)
end;
{.$DEFINE REL}
{$IFDEF REL}
procedure TRecordsCache.SaveToStream(Stream: TStream; SeekBegin: boolean;CacheSize:integer=32*1024);
const
DefDelta:integer = 32*1024 ;
// DefDelta:integer = 0 ;
var
i,j,L:integer;
P:PFIBByteString;
sp:Int64;
begin
with Stream do
begin
if SeekBegin then Seek(0,soFromBeginning);
if (DefDelta>0) and (Size-Position<BlockCount*FBlockSize) then
begin
sp:=Position;
if FBlocks.Count*FBlockSize<DefDelta then
Size:=Size+DefDelta
else
Size:=Size+FBlocks.Count*FBlockSize;
Position:=sp; // For FileStream
end;
i:=BlockCount;
WriteBuffer(i,SizeOf(Integer));
WriteBuffer(FBlockSize ,SizeOf(Integer));
WriteBuffer(FInOutRecordSize ,SizeOf(Integer));
WriteBuffer(FStrFieldCount ,SizeOf(Integer));
WriteBuffer(FSaveChangeLog ,SizeOf(Boolean));
WriteBuffer(FRecordCount ,SizeOf(Integer));
for i := 0 to BlockCount - 1 do
WriteBuffer(FBlocks.List^[i]^,FBlockSize);