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
+38 -21
View File
@@ -17,25 +17,27 @@ type
constructor Create(ATime: TDateTime; const AData: T);
end;
IDataSeriesWriter<T> = interface
procedure Add(const Data: TDataPoint<T>); overload;
procedure Add(const Data: array of TDataPoint<T>; NumToAdd: Integer = -1); overload;
procedure Clear;
end;
// A time-ordered series of data points, optimized for chronological additions.
// The most recently added element has the logical index 0.
IDataSeries<T> = interface
function GetCount: Int64;
function GetItems(Idx: Int64): TDataPoint<T>;
function GetTotalCount: Int64;
function Copy: IDataSeries<T>;
function Immutable: IDataSeries<T>;
function GetWriter: IDataSeriesWriter<T>;
property Count: Int64 read GetCount;
// Accesses data points by their logical index.
// Index 0 is the newest element, Index (Count - 1) is the oldest.
property Items[Idx: Int64]: TDataPoint<T> read GetItems; default;
// The total number of items ever added to the series.
property TotalCount: Int64 read GetTotalCount;
end;
IDataArray<T> = interface(IDataSeries<T>)
procedure Add(const Data: TDataPoint<T>); overload;
procedure Add(const Data: array of TDataPoint<T>; NumToAdd: Integer = -1); overload;
procedure Clear;
property Writer: IDataSeriesWriter<T> read GetWriter;
end;
// Interface Helper for IDataSeries<T>.
@@ -43,12 +45,11 @@ type
TDataSeries<T> = record
private
FDataSeries: IDataSeries<T>;
FMaxLookback: Int64;
function GetCount: Int64;
function GetItems(Idx: Int64): TDataPoint<T>;
function GetData(Idx: Int64): T;
function GetIsWriteable: Boolean;
function GetTime(Idx: Int64): TDateTime;
function GetMaxLookback: Int64;
class function GetNull: IDataSeries<T>; static;
function GetTotalCount: Int64;
public
@@ -60,10 +61,14 @@ type
class operator Implicit(const A: TDataSeries<T>): IDataSeries<T>;
class operator Implicit(const A: IDataSeries<T>): TDataSeries<T>;
class function CreateArray(MaxLookback: Int64): IDataArray<T>; static;
class function CreateWriteable(MaxLookback: Int64): TDataSeries<T>; static;
// Create a guaranteed immutable version of the given series.
function Copy: TDataSeries<T>;
// Create an immutable version of the given series.
function Immutable: TDataSeries<T>;
// Writing (only if IsWriteable=true)
procedure Add(const Data: array of TDataPoint<T>; NumToAdd: Integer = -1);
procedure Clear;
// Searches for a data point by its timestamp.
// Returns the logical index of the matching item.
@@ -75,8 +80,8 @@ type
property Count: Int64 read GetCount;
property Items[Idx: Int64]: TDataPoint<T> read GetItems; default;
property Data[Idx: Int64]: T read GetData;
property IsWriteable: Boolean read GetIsWriteable;
property Time[Idx: Int64]: TDateTime read GetTime;
property MaxLookback: Int64 read GetMaxLookback;
property TotalCount: Int64 read GetTotalCount;
end;
@@ -109,12 +114,24 @@ begin
FDataSeries := Null;
end;
function TDataSeries<T>.Copy: TDataSeries<T>;
procedure TDataSeries<T>.Add(const Data: array of TDataPoint<T>; NumToAdd: Integer = -1);
begin
Result := FDataSeries.Copy;
Assert( IsWriteable );
FDataSeries.Writer.Add( Data, NumToAdd );
end;
class function TDataSeries<T>.CreateArray(MaxLookback: Int64): IDataArray<T>;
procedure TDataSeries<T>.Clear;
begin
Assert( IsWriteable );
FDataSeries.Writer.Clear;
end;
function TDataSeries<T>.Immutable: TDataSeries<T>;
begin
Result := FDataSeries.Immutable;
end;
class function TDataSeries<T>.CreateWriteable(MaxLookback: Int64): TDataSeries<T>;
begin
Result := TDataArray<T>.Create(MaxLookback);
end;
@@ -154,16 +171,16 @@ begin
Result := FDataSeries[Idx].Data;
end;
function TDataSeries<T>.GetIsWriteable: Boolean;
begin
Result := Assigned( FDataSeries.Writer );
end;
function TDataSeries<T>.GetTime(Idx: Int64): TDateTime;
begin
Result := FDataSeries[Idx].Time;
end;
function TDataSeries<T>.GetMaxLookback: Int64;
begin
Result := FMaxLookback;
end;
class function TDataSeries<T>.GetNull: IDataSeries<T>;
begin
Result := TNullDataSeries<T>.Null;