forked from gabr42/OmniThreadLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OtlDataManager.pas
1237 lines (1116 loc) · 44.3 KB
/
OtlDataManager.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
///<summary>Data consumer with parallelization support (backend for the OtlParallel unit).</summary>
///<author>Primoz Gabrijelcic</author>
///<license>
///This software is distributed under the BSD license.
///
///Copyright (c) 2019, Primoz Gabrijelcic
///All rights reserved.
///
///Redistribution and use in source and binary forms, with or without modification,
///are permitted provided that the following conditions are met:
///- Redistributions of source code must retain the above copyright notice, this
/// list of conditions and the following disclaimer.
///- Redistributions in binary form must reproduce the above copyright notice,
/// this list of conditions and the following disclaimer in the documentation
/// and/or other materials provided with the distribution.
///- The name of the Primoz Gabrijelcic may not be used to endorse or promote
/// products derived from this software without specific prior written permission.
///
///THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
///ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
///WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
///DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
///ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
///(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
///LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
///ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
///(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
///SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
///</license>
///<remarks><para>
/// Home : http://www.omnithreadlibrary.com
/// Support : https://en.delphipraxis.net/forum/32-omnithreadlibrary/
/// Author : Primoz Gabrijelcic
/// E-Mail : [email protected]
/// Blog : http://thedelphigeek.com
/// Contributors : GJ, Lee_Nover, Sean B. Durkin
///
/// Creation date : 2010-04-13
/// Last modification : 2017-10-02
/// Version : 1.02a
///</para><para>
/// History:
/// 1.02a: 2017-10-02
/// - Fixed bad implementation of TOmniEnumeratorProvider.GetPackage and
/// TOmniValueEnumeratorProvider.GetPackage.
/// - Increased max package size in TOmniValueEnumeratorDataPackage from
/// 1024 to 10240.
/// 1.02: 2017-09-27
/// - TOmniEnumeratorProvider and TOmniValueEnumeratorProvider support
/// dmoPreserveOrder option.
/// 1.01a: 2016-06-30
/// - TOmniValueEnumeratorDataPackage.Add was incrementing refcount for refcounted
/// data one time too many. This caused memory leaks when iterating over
/// interfaced types in Parallel.For.
/// 1.01: 2015-10-04
/// - Imported mobile support by [Sean].
/// 1.0a: 2013-10-19
/// - Don't crash if the source is empty.
/// 1.0: 2010-12-10
/// - Created.
///</para></remarks>
unit OtlDataManager;
{$I OtlOptions.inc}
interface
uses
OtlCommon,
OtlContainers,
OtlCollections;
type
///<summary>Source provider capabilities.</summary>
TOmniSourceProviderCapability = (
spcCountable, // source provider that knows how much data it holds
spcFast, // source provider operations are O(1)
spcDataLimit // data package can only hold limited amount of data
);
TOmniSourceProviderCapabilities = set of TOmniSourceProviderCapability;
///<summary>Wrapper around a (type specific) data package. All methods can and will be
/// called from multiple threads at the same time!</summary>
TOmniDataPackage = class abstract
public
function GetNext(var value: TOmniValue): boolean; overload; virtual; abstract;
function GetNext(var position: int64; var value: TOmniValue): boolean; overload; virtual; abstract;
function Split(package: TOmniDataPackage): boolean; virtual; abstract;
end; { TOmniDataPackage }
///<summary>Output data buffer.</summary>
TOmniOutputBuffer = class abstract
public
procedure Submit(position: int64; const data: TOmniValue); virtual; abstract;
end; { TOmniOutputBuffer }
///<summary>A data package queue between a single worker and shared data manager.</summary>
TOmniLocalQueue = class abstract
public
procedure AssociateBuffer(buffer: TOmniOutputBuffer); virtual; abstract;
function GetNext(var value: TOmniValue): boolean; overload; virtual; abstract;
function GetNext(var position: int64; var value: TOmniValue): boolean; overload; virtual; abstract;
function Split(package: TOmniDataPackage): boolean; virtual; abstract;
end; { TOmniLocalQueue }
///<summary>Wrapper around the data source. All methods can and will be called from
/// multiple threads at the same time!</summary>
TOmniSourceProvider = class abstract
public
function Count: int64; virtual; abstract;
function CreateDataPackage: TOmniDataPackage; virtual; abstract;
function GetCapabilities: TOmniSourceProviderCapabilities; virtual; abstract;
function GetPackage(dataCount: integer; package: TOmniDataPackage): boolean; virtual; abstract;
function GetPackageSizeLimit: integer; virtual; abstract;
end; { TOmniSourceProvider }
TOmniDataManagerOption = (dmoPreserveOrder);
TOmniDataManagerOptions = set of TOmniDataManagerOption;
///<summary>Data manager. All methods can and will be called from multiple threads
/// at the same time!</summary>
TOmniDataManager = class abstract
public
function CreateLocalQueue: TOmniLocalQueue; virtual; abstract;
function AllocateOutputBuffer: TOmniOutputBuffer; virtual; abstract;
function GetNext(package: TOmniDataPackage): boolean; virtual; abstract;
procedure ReleaseOutputBuffer(buffer: TOmniOutputBuffer); virtual; abstract;
procedure SetOutput(const queue: IOmniBlockingCollection); overload; virtual; abstract;
end; { TOmniDataManager }
function CreateSourceProvider(low, high: integer; step: integer = 1): TOmniSourceProvider; overload;
function CreateSourceProvider(enumerator: TOmniValueEnumerator): TOmniSourceProvider; overload;
function CreateSourceProvider(enumerator: IOmniValueEnumerator): TOmniSourceProvider; overload;
function CreateSourceProvider(enumerator: IEnumerator): TOmniSourceProvider; overload;
function CreateDataManager(sourceProvider: TOmniSourceProvider; numWorkers: integer;
options: TOmniDataManagerOptions): TOmniDataManager;
implementation
uses
{$IFDEF MSWINDOWS}
Windows,
Contnrs,
DSiWin32,
{$ELSE}
System.Diagnostics,
{$ENDIF}
GpLists,
SysUtils,
Classes,
{$IFDEF OTL_HasSystemTypes}
System.Types,
{$ENDIF}
OtlSync;
const
CNumBuffersInSet = 2;
type
TOmniPositionRange = record
First: int64;
Last : int64;
constructor Create(aFirst, aLast: int64);
class function Empty: TOmniPositionRange; static;
end; { TOmniPositionRange }
///<summary>Base class for all data package classes.</summary>
TOmniDataPackageBase = class abstract(TOmniDataPackage)
private
dpbGeneration: integer;
dpbRange : TOmniPositionRange;
dpbQueue : TOmniLocalQueue;
public
procedure NextGeneration; inline;
property Generation: integer read dpbGeneration;
property Range: TOmniPositionRange read dpbRange write dpbRange;
property Queue: TOmniLocalQueue read dpbQueue write dpbQueue;
end; { TOmniDataPackageBase }
///<summary>Integer range data package.</summary>
TOmniIntegerDataPackage = class(TOmniDataPackageBase)
strict private
idpHigh : int64;
idpHighSign: int64;
idpLow : TOmniAlignedInt64;
idpPosition: integer;
idpSign : integer;
idpStep : integer;
idpStepAbs : integer;
public
function GetNext(var value: TOmniValue): boolean; overload; override;
function GetNext(var position: int64; var value: TOmniValue): boolean; overload; override;
function HasData: boolean;
procedure Initialize(low, high, step: integer; newRange: TOmniPositionRange);
function Split(package: TOmniDataPackage): boolean; override;
end; { TOmniIntegerDataPackage }
///<summary>Data package storing a list of TOmniValues.</summary>
TOmniValueEnumeratorDataPackage = class(TOmniDataPackageBase)
strict private
const CMaxValueEnumeratorDataPackageSize = 10240; // pretty arbitrary, should do some performance tests
strict private
vedpApproxCount: TOmniAlignedInt32;
vedpDataQueue : TOmniBaseQueue; //TOmniBaseBoundedQueue;
vedpPosition : int64;
public
constructor Create;
destructor Destroy; override;
class function GetPackageSizeLimit: integer;
procedure Add(const value: TOmniValue);
function GetNext(var value: TOmniValue): boolean; overload; override;
function GetNext(var position: int64; var value: TOmniValue): boolean; overload; override;
function Prepare(dataCount: integer; position: int64): integer;
function Split(package: TOmniDataPackage): boolean; override;
end; { TOmniValueEnumeratorDataPackage }
TOmniSourceProviderBase = class(TOmniSourceProvider)
strict private
spbStorePositions: boolean;
public
property StorePositions: boolean read spbStorePositions write spbStorePositions;
end; { TOmniSourceProviderBase }
///<summary>Integer range source provider.</summary>
TOmniIntegerRangeProvider = class(TOmniSourceProviderBase)
strict private
irpCount : TOmniAlignedInt32;
irpHigh : integer;
irpLock : TOmniCS;
irpLow : integer;
irpPosition: integer;
irpStep : integer;
strict protected
function CalcCount(low, high, step: integer): integer; inline;
public
constructor Create(low, high, step: integer);
function Count: int64; override;
function CreateDataPackage: TOmniDataPackage; override;
function GetCapabilities: TOmniSourceProviderCapabilities; override;
function GetPackage(dataCount: integer; package: TOmniDataPackage): boolean; override;
function GetPackageSizeLimit: integer; override;
end; { TOmniIntegerRangeProvider }
///<summary>TOmniValue source provider supporting mutable collections and parallel enumerators.</summary>
TOmniValueEnumeratorProvider = class(TOmniSourceProviderBase)
strict protected
vepEnumerator: IOmniValueEnumerator;
vepEnumLock : TOmniCS; // used only when StorePositions is used
vepPosition : int64;
public
constructor Create(enumerator: IOmniValueEnumerator);
function Count: int64; override;
function CreateDataPackage: TOmniDataPackage; override;
function GetCapabilities: TOmniSourceProviderCapabilities; override;
function GetPackage(dataCount: integer; package: TOmniDataPackage): boolean; override;
function GetPackageSizeLimit: integer; override;
end; { TOmniValueEnumeratorProvider }
TOmniEnumeratorProvider = class(TOmniSourceProviderBase)
strict private
epEnumeratorIntf: IEnumerator;
epEnumeratorObj : TOmniValueEnumerator;
epEnumLock : TOmniCS;
epPosition : int64;
public
constructor Create(enumerator: IEnumerator); overload;
constructor Create(enumerator: TOmniValueEnumerator); overload;
function Count: int64; override;
function CreateDataPackage: TOmniDataPackage; override;
function GetCapabilities: TOmniSourceProviderCapabilities; override;
function GetPackage(dataCount: integer; package: TOmniDataPackage): boolean; override;
function GetPackageSizeLimit: integer; override;
property EnumeratorIntf: IEnumerator read epEnumeratorIntf;
property EnumeratorObj: TOmniValueEnumerator read epEnumeratorObj;
end; { TOmniEnumeratorProvider }
TOmniBaseDataManager = class;
///<summary>Output buffer implementation.</summary>
TOmniOutputBufferImpl = class(TOmniOutputBuffer)
strict private
obiBuffer : TOmniBlockingCollection;
obiDataManager_ref: TOmniBaseDataManager;
obiEmptyEvent : TOmniTransitionEvent;
obiFull : boolean;
obiHasData : boolean;
obiNextPosition : int64;
obiOutput : IOmniBlockingCollection;
obiRange : TOmniPositionRange;
strict protected
procedure SetRange(range: TOmniPositionRange);
public
constructor Create(owner: TOmniBaseDataManager; output: IOmniBlockingCollection;
const AShareLock: IOmniCriticalSection = nil);
destructor Destroy; override;
procedure CopyToOutput;
procedure MarkFull;
procedure Submit(position: int64; const data: TOmniValue); override;
property EmptyEvent: TOmniTransitionEvent read obiEmptyEvent;
property IsFull: boolean read obiFull;
property Range: TOmniPositionRange read obiRange write SetRange;
end; { TOmniOutputBufferImpl }
TOmniOutputBufferSet = class(TOmniOutputBuffer)
strict private
obsActiveBuffer_ref: TOmniOutputBufferImpl;
obsShareLock : IOmniCriticalSection;
obsBuffers : array [1..CNumBuffersInSet] of TOmniOutputBufferImpl;
{$IFDEF MSWINDOWS}
obsActiveIndex : integer;
obsWaitHandles : array[1..CNumBuffersInSet] of THandle;
{$ELSE}
obsWaitEvents : TWaitFor;
{$ENDIF}
public
constructor Create(owner: TOmniBaseDataManager; output: IOmniBlockingCollection);
destructor Destroy; override;
procedure ActivateBuffer;
procedure Submit(position: int64; const data: TOmniValue); override;
property ActiveBuffer: TOmniOutputBufferImpl read obsActiveBuffer_ref;
end; { TOmniOutputBufferSet }
///<summary>Local queue implementation.</summary>
TOmniLocalQueueImpl = class(TOmniLocalQueue)
strict private
lqiBufferSet : TOmniOutputBufferSet;
lqiDataManager_ref: TOmniBaseDataManager;
lqiDataPackage : TOmniDataPackageBase;
public
constructor Create(owner: TOmniBaseDataManager);
destructor Destroy; override;
procedure AssociateBuffer(buffer: TOmniOutputBuffer); override;
function GetNext(var value: TOmniValue): boolean; overload; override;
function GetNext(var position: int64; var value: TOmniValue): boolean; overload; override;
function Split(package: TOmniDataPackage): boolean; override;
end; { TOmniLocalQueueImpl }
///<summary>Base data manager class.</summary>
TOmniBaseDataManager = class abstract (TOmniDataManager)
strict private
const CMaxPreserveOrderPackageSize = 10240; // pretty arbitrary, should do some performance tests
strict private
dmBufferRangeList : TGpInt64ObjectList;
dmBufferRangeLock : TOmniCS;
dmNextPosition : int64;
dmNumWorkers : integer;
dmOptions : TOmniDataManagerOptions;
dmOutputIntf : IOmniBlockingCollection;
dmPacketSizes : array of integer;
dmQueueList : TObjectList;
dmQueueLock : TOmniCS;
dmSourceProvider_ref: TOmniSourceProviderBase;
dmStealIdx : integer;
dmUnusedBuffers : TObjectList;
dmUnusedBuffersLock : TOmniCS;
strict protected
function GetBufferList(idxBuffer: integer): TOmniOutputBufferImpl; inline;
function GetSourceProvider: TOmniSourceProvider;
procedure InitializePacketSizes;
property BufferList[idxBuffer: integer]: TOmniOutputBufferImpl read GetBufferList;
public
constructor Create(sourceProvider: TOmniSourceProvider; numWorkers: integer;
options: TOmniDataManagerOptions);
destructor Destroy; override;
function AllocateOutputBuffer: TOmniOutputBuffer; override;
function CreateLocalQueue: TOmniLocalQueue; override;
function GetDataCountForGeneration(generation: integer): integer;
function GetNext(package: TOmniDataPackage): boolean; override;
function GetNextFromProvider(package: TOmniDataPackage;
generation: integer): boolean; virtual; abstract;
procedure LocalQueueDestroyed(queue: TOmniLocalQueue);
procedure NotifyBufferFull(buffer: TOmniOutputBufferImpl);
procedure NotifyBufferRangeChanged(buffer: TOmniOutputBufferImpl);
procedure ReleaseOutputBuffer(buffer: TOmniOutputBuffer); override;
procedure SetOutput(const queue: IOmniBlockingCollection); overload; override;
function StealPackage(package: TOmniDataPackage): boolean;
property SourceProvider: TOmniSourceProvider read GetSourceProvider;
end; { TOmniBaseDataManager }
///<summary>Data manager for countable data.</summary>
TOmniCountableDataManager = class(TOmniBaseDataManager)
public
function GetNextFromProvider(package: TOmniDataPackage;
generation: integer): boolean; override;
end; { TOmniCountableDataManager }
///<summary>Data manager for unbounded data.</summary>
TOmniHeuristicDataManager = class(TOmniBaseDataManager)
strict private
const CFetchTimeout_ms = 10;
strict private
hdmEstimatedPackageSize: TOmniAlignedInt32;
{$IFNDEF MSWINDOWS}
FStopWatch: TStopwatch;
{$ENDIF}
public
constructor Create(sourceProvider: TOmniSourceProvider; numWorkers: integer; options:
TOmniDataManagerOptions);
function GetNextFromProvider(package: TOmniDataPackage;
generation: integer): boolean; override;
end; { TOmniHeuristicDataManager }
{ exports }
function CreateSourceProvider(low, high, step: integer): TOmniSourceProvider;
begin
Result := TOmniIntegerRangeProvider.Create(low, high, step);
end; { CreateSourceProvider }
function CreateSourceProvider(enumerator: IOmniValueEnumerator): TOmniSourceProvider;
begin
Result := TOmniValueEnumeratorProvider.Create(enumerator);
end; { CreateSourceProvider }
function CreateSourceProvider(enumerator: IEnumerator): TOmniSourceProvider; overload;
begin
Result := TOmniEnumeratorProvider.Create(enumerator);
end; { CreateSourceProvider }
function CreateSourceProvider(enumerator: TOmniValueEnumerator): TOmniSourceProvider; overload;
begin
Result := TOmniEnumeratorProvider.Create(enumerator);
end; { CreateSourceProvider }
function CreateDataManager(sourceProvider: TOmniSourceProvider; numWorkers: integer;
options: TOmniDataManagerOptions): TOmniDataManager;
begin
if spcCountable in sourceProvider.GetCapabilities then
Result := TOmniCountableDataManager.Create(sourceProvider, numWorkers, options)
else
Result := TOmniHeuristicDataManager.Create(sourceProvider, numWorkers, options);
end; { CreateDataManager }
{ TOmniPositionRange }
constructor TOmniPositionRange.Create(aFirst, aLast: int64);
begin
First := aFirst;
Last := aLast;
end; { TOmniPositionRange.Create }
class function TOmniPositionRange.Empty: TOmniPositionRange;
begin
Result := TOmniPositionRange.Create(0, 0);
end; { TOmniPositionRange.Empty }
{ TOmniDataPackageBase }
procedure TOmniDataPackageBase.NextGeneration;
begin
dpbGeneration := Abs((dpbGeneration SHL 1) + 3) SHR 1;
end; { TOmniDataPackageBase.NextGeneration }
{ TOmniIntegerDataPackage }
function TOmniIntegerDataPackage.GetNext(var value: TOmniValue): boolean;
begin
if not HasData then
Result := false
else begin
value.AsInt64 := idpLow.Add(idpStep) - idpStep;
if idpStep > 0 then
Result := (value.AsInt64 <= idpHigh)
else
Result := (value.AsInt64 >= idpHigh);
end;
end; { TOmniIntegerDataPackage.GetNext }
function TOmniIntegerDataPackage.GetNext(var position: int64; var value: TOmniValue):
boolean;
begin
Result := GetNext(value);
if Result then begin
position := idpPosition;
Inc(idpPosition);
end;
end; { TOmniIntegerDataPackage.GetNext }
function TOmniIntegerDataPackage.HasData: boolean;
begin
Result := idpStep <> 0;
end; { TOmniIntegerDataPackage.HasData }
procedure TOmniIntegerDataPackage.Initialize(low, high, step: integer;
newRange: TOmniPositionRange);
begin
{$IFDEF Debug}Assert(step <> 0);{$ENDIF}
idpLow.Value := low;
idpHigh := high;
idpStep := step;
idpStepAbs := Abs(step);
if idpStep > 0 then
idpSign := 1
else
idpSign := -1;
idpHighSign := idpHigh * idpSign;
Range := newRange;
idpPosition := Range.First;
end; { TOmniIntegerDataPackage.Initialize }
function TOmniIntegerDataPackage.Split(package: TOmniDataPackage): boolean;
var
high : integer;
intPackage: TOmniIntegerDataPackage absolute package;
low : integer;
midSteps : integer;
begin
{$IFDEF Debug}Assert(package is TOmniIntegerDataPackage);{$ENDIF}
if not HasData then
Result := false
else begin
low := idpLow.Value;
if (low * idpSign) > idpHighSign then
Result := false
else begin
midSteps := (((idpHigh - idpLow.Value) div 2 + 1) div idpStep) * idpStep;
low := idpLow.Add(midSteps) - midSteps;
high := low + midSteps - idpStep;
Result := (low * idpSign <= idpHighSign);
if Result and (high * idpSign > idpHighSign) then
high := (idpHigh div idpStepAbs) * idpStepAbs;
if Result then begin
intPackage.Initialize(low, high, idpStep, TOmniPositionRange.Empty);
end;
end;
end;
end; { TOmniIntegerDataPackage.Split }
{ TOmniIntegerRangeProvider }
constructor TOmniIntegerRangeProvider.Create(low, high, step: integer);
begin
inherited Create;
{$IFDEF Debug}Assert(step <> 0);{$ENDIF}
irpLow := low;
irpHigh := high;
irpStep := step;
irpCount.Value := CalcCount(low, high, step);
end; { TOmniIntegerRangeProvider.Create }
function TOmniIntegerRangeProvider.CalcCount(low, high, step: integer): integer;
begin
if step > 0 then
Result := (high - low + step) div step
else
Result := (low - high - step) div (-step);
end; { TOmniIntegerRangeProvider.CalcCount }
function TOmniIntegerRangeProvider.Count: int64;
begin
Result := irpCount.Value;
end; { TOmniIntegerRangeProvider.Count }
function TOmniIntegerRangeProvider.CreateDataPackage: TOmniDataPackage;
begin
Result := TOmniIntegerDataPackage.Create;
end; { TOmniIntegerRangeProvider.CreateDataPackage }
function TOmniIntegerRangeProvider.GetCapabilities: TOmniSourceProviderCapabilities;
begin
Result := [spcCountable, spcFast];
end; { TOmniIntegerRangeProvider.GetCapabilities }
function TOmniIntegerRangeProvider.GetPackage(dataCount: integer; package:
TOmniDataPackage): boolean;
var
high : int64;
intPackage: TOmniIntegerDataPackage absolute package;
begin
{$IFDEF Debug}Assert(package is TOmniIntegerDataPackage);{$ENDIF}
{$IFDEF Debug}Assert(dataCount >= 0);{$ENDIF}
if irpCount.Value <= 0 then
Result := false
else begin
irpLock.Acquire;
try
if dataCount > irpCount.Value then
dataCount := irpCount.Value;
high := irpLow + (dataCount - 1) * irpStep;
intPackage.Initialize(irpLow, high, irpStep, TOmniPositionRange.Create(irpPosition, irpPosition + dataCount - 1));
Inc(irpPosition, dataCount);
irpLow := high + irpStep;
irpCount.Value := CalcCount(irpLow, irpHigh, irpStep);
finally irpLock.Release; end;
Result := true;
end;
end; { TOmniIntegerRangeProvider.GetPackage }
function TOmniIntegerRangeProvider.GetPackageSizeLimit: integer;
begin
raise Exception.Create('No limit for TOmniIntegerRangeProvider packages');
end; { TOmniIntegerRangeProvider.GetPackageSizeLimit }
{ TOmniValueEnumeratorDataPackage }
constructor TOmniValueEnumeratorDataPackage.Create;
begin
inherited Create;
vedpDataQueue := TOmniBaseQueue.Create; //TOmniBaseBoundedQueue.Create;
// vedpDataQueue.Initialize(2*CMaxValueEnumeratorDataPackageSize+2, SizeOf(TOmniValue)); // there are bugs in the TOmniBaseBoundedQueue
end; { TOmniValueEnumeratorDataPackage.Create }
destructor TOmniValueEnumeratorDataPackage.Destroy;
begin
FreeAndNil(vedpDataQueue);
inherited;
end; { TOmniValueEnumeratorDataPackage.Destroy }
procedure TOmniValueEnumeratorDataPackage.Add(const value: TOmniValue);
var
tmp: TOmniValue;
begin
// {$IFDEF Debug} Assert(not vedpDataQueue.IsFull); {$ENDIF}
tmp := value;
vedpDataQueue.Enqueue(tmp);
tmp.RawZero; // queue keeps holding the reference
vedpApproxCount.Increment;
// {$IFDEF Debug} Assert(not vedpDataQueue.IsFull); {$ENDIF} // full queue corrupts data
end; { TOmniValueEnumeratorDataPackage.Add }
function TOmniValueEnumeratorDataPackage.GetNext(var value: TOmniValue): boolean;
var
tmp: TOmniValue;
begin
tmp.RawZero;
Result := vedpDataQueue.TryDequeue(tmp);
if not Result then
Exit;
value := tmp;
tmp._Release; // queue was holding the reference
vedpApproxCount.Decrement;
end; { TOmniValueEnumeratorDataPackage.GetNext }
function TOmniValueEnumeratorDataPackage.GetNext(var position: int64;
var value: TOmniValue): boolean;
begin
Result := GetNext(value);
if Result then begin
position := vedpPosition;
Inc(vedpPosition);
end;
end; { TOmniValueEnumeratorDataPackage.GetNext }
class function TOmniValueEnumeratorDataPackage.GetPackageSizeLimit: integer;
begin
Result := CMaxValueEnumeratorDataPackageSize;
end; { TOmniValueEnumeratorDataPackage.GetPackageSizeLimit }
function TOmniValueEnumeratorDataPackage.Prepare(dataCount: integer; position: int64): integer;
begin
// only called when the package is empty
if dataCount <= CMaxValueEnumeratorDataPackageSize then
Result := dataCount
else
Result := CMaxValueEnumeratorDataPackageSize;
vedpApproxCount.Value := 0;
vedpPosition := position;
end; { TOmniValueEnumeratorDataPackage.Prepare }
function TOmniValueEnumeratorDataPackage.Split(package: TOmniDataPackage): boolean;
var
intPackage: TOmniValueEnumeratorDataPackage absolute package;
iValue : integer;
value : TOmniValue;
begin
{$IFDEF Debug} Assert(package is TOmniValueEnumeratorDataPackage); {$ENDIF}
Result := false;
for iValue := 1 to intPackage.Prepare(vedpApproxCount.Value div 2, 0) do begin
if not GetNext(value) then
break; //for
intPackage.Add(value);
Result := true;
end;
end; { TOmniValueEnumeratorDataPackage.Split }
{ TOmniValueEnumeratorProvider }
function TOmniValueEnumeratorProvider.Count: int64;
begin
raise Exception.Create('IOmniValueEnumerable cannot be counted');
end; { TOmniValueEnumeratorProvider.Count }
constructor TOmniValueEnumeratorProvider.Create(enumerator: IOmniValueEnumerator);
begin
inherited Create;
vepEnumerator := enumerator;
end; { TOmniValueEnumeratorProvider.Create }
function TOmniValueEnumeratorProvider.CreateDataPackage: TOmniDataPackage;
begin
Result := TOmniValueEnumeratorDataPackage.Create;
end; { TOmniValueEnumeratorProvider.CreateDataPackage }
function TOmniValueEnumeratorProvider.GetCapabilities: TOmniSourceProviderCapabilities;
begin
Result := [spcDataLimit];
end; { TOmniValueEnumeratorProvider.GetCapabilities }
function TOmniValueEnumeratorProvider.GetPackage(dataCount: integer; package:
TOmniDataPackage): boolean;
var
iData : integer;
intPackage: TOmniValueEnumeratorDataPackage absolute package;
lastPos : int64;
timeout : cardinal;
value : TOmniValue;
begin
// Assert(not StorePositions);
Result := false;
if StorePositions then
vepEnumLock.Acquire;
try
lastPos := vepPosition;
dataCount := intPackage.Prepare(dataCount, lastPos);
timeout := INFINITE;
for iData := 1 to dataCount do begin
if not vepEnumerator.TryTake(value, timeout) then
break; //for
intPackage.Add(value);
Inc(lastPos);
timeout := 0;
Result := true;
end;
if StorePositions then begin
intPackage.Range := TOmniPositionRange.Create(vepPosition, lastPos - 1);
vepPosition := lastPos;
end;
finally
if StorePositions then
vepEnumLock.Release;
end;
end; { TOmniValueEnumeratorProvider.GetPackage }
function TOmniValueEnumeratorProvider.GetPackageSizeLimit: integer;
begin
Result := TOmniValueEnumeratorDataPackage.GetPackageSizeLimit;
end; { TOmniValueEnumeratorProvider.GetPackageSizeLimit }
{ TOmniEnumeratorProvider }
constructor TOmniEnumeratorProvider.Create(enumerator: IEnumerator);
begin
inherited Create;
epEnumeratorIntf := enumerator;
end; { TOmniEnumeratorProvider.Create }
constructor TOmniEnumeratorProvider.Create(enumerator: TOmniValueEnumerator);
begin
inherited Create;
epEnumeratorObj := enumerator;
end; { TOmniEnumeratorProvider.Create }
function TOmniEnumeratorProvider.Count: int64;
begin
raise Exception.Create('IEnumerable cannot be counted');
end; { TOmniEnumeratorProvider.Count }
function TOmniEnumeratorProvider.CreateDataPackage: TOmniDataPackage;
begin
Result := TOmniValueEnumeratorDataPackage.Create;
end; { TOmniEnumeratorProvider.CreateDataPackage }
function TOmniEnumeratorProvider.GetCapabilities: TOmniSourceProviderCapabilities;
begin
Result := [spcDataLimit];
end; { TOmniEnumeratorProvider.GetCapabilities }
function TOmniEnumeratorProvider.GetPackage(dataCount: integer;
package: TOmniDataPackage): boolean;
var
iData : integer;
intPackage: TOmniValueEnumeratorDataPackage absolute package;
lastPos : int64;
begin
// Assert(not StorePositions);
Result := false;
epEnumLock.Acquire;
try
lastPos := epPosition;
dataCount := intPackage.Prepare(dataCount, lastPos);
if assigned(epEnumeratorObj) then begin
for iData := 1 to dataCount do begin
if not epEnumeratorObj.MoveNext then
break; //for iData
intPackage.Add(epEnumeratorObj.Current);
Inc(lastPos);
Result := true;
end;
end
else begin
for iData := 1 to dataCount do begin
if not epEnumeratorIntf.MoveNext then
break; //for iData
intPackage.Add(epEnumeratorIntf.Current);
Inc(lastPos);
Result := true;
end;
end;
if StorePositions then begin
intPackage.Range := TOmniPositionRange.Create(epPosition, lastPos - 1);
epPosition := lastPos;
end;
finally epEnumLock.Release; end;
end; { TOmniEnumeratorProvider.GetPackage }
function TOmniEnumeratorProvider.GetPackageSizeLimit: integer;
begin
Result := TOmniValueEnumeratorDataPackage.GetPackageSizeLimit;
end; { TOmniEnumeratorProvider.GetPackageSizeLimit }
{ TOmniLocalQueueImpl }
procedure TOmniLocalQueueImpl.AssociateBuffer(buffer: TOmniOutputBuffer);
begin
lqiBufferSet := buffer as TOmniOutputBufferSet;
end; { TOmniLocalQueueImpl.AssociateBuffer }
constructor TOmniLocalQueueImpl.Create(owner: TOmniBaseDataManager);
begin
inherited Create;
lqiDataManager_ref := owner;
lqiDataPackage := lqiDataManager_ref.SourceProvider.CreateDataPackage as TOmniDataPackageBase;
lqiDataPackage.Queue := Self;
end; { TOmniLocalQueueImpl.Create }
destructor TOmniLocalQueueImpl.Destroy;
begin
if assigned(lqiDataManager_ref) then
lqiDataManager_ref.LocalQueueDestroyed(Self);
FreeAndNil(lqiDataPackage); // keep this destruction order! (see Split for more info)
inherited;
end; { TOmniLocalQueueImpl.Destroy }
function TOmniLocalQueueImpl.GetNext(var value: TOmniValue): boolean;
begin
Result := lqiDataPackage.GetNext(value);
if not Result then begin
{$IFDEF Debug}Assert(not assigned(lqiBufferSet));{$ENDIF Debug}
Result := lqiDataManager_ref.GetNext(lqiDataPackage);
if Result then
Result := lqiDataPackage.GetNext(value);
end;
end; { TOmniLocalQueueImpl.GetNext }
function TOmniLocalQueueImpl.GetNext(var position: int64; var value: TOmniValue): boolean;
begin
Result := lqiDataPackage.GetNext(position, value);
if not Result then begin
{$IFDEF Debug}Assert(assigned(lqiBufferSet));{$ENDIF Debug}
lqiBufferSet.ActiveBuffer.MarkFull;
lqiBufferSet.ActivateBuffer; // this will block if alternate buffer is also full
Result := lqiDataManager_ref.GetNext(lqiDataPackage);
if Result then begin
Result := lqiDataPackage.GetNext(position, value);
if Result then
lqiBufferSet.ActiveBuffer.Range := lqiDataPackage.Range;
end;
end;
end; { TOmniLocalQueueImpl.GetNext }
function TOmniLocalQueueImpl.Split(package: TOmniDataPackage): boolean;
begin
// this method is called when queue list is locked, therefore lqiDataPackage is
// guaranteed to still be created
{$IFDEF Debug} Assert(assigned(lqiDataPackage)); {$ENDIF}
Result := lqiDataPackage.Split(package);
end; { TOmniLocalQueueImpl.Split }
{ TOmniOutputBufferImpl }
constructor TOmniOutputBufferImpl.Create(owner: TOmniBaseDataManager;
output: IOmniBlockingCollection; const AShareLock: IOmniCriticalSection);
begin
inherited Create;
obiDataManager_ref := owner;
obiOutput := output;
obiBuffer := TOmniBlockingCollection.Create;
{$IFDEF MSWINDOWS}
obiEmptyEvent := CreateEvent(nil, false, true, nil);
{$ELSE}
obiEmptyEvent := CreateOmniEvent(false, true, AShareLock);
{$ENDIF ~MSWINDOWS}
end; { TOmniOutputBufferImpl.Create }
destructor TOmniOutputBufferImpl.Destroy;
begin
{$IFDEF MSWINDOWS}
DSiCloseHandleAndNull(obiEmptyEvent);
{$ELSE}
obiEmptyEvent := nil;
{$ENDIF ~MSWINDOWS}
FreeAndNil(obiBuffer);
inherited;
end; { TOmniOutputBufferImpl.Destroy }
procedure TOmniOutputBufferImpl.CopyToOutput;
var
value: TOmniValue;
begin
while obiBuffer.TryTake(value) do
obiOutput.Add(value);
SetEvent(obiEmptyEvent);
obiFull := false;
end; { TOmniOutputBufferImpl.CopyToOutput }
procedure TOmniOutputBufferImpl.MarkFull;
begin
obiFull := true;
if obiHasData then
obiDataManager_ref.NotifyBufferFull(Self);
obiHasData := false;
end; { TOmniOutputBufferImpl.MarkFull }
procedure TOmniOutputBufferImpl.SetRange(range: TOmniPositionRange);
begin
obiRange := range;
obiDataManager_ref.NotifyBufferRangeChanged(Self);
obiNextPosition := obiRange.First;
obiHasData := true;
end; { TOmniOutputBufferImpl.SetRange }
procedure TOmniOutputBufferImpl.Submit(position: int64; const data: TOmniValue);
begin
{$IFDEF DEBUG}Assert(position >= obiNextPosition, Format('%d < %d', [position, obiNextPosition])); obiNextPosition := position + 1;{$ENDIF}
obiBuffer.Add(data);
end; { TOmniOutputBufferImpl.Submit }
{ TOmniOutputBufferSet }
constructor TOmniOutputBufferSet.Create(owner: TOmniBaseDataManager;
output: IOmniBlockingCollection);
var
iBuffer: integer;
{$IFNDEF MSWINDOWS}
Events: array of IOmniSynchro;
{$ENDIF ~MSWINDOWS}
begin
inherited Create;
obsShareLock := CreateOmniCriticalSection;
{$IFNDEF MSWINDOWS}
SetLength(Events, CNumBuffersInSet);
{$ENDIF}
for iBuffer := 1 to CNumBuffersInSet do begin
obsBuffers[iBuffer] := TOmniOutputBufferImpl.Create(owner, output);
{$IFDEF MSWINDOWS}
obsWaitHandles[iBuffer] := obsBuffers[iBuffer].EmptyEvent;
{$ELSE}
Events[iBuffer-1] := obsBuffers[iBuffer].EmptyEvent;
{$ENDIF ~MSWINDOWS}
end;
{$IFNDEF MSWINDOWS}
obsWaitEvents := TWaitFor.Create(Events, obsShareLock);
{$ENDIF}
ActivateBuffer;
end; { TOmniOutputBufferSet.Create }
destructor TOmniOutputBufferSet.Destroy;
var
iBuffer: integer;
begin
for iBuffer := 1 to CNumBuffersInSet do
obsBuffers[iBuffer].Free;
{$IFNDEF MSWINDOWS}
FreeAndNil(obsWaitEvents);
{$ENDIF ~MSWINDOWS}
inherited;
end; { TOmniOutputBufferSet.Destroy }
procedure TOmniOutputBufferSet.ActivateBuffer;
var
{$IFDEF MSWINDOWS}
awaited: cardinal;
{$ELSE}
Signaller: IOmniSynchro;
iBuffer : integer;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
awaited := WaitForMultipleObjects(CNumBuffersInSet, @obsWaitHandles, false, INFINITE);
Assert({(awaited >= WAIT_OBJECT_0) and } (awaited < (WAIT_OBJECT_0 + CNumBuffersInSet)));
obsActiveIndex := awaited - WAIT_OBJECT_0 + 1;
obsActiveBuffer_ref := obsBuffers[obsActiveIndex];
{$ELSE}
obsWaitEvents.WaitAny(INFINITE, Signaller);
for iBuffer := 1 to CNumBuffersInSet do begin
if Signaller <> obsBuffers[iBuffer].EmptyEvent then
continue; //for
obsActiveBuffer_ref := obsBuffers[iBuffer];
break; //for
end;
{$ENDIF ~MSWINDOWS}
end; { TOmniOutputBufferSet.ActivateBuffer }
procedure TOmniOutputBufferSet.Submit(position: int64; const data: TOmniValue);
begin
obsActiveBuffer_ref.Submit(position, data);
end; { TOmniOutputBufferSet.Submit }