Skip to content

Commit 06f3103

Browse files
committed
Use SetMetaData to initialize data writers in Recorder
Refactor the Recorder system to initialize IDataWriter outputs using the new SetMetaData method instead of Initialize. FileDataWriter now requires file paths on construction and accepts metadata via SetMetaData. NetworkDataWriter implements a no-op SetMetaData to satisfy the interface. This change improves separation of responsibilities by decoupling metadata setup from generic Initialize calls.
1 parent 2f7d3b4 commit 06f3103

4 files changed

Lines changed: 27 additions & 39 deletions

File tree

Runtime/Core/Recorder/Recorder.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,19 @@ private void StartRecordingInternal(string name, string extraMetadata = "", IDat
7777

7878
if (outputs == null)
7979
{
80-
IDataWriter fileDataWriter = new FileDataWriter();
80+
var outputDir = Application.persistentDataPath;
81+
82+
FileDataWriter.GenerateFilePath(outputDir, name, out string filePath, out string metaFilePath);
83+
84+
IDataWriter fileDataWriter = new FileDataWriter(filePath, metaFilePath);
8185
outputs = new IDataWriter[] { fileDataWriter };
8286
}
8387

8488
Assert.IsFalse(outputs.Length == 0, "The outputs length is 0");
8589

8690
foreach (IDataWriter output in outputs)
8791
{
88-
output.Initialize(record);
92+
output.SetMetaData(recordMetadata);
8993
}
9094

9195
_dataDispatcher.Start(_context.CurrentRecord, outputs);

Runtime/Core/Recorder/Writer/FileDataWriter.cs

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using K4os.Compression.LZ4.Streams;
88
using PLUME.Sample;
99
using UnityEngine;
10+
using UnityEngine.Assertions;
1011

1112
namespace PLUME.Core.Recorder.Writer
1213
{
@@ -15,39 +16,20 @@ namespace PLUME.Core.Recorder.Writer
1516
// TODO: use memory mapped files
1617
public class FileDataWriter : IDataWriter, IDisposable
1718
{
18-
private Stream _stream;
19-
private Stream _metaStream;
20-
private CodedOutputStream _metaCodedOutputStream;
19+
private readonly Stream _stream;
20+
private readonly Stream _metaStream;
21+
private readonly CodedOutputStream _metaCodedOutputStream;
2122

23+
private readonly RecordMetrics _metrics;
2224
private Sample.RecordMetadata _metadata;
23-
private RecordMetrics _metrics;
24-
25-
private string filePath;
26-
private string metaFilePath;
27-
28-
public FileDataWriter() { }
2925

3026
/// <param name="filePath">The file path for the plm file.</param>
3127
/// <param name="metaFilePath">The file path for the meta file.</param>
3228
public FileDataWriter(string filePath, string metaFilePath)
3329
{
34-
this.filePath = filePath;
35-
this.metaFilePath = metaFilePath;
36-
}
37-
38-
/// <inheritdoc />
39-
public void Initialize(Record record)
40-
{
41-
if (string.IsNullOrWhiteSpace(filePath) ^ string.IsNullOrWhiteSpace(metaFilePath))
42-
{
43-
throw new InvalidOperationException($"Only one of filePath and metaFilePath are specifed. Either both need to be specified, or neither should be");
44-
}
45-
else if (string.IsNullOrWhiteSpace(filePath) && string.IsNullOrWhiteSpace(metaFilePath))
46-
{
47-
var outputDir = Application.persistentDataPath;
30+
Assert.IsFalse(string.IsNullOrWhiteSpace(filePath), "filePath is null or empty");
31+
Assert.IsFalse(string.IsNullOrWhiteSpace(metaFilePath), "metaFilePath is null or empty");
4832

49-
GenerateFilePath(outputDir, record.Metadata.Name, out filePath, out metaFilePath);
50-
}
5133
Logger.Log($"Record will be saved to '{filePath}'.");
5234

5335
PinnedMemory.MaxPooledSize = 0;
@@ -68,12 +50,15 @@ public void Initialize(Record record)
6850
_metaStream = File.Create(metaFilePath);
6951
_metaCodedOutputStream = new CodedOutputStream(_metaStream);
7052

71-
_metadata = record.Metadata.ToPayload();
7253
_metrics = new RecordMetrics
7354
{
7455
IsSequential = true
7556
};
76-
57+
}
58+
59+
public void SetMetaData(RecordMetadata metaData)
60+
{
61+
_metadata = metaData.ToPayload();
7762
UpdateMetaFile();
7863
}
7964

@@ -123,6 +108,10 @@ public void WriteTimestampedData(DataChunksTimestamped dataChunks)
123108

124109
private void UpdateMetaFile()
125110
{
111+
if (_metadata == null)
112+
{
113+
return;
114+
}
126115
_metaStream.SetLength(0);
127116
_metaStream.Position = 0;
128117
_metaCodedOutputStream.WriteLength(_metadata.CalculateSize());

Runtime/Core/Recorder/Writer/IDataWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace PLUME.Core.Recorder.Writer
22
{
33
public interface IDataWriter
44
{
5-
public void Initialize(Record record);
5+
public void SetMetaData(RecordMetadata metadata);
66

77
public void WriteTimelessData(DataChunks dataChunks);
88

Runtime/Core/Recorder/Writer/NetworkDataWriter.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,9 @@ namespace PLUME.Core.Recorder.Writer
1010
{
1111
public class NetworkDataWriter : IDataWriter
1212
{
13-
private Stream _stream;
14-
private string ipAddress;
15-
private int port;
13+
private readonly Stream _stream;
1614

1715
public NetworkDataWriter(string ipAddress="127.0.0.1", int port=8000)
18-
{
19-
this.ipAddress = ipAddress;
20-
this.port = port;
21-
}
22-
23-
public void Initialize(Record _)
2416
{
2517
// Create a tcp server
2618
var server = new TcpListener(IPAddress.Parse(ipAddress), port);
@@ -37,6 +29,9 @@ public void Initialize(Record _)
3729
_stream = LZ4Stream.Encode(stream, LZ4Level.L00_FAST);
3830
}
3931

32+
public void SetMetaData(RecordMetadata _)
33+
{}
34+
4035
public void WriteTimelessData(DataChunks dataChunks)
4136
{
4237
}

0 commit comments

Comments
 (0)