Writeables revised

Read-ahead for DataStream Processing
This commit is contained in:
Michael Schimmel
2025-06-25 10:07:00 +02:00
parent f791667264
commit e1159e883b
9 changed files with 272 additions and 227 deletions
+52 -34
View File
@@ -7,7 +7,7 @@ uses
type
// The implementation class for IDataSeries<T>.
TDataArray<T> = class(TInterfacedObject, IDataSeries<T>, IDataArray<T>)
TDataArray<T> = class(TInterfacedObject, IDataSeries<T>, IDataSeriesWriter<T>)
const
ChunkSize = 1024;
type
@@ -15,7 +15,7 @@ type
private
FChunks: TArray<TChunk>;
FCount: Int64;
FMaxLookback: Int64;
FLookback: Int64;
FTotalCount: Int64;
function LogicalToPhysicalIndex(LogicalIndex: Int64): Int64; inline;
@@ -25,15 +25,16 @@ type
// IDataSeries<T> implementation
function GetCount: Int64;
function GetItems(Idx: Int64): TDataPoint<T>;
function GetMaxLookback: Int64;
function GetLookback: Int64;
function GetTotalCount: Int64;
function GetWriter: IDataSeriesWriter<T>;
public
constructor Create(AMaxLookback: Int64);
constructor Create(ALookback: Int64);
procedure Add(const Data: TDataPoint<T>); overload;
procedure Add(const Data: array of TDataPoint<T>; NumToAdd: Integer = -1); overload;
procedure Clear;
function Copy: IDataSeries<T>;
property MaxLookback: Int64 read GetMaxLookback;
function Immutable: IDataSeries<T>;
property Lookback: Int64 read GetLookback;
end;
// Null object implementation for IDataSeries<T>
@@ -43,12 +44,13 @@ type
FNull: IDataSeries<T>;
private
class constructor CreateClass;
function GetWriter: IDataSeriesWriter<T>;
public
function GetCount: Int64;
function GetItems(Idx: Int64): TDataPoint<T>;
function GetTotalCount: Int64;
function IndexOf(TimeStamp: TDateTime): Int64;
function Copy: IDataSeries<T>;
function Immutable: IDataSeries<T>;
class property Null: IDataSeries<T> read FNull;
end;
@@ -57,7 +59,7 @@ type
private
FChunks: TArray<TDataArray<T>.TChunk>;
FCount: Int64;
FMaxLookback: Int64;
FLookback: Int64;
FTotalCount: Int64;
function LogicalToPhysicalIndex(LogicalIndex: Int64): Int64; inline;
@@ -66,23 +68,24 @@ type
// IDataSeries<T> implementation
function GetCount: Int64;
function GetItems(Idx: Int64): TDataPoint<T>;
function GetMaxLookback: Int64;
function GetLookback: Int64;
function GetTotalCount: Int64;
function GetWriter: IDataSeriesWriter<T>;
public
constructor Create(const AChunks: TArray<TDataArray<T>.TChunk>; ACount, AMaxLookback, ATotalCount: Int64);
function Copy: IDataSeries<T>;
property MaxLookback: Int64 read GetMaxLookback;
constructor Create(const AChunks: TArray<TDataArray<T>.TChunk>; ACount, ALookback, ATotalCount: Int64);
function Immutable: IDataSeries<T>;
property Lookback: Int64 read GetLookback;
end;
implementation
{ TDataArray<T> }
constructor TDataArray<T>.Create(AMaxLookback: Int64);
constructor TDataArray<T>.Create(ALookback: Int64);
begin
inherited Create;
FMaxLookback := AMaxLookback;
FLookback := ALookback;
FCount := 0;
FTotalCount := 0;
FChunks := nil;
@@ -170,16 +173,16 @@ begin
FTotalCount := 0;
end;
function TDataArray<T>.Copy: IDataSeries<T>;
function TDataArray<T>.Immutable: IDataSeries<T>;
begin
Result := TStaticDataSeries<T>.Create(FChunks, FCount, FMaxLookback, FTotalCount);
Result := TStaticDataSeries<T>.Create(FChunks, FCount, FLookback, FTotalCount);
end;
function TDataArray<T>.GetCount: Int64;
begin
Result := FCount;
if (FMaxLookback > 0) and (Result > FMaxLookback) then
Result := FMaxLookback;
if (FLookback > 0) and (Result > FLookback) then
Result := FLookback;
end;
function TDataArray<T>.GetItems(Idx: Int64): TDataPoint<T>;
@@ -191,9 +194,9 @@ begin
Result := FChunks[physicalIndex div ChunkSize][physicalIndex mod ChunkSize];
end;
function TDataArray<T>.GetMaxLookback: Int64;
function TDataArray<T>.GetLookback: Int64;
begin
Result := FMaxLookback;
Result := FLookback;
end;
function TDataArray<T>.GetTotalCount: Int64;
@@ -201,9 +204,14 @@ begin
Result := FTotalCount;
end;
function TDataArray<T>.GetWriter: IDataSeriesWriter<T>;
begin
Result := Self;
end;
function TDataArray<T>.IsIndexInLimits(LogicalIndex: Int64): Boolean;
begin
Result := (FMaxLookback <= 0) or (LogicalIndex < FMaxLookback);
Result := (FLookback <= 0) or (LogicalIndex < FLookback);
end;
function TDataArray<T>.LogicalToPhysicalIndex(LogicalIndex: Int64): Int64;
@@ -216,12 +224,12 @@ procedure TDataArray<T>.Trim;
var
itemsToRemove, chunksToRemove: Int64;
begin
if (FCount = 0) or (FMaxLookback <= 0) then
if (FCount = 0) or (FLookback <= 0) then
Exit;
itemsToRemove := 0;
if FCount > FMaxLookback then
itemsToRemove := FCount - FMaxLookback;
if FCount > FLookback then
itemsToRemove := FCount - FLookback;
if itemsToRemove <= 0 then
Exit;
@@ -231,7 +239,7 @@ begin
if chunksToRemove > 0 then
begin
var itemsInFreedChunks := chunksToRemove * ChunkSize;
FChunks := System.Copy(FChunks, chunksToRemove, Length(FChunks) - chunksToRemove);
FChunks := Copy(FChunks, chunksToRemove, Length(FChunks) - chunksToRemove);
FCount := FCount - itemsInFreedChunks;
end;
end;
@@ -243,7 +251,7 @@ begin
FNull := TNullDataSeries<T>.Create;
end;
function TNullDataSeries<T>.Copy: IDataSeries<T>;
function TNullDataSeries<T>.Immutable: IDataSeries<T>;
begin
Result := FNull;
end;
@@ -264,6 +272,11 @@ begin
Result := 0;
end;
function TNullDataSeries<T>.GetWriter: IDataSeriesWriter<T>;
begin
Result := nil;
end;
function TNullDataSeries<T>.IndexOf(TimeStamp: TDateTime): Int64;
begin
Result := -1;
@@ -271,16 +284,16 @@ end;
{ TStaticDataSeries<T> }
constructor TStaticDataSeries<T>.Create(const AChunks: TArray<TDataArray<T>.TChunk>; ACount, AMaxLookback, ATotalCount: Int64);
constructor TStaticDataSeries<T>.Create(const AChunks: TArray<TDataArray<T>.TChunk>; ACount, ALookback, ATotalCount: Int64);
begin
inherited Create;
FChunks := AChunks;
FCount := ACount;
FMaxLookback := AMaxLookback;
FLookback := ALookback;
FTotalCount := ATotalCount;
end;
function TStaticDataSeries<T>.Copy: IDataSeries<T>;
function TStaticDataSeries<T>.Immutable: IDataSeries<T>;
begin
Result := Self;
end;
@@ -288,8 +301,8 @@ end;
function TStaticDataSeries<T>.GetCount: Int64;
begin
Result := FCount;
if (FMaxLookback > 0) and (Result > FMaxLookback) then
Result := FMaxLookback;
if (FLookback > 0) and (Result > FLookback) then
Result := FLookback;
end;
function TStaticDataSeries<T>.GetItems(Idx: Int64): TDataPoint<T>;
@@ -301,9 +314,9 @@ begin
Result := FChunks[physicalIndex div TDataArray<T>.ChunkSize][physicalIndex mod TDataArray<T>.ChunkSize];
end;
function TStaticDataSeries<T>.GetMaxLookback: Int64;
function TStaticDataSeries<T>.GetLookback: Int64;
begin
Result := FMaxLookback;
Result := FLookback;
end;
function TStaticDataSeries<T>.GetTotalCount: Int64;
@@ -311,9 +324,14 @@ begin
Result := FTotalCount;
end;
function TStaticDataSeries<T>.GetWriter: IDataSeriesWriter<T>;
begin
Result := nil;
end;
function TStaticDataSeries<T>.IsIndexInLimits(LogicalIndex: Int64): Boolean;
begin
Result := (FMaxLookback <= 0) or (LogicalIndex < FMaxLookback);
Result := (FLookback <= 0) or (LogicalIndex < FLookback);
end;
function TStaticDataSeries<T>.LogicalToPhysicalIndex(LogicalIndex: Int64): Int64;