TDataPoint<T> review
This commit is contained in:
@@ -13,19 +13,21 @@ type
|
||||
end;
|
||||
|
||||
TDataPoint<T> = record
|
||||
Idx: Int64;
|
||||
Time: TDateTime;
|
||||
Data: T;
|
||||
constructor Create(AIdx: Int64; ATime: TDateTime; const AData: T);
|
||||
constructor Create(ATime: TDateTime; const AData: T);
|
||||
end;
|
||||
|
||||
IMycStream<T: record> = interface
|
||||
procedure Add(const Data: TDataPoint<T>);
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
{ TDataPoint }
|
||||
|
||||
constructor TDataPoint<T>.Create(AIdx: Int64; ATime: TDateTime; const AData: T);
|
||||
constructor TDataPoint<T>.Create(ATime: TDateTime; const AData: T);
|
||||
begin
|
||||
Idx := AIdx;
|
||||
Time := ATime;
|
||||
Data := AData;
|
||||
end;
|
||||
|
||||
@@ -72,8 +72,8 @@ type
|
||||
|
||||
IAuraDataServer<T: record> = interface(IDataServer<T>)
|
||||
['{481E27DE-DC58-49AF-B8A8-B6316980C423}']
|
||||
function LoadDataFile(const FileName: string): TFuture<TArray<TAuraFileDataRecord<T>>>;
|
||||
function LoadDataSeries(const FileName: string): TFuture<TArray<TAuraFileDataRecord<T>>>;
|
||||
function LoadDataFile(const FileName: string): TFuture<TArray<TDataPoint<T>>>;
|
||||
function LoadDataSeries(const FileName: string): TFuture<TArray<TDataPoint<T>>>;
|
||||
function EnumerateAssetFiles: TArray<String>;
|
||||
end;
|
||||
|
||||
@@ -88,7 +88,7 @@ type
|
||||
Name: String;
|
||||
Age: TDateTime;
|
||||
LastUsed: TDateTime;
|
||||
Data: TFuture<TArray<TAuraFileDataRecord<T>>>;
|
||||
Data: TFuture<TArray<TDataPoint<T>>>;
|
||||
end;
|
||||
private
|
||||
// The cache is per-instance.
|
||||
@@ -102,8 +102,8 @@ type
|
||||
FLoadGate: TLatch;
|
||||
|
||||
protected
|
||||
class function ReadCompressedData(const InputStream: TStream): TArray<TAuraFileDataRecord<T>>; static;
|
||||
class function ReadUncompressedData(const InputStream: TStream): TArray<TAuraFileDataRecord<T>>; static;
|
||||
class function ReadCompressedData(const InputStream: TStream): TArray<TDataPoint<T>>; static;
|
||||
class function ReadUncompressedData(const InputStream: TStream): TArray<TDataPoint<T>>; static;
|
||||
|
||||
public
|
||||
constructor Create(const APath: String);
|
||||
@@ -124,8 +124,8 @@ type
|
||||
procedure ClearCache;
|
||||
function EnumerateAssetFiles: TArray<String>;
|
||||
|
||||
function LoadDataFile(const FileName: string): TFuture<TArray<TAuraFileDataRecord<T>>>;
|
||||
function LoadDataSeries(const FileName: string): TFuture<TArray<TAuraFileDataRecord<T>>>;
|
||||
function LoadDataFile(const FileName: string): TFuture<TArray<TDataPoint<T>>>;
|
||||
function LoadDataSeries(const FileName: string): TFuture<TArray<TDataPoint<T>>>;
|
||||
|
||||
property Path: String read GetPath;
|
||||
end;
|
||||
@@ -136,11 +136,10 @@ type
|
||||
FDataServer: TAuraDataServer<T>;
|
||||
FIsLiveData: TMutable<Boolean>.IWriteable;
|
||||
FCurrentFileName: string;
|
||||
FCurrentData: TFuture<TArray<TAuraFileDataRecord<T>>>;
|
||||
FCurrentData: TFuture<TArray<TDataPoint<T>>>;
|
||||
FNextFileName: string;
|
||||
FNextData: TFuture<TArray<TAuraFileDataRecord<T>>>;
|
||||
FNextData: TFuture<TArray<TDataPoint<T>>>;
|
||||
FCurrPosInFile: Int64;
|
||||
FCurrentIdx: Int64;
|
||||
FLastTimeStamp: TDateTime;
|
||||
protected
|
||||
function GetChunk(var Data: array of TDataPoint<T>): Integer; override;
|
||||
@@ -315,12 +314,12 @@ begin
|
||||
Result := FPath;
|
||||
end;
|
||||
|
||||
function TAuraDataServer<T>.LoadDataFile(const FileName: string): TFuture<TArray<TAuraFileDataRecord<T>>>;
|
||||
function TAuraDataServer<T>.LoadDataFile(const FileName: string): TFuture<TArray<TDataPoint<T>>>;
|
||||
var
|
||||
parsedPath, parsedSymbol: string;
|
||||
parsedYear, parsedMonth: Integer;
|
||||
begin
|
||||
Result := TFuture<TArray<TAuraFileDataRecord<T>>>.Null;
|
||||
Result := TFuture<TArray<TDataPoint<T>>>.Null;
|
||||
|
||||
if not TryParseFileName(FileName, parsedPath, parsedSymbol, parsedYear, parsedMonth) then
|
||||
exit;
|
||||
@@ -369,8 +368,8 @@ begin
|
||||
.Construct(
|
||||
TLatch.Enqueue(FLoadGate), // Use shared class var FLoadGate
|
||||
function: TBytes begin Result := TFile.ReadAllBytes(capFileName); end)
|
||||
.Chain<TArray<TAuraFileDataRecord<T>>>(
|
||||
function(bytes: TBytes): TArray<TAuraFileDataRecord<T>>
|
||||
.Chain<TArray<TDataPoint<T>>>(
|
||||
function(bytes: TBytes): TArray<TDataPoint<T>>
|
||||
begin
|
||||
var zipMemoryStream := TBytesStream.Create(bytes);
|
||||
try
|
||||
@@ -384,9 +383,9 @@ begin
|
||||
else if TFile.Exists(FileName) then
|
||||
begin
|
||||
Result :=
|
||||
TFuture<TArray<TAuraFileDataRecord<T>>>.Construct(
|
||||
TFuture<TArray<TDataPoint<T>>>.Construct(
|
||||
TLatch.Enqueue(FLoadGate), // Use shared class var FLoadGate
|
||||
function: TArray<TAuraFileDataRecord<T>>
|
||||
function: TArray<TDataPoint<T>>
|
||||
begin
|
||||
if TFile.Exists(capFileName) then
|
||||
begin
|
||||
@@ -419,11 +418,11 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
function TAuraDataServer<T>.LoadDataSeries(const FileName: string): TFuture<TArray<TAuraFileDataRecord<T>>>;
|
||||
function TAuraDataServer<T>.LoadDataSeries(const FileName: string): TFuture<TArray<TDataPoint<T>>>;
|
||||
var
|
||||
loadedState: TState;
|
||||
loadedFiles: TArray<TFuture<TArray<TAuraFileDataRecord<T>>>>;
|
||||
liveData: TFuture<TArray<TAuraFileDataRecord<T>>>;
|
||||
loadedFiles: TArray<TFuture<TArray<TDataPoint<T>>>>;
|
||||
liveData: TFuture<TArray<TDataPoint<T>>>;
|
||||
tabFiles: TList<string>;
|
||||
currentFile: string;
|
||||
liveFilePath: string;
|
||||
@@ -453,7 +452,7 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
var loadedFileList := TList<TFuture<TArray<TAuraFileDataRecord<T>>>>.Create;
|
||||
var loadedFileList := TList<TFuture<TArray<TDataPoint<T>>>>.Create;
|
||||
var loadStates := TList<TState>.Create;
|
||||
try
|
||||
for currentFile in tabFiles do
|
||||
@@ -463,7 +462,7 @@ begin
|
||||
loadStates.Add(data.Done);
|
||||
end;
|
||||
|
||||
liveData := TFuture<TArray<TAuraFileDataRecord<T>>>.Null;
|
||||
liveData := TFuture<TArray<TDataPoint<T>>>.Null;
|
||||
if liveFilePath <> '' then
|
||||
begin
|
||||
liveData := LoadDataFile(liveFilePath);
|
||||
@@ -482,11 +481,11 @@ begin
|
||||
end;
|
||||
|
||||
Result :=
|
||||
TFuture<TArray<TAuraFileDataRecord<T>>>.Construct(
|
||||
TFuture<TArray<TDataPoint<T>>>.Construct(
|
||||
loadedState,
|
||||
function: TArray<TAuraFileDataRecord<T>>
|
||||
function: TArray<TDataPoint<T>>
|
||||
begin
|
||||
var tickList := TList<TAuraFileDataRecord<T>>.Create;
|
||||
var tickList := TList<TDataPoint<T>>.Create;
|
||||
try
|
||||
var overallLastTabTickTime: TDateTime := 0;
|
||||
var cnt := 0;
|
||||
@@ -496,10 +495,10 @@ begin
|
||||
|
||||
for var P in loadedFiles do
|
||||
for var iterTickData in P.Value do
|
||||
if overallLastTabTickTime < iterTickData.TimeStamp then
|
||||
if overallLastTabTickTime < iterTickData.Time then
|
||||
begin
|
||||
tickList.Add(iterTickData);
|
||||
overallLastTabTickTime := iterTickData.TimeStamp;
|
||||
overallLastTabTickTime := iterTickData.Time;
|
||||
end;
|
||||
Result := tickList.ToArray;
|
||||
finally
|
||||
@@ -509,7 +508,7 @@ begin
|
||||
);
|
||||
end;
|
||||
|
||||
class function TAuraDataServer<T>.ReadCompressedData(const InputStream: TStream): TArray<TAuraFileDataRecord<T>>;
|
||||
class function TAuraDataServer<T>.ReadCompressedData(const InputStream: TStream): TArray<TDataPoint<T>>;
|
||||
var
|
||||
decompressionStream: TStream;
|
||||
localHeader: TZipHeader;
|
||||
@@ -548,10 +547,11 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
class function TAuraDataServer<T>.ReadUncompressedData(const InputStream: TStream): TArray<TAuraFileDataRecord<T>>;
|
||||
class function TAuraDataServer<T>.ReadUncompressedData(const InputStream: TStream): TArray<TDataPoint<T>>;
|
||||
var
|
||||
fileSize: Int64;
|
||||
recordCount, bytesRead: Integer;
|
||||
rec: TAuraFileDataRecord<T>;
|
||||
begin
|
||||
SetLength(Result, 0);
|
||||
InputStream.Position := 0;
|
||||
@@ -563,9 +563,13 @@ begin
|
||||
if recordCount > 0 then
|
||||
begin
|
||||
SetLength(Result, recordCount);
|
||||
bytesRead := InputStream.Read(Result[0], fileSize);
|
||||
if bytesRead <> fileSize then
|
||||
raise EReadError.CreateFmt('Read error. Expected %d bytes, read %d.', [fileSize, bytesRead]);
|
||||
for var i:=0 to High(Result) do
|
||||
begin
|
||||
bytesRead := InputStream.Read(rec, SizeOf(TAuraFileDataRecord<T>));
|
||||
if bytesRead <> SizeOf(TAuraFileDataRecord<T>) then
|
||||
raise EReadError.CreateFmt('Read error. Expected %d bytes, read %d.', [fileSize, bytesRead]);
|
||||
Result[i].Create( rec.TimeStamp, rec.Data );
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
@@ -585,7 +589,6 @@ begin
|
||||
inherited;
|
||||
FCurrentData := FDataServer.LoadDataFile(FCurrentFileName);
|
||||
FCurrPosInFile := 0;
|
||||
FCurrentIdx := 0;
|
||||
FIsLiveData.SetValue(false);
|
||||
FLastTimeStamp := 0;
|
||||
FNextFileName := FDataServer.FindNextDataFile(FCurrentFileName);
|
||||
@@ -597,8 +600,8 @@ end;
|
||||
|
||||
function TAuraFileStream<T>.GetChunk(var Data: array of TDataPoint<T>): Integer;
|
||||
var
|
||||
item: TAuraFileDataRecord<T>;
|
||||
currData: TArray<TAuraFileDataRecord<T>>;
|
||||
item: TDataPoint<T>;
|
||||
currData: TArray<TDataPoint<T>>;
|
||||
begin
|
||||
Result := 0;
|
||||
if not FCurrentData.Done.IsSet then
|
||||
@@ -635,12 +638,11 @@ begin
|
||||
end;
|
||||
|
||||
item := currData[FCurrPosInFile];
|
||||
if FLastTimeStamp < item.TimeStamp then
|
||||
if FLastTimeStamp < item.Time then
|
||||
begin
|
||||
FLastTimeStamp := item.TimeStamp;
|
||||
Data[Result].Create(FCurrentIdx, FLastTimeStamp, item.Data);
|
||||
FLastTimeStamp := item.Time;
|
||||
Data[Result].Create(FLastTimeStamp, item.Data);
|
||||
Inc(Result);
|
||||
Inc(FCurrentIdx);
|
||||
end;
|
||||
Inc(FCurrPosInFile);
|
||||
end;
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user